class_mcPUIX_DOM.inc.php 15 KB
<?

class mcPUIX_DOM {
	
	
	/**
	 *	copy the param_ConfigArray into the value param_ValueArray
	 *		examples @see mcPUIX_PHPTest::testPrepareArrayForDOMElement()
	 *		function prepare Array for @see mcPUIX_DOM::getDOMElement_DBUIArray
	 *
	 * @param string $param_tablename
	 * @param array $param_ConfigArray	-- $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename] = array()
	 *									-- return from mcPUIX_CONFIG::getTableConfig
	 * @param array $param_ValueArray	-- $param_ValueArray[mcPUIX::DBUIArray][mcPUIX::PDOValues] = array(0=>val1, 1=>val2)
	 * @param array $param_SetDefault	-- array( columnName1 => array( type=>, value=>, edit=>, show=>, ...), ... )
	 * @return array -- array(
	 *			mcPUIX::DBUIArray => array(
	 *				mcPUIX::DOMValues => array()
	 *			)
	 *		);
	 *
	 * @since 20120307
	 * @author f
	 */
	public static function prepareArrayForDOMElement($param_tablename, array $param_ConfigArray, array $param_ValueArray=null, array $param_SetDefault=null) {
//		D::show(array($param_ConfigArray, $param_ValueArray ) ,$param_tablename);
		if(mcArray::checkKeyHasFullArray($param_ConfigArray, mcPUIX::DBUIArray, mcPUIX::DBUIConfig, $param_tablename)) {
			if(mcArray::checkKeyHasFullArray($param_ValueArray, mcPUIX::DBUIArray, mcPUIX::PDOValues)) {
				/**
				 * condition: check if there is a reference table as list
				 */
				if(mcArray::checkKeyHasFullArray($param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename], 'TABLE', 'LIST_TABLE')
						&& $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['LIST_TABLE']['TABLENAME']
						&& $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['LIST_TABLE']['REFERENCE_KEY']
						&& $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['ELEMENTID']
						&& mcArray::checkKeyHasFullArray($param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig], $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['LIST_TABLE']['TABLENAME'])
				) {
					$Reference_Config = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['LIST_TABLE']['TABLENAME']];
					$Reference_TableName = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['LIST_TABLE']['TABLENAME'];
					$Reference_Config = array( mcPUIX::DBUIArray => array( mcPUIX::DBUIConfig => array( $Reference_TableName => $Reference_Config) ) );
					$Reference_ElementID = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['ELEMENTID'];
					$Reference_REFERENCE_KEY = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['LIST_TABLE']['REFERENCE_KEY'];

	//				D::show($Reference_Config, '$Reference_Config $Reference_ElementID:'.$Reference_ElementID.' ## $Reference_REFERENCE_KEY:'.$Reference_REFERENCE_KEY);
					$Reference = true;
				}  else {
					$Reference = false;
				}

