Return to Snippet

Revision: 45305
at April 28, 2011 08:20 by iroybot


Updated Code
class XML_Form extends XML_Helper {
	/**
	 * 
	 * Turns a Zend_Form object into a DOMDocument for use in your XSLT-powered frontend
	 * @param Zend_Form $object
	 * @param array $data to validate form against
	 * @return DOMDocument
	 */
	public static function formToDOM(Zend_Form $object=null, $data=null) {
		if (is_a($object, "Zend_Form")) {
			if (!is_null($data)) {
				if ($object->isValid($data)) $state = "valid";
				else $state = "fail";
			} else {
				$state = "init";
			}

			$doc = new DOMDocument('1.0', 'UTF-8');
			$doc->loadXML('<item type="form" />');
			$root = $doc->documentElement;

			// 1st level item 
			$item = $doc->getElementsByTagName('item')->item(0);
			$item->setAttribute('id',$object->getAttrib('id'));
			$item->setAttribute('state',$state);
			
			// 2nd level fragment form properties
			$options = $object->getAttribs();
			$options['enctype'] = $object->getEnctype();
			foreach ($options as $attribute => $value) {
				$fragment = $doc->createElement('fragment',$value);
				$fragment->setAttribute('id',$attribute);
				$item->appendChild($fragment);
			}
	
			// 3rd level group for form elements
			$group_fields = $doc->createElement('group');
			$group_fields->setAttribute('id','fields');
			foreach ($object->getElements() as $element) {
				/** set redirect value if <input type="hidden" name="redirect" /> is present in current form and $_POST
				  * @todo test redirect functionality, retrieve redirect value in 
				  **/
				
				$class = strtolower(str_replace('Zend_Form_Element_','',get_class($element)));
				if ($class === 'hidden' && $element->getName() === 'redirect' && isset($_SESSION['reservationform'])) {
					$lang = Royal_Language::getLanguage();
					$region = Royal_FrontendRegion::getRegion();
					$element->setValue("/$lang/$region/memberhome/");
				}
				
				// 3rd level form elements
				$item_element = $doc->createElement('item');
				$item_element->setAttribute('id',$element->getName());
				$item_element->setAttribute('type', $class);
				
				//4th level fragments of element
				parent::createFragment('name', $item_element, $element->getName());
				parent::createFragment('label', $item_element, $element->getLabel());
				parent::createFragment('value', $item_element, $element->getValue());
				parent::createFragment('required', $item_element, $element->isRequired());
				parent::createFragment('maxlength', $item_element, $element->getAttrib('maxlength'));
				parent::createFragment('size', $item_element, $element->getAttrib('size'));
				parent::createFragment('readonly', $item_element, $element->getAttrib('readonly'));
				parent::createFragment('description', $item_element, $element->getDescription());
				if (!is_null($data) && array_key_exists($element->getName(), $data)) {
					foreach ($element->getErrors() as $error) {
						parent::createFragment('error', $item_element, $error);
					}
				}
				// possible errors
				if ($element->isValid($data[$element->getName()])===true) {
					$valid = 'true';
				} else {
					$valid = 'false';
				}
				parent::createFragment('valid', $item_element, $valid);
	
				if (method_exists($element, 'getMultiOptions')) {
					$group_options = parent::createGroup('options', $item_element);
					foreach ($element->getMultiOptions() as $key => $text) {
						$item_option = $doc->createElement('item');
						$item_option->setAttribute('id', 'option');
						parent::createFragment('text', $item_option, $text);
						parent::createFragment('value', $item_option, $key);
						$group_options->appendChild($item_option);
					}
				}
				
				$group_fields->appendChild($item_element);
				$item->appendChild($group_fields);
			}
			return $doc->saveXML($root, LIBXML_NOEMPTYTAG); // finally return DOMDocument
		} else {
			throw new Exception("Parameter <samp>\$object</samp> passed to XML_Form::formToDOM() is required and needs to be of type Zend_Form");
		}
	}
}

