Location: PHPKode > scripts > Dynamic Elements Processor with PHP 5 > dynamic-elements-processor-with-php-5/classes/ProcessElements.class.php
<?php
//error_reporting(0);
//error_reporting(E_ALL);

/**
 *
 * Dynamic Elements Processor 
 * (requires ElementIterator.class.php
 *  RecursiveArrayObject.class.php)
 *
 * @author Claudio Biesele, <hide@address.com>
 *
 * Generated 22.11.2009
 *
 */

interface ElementParts {
	public function createFormStart($formdata);
	public function createFormEnd();
	public function createElement($ftype);
	public function showElementType();
	public function addOptions($option);
	public function showOptions();
	public function appendElement($type=null,$name=null,$value=null);
}


class ProcessElements implements ElementParts {
	protected $fparts;
	protected $option;
	protected $foptions;
	protected $type;
	protected $ftype;
	protected $name;
	protected $value;
	protected $newform;
	protected $elementtype;
	protected $elementvalue;
	protected $elementname;
	protected $elementend;
	protected $pos;
	protected $fpos;
	protected $posval;
	protected $txtareaoption;
	protected $commaval;
	protected $commaval1;
	protected $commaval2;
	protected $commaval3;
	protected $commaval4;
	protected $length;
	
	// create top part of form
	public function createFormStart($formdata) {
		$this->formdata = $formdata;

		while(list($this->key, $this->val) = each($this->formdata))
		{
			if (isset($this->val[4])) { $this->val[4] = $this->val[4]; } else { $this->val[4] = null; }
			if (isset($this->val[5])) { $this->val[5] = $this->val[5]; } else { $this->val[5] = null; }
			echo "<form action=\"{$this->val[0]}\" method=\"{$this->val[1]}\" name=\"{$this->val[2]}\" enctype=\"{$this->val[3]}\" {$this->val[4]} {$this->val[5]}>";
		}
	}
	
	// create bottom part of form
	public function createFormEnd() {
		echo "</form>";
	}
	
	// create element name
	public function createElement($ftype) {
		$this->ftype = $ftype;
		
		if ($this->ftype == null) {
			die("Element name is missing.");
		}
	}
	
	// show element name
	public function showElementType() {		
		return $this->ftype;
	}
	
	// add element options
	public function addOptions($option) {
		$this->option = $option;
	}
	
	// show element options
	public function showOptions() {		
		return $this->option;
	}
	