				$copy_ConfigArray = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename];
	//D::show(array($copy_ConfigArray, $param_ConfigArray) ,$param_tablename);						
				foreach($param_ValueArray[mcPUIX::DBUIArray][mcPUIX::PDOValues] as  &$value_row) {


					foreach($value_row as $row_key => &$row_val) {

						/**
						 * reference
						 */
						if($row_key === mcPUIX::DBUIArray) {

						} 
						/**
						 * Reference List
						 */
						else if($row_val === mcPUIX::DBUIArray) {
							if($Reference) {
								
								$Reference_SetDefault = array( 
									$Reference_REFERENCE_KEY => array(
										'value' => $value_row[$Reference_REFERENCE_KEY]['value']
									) 
								);
//								D::show(array($Reference_SetDefault, $value_row), '$Reference_SetDefault');
								if(mcArray::checkKeyHasFullArray($value_row, mcPUIX::DBUIArray, mcPUIX::PDOValues)) {
									$ReferenceValues = array(mcPUIX::DBUIArray => array(mcPUIX::PDOValues => $value_row[mcPUIX::DBUIArray][mcPUIX::PDOValues]));
									$Reference_DOMValues = mcPUIX_DOM::prepareArrayForDOMElement($Reference_TableName, $Reference_Config, $ReferenceValues, $Reference_SetDefault);
		//							$value_row[mcPUIX::DBUIArray][mcPUIX::DOMValues] = $Reference_DOMValues[mcPUIX::DBUIArray][mcPUIX::DOMValues];
									unset($value_row[mcPUIX::DBUIArray]);
								} else {
									$Reference_DOMValues = mcPUIX_DOM::prepareArrayForDOMElement($Reference_TableName, $Reference_Config, null, $Reference_SetDefault);
								}
								if(array_key_exists($row_key, $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename])) {
									$row_val = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename][$row_key];
									unset($copy_ConfigArray[$row_key]);
								} else {
									$row_val = array();
								}									
								$row_val['value'] = $Reference_DOMValues;						
							}

						} 
						/**
						 * normal
						 */
						else {	
							$value = $row_val;

							if(array_key_exists($row_key, $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename])) {
								$row_val = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename][$row_key];
								unset($copy_ConfigArray[$row_key]);
							} else {
								$row_val = array();
							}
							if(!is_null($param_SetDefault)) {
								foreach($param_SetDefault as $default_rowkey => $default_rowvalues) {
									if($default_rowkey=='*' || $default_rowkey==$row_key) {
										foreach($default_rowvalues as $default_key => $default_value) {
											$row_val[$default_key] = $default_value;
											if($default_key==='value') {
												$value = $default_value;
											}
										}
									}
								}

							}
							$row_val['value'] = $value;						
						}
					}

					/**
					 * @todo Performance muss theoretisch nur einmal ausgeführt werden @f 100722
					 */
					if(!is_null($param_SetDefault) && is_array($copy_ConfigArray)) {
						foreach($copy_ConfigArray as $copy_ConfigDataKey =>  &$copy_ConfigDataArray) {
							foreach($param_SetDefault as $default_rowkey => &$default_rowvalues) {
								if($default_rowkey=='*' || $default_rowkey==$copy_ConfigDataKey) {
									foreach($default_rowvalues as $default_key => $default_value) {
										$copy_ConfigDataArray[$default_key] = $default_value;
									}
								}
							}
						}
					}
					/**
					 * alle Datensätze sollen erstmal makiert sein -- da sie sonst gelöscht werden 
					 */
					if(array_key_exists(mcPUIX::FORM_DO_ROW, $copy_ConfigArray)) {
						$copy_ConfigArray[mcPUIX::FORM_DO_ROW]['value'] = mcPUIX::FORM_DO_SAVE;
					}
//					D::show(array('array_merge' => array($value_row, $copy_ConfigArray)), '$value_row, $copy_ConfigArray');
					$value_row= array_merge($value_row, $copy_ConfigArray);