class XML_Helper {
	/**
	 * 
	 * __constructor
	 */
	public function Royal_XML_Creator() {
	}
	
	/**
	 * 
	 * Appends a <fragment id='id'>value</fragment> to a given origin and returns the node for further use
	 * @param string $id
	 * @param DOMNode $origin
	 * @param string $value
	 * @return DOMNode
	 */
	public static function createFragment($id, DOMNode $appendTo, $value='') {
		/** @todo maybe encode the value in order to make sure the XML stays valid (non-UTF-8 characters, HTML, etc.) **/
		$fragment = $appendTo->ownerDocument->createElement('fragment', $value);
		$fragment->setAttribute('id', (string) $id);
		$appendTo->appendChild($fragment);
		return $fragment;
	}
	
	/**
	 * 
	 * Appends a <fragment id='id'><![CDATA[value]]></fragment> to a given DOM
	 * @param unknown_type $id
	 * @param DOMNode $origin
	 * @param unknown_type $value
	 */
	public static function createCDATAFragment($id, DOMNode $appendTo, $value='') {
		$fragment = $appendTo->ownerDocument->createElement('fragment');
		$fragment->appendChild($appendTo->ownerDocument->createCDATASection($value));
		$fragment->setAttribute('id', $id);
		$appendTo->appendChild($fragment);
		return $fragment;
	}
	
	/**
	 * 
	 * Appends a group <group id='id' /> to a given origin
	 * @param string $id
	 * @param DOMNode $origin
	 * @return DOMNode
	 */
	public static function createGroup($id, DOMNode $appendTo) {
		$group = $appendTo->ownerDocument->createElement('group');
		$group->setAttribute('id', $id);
		$appendTo->appendChild($group);
		return $group;
	}
}

/**
Example Usage:
**/
$proc = new XSLTProcessor(); 
$xsldoc = new DOMDocument(); 
$xsldoc->load(APPLICATION_PATH.'/xsl/forms.xsl'); 
$proc->importStyleSheet($xsldoc); 
echo $proc->transformToXML(new XML_Form::formToDOM(new My_Custom_Zend_Form()));

Revision: 45304
at April 28, 2011 08:12 by iroybot


