class_DataTransformator.inc.php 9.79 KB
<?php
/**
 * @file	DataTransformator.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 DataTransformator
 *
 *	Klasse die die Umwandlung der Datenbanktabellen in Objekte voll zieht
 *	@version 		0.2.0803
 *	@author			dev [at] mensch [dot] coop
 */
class DataTransformator {

	/****************************************************************************************
	 *																						*
	 *	Schnittstellenfunktionen zwischen diese Klasse und der DB Klasse (bzw Datenbank)	*
	 *																						*
	 ****************************************************************************************/
	/**
	 *	Schnittstellenfunktion zwischen Klasse Profile und der Datenbank
	 *	-	wandelt die Profil Daten aus der Tabelle in eine ProfileBCe um
	 *	-	könnte evt. auch in die Datenbank Klasse  oder in BasisElement
	 *
	 *
	 *	@return Object List
	 */
	public static function &transformDBTableToObjectList(Account &$param_HostAccount, iStorable &$param_iStorable, $tablerow, $KindOf=null, $PartOf=null, array &$out_ObjecCloakArray = null){
		if(!is_array($out_ObjecCloakArray)) {
			$out_ObjecCloakArray = array();
		}
		$List = array();
		for($i=0; $i<count($tablerow); $i++) {

			if(is_array($tablerow[$i])) {
				$out_ObjecCloakArray[$i] =& self::transformDBTableRowToObject($param_HostAccount, $param_iStorable,$tablerow[$i]);
				$List[$i] = & $out_ObjecCloakArray[$i]->getObject();
			} else {
				D::backtrace('Array  existierte nicht');
				return false;
			}
		}
		return $List;
	}

	private static function &transformDBTableRowToMTextObject(Account &$param_HostAccount, iStorable &$param_iStorable, array $tablerow, BasisElement &$Object=null) {

		if(is_subclass_of($Object, "BasisElement")) {
			$tablerow["Author_NickName"] = Profile::IDToNickName($param_iStorable, $tablerow["Author_ProfileID"]);
			$tablerow["Author_Avatar"] = Profile::IDToAvatar($param_iStorable, $tablerow["Author_ProfileID"]);
			$tablerow["Author_Signature"] = Profile::IDToSignature($param_iStorable, $tablerow["Author_ProfileID"]);
			$tablerow["AuthorProfile"]=null;
			$tablerow["ReadPrivate"] = $tablerow["AuthReadListID"];
			$tablerow["WritePrivate"] = $tablerow["AuthWriteListID"];

//if($tablerow['ID']=='2355') {
//	D::show($tablerow['@write'], '------- '.__FUNCTION__.$param_BEID,1,1);
//}


			if(is_array($tablerow['@write']) && !empty($tablerow['@write'])) {

				$Object->getObjVar('RightManagement')->setWriteRightProfiles($tablerow['@write']);



//				$tablerow['WriteRight'] = false;
//				foreach($tablerow['@write'] as $var_PID => $var_PID_states) {
//					if(array_key_exists('WriteRight', $var_PID_states)) {
//						$tablerow['WriteRight'] = $var_PID_states['WriteRight'];
//						break;
//					} else if($var_PID == 'profilecollection') {
//						foreach($var_PID_states as $var_PCID => $var_PCID_states) {
//							if(array_key_exists('WriteRight', $var_PCID_states)) {
//								$tablerow['WriteRight'] = $var_PCID_states['WriteRight'];
//								break;
//							}
//						}
//					}
//				}
				$tablerow['WriteRight'] = $Object->getObjVar('RightManagement')->isWriteAble($param_HostAccount);
//				D::li(,1,1);


			}

			if(is_array($tablerow['@read']) && !empty($tablerow['@read'])) {
				$Object->getObjVar('RightManagement')->setReadRightProfiles($tablerow['@read']);
				$tablerow['ReadRight'] = true;
			}

			$Object->setRequestProfileID($param_HostAccount->getActiveProfileID());
			$Object->setData($param_HostAccount, $param_iStorable, $tablerow);
			$Object->getObjVar('RightManagement')->setAuthorisationList(AuthorisationList::load($param_HostAccount, $param_iStorable, $tablerow["AuthReadListID"], $tablerow["AuthWriteListID"]));
			$Object->getObjVar('RightManagement')->loadObject($param_HostAccount, $param_iStorable);

		} else if($tablerow['losp_object']) {
			$Object = &$tablerow['losp_object'];
		} else D::h1("FEHLER warum passiert das");


		if(array_key_exists('PartOfID', $tablerow) && $tablerow['PartOfID']) {

			$var_PartOfArray = array('PartOfID'=>$tablerow['PartOfID']);
			if(array_key_exists('PartOfObjVar', $tablerow)) {
				$var_PartOfArray['PartOfObjVar'] = $tablerow['PartOfObjVar'];
				$var_PartOfArray['AttachID']  = $tablerow['AttachID'];
			}
			if(array_key_exists('CDate', $tablerow)) {
				$var_PartOfArray['CDate']  = $tablerow['CDate'];
			}
			if(array_key_exists('PartOfObjVarID', $tablerow)) {
				$var_PartOfArray['PartOfObjVarID']  = $tablerow['PartOfObjVarID'];
			} else {
				$var_PartOfArray['PartOfObjVarID'] = BasisClipboard::PartObjVarToID($param_iStorable,$var_PartOfArray['PartOfObjVar']);
			}

			$var_PartOfArray['AttachStateID']  = $tablerow['AttachStateID'];
			$var_PartOfArray['LinkerProfileID']  = $tablerow['LinkerProfileID'];
			if(array_key_exists('CategoryID', $tablerow)) {
				$var_PartOfArray['CategoryID']  = $tablerow['CategoryID'];
				$var_PartOfArray['CategoryName']  = $tablerow['CategoryName'];
			}

			$Object->addToPartOfDataArray($tablerow['PartOfID'], $var_PartOfArray);

			$var_CloakObject = &CloakObject::createCloakObject($Object);
			$var_CloakObject->setObjVar('PartOfID', $tablerow['PartOfID']);
		} else {
			$var_CloakObject = &CloakObject::createCloakObject($Object);
		}
		return $var_CloakObject;
	}

