class_TreeManipulator.inc.php 16.4 KB
<?php
/**
 * @file	TreeManipulator.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 TreeManipulator {

	/**
	 * @var array of the TreeManipulatorCallback objects
	 */
	private static $TreeManipulatorCallbackArray = array();

	/**
	 * return the TreeManipulatorCallback object
	 */
	public static function &getTreeManipulatorCallback($param_TMC) {
		if(!array_key_exists($param_TMC, self::$TreeManipulatorCallbackArray) || !is_object(self::$TreeManipulatorCallbackArray[$param_TMC])) {
			if(is_subclass_of($param_TMC, 'iTreeManipulatorCallback')) {
				self::$TreeManipulatorCallbackArray[$param_TMC] = new  $param_TMC();
			} else {
				D::li('Fehler is_subclass_of('.$param_TMC.', iTreeManipulatorCallback)=('.is_subclass_of($param_TMC, 'iTreeManipulatorCallback').'): TreeManipulator::&getTreeManipulatorCallback('.$param_TMC.')');
				return false;
			}
		}
		return self::$TreeManipulatorCallbackArray[$param_TMC];
	}

	/**
	 * gibt das letzte (höchste) BasisClipboard/BasisElement Element welches im Array existiert zurück
	 */
	public static function getLastBasisClipboardFromUrlArray(array $param_UrlArray, $param_GetBasisClipboardCategory=false) {
		$var_max = count($param_UrlArray)-2;

		for($i=$var_max; $i>=0; $i--) {
			$var_UrlTmp = CONFIG::transformStringToArray($param_UrlArray[$i],'category');
			if (class_exists( $tmp=array_shift($var_UrlTmp))
			//&&  (is_subclass_of($param_UrlArray[$i], 'BasisClipboard') || is_subclass_of($param_UrlArray[$i], 'BasisElement'))
			) {
				if(!$param_GetBasisClipboardCategory) {
					return $tmp;
				} else {
					return $param_UrlArray[$i];
				}
			}
		}
		return false;
	}

	public static function getLastShowSingleIDFromUrlArray(array $param_UrlArray) {
		$var_max = count($param_UrlArray)-2;

		for($i=$var_max; $i>=0; $i--) {
			$var_UrlTmp = CONFIG::transformStringToArray($param_UrlArray[$i],'category');
			if (class_exists( $tmp=array_shift($var_UrlTmp))
			//&&  (is_subclass_of($param_UrlArray[$i], 'BasisClipboard') || is_subclass_of($param_UrlArray[$i], 'BasisElement'))
			) {
				if($param_UrlArray[$i+1]=='showSingle') {
					if($param_UrlArray[$i+2]=='ID') {
						if($param_UrlArray[$i+3]) {
							return $param_UrlArray[$i+3];
						}
					}
					return false;
				} else {
					for($j=$i;$j<$var_max;$j++) {
						unset($param_UrlArray[$j]);
					}
					return TreeManipulator::getLastShowSingleIDFromUrlArray($param_UrlArray);
				}
				return $param_UrlArray[$i];

			}
		}
		return false;
	}

	public static function getLastHistoryActionFromUrlArray(array $param_UrlArray) {
		$var_max = count($param_UrlArray)-1;

		$var_HistoryConfig = CONFIG::getHistory();
		//D::show($var_HistoryConfig);
		//D::show($param_UrlArray, '$param_UrlArray');
		for($i=$var_max; $i>=0; $i--) {
			if ( class_exists($param_UrlArray[$i])
			&&  (is_subclass_of($param_UrlArray[$i], BasisClipboard) || is_subclass_of($param_UrlArray[$i], BasisElement))
			&&	$i!=$var_max
			) {
				return FALSE;
			}

			if(array_search($param_UrlArray[$i], $var_HistoryConfig['PossibleAction'])!==false) {
				return $param_UrlArray[$i];
			}
		}
		return false;
	}

	/**
	 * transformiert ein Url Array in ein Url String um
	 *
	 * vorischtig klappt nicht mit c
	 */
	public static function transformArrayToUrl(array $param_UrlArray, BasisElement $param_PartOf=null, $param_BasisClipboardname=null) {
		//		D::backtrace();

		//		D::show($param_UrlArray, '$param_UrlArray');

		if(is_object($param_PartOf)) {

			if($param_PartOf->getObjVar('PartOfID')) {
				$PartOf_PartOfID = $param_PartOf->getObjVar('PartOfID');
			} else if(is_object($param_PartOf->getObjVar('PartOf'))) {
				$PartOf_PartOfID = $param_PartOf->getObjVar('PartOf')->getObjVar('ID');
			}
			/*if($PartOf_PartOfID) {


			}*/
		}

		$var_Url = '?';
		$url_index=0;
		$count_UrlArray = count($param_UrlArray);
		//		if($param_UrlArray[0]!='start'){
		//			D::backtrace();
		//			D::show($param_UrlArray);
		//		}
		for($i=1; $i<$count_UrlArray; $i++) {
			$var_Url .= 'p['.$url_index++.']='.$param_UrlArray[$i];
			if($i!=$count_UrlArray-1) {
				$var_Url .= '&';
			}
		}

		if($var_Url==='?' && $param_PartOf!==null && $param_BasisClipboardname!==null) {
			$var_Url .= 'p[0]=showSingle&p[1]=ID&p[2]='.$param_PartOf->getObjVar('ID').'&p[3]='.$param_BasisClipboardname;
			//D::ulli('if  '.$var_Url.' ||     '.$param_PartOf.'->'.$param_BasisClipboardname);

		} else {
			//D::ulli('else  '.$var_Url.' ||      '.$param_PartOf.'->'.$param_BasisClipboardname);
		}

		//		if($PartOf_PartOfID) {
		//			D::show($param_UrlArray, '$param_UrlArray');
		//			D::ulli($PartOf_PartOfID.' ->  '.$param_PartOf.'->'.$param_BasisClipboardname.'  || <b>'.$var_Url.'</b> ||   ');
		//		}
		//D::ulli($var_Url);
		return $var_Url;
	}

	//	@old @until 090806
	//	public static function transformArrayToUrl(array $param_UrlArray, BasisElement $param_PartOf=null, $param_BasisClipboardname=null) {
	//		$var_Url = '?';
	//		$url_index=0;
	//		$count_UrlArray = count($param_UrlArray);
	//		for($i=1; $i<$count_UrlArray; $i++) {
	//			$var_Url .= 'p['.$url_index++.']='.$param_UrlArray[$i];
	//			if($i!=$count_UrlArray-1) {
	//				$var_Url .= '&';
	//			}
	//		}
	//
	//		if($var_Url==='?' && $param_PartOf!==null && $param_BasisClipboardname!==null) {
	//			$var_Url .= 'p[0]=showSingle&p[1]=ID&p[2]='.$param_PartOf->getObjVar('ID').'&p[3]='.$param_BasisClipboardname;
	//
	//		}
	//		return $var_Url;
	//	}



	private static function callback_ShowSingle( $param_Account, MultimediaText $param_Object, $param_CurrentObjectName, $param_BasisClipboardname, $param_url, &$param_return) {
		D::li(" $param_Object -- $param_CurrentObjectName -- $param_BasisClipboardname");
		D::ulli(self::transformArrayToUrl($param_url));
	}

	public static $count_runTreeCallback_getBasisClipboard = 0;

	/**
	 *	callback function for the TreeManipulator::@see runTree function
	 *
	 *	Funktion sammelt Daten
	 */
	private static function runTreeCallback_getBasisClipboard( $param_Account, MultimediaText $param_Object, $param_CurrentObjectName, $param_BasisClipboardname, $param_url, &$param_return) {
		TreeManipulator::$count_runTreeCallback_getBasisClipboard++;

		if(!array_key_exists('result', $param_return) || !is_array($param_return['result'])) {
			$param_return['result'] = array();
		}
		if($param_return['BasisClipboard'] == $param_BasisClipboardname) {
			$param_return['result'][] = array(
												'url' => $param_url,
												'CurrentObjectName' => $param_CurrentObjectName,
												'BasisClipboardname' => $param_BasisClipboardname,
												'HostID' => $param_Object->getObjVar('ID'),
												'HostSubject' => $param_Object->getObjVar('Subject'),
												'HostClassname' => $param_Object->getObjVar('ThisClassName'),
												'HostObject' => $param_Object,
			);
		}
	}


	public static $count_runTree=0;
	/**
	 *	durchläuft den Vererbungsbaum und die angehängten Clipboards
	 *	führt die Callback Funktion bei jedem vererbten Klassennamen "Zustand" und jedem darangehängten BasisClipboard aus
	 *
	 *	@param string 			param_BasisClipboardname
	 *	@param Account 			param_Account (only callback parameter)
	 *	@param BasisElement 	param_PartOf=null (callback parameter)
	 *	@param int				param_GET_index=0 SINNLOS
	 *	@param Array 			param_url=null (callback parameter)
	 *	@param string			param_callback='fncShowSingle' callback function (definiert in der TreeManipulator Klasse oder im param_Object_test Objekt)
	 *	@param Array			& param_return Array welches in der Callback Funktion manipuliert werden kann (only callback parameter)
	 *	@param TestOfController param_Object_test =null (callback parameter)
	 *
	 */
	public static  function  runTree($param_BasisClipboardname, $param_Account,  $param_PartOf=null ,$param_GET_index= 0,$param_url=null,$param_callback='fncShowSingle', &$param_return, $param_Object_test =null){
		TreeManipulator::$count_runTree++;
		/** Make Arrays if not existant && get Data from the CONFIG class */
		{

			if($param_BasisClipboardname == 'start'){
				$param_url = array($param_BasisClipboardname);
			} else {
				$param_url[] = $param_BasisClipboardname;
			}


			if(!$FncVar_LastBasisClipboard = TreeManipulator::getLastBasisClipboardFromUrlArray($param_url)) {
				$FncVar_LastBasisClipboard = get_class($param_PartOf);
			}
			if($param_BasisClipboardname){
				$FncVar_BasisClipboardnameArray = CONFIG::transformStringToArray($param_BasisClipboardname, 'category');
				$var_BasisClipboardnameArray = CONFIG::transformStringToArray($param_BasisClipboardname, 'category');
				//				$param_FunctionName = CONFIG::getLinkForBC($param_BasisClipboardname);
			}
			$FncVar_LastBasisClipboard_MtextClass = CONFIG::getBC_MultimediaText($FncVar_LastBasisClipboard);
			$FncVar_BasisClipboardLink = CONFIG::getLinkForBC($param_BasisClipboardname);
		}
		/** Get Parent Multimediatext Classes and Clipboards */
		{
			if($param_BasisClipboardname == 'start' && $param_PartOf != null  ){
				$class_parents = class_parents($param_PartOf);
				$class_parents[get_class($param_PartOf)] =get_class($param_PartOf);
				unset($class_parents['BasisElement']);
				unset($class_parents['MultimediaText']);
				unset($class_parents['0']);
				/**
				 * fill the $class_parents array with BasisClipboards
				 */
				foreach ($class_parents as $MTextname) {

					$var_index =count($param_url);
					$param_url[$var_index] = $MTextname;
					//					D::li(''.$MTextname);
					if(!$param_Object_test){
						self::$param_callback( $param_Account, $param_PartOf, $MTextname, '', $param_url, $param_return,$param_Object_test);
					}else{
						$param_Object_test->$param_callback( $param_Account, $param_PartOf, $MTextname, $tmp_Clipboard=null, $param_url, $param_return,$param_Object_test);
					}

					if($MTextname) {
						$class_parents[$MTextname]= CONFIG::getMultimediaTextPreferencs($MTextname, 'BasisClipboards');
					}
					if(is_array($class_parents[$MTextname]) ){
						foreach ($class_parents[$MTextname] as $Clipboard => $ClipboardInfo) {

							$var_index_1 =count($param_url);
							$param_url[$var_index_1] = $Clipboard;
							//							D::ulli($MTextname.' >=> '.$Clipboard);
							if(!$param_Object_test){
								self::$param_callback( $param_Account, $param_PartOf, $MTextname, $Clipboard, $param_url, $param_return,$param_Object_test);
							} else {
								$param_Object_test->$param_callback( $param_Account, $param_PartOf, $MTextname, $Clipboard, $param_url, $param_return,$param_Object_test);
							}
							unset($param_url[$var_index_1] );
						}
					}
					unset($param_url[$var_index] );
				}
			}
		}
	}

	public static $count_runDBTree=0;
	/**
	 *	durchlaufe den Objekt Datenbank Baum (mit seinen verknüpften Objekten)
	 *
	 *	begin with the object param_RootArray(ID=>int) and run until param_Depth
	 *
	 *	@param iBasicSql 			param_DB 						(callback parameter)
	 *	@param int 					param_RequestProfileID 			(callback parameter)
	 *	@param array 				param_RootArray 				array( 'ID' => int)  (callback parameter)
	 *	@param string 				param_BasisClipboardname=null	beschränke den Objekt Datenbaum (DB->getFromClipboard parameter)
	 *	@param array				param_callback 					callback funktion's array('callback_fnc_name' => true)
	 *	@param 						&param_return 					Array welches in der Callback Funktion manipuliert werden kann (callback parameter) Ergebnis in $param_return[callback*][result]
	 *	@param int 					param_Depth=0 					run tree until depth
	 *	@param						param_KindOfObj=null			(DB->getFromClipboard parameter)
	 *	@param						param_State=null				(DB->getFromClipboard parameter)
	 *	@param array 				param_ConditionArray=null 		(DB->getFromClipboard parameter)
	 *	@param boolean				param_checkWrite=null 			(DB->getFromClipboard parameter)
	 */