Updated Code
class XML_Form extends XML_Helper {
	/**
	 * 
	 * Turns a Zend_Form object into a DOMDocument for use in your XSLT-powered frontend
	 * @param Zend_Form $object
	 * @param array $data to validate form against
	 * @return DOMDocument
	 */
	public static function formToDOM(Zend_Form $object=null, $data=null) {
		if (is_a($object, "Zend_Form")) {
			if (!is_null($data)) {
				if ($object->isValid($data)) $state = "valid";
				else $state = "fail";
			} else {
				$state = "init";
			}

			$doc = new DOMDocument('1.0', 'UTF-8');
			$doc->loadXML('<item type="form" />');
			$root = $doc->documentElement;

			// 1st level item 
			$item = $doc->getElementsByTagName('item')->item(0);
			$item->setAttribute('id',$object->getAttrib('id'));
			$item->setAttribute('state',$state);
			
			// 2nd level fragment form properties
			$options = $object->getAttribs();
			$options['enctype'] = $object->getEnctype();
			foreach ($options as $attribute => $value) {
				$fragment = $doc->createElement('fragment',$value);
				$fragment->setAttribute('id',$attribute);
				$item->appendChild($fragment);
			}
	
			// 3rd level group for form elements
			$group_fields = $doc->createElement('group');
			$group_fields->setAttribute('id','fields');
			foreach ($object->getElements() as $element) {
				/** set redirect value if <input type="hidden" name="redirect" /> is present in current form and $_POST
				  * @todo test redirect functionality, retrieve redirect value in 
				  **/
				
				$class = strtolower(str_replace('Zend_Form_Element_','',get_class($element)));
				if ($class === 'hidden' && $element->getName() === 'redirect' && isset($_SESSION['reservationform'])) {
					$lang = Royal_Language::getLanguage();
					$region = Royal_FrontendRegion::getRegion();
					$element->setValue("/$lang/$region/memberhome/");
				}
				
				// 3rd level form elements
				$item_element = $doc->createElement('item');
				$item_element->setAttribute('id',$element->getName());
				$item_element->setAttribute('type', $class);
				
				//4th level fragments of element
				parent::createFragment('name', $item_element, $element->getName());
				parent::createFragment('label', $item_element, $element->getLabel());
				parent::createFragment('value', $item_element, $element->getValue());
				parent::createFragment('required', $item_element, $element->isRequired());
				parent::createFragment('maxlength', $item_element, $element->getAttrib('maxlength'));
				parent::createFragment('size', $item_element, $element->getAttrib('size'));
				parent::createFragment('readonly', $item_element, $element->getAttrib('readonly'));
				parent::createFragment('description', $item_element, $element->getDescription());
				if (!is_null($data) && array_key_exists($element->getName(), $data)) {
					foreach ($element->getErrors() as $error) {
						parent::createFragment('error', $item_element, $error);
					}
				}
				// possible errors
				if ($element->isValid($data[$element->getName()])===true) {
					$valid = 'true';
				} else {
					$valid = 'false';
				}
				parent::createFragment('valid', $item_element, $valid);
	
				if (method_exists($element, 'getMultiOptions')) {
					$group_options = parent::createGroup('options', $item_element);
					foreach ($element->getMultiOptions() as $key => $text) {
						$item_option = $doc->createElement('item');
						$item_option->setAttribute('id', 'option');
						parent::createFragment('text', $item_option, $text);
						parent::createFragment('value', $item_option, $key);
						$group_options->appendChild($item_option);
					}
				}
				
				$group_fields->appendChild($item_element);
				$item->appendChild($group_fields);
			}
			return $doc->saveXML($root, LIBXML_NOEMPTYTAG); // finally return DOMDocument
		} else {
			throw new Exception("Parameter <samp>\$object</samp> passed to XML_Form::formToDOM() is required and needs to be of type Zend_Form");
		}
	}
}

class XML_Helper {
	/**
	 * 
	 * __constructor
	 */
	public function Royal_XML_Creator() {
	}
	
	/**
	 * 
	 * Appends a <fragment id='id'>value</fragment> to a given origin and returns the node for further use
	 * @param string $id
	 * @param DOMNode $origin
	 * @param string $value
	 * @return DOMNode
	 */
	public static function createFragment($id, DOMNode $appendTo, $value='') {
		/** @todo maybe encode the value in order to make sure the XML stays valid (non-UTF-8 characters, HTML, etc.) **/
		$fragment = $appendTo->ownerDocument->createElement('fragment', $value);
		$fragment->setAttribute('id', (string) $id);
		$appendTo->appendChild($fragment);
		return $fragment;
	}
	
	/**
	 * 
	 * Appends a <fragment id='id'><![CDATA[value]]></fragment> to a given DOM
	 * @param unknown_type $id
	 * @param DOMNode $origin
	 * @param unknown_type $value
	 */
	public static function createCDATAFragment($id, DOMNode $appendTo, $value='') {
		$fragment = $appendTo->ownerDocument->createElement('fragment');
		$fragment->appendChild($appendTo->ownerDocument->createCDATASection($value));
		$fragment->setAttribute('id', $id);
		$appendTo->appendChild($fragment);
		return $fragment;
	}
	
	/**
	 * 
	 * Appends a group <group id='id' /> to a given origin
	 * @param string $id
	 * @param DOMNode $origin
	 * @return DOMNode
	 */
	public static function createGroup($id, DOMNode $appendTo) {
		$group = $appendTo->ownerDocument->createElement('group');
		$group->setAttribute('id', $id);
		$appendTo->appendChild($group);
		return $group;
	}
}

Revision: 45303
at April 28, 2011 08:10 by iroybot


