<?php
/**
 * @file	PostController.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 PostController {
/**
 * @filesource 	class_PostController.inc.php
 *
 * @category freeSN
 * @copyright Copyright by mensch.coop e.G. 2009
 * @mailto	dev [at] mensch.coop
 * @version 0.4.200901
 * @link http://mensch.coop
 */
/**
 *	Diese Klasse handelt den $_POST -Array
 *
 *	@static			Diese Klasse besitzt nur statische Funktionen
 */
	private static $LinkClassArray = array();
	/**
	 * @var array for the function post_setPreValues
	 */
	private static $PreValueArray;

	/**
	 * Funktion erstellt ein neues BasisElement
	 *
	 * @param Account param_HostAccount
	 *
	 * @param array param_BEData hält die Daten des BEs
	 * @param array param_AddData hält BCs und IDs
	 * @param datatype param_Tree
	 * @param array param_FunctionData hält Post-Funktionen
	 *
	 * @return int die ID des erstellten BasisElements
	 */

	public static function makeNew(Account &$param_HostAccount, iStorable &$param_iStorable, $param_BEData, $param_AddData,array  $param_Tree, $param_FunctionData){
//D::li("makeNew(Account &$param_HostAccount, iStorable &$param_iStorable, $param_BEData, $param_AddData,array  $param_Tree, $param_FunctionData)");

		$the_BasisClipboardname = null;
		$the_BasisElementName = key($param_BEData);
		$the_PartOfID = $param_BEData[$the_BasisElementName]['PartOfID'];


		if(array_key_exists('AuthorProfileID',$param_BEData[$the_BasisElementName]) && $param_BEData[$the_BasisElementName]['AuthorProfileID']){
			$param_HostAccount->setAuthorProfileID($param_BEData[$the_BasisElementName]['AuthorProfileID']);
		}
// 		searchCodeAuthorisationList
// 		if($param_HostAccount->getAuthorProfileID() != $param_HostAccount->getActiveProfileID()){
// 			$AuthorProfile = MultimediaText::loadElement($param_HostAccount, $param_iStorable, null, $param_HostAccount->getAuthorProfileID());
// 		}
// 		else{
// 			$AuthorProfile = $param_HostAccount->getActiveProfile();
// 		}

//D::show($param_AddData,'addData');
		foreach($param_AddData as $dataKey => $dataValue){
			foreach($dataValue as $key => $value){
				$BasisClipboardname[] = $key;
				if($value){
					if($value=='AuthorProfileID'){
						//$value = $param_HostAccount->getAuthorProfileID();
						$param_AddData[$dataKey][$key] =$param_HostAccount->getAuthorProfileID();
						$the_BasisClipboardname = $key;
						/*if($param_HostAccount->getAuthorProfileID() == $the_PartOfID){
							$the_BasisClipboardname = $key;
						}*/

						//D::show($param_HostAccount->getAuthorProfileID(),'AuthorProfileID');
					}
					//$PartOfID[] = $value;

					if($value == $the_PartOfID){
						$the_BasisClipboardname = $key;
					}
				}
			}
		}
//		D::show($param_AddData,'SecaddData');
//		D::show($param_HostAccount->getAuthorProfileID(),$the_PartOfID);

		$tmp_BasisElement =& MultimediaText::loadElement($param_HostAccount, $param_iStorable,null, $the_PartOfID);

		$FncVar_BasisClipboardnameArray = CONFIG::transformStringToArray($the_BasisClipboardname, 'category');
/**@todo warum muss denn hier das Clipboard geldaen werden? @um 100425*/
		if(is_subclass_of($FncVar_BasisClipboardnameArray[0], 'BasisClipboard') ) {
			$Clipboard =& $tmp_BasisElement->getClipboard($param_HostAccount, $param_iStorable, $the_BasisClipboardname);
			BasisClipboard::loadObjectLevel($param_HostAccount, $param_iStorable, $Clipboard, 'makeNew' );
		}
		if(Form::checkDataCompleteness($param_BEData[$the_BasisElementName], $the_BasisElementName)) {
			Form::replaceData($param_BEData[$the_BasisElementName]);
			$BasisElement = Form::makeNewObjectFromFormData($param_HostAccount, $param_iStorable, $param_BEData[$the_BasisElementName], $tmp_BasisElement, $the_BasisClipboardname, $the_BasisElementName, null, null, $param_Tree, $param_AddData);
		}
		if(is_object($BasisElement) &&  $BasisElement->getID()){
			if(is_array($param_FunctionData)) {
				foreach($param_FunctionData as $kind => $value){
					PostController::post_fnc($param_HostAccount, $param_iStorable,$kind, $value, $BasisElement->getID(),$BasisElement);
				}
			}
			Message::newMessage('makeNew', $Clipboard->ThisClassName,$BasisElement->ThisClassName);
			$param_HostAccount->resetAuthorProfileID();
			return $BasisElement->getID();
		} else{
			$param_HostAccount->resetAuthorProfileID();
            return Error::newError("Error",$param_BEData[$the_BasisElementName]['Subject'],"");
		}

	}

	/**
	 * Funktion verändert ein besthendes BasisElement
	 *
	 * @param Account param_HostAccount
	 *
	 * @param array param_BEData hält die Daten des BEs
	 * @param array param_AddData hält BCs und IDs
	 * @param datatype param_Tree
	 * @param array param_FunctionData hält Post-Funktionen
	 *
	 * @return int die ID des erstellten BasisElements
	 */
	public static function edit(Account &$param_HostAccount, iStorable &$param_iStorable, $param_BEData, $param_AddData, $param_Tree, $param_FunctionData){
		$Clipboard = $the_PartOfID = null;

		$the_BasisElementName = key($param_BEData);

		$BasisClipboardname = $PartOfID = array();

		if(array_key_exists('AuthorProfileID',$param_BEData[$the_BasisElementName]) && $param_BEData[$the_BasisElementName]['AuthorProfileID']){
			$param_HostAccount->setAuthorProfileID($param_BEData[$the_BasisElementName]['AuthorProfileID']);
		}

// 		searchCodeAuthorisationList
//		if($param_HostAccount->getAuthorProfileID() != $param_HostAccount->getActiveProfileID()){
//			$AuthorProfile = MultimediaText::loadElement($param_HostAccount, $param_iStorable, null, $param_HostAccount->getAuthorProfileID());
//		}
//		else{
//			$AuthorProfile = $param_HostAccount->getActiveProfile();
//		}
//		if(!$the_PartOfID){
//			$the_PartOfID=$AuthorProfile->getID();
//		}
		//$AuthorProfile = $param_HostAccount->getActiveProfile();


		foreach($param_AddData as $dataKey => $dataValue){
			foreach($dataValue as $key => $value){
				$BasisClipboardname[] = $key;
				if($value){
					if($value=='AuthorProfileID'){
						//$value = $param_HostAccount->getAuthorProfileID();
						$param_AddData[$dataKey][$key] =$param_HostAccount->getAuthorProfileID();
						/*if($param_HostAccount->getAuthorProfileID() == $the_PartOfID){
							$the_BasisClipboardname = $key;
						}*/

						//D::show($param_HostAccount->getAuthorProfileID(),'AuthorProfileID');
					}
					//$PartOfID[] = $value;
				}
			}
		}/*
		foreach($param_AddData as $dataValue){
			foreach($dataValue as $key => $value){
				$BasisClipboardname[] = $key;
				if($value){
					$PartOfID[] = $value;
				}
				else{
					$PartOfID[] = $param_HostAccount->getActiveProfileID();
				}
			}
		}*/

		if(array_key_exists('PartOfID', $param_BEData[$the_BasisElementName]) && $param_BEData[$the_BasisElementName]['PartOfID']) {
			$the_PartOfID = $param_BEData[$the_BasisElementName]['PartOfID'];
		} else if($param_HostAccount->getAuthorProfileID() != $param_HostAccount->getActiveProfileID()){
			$AuthorProfile = MultimediaText::loadElement($param_HostAccount, $param_iStorable, null, $param_HostAccount->getAuthorProfileID());
			$the_PartOfID=$AuthorProfile->getID();
		} else{
			$the_PartOfID= $param_HostAccount->getActiveProfile()->getID();
		}
		$tmp_BasisElement =& MultimediaText::loadElement($param_HostAccount, $param_iStorable, null, $the_PartOfID);

		/** solange das KAtegorie Problem besteht muss dies bestehen bleiben um das BE zu erzeugen*/

		{
			for($i=count($BasisClipboardname)-1;$i>=0;$i--){
				$FncVar_BasisClipboardnameArray = CONFIG::transformStringToArray($BasisClipboardname[$i], 'category');
				if(class_exists($FncVar_BasisClipboardnameArray[0]) && is_subclass_of($FncVar_BasisClipboardnameArray[0], 'BasisClipboard') ) {
					if(array_key_exists($BasisClipboardname[$i],Config::getMT_BasisClipboards($tmp_BasisElement->ThisClassName))){
						$Clipboard =& $tmp_BasisElement->getClipboard($param_HostAccount, $param_iStorable, $BasisClipboardname[$i]);
						BasisClipboard::loadObjectLevel($param_HostAccount, $param_iStorable, $Clipboard, 'Edit' );
						break;
					}
					else if(array_key_exists($FncVar_BasisClipboardnameArray[0],Config::getMT_BasisClipboards($tmp_BasisElement->ThisClassName))){
						$Clipboard =& $tmp_BasisElement->getClipboard($param_HostAccount, $param_iStorable, $FncVar_BasisClipboardnameArray[0]);
						break;
					}
				}
			}

			if((boolean)($Clipboard)) {
				BasisClipboard::loadObjectLevel($param_HostAccount, $param_iStorable, $Clipboard, 'Edit' );
			} else if ($tmp_BasisElement->ThisClassName == 'Profile') {
			} else {
				return Error::newError('NoLoadBE','Fehler BasisClipboard '.$BasisClipboardname[0].' konnte nicht geladen werden',"");
			}

		}


		if(array_key_exists('Delete', $param_BEData[$the_BasisElementName]) && $param_BEData[$the_BasisElementName]['Delete'] && $param_BEData[$the_BasisElementName]['MTextID']) {
//D::show($param_BEData[$the_BasisElementName],'delete object',1,1);
			if(is_object($Clipboard) && $BasisClipboardname[0] != 'none'){
				$BasisElement =& $Clipboard->get($param_HostAccount, $param_iStorable, $param_BEData[$the_BasisElementName]['MTextID']);
			}
			else if($tmp_BasisElement->ThisClassName != 'Profile'){
				$BasisElement =$tmp_BasisElement;
			}

			if($BasisElement) {
				if(BasisElement::deleteObject($param_HostAccount, $param_iStorable, $BasisElement)){
					Message::newMessage('post_del', $BasisElement->ThisClassName,$param_BEData[$the_BasisElementName]['Subject']);
				}
			}
			else {
				$param_HostAccount->resetAuthorProfileID();
				return Error::newError('NoLoadBE','Fehler BasisElement '.$_POST['MTextID'].' konnte nicht geladen werden',"");
			}

		}
		else if(Form::checkDataCompleteness($param_BEData[$the_BasisElementName], $the_BasisElementName)) {

			Form::replaceData($param_BEData[$the_BasisElementName]);
D::show($the_PartOfID,'$the_PartOfID');
			if(is_object($Clipboard) && ( $BasisClipboardname[0] != 'none' && $param_BEData[$the_BasisElementName]['MTextID']!=$the_PartOfID )){
				$BasisElement =& $Clipboard->get($param_HostAccount, $param_iStorable, $param_BEData[$the_BasisElementName]['MTextID']);
			} else{
				$BasisElement =$tmp_BasisElement;
			}

			if(is_object($BasisElement)) {
				BasisElement::loadObjectLevel($param_HostAccount, $param_iStorable, $BasisElement, 'Edit' );

//			śearchCodeAuthorisationList 100613
//D::show($param_BEData[$the_BasisElementName]['RightManagement']['AuthorisationList'],$the_BasisElementName );
//				$WriteProfileIDList = $ReadProfileIDList = array();
//				Form::manageRightFromForm($param_HostAccount, $param_iStorable, $param_BEData[$the_BasisElementName]['RightManagement'], $ReadProfileIDList, $WriteProfileIDList, $BasisElement->ThisClassName);//, $ReadPropfileCollection);
//D::show($ReadProfileIDList, 'ReadProfileIDList');
//D::show($WriteProfileIDList, 'WriteProfileIDList');
////D::li($param_BEData[$the_BasisElementName]['ReadKind'].' = '. CONSTKindOf::BC_RIGHT_ALL.' -- '.$the_BasisElementName,1,1);
//				/**
//				 * @todo muss anscih wo anders hin ... im Zuge der AuthorisationList => RightMAnagement Aktion sollte das passieren 100428 @f
//				 */
//				if(array_key_exists('ReadKind', $param_BEData[$the_BasisElementName]) && ($param_BEData[$the_BasisElementName]['ReadKind']== CONSTKindOf::BC_RIGHT_LOG || $param_BEData[$the_BasisElementName]['ReadKind']==CONSTKindOf::BC_RIGHT_ALL)){
//					unset($param_BEData[$the_BasisElementName]['RightManagement']['RMProfileCollection']['ReadSelected']);
//				}
//
//
//				/*if(!$ReadPropfileCollection && array_key_exists('RightManagement',$param_BEData[$the_BasisElementName]) && array_key_exists('RMProfileCollection',$param_BEData[$the_BasisElementName]['RightManagement']) && array_key_exists('ReadSelected',$param_BEData[$the_BasisElementName]['RightManagement']['RMProfileCollection'])){
//					unset($param_BEData[$the_BasisElementName]['RightManagement']['RMProfileCollection']['ReadSelected']);
//				}*/
//				/**
//				 *	setAuth edit
//				 */
//				$BasisElement->getObjVar('RightManagement')->getAuthorisationList()->updateAuthList($ReadProfileIDList, $WriteProfileIDList);

				$BasisElement->setData($param_HostAccount, $param_iStorable, $param_BEData[$the_BasisElementName]);

				if($BasisClipboardname[0] != 'none'){
					Form::updateAddToLinkList ($param_HostAccount, $param_iStorable, $param_Tree, $tmp_BasisElement, $BasisClipboardname[0], $BasisElement, $param_AddData);
				}

				$BasisElement->saveObject($param_HostAccount, $param_iStorable);
				if(is_object($Clipboard)) {
					Message::newMessage('edit', $Clipboard->ThisClassName,$param_BEData[$the_BasisElementName]['Subject']);
				}


				if(is_array($param_FunctionData)) {
					foreach($param_FunctionData as $kind => $value){
						PostController::post_fnc($param_HostAccount, $param_iStorable,$kind, $value, $BasisElement->getID(),$BasisElement);
					}
				}
				$param_HostAccount->resetAuthorProfileID();
			} else {
				$param_HostAccount->resetAuthorProfileID();
				return Error::newError('NoLoadBE','Fehler BasisElement '.$_POST['MTextID'].' konnte nicht geladen werden',"");
			}
		}
		$param_HostAccount->resetAuthorProfileID();
	}

	/**
	 *
	 * function calls static or object based post_functions
	 *
	 */
	public static function post_fnc(Account &$param_HostAccount, iStorable &$param_iStorable, $param_kind, $param_value, $param_makeNewID, $param_ObjectBE = false){

		if($param_kind == 'Object'){

			if(array_key_exists('post_action', $param_value) && $param_value['post_action']){

				foreach($param_value['post_action'] as $key => $index){

					if(array_key_exists('post_fnc', $param_value) && $param_value['post_fnc'] && array_key_exists($index, $param_value['post_fnc'])){

						$the_function = $param_value['post_fnc'][$index];
	//$the_function = $param_value['post_fnc'][$index];
						if(array_key_exists('PartOfID', $param_value) && array_key_exists($index, $param_value['PartOfID']) && $param_value['PartOfID'][$index]){

							if($param_value['PartOfID'][$index] == 'makeNew'){
								$param_value['PartOfID'][$index] = $param_makeNewID;
								$array_PartOfID = array($param_makeNewID);
							}
							else if($param_value['PartOfID'][$index] == 'allProfileIDs'){
								$array_PartOfID = array();
								foreach($param_value['Param'][$the_function][$index] as $keyID => $value){
									$array_PartOfID[]=$keyID;
								}
							}
							else{
								$array_PartOfID = array($param_value['PartOfID'][$index]);
							}

							foreach($array_PartOfID as $var_PartOfID){

								if($param_value['PartOfID'][$index] == 'allProfileIDs'){
									$param_HostAccount->setAuthorProfileID($var_PartOfID);
								}

								if($the_Object = $param_ObjectBE){}
								else{
									$the_Object =& MultimediaText::loadElement($param_HostAccount, $param_iStorable, null, $var_PartOfID);
								}

								/**
								 * @todo  ... wird hier die Variable $the_Object unterschiedlich überschrieben ... weil das ist ja schon ein wenig irrefürhrend (einmal BE, einmal BC) 100320 @f
								 */
								if(is_object($the_Object) && array_key_exists('BasisClipboardName', $param_value) && array_key_exists($index, $param_value['BasisClipboardName']) && $param_value['BasisClipboardName'][$index]){

									$FncVar_BasisClipboardnameArray = CONFIG::transformStringToArray($param_value['BasisClipboardName'][$index], 'category');
									if(is_subclass_of($FncVar_BasisClipboardnameArray[0], 'BasisClipboard') ) {

										$the_Object =& $the_Object->getClipboard($param_HostAccount, $param_iStorable, $param_value['BasisClipboardName'][$index]);

									}
								}
								if(is_object($the_Object)){
									/**
									 * @todo array_key 0 raus@um
									 */
								 	if(!(array_key_exists('Param',$param_value) && array_key_exists($the_function,$param_value['Param']) && array_key_exists(0,$param_value['Param'][$the_function]))){
										$var_value = array();
								 	}
								 	else if (array_key_exists($var_PartOfID,$param_value['Param'][$the_function][0])){
								 		$var_value = $param_value['Param'][$the_function][0][$var_PartOfID];
								 	}
								 	else{
								 		$var_value = $param_value['Param'][$the_function][0];
								 	}
									/**
									 * post_* Funktions Aufrufe von BasisClipboard/BasisElement (post_deny, post_del, post_add .....)
									 */
								 	D::show($var_value,'$var_value');
								 	D::show($the_function,'$the_function');
								 	D::show($index,'$index');

									$the_Object->$the_function($param_HostAccount, $param_iStorable, $var_value);
								}
							}
						}
					}
				}
			}
		} else if($param_kind == 'Static'){
			foreach($param_value as $fnc_Name => $postAction){
				if(array_key_exists('class',$postAction) && $postAction['class'] && array_key_exists('value',$postAction)){
					if(!array_key_exists($postAction['class'], self::$LinkClassArray)){
						self::$LinkClassArray [$postAction['class']] = new $postAction['class']();
					}
//					self::$LinkClassArray [$postAction['class']] -> $fnc_Name($postAction['value'] );
					/**
					 *  das sind doch keine statischen Funktionsaufrufe ? 100320 @f
					 *  
					 *  Natürlich oder ist zB die funktion post_setPreValues etwa nicht statisch?! 100817 @ um  
					 */
					self::$LinkClassArray [$postAction['class']] -> $fnc_Name($param_HostAccount, $param_iStorable, $postAction['value'] );
				}
			}
		}
		$param_HostAccount->resetAuthorProfileID();
	}

	/**
	 * 	sets the Array $PreValueArray
	 * @param Account 	&$param_HostAccount
	 * @param iStorable &$param_iStorable
	 * @param array 	$param_values
	 */
	private static function post_setPreValues(Account &$param_HostAccount, iStorable &$param_iStorable, array $param_values){
		self::$PreValueArray = $param_values;
	}

	public static function appendDOMPreValues($param_DOMDocument,$param_DOMElement){
		if(is_array(self::$PreValueArray)){
			$DOMElement_PreValue = $param_DOMDocument->createElement('PreValues');
			foreach(self::$PreValueArray as $key => $value){
				if(is_array($value)){
					$DOMElement_Key = $param_DOMDocument->createElement($key);
					foreach($value as $key2 => $value2){
						$DOMElement_Value=$param_DOMDocument->createElement('Element',$value2);
						$DOMElement_Value -> setAttribute('key',$key2);
						$DOMElement_Key -> appendChild($DOMElement_Value);
					}
					$DOMElement_PreValue->appendChild($DOMElement_Key);
				}
				else{
					$DOMElement_PreValue->appendChild($param_DOMDocument->createElement($key,$value));
				}
			}
			$param_DOMElement->appendChild($DOMElement_PreValue);
		}
	}
}
?>