//					D::show($value_row, '$value_row');
				}
				$return = array(
					mcPUIX::DBUIArray => array(
						mcPUIX::DOMValues => $param_ValueArray[mcPUIX::DBUIArray][mcPUIX::PDOValues]
					)
				);


				
			} else {
				$return = array(
					mcPUIX::DBUIArray => array(
						mcPUIX::DOMValues => array()
					)
				);				
			}

			if(mcArray::checkKeyHasFullArray($param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename], 'TABLE') 
					&& array_key_exists('NEW_ROW', $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']) 
					&& $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['NEW_ROW']
			) {
				$return[mcPUIX::DBUIArray][mcPUIX::DOMValues]['NEW_ROW'] = $param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename];

				foreach($return[mcPUIX::DBUIArray][mcPUIX::DOMValues]['NEW_ROW'] as $NEW_key => &$NEW_values) {
					if(array_key_exists('type', $NEW_values) && $NEW_values['type'] === mcPUIX::DBUIArray) {
						unset($return[mcPUIX::DBUIArray][mcPUIX::DOMValues]['NEW_ROW'][$NEW_key]);
					}
					/**
					 * copy default values into NEW_ROW
					 */
					if(is_array($param_SetDefault)) {
						if(mcArray::checkKeyHasFullArray($param_SetDefault, $NEW_key)) {
							 $NEW_values = array_merge ( $NEW_values,  $param_SetDefault[$NEW_key]);
						}
					}
				}
				if($param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['ELEMENTID']) {
					if($param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['KINDOF'] === 'LIST') {
						unset($return[mcPUIX::DBUIArray][mcPUIX::DOMValues]['NEW_ROW'][$param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['ELEMENTID']]);
						
					} else if($param_ConfigArray[mcPUIX::DBUIArray][mcPUIX::DBUIConfig][$param_tablename]['TABLE']['KINDOF'] === 'RELATION') {
						
					}
						
				}

				$return[mcPUIX::DBUIArray][mcPUIX::DOMValues]['NEW_ROW'][mcPUIX::FORM_DO_ROW]['value'] = mcPUIX::FORM_DO_NEW;
			}
			
			return $return;							
		}
		return false;
	}	
	


	/**
	 *	return DOMElement with DBUIArray
	 * 
	 * @param DOMDocument $param_DOMDocument
	 * @param array $param_Array	-- $param_Array[mcPUIX::DBUIArray][mcPUIX::DOMValues] = array()
	 *								-- return from mcPUIX_DOM::prepareArrayForDOMElement
	 * @return type 
	 */
	public static function getDOMElement_DBUIArray(DOMDocument $param_DOMDocument, array $param_Array) {
//		D::show($param_Array, 'getDOMElement_DBUIArray');
		$DOMElement = $param_DOMDocument->createElement(mcPUIX::DBUIArray);
		
		if(mcArray::checkKeyHasFullArray($param_Array, mcPUIX::DBUIArray, mcPUIX::DOMValues)) {
			$DOMElement->setAttribute('count', count($param_Array));

			foreach($param_Array[mcPUIX::DBUIArray][mcPUIX::DOMValues] as $i => $row) {
				$DOMElement_Row = $DOMElement->appendChild($param_DOMDocument->createElement(mcPUIX::DOMRow));
				$DOMElement_Row->setAttribute('i', $i);
				
				if(is_array($row)) {
//					D::show($row, 'getDOMElement_DBUIArray'.$i);
					foreach($row as $column_name => $column_value) {
//						if(mcArray::checkKeyHasFullArray($column_value, 'value', mcPUIX::DBUIArray)) {
//							$DOM_Reference = mcPUIX_DOM::getDOMElement_DBUIArray($param_DOMDocument, $row);
//							$DOMElement_Row->appendChild($DOM_Reference);
//	//						D::show($row,' JETZT NE NEUE TABELLE');
//						} else 
						if($column_name === 'TABLE') {
						} else if ($column_name === mcPUIX::FORM_DO_ROW) {
						} else {
							$DOMElement_Row->appendChild(mcPUIX_DOM::getDOMElement_Element($param_DOMDocument, $column_name, $column_value));
						}
					}
					if(mcArray::checkKeyHasFullArray($row, mcPUIX::FORM_DO_ROW)) {
						$DOMElement_Row->appendChild(mcPUIX_DOM::getDOMElement_Element($param_DOMDocument, mcPUIX::FORM_DO_ROW, $row[mcPUIX::FORM_DO_ROW]));
					}
				}
			}
		}
		return $DOMElement;
	}

	
	
	public static function getDOMElement_DBUIList(DOMDocument $param_DOMDocument, array $param_Array) {
		$DOMElement = $param_DOMDocument->createElement(mcPUIX::DOMList);
		$DOMElement->setAttribute('count', count($param_Array));
		foreach($param_Array as $key => $value) {
			$DOMElement_Row = $DOMElement->appendChild($param_DOMDocument->createElement('item'));
			$DOMElement_Row->setAttribute('key', $key);
			$DOMElement_Row->setAttribute('value', $value);
			$DOMElement_Row->setAttribute('label', $value);
		}
		return $DOMElement;
	}

	/**
	 *	return a DOMElement like <Element value='' label='' objvar='' ... /> with optional attributes
	 *
	 *	special cases depends from $param_Values['type']
	 *
	 * @since 100713
	 * @author f
	 *
	 * @param DOMDocument $param_DOMDocument
	 * @param string $param_objvar
	 * @param array $param_ValuesAttributes=array() ... attributes for the XML DOMElement
	 * @param string $param_ElementName='Element'
	 * @return DOMElement  
	 */
	public static function getDOMElement_Element(DOMDocument $param_DOMDocument, $param_objvar, array $param_ValuesAttributes=array(), $param_ElementName=mcPUIX::DOMElement) {

		$value = '';
		$values_Select_List_values = null;
		$values_DBUArray = null;

		if(array_key_exists('value', $param_ValuesAttributes)) {
			$value = $param_ValuesAttributes['value'];
			unset($param_ValuesAttributes['value']);
			if(mcArray::checkKeyHasFullArray($value, mcPUIX::DBUIArray, mcPUIX::DOMValues)) {
				$values_DBUArray = $value;
				$value = null;
			}
		}
		if(array_key_exists('values', $param_ValuesAttributes) && is_array($param_ValuesAttributes['values'])) {
			$values_Select_List_values = $param_ValuesAttributes['values'];
			unset ($param_ValuesAttributes['values']);
		}

		if(!array_key_exists('label', $param_ValuesAttributes) || !$param_ValuesAttributes['label']) {
			/**
			 *	@todo schönheitshalber sollte Language hier raus, da es zu sehr spezifisch ist @f 100721
			 */
			$param_ValuesAttributes['label'] = Language::getWord($param_objvar);
		}
		if(!array_key_exists('objvar', $param_ValuesAttributes) || !$param_ValuesAttributes['objvar']) {
			$param_ValuesAttributes['objvar'] = $param_objvar;
		}


		$DOMElement =$param_DOMDocument->createElement($param_ElementName);

		$DOMElement->nodeValue = Converter::encodeString($value);


		if(array_key_exists('type', $param_ValuesAttributes)) {
			if($param_ValuesAttributes['type'] == mcPUIX::DBUIArray) {
				$DOM_Reference = mcPUIX_DOM::getDOMElement_DBUIArray($param_DOMDocument, $values_DBUArray);
				$DOMElement->appendChild($DOM_Reference);				 
			} else if($param_ValuesAttributes['type'] == 'text') {
				$DOMElement->nodeValue = Converter::encodeString($value);
				
				/**
				 * @todo wenn als value dann alle HTML-tags rauschmeissen!!!
				 */
				//$param_ValuesAttributes['value'] = substr($value, 0, 508).' ...' ;

			} else {
				if($param_ValuesAttributes['type'] == 'url') {
					$DOMElement->nodeValue = Converter::StringToUrl($value);

				} else if($param_ValuesAttributes['type'] == 'time') {
					if(!$value) {
						$value = time();
					}
					$param_ValuesAttributes['day'] = date('d', $value);
					$param_ValuesAttributes['month'] = date('m', $value);
					$param_ValuesAttributes['year'] = date('Y', $value);
					$param_ValuesAttributes['hour'] = date('G', $value);
					$param_ValuesAttributes['minute'] = date('i', $value);
					$param_ValuesAttributes['weekday'] = date('N', $value);

				} else if($param_ValuesAttributes['type'] == 'select') {
					if($values_Select_List_values) {
						$DOMElement->appendChild(mcPUIX_DOM::getDOMElement_DBUIList($param_DOMDocument, $values_Select_List_values));
					}

				}
				$param_ValuesAttributes['value'] = $value;

			}
		} else {
			$param_ValuesAttributes['value'] = $value;
		}



		if(!is_null($param_ValuesAttributes)) {
//			D::show($param_ValuesAttributes,$param_objvar )
			if(array_key_exists('objvar', $param_ValuesAttributes)) {
				$DOMElement->setAttribute('objvar', $param_ValuesAttributes['objvar']);
				unset($param_ValuesAttributes['objvar']);
			}

			/**
			 *	@todo hier ist bei französischen Strings ne WArnung, da kein Utf8 @f 100718
			 */
			foreach($param_ValuesAttributes as $PName => $PValue) {
				if(is_array($PValue)) {
					/**
					 * @todo noch mal checken was, wann hier kommt ... @f 100724
					 */
//					D::show($PValue, $param_objvar.' - '.$PName);
				} else {
					$DOMElement->setAttribute($PName, $PValue);
				}
				
			}
		}
		
		return $DOMElement;
	}
}

?>