Initial Code
class XML_Form {
	/**
	 * 
	 * Turns a Zend_Form object into a DOMDocument for use in your XSLT-powered frontend
	 * @param Zend_Form $object
	 * @param array $data to validate form against
	 * @return DOMDocument
	 */
	public static function formToDOM(Zend_Form $object=null, $data=null) {
		if (is_a($object, "Zend_Form")) {
			if (!is_null($data)) {
				if ($object->isValid($data)) $state = "valid";
				else $state = "fail";
			} else {
				$state = "init";
			}

			$doc = new DOMDocument('1.0', 'UTF-8');
			$doc->loadXML('<item type="form" />');
			$root = $doc->documentElement;

			// 1st level item 
			$item = $doc->getElementsByTagName('item')->item(0);
			$item->setAttribute('id',$object->getAttrib('id'));
			$item->setAttribute('state',$state);
			
			// 2nd level fragment form properties
			$options = $object->getAttribs();
			$options['enctype'] = $object->getEnctype();
			foreach ($options as $attribute => $value) {
				$fragment = $doc->createElement('fragment',$value);
				$fragment->setAttribute('id',$attribute);
				$item->appendChild($fragment);
			}
	
			// 3rd level group for form elements
			$group_fields = $doc->createElement('group');
			$group_fields->setAttribute('id','fields');
			foreach ($object->getElements() as $element) {
				/** set redirect value if <input type="hidden" name="redirect" /> is present in current form and $_POST
				  * @todo test redirect functionality, retrieve redirect value in 
				  **/
				
				$class = strtolower(str_replace('Zend_Form_Element_','',get_class($element)));
				if ($class === 'hidden' && $element->getName() === 'redirect' && isset($_SESSION['reservationform'])) {
					$lang = Royal_Language::getLanguage();
					$region = Royal_FrontendRegion::getRegion();
					$element->setValue("/$lang/$region/memberhome/");
				}
				
				// 3rd level form elements
				$item_element = $doc->createElement('item');
				$item_element->setAttribute('id',$element->getName());
				$item_element->setAttribute('type', $class);
				
				//4th level fragments of element
				parent::createFragment('name', $item_element, $element->getName());
				parent::createFragment('label', $item_element, $element->getLabel());
				parent::createFragment('value', $item_element, $element->getValue());
				parent::createFragment('required', $item_element, $element->isRequired());
				parent::createFragment('maxlength', $item_element, $element->getAttrib('maxlength'));
				parent::createFragment('size', $item_element, $element->getAttrib('size'));
				parent::createFragment('readonly', $item_element, $element->getAttrib('readonly'));
				parent::createFragment('description', $item_element, $element->getDescription());
				if (!is_null($data) && array_key_exists($element->getName(), $data)) {
					foreach ($element->getErrors() as $error) {
						parent::createFragment('error', $item_element, $error);
					}
				}
				// possible errors
				if ($element->isValid($data[$element->getName()])===true) {
					$valid = 'true';
				} else {
					$valid = 'false';
				}
				parent::createFragment('valid', $item_element, $valid);
	
				if (method_exists($element, 'getMultiOptions')) {
					$group_options = parent::createGroup('options', $item_element);
					foreach ($element->getMultiOptions() as $key => $text) {
						$item_option = $doc->createElement('item');
						$item_option->setAttribute('id', 'option');
						parent::createFragment('text', $item_option, $text);
						parent::createFragment('value', $item_option, $key);
						$group_options->appendChild($item_option);
					}
				}
				
				$group_fields->appendChild($item_element);
				$item->appendChild($group_fields);
			}
			return $doc->saveXML($root, LIBXML_NOEMPTYTAG); // finally return DOMDocument
		} else {
			throw new Exception("Parameter <samp>\$object</samp> passed to XML_Form::formToDOM() is required and needs to be of type Zend_Form");
		}

Initial URL
http://snipplr.com/view/52714/example-xml-representation-of-zendform/

Initial Description
Turns a Zend_Form object into XML data. Click the associated link to see an Example XML result and a XSL Stylesheet used to display regular HMTL.

Initial Title
Zend_Form  Object to XML Data Representation (for use in XSLT)

Initial Tags
php

Initial Language
PHP