class_DOMElementHelper.inc.php 10.6 KB
<?php
/**
 * @file	DOMElementHelper.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

class DOMElementHelper {
	public static function getJsLinkDOMElement(DOMDocument $param_DOMDocument,array $param_Language, $param_functionName, array $param_values){
		$XMLMenuLink = $param_DOMDocument->createElement('Link');
		$XMLMenuLink->setAttribute('parameter', 'javascript:'.$param_functionName);
		DOMElementHelper::setDOMElementLanguageParameter($XMLMenuLink, $param_Language);
		foreach($param_values as $key => $value){
			if($key == 'param_xml'){
				$XMLParam_xml = $param_DOMDocument->createElement($key);
					foreach($value as $path){
						$XMLParam_xml->appendChild($param_DOMDocument->createElement('param',$path));
					}

				$XMLMenuLink->appendChild($XMLParam_xml);
			}
			else
				$XMLMenuLink->setAttribute($key, $value);

		}
		return $XMLMenuLink;

	}
	public static function getLinkDOMElement(DOMDocument $param_DOMDocument, $param_url, $param_parameter, array $param_Language, $param_Host=null, $param_OptionalValues = null, $param_active = false) {

		//D::li("public static function getLinkDOMElement(DOMDocument param_DOMDocument, $param_url, $param_parameter, array $param_Language, $param_Host=null, $param_OptionalValues = null) {");

		if($param_Host===null) {
			$param_Host = CONFIG::getConfigHost();
		}
		$XMLMenuLink = $param_DOMDocument->createElement('Link');
		$XMLMenuLink->setAttribute('host', $param_Host);
		$XMLMenuLink->setAttribute('url', $param_url);

		if($param_OptionalValues != null)
			$XMLMenuLink->setAttribute('name', $param_OptionalValues);

		if($param_active)
			$XMLMenuLink->setAttribute('active', 'active');

		if(strpos(  $param_parameter , '?'  ) === 0) {
			$param_parameter = substr  ( $param_parameter, 1 );
		}
		$XMLMenuLink->setAttribute('parameter', $param_parameter.'&lang='.Language::getISOLanguage());
		//D::show($param_Language, '$param_parameter('.$param_parameter.')');
		DOMElementHelper::setDOMElementLanguageParameter($XMLMenuLink, $param_Language);

		if(is_array($param_OptionalValues)) {
			foreach($param_OptionalValues as $key => $value) {
				$XMLMenuLink->setAttribute($key, $value);
			}
		}

		return $XMLMenuLink;
	}

	public static function setDOMElementLanguageParameter(DOMElement &$param_XMLElement, array $param_Language) {

		if(is_array($param_Language)) {
			foreach($param_Language as $key => $value) {
				if($key == 'image') {
					global $_LOSP_PATH_TO_ROOT;
					$param_XMLElement->setAttribute($key, $_LOSP_PATH_TO_ROOT.$value);
				} else {
					//					D::ulli("param_XMLElement->setAttribute($key, $value);");
					$param_XMLElement->setAttribute($key, $value);
				}

			}
		}
		//		return $param_XMLElement;
		//		$param_XMLElement->setAttribute('label', $param_Language['label']);
		//		if(array_key_exists('image', $param_Language)) {
		//			$param_XMLElement->setAttribute('image', $param_Language['image']);
		//		}
		//		if(array_key_exists('tooltip', $param_Language)) {
		//			$param_XMLElement->setAttribute('tooltip', $param_Language['tooltip']);
		//		}
	}

	/**
	 * generate a XMLButton DOMElement and returned it
	 *
	 *	@deprecated 0.4 - 25.02.2009
	 * @param DOMDocument param_DOMDocument
	 *
	 */
	public static function &generateXMLButton(DOMDocument $param_DOMDocument, $param_Url, $param_label, $param_image=false, $param_Parameter=false) {
		//D::ulli('DOMElementHelper::generateXMLButton($param_DOMDocument, $param_Url='.$param_Url.', $param_label='.$param_label.', $param_image='.$param_image.' $param_Parameter='.$param_Parameter.')');
		$XMLButton = $param_DOMDocument->createElement("XML_Button");
		$XMLButton->setAttribute('url', $param_Url);
		$XMLButton->setAttribute('deprecated', 'deprecated');
		if($param_Parameter) {
			$XMLButton->setAttribute('parameter', $param_Parameter);
		}

		$XMLButton->setAttribute('label', $param_label);
		if($param_image) {
			$XMLButton->setAttribute('image', $param_image);
		}

		return $XMLButton;
	}

	public static function generateXMLMenu(DOMDocument $param_DOMDocument, $param_GET, $param_Parameter, $param_label, $param_image=null) {

	}

	public static function getDOMElementWithParameter(DOMDocument $param_DOMDocument, $param_ElementName, $param_OptionalValues = null, $param_Value=null) {
		if($param_Value) {
			$DOMElement = $param_DOMDocument->createElement($param_ElementName, $param_Value);
		} else {
			$DOMElement = $param_DOMDocument->createElement($param_ElementName);
		}

		if(is_array($param_OptionalValues)) {
			foreach($param_OptionalValues as $key => $value) {

				if(!is_array($value) && !is_numeric($key)) {
					$DOMElement->setAttribute($key, $value);
				} else {
					//D::ulli($key.' ## '.is_numeric($key).' ## '.$value);
					//					D::backtrace();
					//					D::show($value,
				}

			}
		}
		return $DOMElement;
	}

	/**
	 * 	transform Array to Xml
	 *
	 * 	XML Tags:	if key is int, then tagname is element
	 * 	Attributes:	if key is int, then no value into xml
	 *
	 *	@param DOMDocument param_DOMDocument
	 * 	@param array param_Array (wenn key=='_attributes' => value=array ist, werden die Werte als Attribute hinzugefügt)
	 * 	@param DOMElement param_DOMElement
	 */
	public static function arrayToXml(DOMDocument $param_DOMDocument, array $param_Array, DOMElement $param_DOMElement) {
		$i = 0;
		foreach($param_Array as $key => $value){
			if(is_array($value)){
				if($key=='_attributes') {
					foreach($value as $var_AttriKey => $var_AttriValue) {
						if(is_array($var_AttriValue)) {
							if(count($var_AttriValue)!=0) {
								foreach($var_AttriValue as $var_attriArray_key => $var_attriArray_value) {
									if(is_int($var_attriArray_key)) {
										$var_attriArray_tagname = 'element';
									} else {
										$var_attriArray_tagname = $var_attriArray_key;
									}
									//									D::li('('.$var_AttriKey.')(element value='.$var_attriArray_tagname.'/)(/'.$var_AttriKey.')');
								}
								//								D::show($var_AttriValue, ' '.$var_AttriKey.' => '.$var_AttriValue. ' +++ '.count($var_AttriValue));
								//								D::show($param_Array);
								//	self::arrayToXml($param_DOMDocument, $value, $sub_obj[$i]);
							}
						} else {
							if(!is_int($var_AttriKey)) {
								$param_DOMElement->setAttribute($var_AttriKey, $var_AttriValue);
							}
						}
					}
				} else if($key) {
					if(is_int($key)) {
						$var_tagname = 'element';
					} else {
						$var_tagname = $key;
					}
					$sub_obj[$i] = $param_DOMDocument->createElement($var_tagname);
					$param_DOMElement->appendChild($sub_obj[$i]);
					self::arrayToXml($param_DOMDocument, $value, $sub_obj[$i]);
				}

			} elseif(is_object($value)) {
				$sub_obj[$i] = $param_DOMDocument->createElement($key, 'Object: "' . $key . '" type: "'  . get_class($value) . '"');
				$param_DOMElement->appendChild($sub_obj[$i]);


				//			} else if(!is_numeric($key)) {
			} else {
				//				$key = (string) $key;

				$sub_obj[$i] = $param_DOMDocument->createElement($key);
				$sub_obj[$i]->setAttribute('value', $value);
				$param_DOMElement->appendChild($sub_obj[$i]);

				//			} else {
				//				D::li($i.' - key(<b>'.$key.'</b>) => value('.$value.')  ');
				//D::kindof($key, $i.' - key('.$key.') => value(<b>'.$value.'</b>)  ');
			}
			$i++;
		}
		return $param_DOMElement;
	}

	/**
	 * transform param_Values into XML, analog to <xsl:template match="Values">
	 */
	public static function valuesToXml(DOMDocument $param_DOMDocument, array $param_Values, $param_label=null) {
		$DOMElement_Values = $param_DOMDocument->createElement('Values');
		if($param_label===null) {
			$param_label = 'Values';
		}
		$DOMElement_Values->setAttribute('label', $param_label);

		$tmp_array = array();

		if(array_key_exists(CONSTKindOf::DESIGN_MENUADDON,$param_Values)){
			foreach(explode(',',$param_Values[CONSTKindOf::DESIGN_MENUADDON]) as $name){
				$value =false;
				if($tmp_values = explode('[-]',$name)){
					$name=$tmp_values[0];
					unset($tmp_values[0]);
					foreach($tmp_values as $tmp_value){
						$value[]=$tmp_value;
					}
				}
				$tmp_array[$name] = $value ;
			}
		}
		if(is_array(TreeCONFIG::$BasisDesign_MenuAddons)){
			$DOMElement_Value = $param_DOMDocument->createElement('Element');
			$DOMElement_Value->setAttribute('key', CONSTKindOf::DESIGN_MENUADDON);
			foreach(TreeCONFIG::$BasisDesign_MenuAddons as $name) {
				$DOMElement_AddOn = $param_DOMDocument->createElement('Addon');
				$DOMElement_AddOn->setAttribute('name', $name);
				if(array_key_exists($name,$tmp_array)){
					$DOMElement_AddOn->setAttribute('selected', 'selectetd');
					if($tmp_array[$name]){
						foreach($tmp_array[$name] as $value){
							$DOMElement_AddOn->appendChild($param_DOMDocument->createElement('Value',$value));
						}
					}
				}
				$DOMElement_Value->appendChild($DOMElement_AddOn);
			}
			$DOMElement_Values->appendChild($DOMElement_Value);
		}

		foreach($param_Values as $var_key => $var_value) {

			if($var_key != CONSTKindOf::DESIGN_MENUADDON){

				$DOMElement_Value = $param_DOMDocument->createElement('Element');
				$DOMElement_Value->setAttribute('key', $var_key);
				$DOMElement_Value->setAttribute('value', $var_value);
				if($var_key == CONSTKindOf::DESIGN_MENUSORT && $var_value != CONSTKindOf::DESIGN_NOSORT){
					foreach(explode(',',$var_value) as $nr_bc){
						$tmp_array = explode('-',$nr_bc);
						if(array_key_exists(0,$tmp_array) && $tmp_array[0] && array_key_exists(1,$tmp_array) && $tmp_array[1]){
							$DOMElement_BCMenu = $param_DOMDocument->createElement('MenuEntry');
							$DOMElement_BCMenu->setAttribute('pos', $tmp_array[0]);
							$DOMElement_BCMenu->setAttribute('bc_name', $tmp_array[1]);
							if(array_key_exists(2,$tmp_array) && $tmp_array[2]){
								$DOMElement_BCMenu->setAttribute('id', $tmp_array[2]);
							}
							$DOMElement_Value->appendChild($DOMElement_BCMenu);
						}

					}
				}
				$DOMElement_Values->appendChild($DOMElement_Value);
			}
		}
		return $DOMElement_Values;
	}
	public static function mkDOMMessage(DOMDocument $param_DOMDocument, $param_kind, array $param_values) {

	}
}
?>