	// append element
	public function appendElement($type=null,$name=null,$value=null) {
		$this->type 	= $type;
		$this->name 	= $name;
		$this->value 	= $value;
		
		$this->input 	= "$this->type,$this->name,$this->value";		
		$this->option 	= $this->showOptions();
		$this->newform 	= array(
				array($this->input),
					array(
						array($this->option				
				)
			)
		);

		$this->it = new ElementIterator(new RecursiveArrayObject($this->newform));

		$this->in_list = false;
		$this->ftype = $this->showElementType();
		foreach($this->it as $this->item) {
			
			switch($this->it->getDepth()) {
			// set standard options to element
			case 1:
				if (substr_count($this->item, ',') > 2) {
					if ($this->ftype == 'textarea') {
						$this->pos = strpos($this->item, ',', 1);
						$this->commaval = substr($this->item, $this->pos+1);
						
						$this->fpos = strpos($this->item, ',', 0);
						$this->posval = $this->pos-$this->fpos;
						$this->txtareaoption = str_replace(",","",substr($this->item,$this->fpos,$this->posval));
					}
					elseif ($this->ftype == 'span') {
						$this->pos = strpos($this->item, ',', 1);
						$this->commaval1 = substr($this->item, $this->pos+1);
						
						$this->fpos = strpos($this->item, ',', 0);
						$this->posval = $this->pos-$this->fpos;
						$this->spanoption = str_replace(",","",substr($this->item,$this->fpos,$this->posval));
					}
					elseif ($this->ftype == 'div') {
						$this->pos = strpos($this->item, ',', 1);
						$this->commaval2 = substr($this->item, $this->pos+1);
						
						$this->fpos = strpos($this->item, ',', 0);
						$this->posval = $this->pos-$this->fpos;
						$this->divoption = str_replace(",","",substr($this->item,$this->fpos,$this->posval));
					}
					elseif ($this->ftype == 'label') {
						$this->pos = strpos($this->item, ',', 0);
						$this->commaval3 = substr($this->item, $this->pos+2);
					}
					elseif ($this->ftype == 'input') {
						$this->fparts = split(',',$this->item);
						if ($this->fparts[0] == 'text') {
							$this->length = strlen($this->fparts[1]);
							$this->pos = strpos($this->item, ',', 1);
							$this->commaval4 = substr($this->item, $this->pos+$this->length+2);
							$this->elementvalue = " value=\"{$this->commaval4}\" ";
						}
						if ($this->fparts[0] == 'hidden') {
							$this->length = strlen($this->fparts[1]);
							$this->pos = strpos($this->item, ',', 1);
							$this->commaval4 = substr($this->item, $this->pos+$this->length+2);
							$this->elementvalue = " value=\"{$this->commaval4}\" ";
						}					
					}					
				} else {
					$this->fparts = split(',',$this->item);
				}				
				if ($this->ftype == 'textarea' 
					|| $this->ftype == 'select'
					|| $this->ftype == 'keygen'
					|| $this->ftype == 'span'
					|| $this->ftype == 'div') {
					$this->elementtype = "";
					$this->elementvalue = "";			
				} 
				else { 	$this->elementtype 	= " type=\"{$this->fparts[0]}\" ";
						if (substr_count($this->item, ',') == 2) {
							$this->elementvalue = " value=\"{$this->fparts[2]}\" "; 
						}
				}
				if ($this->ftype == 'label'
					|| $this->ftype == 'table') {
					$this->elementname = "";
					$this->elementtype = "";
					$this->elementvalue = "";
				} else {
					$this->elementname = " name=\"{$this->fparts[1]}\" ";
					if ($this->ftype == 'textarea' && strpos($this->commaval,",")) {
						$this->elementname = " name=\"{$this->txtareaoption}\" ";
					}
					elseif ($this->ftype == 'span' && $this->commaval1 != '') {
						$this->elementname = " name=\"{$this->spanoption}\" ";
					}
					elseif ($this->ftype == 'div' && $this->commaval2 != '') {
						$this->elementname = " name=\"{$this->divoption}\" ";
					}				
				}
				echo "<{$this->ftype} {$this->elementtype}{$this->elementname}{$this->elementvalue}";
				break;
			// set extra options to element
			case 2:											
				if (strpos($this->item,',') != null) {
					if (strpos($this->item,"(")) {
						$pos1 = strpos($this->item,"(");
						$pos2 = strpos($this->item,")");
						$this->foptionsjs1 = substr($this->item,0,$pos1);
						$this->foptionsrepl = str_replace(',',' ',$this->foptionsjs1);
						$this->foptionsjs2 = substr($this->item,$pos1,$pos2);
						$this->foptions = $this->foptionsrepl.$this->foptionsjs2;
					} else {				
					$this->foptions = str_replace(',',' ',$this->item); }
				} else { $this->foptions = $this->item; }
				if ($this->ftype == 'select') {
					$this->elementend = ">{$this->fparts[2]}</select>";				
				}
				elseif ($this->ftype == 'textarea') {
					if ($this->commaval != '') {						
						$this->elementend = ">{$this->commaval}</textarea>"; } 
					else { $this->elementend = ">{$this->fparts[2]}</textarea>"; }									
				}
				elseif ($this->ftype == 'span') {
					if ($this->commaval1 != '') { 
						$this->elementend = ">{$this->commaval1}</span>"; } 
					else { $this->elementend = ">{$this->fparts[2]}</span>"; }				
				}
				elseif ($this->ftype == 'div') {
					if ($this->commaval2 != '') { 
						$this->elementend = ">{$this->commaval2}</div>"; } 
					else { $this->elementend = ">{$this->fparts[2]}</div>"; }			
				}
				elseif ($this->ftype == 'label') {
					if ($this->commaval3 != '') { 
						$this->elementend = ">{$this->commaval3}</label>"; } 
					else { $this->elementend = ">{$this->fparts[2]}</label>"; }				
				}
				elseif ($this->ftype == 'table') {
					$this->elementend = ">{$this->fparts[2]}</table>";				
				}
				else { 
					$this->elementend = "/>";
				}		
				echo " {$this->foptions} {$this->elementend}". PHP_EOL;
				break;
			default:
				echo " " . PHP_EOL;
			}
		}
	}	

}

?>
Return current item: Dynamic Elements Processor with PHP 5