//	public static function runDBTree(iBasicSql $param_DB, $param_RequestProfileID, array $param_RootArray, $param_BasisClipboardname=null, array $param_callback, array &$param_return, $param_Depth=0, $param_KindOfObj=null, $param_State=null, array $param_ConditionArray=null, $param_checkWrite=null, array &$param_FinishIDArray=null) {
	public static function runDBTree(Account &$param_HostAccount, iStorable &$param_iStorable, array $param_RootArray, $param_BasisClipboardname=null, array $param_callback, array &$param_return, $param_Depth=0, $param_KindOfObj=null, $param_State=null, array $param_ConditionArray=null, $param_checkWrite=null, array &$param_FinishIDArray=null) {
//				D::li("TreeManipulator::runDBTree(iBasicSql $param_iStorable, $param_RequestProfileID, array param_RootArray[ID]=<b>".$param_RootArray['ID']."</b>, BC=$param_BasisClipboardname, callback=$param_callback, return=$param_return, $param_Depth=0, KindOfObj=$param_KindOfObj, State=$param_State, ConArray=$param_ConditionArray, checkWrite=$param_checkWrite=null, $param_FinishIDArray)",1,1);
		TreeManipulator::$count_runDBTree++;
		//		@new
		if(true) {
			if(array_key_exists('ID', $param_RootArray) && $param_RootArray['ID'] && $param_callback) {
				if(!is_array($param_FinishIDArray)) {
					$param_FinishIDArray=array();
				}

				foreach($param_callback as $var_TreeManipulatorClass => $var_function) {
					$var_TreeManipulatorObj = TreeManipulator::getTreeManipulatorCallback($var_TreeManipulatorClass);
					if($var_TreeManipulatorObj) {
						if(!array_key_exists($var_TreeManipulatorClass, $param_return)) {
							$param_return[$var_TreeManipulatorClass] = array();
						}
						/**
						 * CallbackFunction wird auf gerufen ... definiert in der  CONFIG::$MultimediaTextPreferencs[BasisElementName]['Config']['CallbackFunction']
						 *	Funktion muss abgeleitet sein von: aTreeManipulatorCallback::callbackFunction(Account &$param_HostAccount, iStorable &$param_iStorable, array $param_RootArray, &$param_return)
						 */
						$var_TreeManipulatorObj->$var_function( $param_HostAccount, $param_iStorable, $param_RootArray, $param_return[$var_TreeManipulatorClass]);
						//					D::show($param_return, '$param_return');
					}
				}

				if($param_Depth>0) {
					$var_ClipboardIDArray = $param_iStorable->getFromClipboard($param_HostAccount, $param_RootArray['ID'], $param_BasisClipboardname, $param_KindOfObj, $param_State, $param_ConditionArray, $param_checkWrite);

					foreach($var_ClipboardIDArray as $var_ChildArray) {
						if(!array_key_exists($var_ChildArray['ID'], $param_FinishIDArray) || !$param_FinishIDArray[$var_ChildArray['ID']]) {
							$param_FinishIDArray[$var_ChildArray['ID']] = true;
							self::runDBTree($param_HostAccount, $param_iStorable, $var_ChildArray, $param_BasisClipboardname, $param_callback, $param_return, $param_Depth-1, $param_KindOfObj, $param_State, $param_ConditionArray, $param_checkWrite, $param_FinishIDArray);
						}
					}
				}
			} else {
				return false;
			}
		}

		//		@old @until 090507
		if(false){
			if ($param_callback) {
				foreach($param_callback as $var_TreeManipulatorClass => $var_function) {
					$var_TreeManipulatorObj = TreeManipulator::getTreeManipulatorCallback($var_TreeManipulatorClass);
					if($var_TreeManipulatorObj) {
						if(!array_key_exists($var_TreeManipulatorClass, $param_return)) {
							$param_return[$var_TreeManipulatorClass] = array();
						}
						$var_TreeManipulatorObj->$var_function($param_HostAccount, $param_iStorable,  $param_RootArray, $param_return[$var_TreeManipulatorClass]);
						//					D::show($param_return, '$param_return');
					}
				}

				if($param_Depth>0) {
					$var_ClipboardIDArray = $param_iStorable->getFromClipboard($param_HostAccount, $param_RootArray['ID'], $param_BasisClipboardname, $param_KindOfObj, $param_State, $param_ConditionArray, $param_checkWrite);

					foreach($var_ClipboardIDArray as $var_ChildArray) {
						self::runDBTree($param_HostAccount, $param_iStorable, $var_ChildArray, $param_BasisClipboardname, $param_callback, $param_return, $param_Depth-1, $param_KindOfObj, $param_State, $param_ConditionArray, $param_checkWrite);
					}
				}
			} else {
				return false;
			}
		}
	}
}
?>