	/**
	 * @todo wozu gibt es den $Object  Parameter !!!! ? 100318 @f
	 *
	 *	Schnittstellenfunktion zwischen Klasse Profile und Klasse DB (bzw der Datenbank)
	 *	-	wandelt die Profil Daten aus der TabelleZeile in ein Profil um
	 *	-	könnte evt. auch in die Datenbank Klasse
	 *
	 *	@return an CloakObject Object (inside is the real Object)
	 */
	public static function &transformDBTableRowToObject(Account &$param_HostAccount, iStorable &$param_iStorable, array $tablerow, &$Object=null){
		$var_Data = array();

		if(is_array($tablerow)) {
			if( array_key_exists('losp_object', $tablerow) && is_object($tablerow['losp_object'])) {

				if(is_subclass_of($tablerow['losp_object'], 'BasisElement')) {

					$var_CloakObject  = &self::transformDBTableRowToMTextObject($param_HostAccount, $param_iStorable, $tablerow);
					return $var_CloakObject;
				} else {
					D::li('Fehler');
					return false;
				}

			} else {
				if(!$Object) {

					if((!array_key_exists('losp_classname', $tablerow) || !$tablerow['losp_classname'] ) && array_key_exists('ClassID', $tablerow)) {
						$tablerow['losp_classname'] = $param_iStorable->getClassName($tablerow['ClassID']);
					}

					if(array_key_exists('losp_classname', $tablerow)) {
						if(is_subclass_of($tablerow['losp_classname'],"BasisElement")){
							if(array_key_exists('PartOfID', $tablerow)) {
								$var_PartOf =& MultimediaText::loadElement($param_HostAccount, $param_iStorable, $param_MultimediaTextClassName='*', $tablerow['PartOfID']);
							} else {
								$var_PartOf = null;
							}

							$Object = BasisElement::createNewInstance($param_iStorable, $tablerow['losp_classname'], $tablerow['ID'], $var_PartOf);
						} else if(is_subclass_of($tablerow['losp_classname'],"Authorisation")) {
							$Object = new $tablerow['losp_classname']($param_iStorable, 0, 0, 0, 0);
						} else if($tablerow['losp_classname']=='') {
							D::h1('darf nie mals nie vorkommen');
							return false;
						}
					} else {
						D::h1('darf nie mals nie vorkommen: losp_classname muss vorkommen (sql:select Befehl ist falsch)');
						return false;
					}
				}


				/*** Account	*/
				if($Object->ThisClassName=='Account') {
					$Object->setData($param_HostAccount, $param_iStorable, $tablerow);
					$var_CloakObject = &CloakObject::createCloakObject($Object);
					return $var_CloakObject;

					/***	Clipboard	*/
				} else if(is_subclass_of($Object, 'BasisClipboard')) {
					$CloakObjectArray = array();
					self::transformDBTableToObjectList($param_HostAccount, $param_iStorable, $tablerow, CONFIG::getBC_MultimediaText(get_class($Object).'::'.$Object->getObjVar('CategoryName')), $Object, $CloakObjectArray);

					foreach($CloakObjectArray as $CloakObject) {
						$Object->addToAttachIDList($CloakObject->getObject()->getObjVar("ID"), $CloakObject->getObjVar('PartOfID'), $CloakObject->getObject()->getPartOfDataArray($CloakObject->getObjVar('PartOfID')));
					}
					$var_CloakObject = &CloakObject::createCloakObject($Object);
					return $var_CloakObject;

					/***	ReadAuthorisation / WriteAuthorisation*/
				} else if($Object->ThisClassName=="ReadAuthorisation" || $Object->ThisClassName=="WriteAuthorisation") {
					$Object->setListID($tablerow["ListID"]);
					$Object->setProfileID($tablerow["ProfileID"]);
					$Object->setProfileNickName($tablerow["NickName"]);
					$var_CloakObject = &CloakObject::createCloakObject($Object);
					return $var_CloakObject;

				} else if(is_subclass_of($Object, 'BasisElement')) {
					$var_CloakObject = &self::transformDBTableRowToMTextObject($param_HostAccount, $param_iStorable, $tablerow, $Object);

					MultimediaText::addMTextToArray($Object);
					if(is_subclass_of($Object, 'BasisElement') ) {
						BasisElement::loadObjectLevel($param_HostAccount, $param_iStorable, $Object, 'Always' );
					}

					return $var_CloakObject;
				} else {
					D::h1('nichts passiert, für diesen fall ist noch nichts implementiert');
					return false;
				}
			}

		}
		return FALSE;
	}
}
?>