class_AddonManager.inc.php 4.53 KB
<?php
/**
 * @file	AddonManager.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 AddonManager {
	private $AddonArray = array();
	private $Object;
	private $DB;

	public function __construct(iStorable $param_Database, &$param_Object=null) {
		$this->DB = $param_Database;
		if(is_object($param_Object)) {
			$this->Object = &$param_Object;
		}
	}

	public function delete(){
		if(is_array($this->AddonArray)) {
			foreach($this->AddonArray as &$var_ClassObj) {
				if(is_object($var_ClassObj)) {
					$var_ClassObj->delete();
				}
			}
		}
	}

	public function &getAddon(Account &$param_HostAccount, iStorable &$param_iStorable, $param_Addon, $param_Parameter=null) {

		if(is_subclass_of($param_Addon, 'aBasisElementAddon')) {
			$this->loadAddon($param_HostAccount, $param_iStorable, $param_Addon, $param_Parameter);
			return $this->AddonArray[$param_Addon];
		} else {
			return false;
		}
	}
	public function setAddon(aBasisElementAddon &$param_Addon) {

		$this->AddonArray[get_class($param_Addon)] = &$param_Addon;

	}

	public function &getObjVar($param_ObjVar) {
		$ObjVar = trim($param_ObjVar);
		return $this->$ObjVar;
	}

	public function loadAddon(Account &$param_HostAccount, iStorable &$param_iStorable, $param_ClassName, $param_Parameter) {
		if((!array_key_exists($param_ClassName, $this->AddonArray) || !is_object($this->AddonArray[$param_ClassName])) && is_subclass_of($param_ClassName, 'aBasisElementAddon')) {
			$this->AddonArray[$param_ClassName] = new $param_ClassName($param_iStorable, $this->Object);
		} else if(!is_object($this->AddonArray[$param_ClassName])) {
			return Error::newError('DevError','NotImplementaded','Addon '.$param_ClassName.' für '. $this->Object.' existiert nicht!');
		}

		$this->AddonArray[$param_ClassName]->loadObject($param_HostAccount, $param_iStorable, $param_Parameter);

		/**
		 * @if
		 *
		 *	'aBasisElementAddon' => array (
		 *		'Logger' => array(
		 * 			'function' => array(
		 *				'addOneToVisited' => array('LoadLevel' => true),
		 *			)
		 *		)
		 *	),
		 */
		if(is_array($param_Parameter) and !empty($param_Parameter)) {
			foreach($param_Parameter as $var_BEAKey => $var_BEAParameter) {
				if($var_BEAKey == 'function' and is_array($var_BEAParameter) and !empty($var_BEAParameter)) {
					foreach($var_BEAParameter as $var_fncName => $var_fncParameter) {
						if(is_array($var_fncParameter) && array_key_exists('LoadLevel',$var_fncParameter) && $var_fncParameter['LoadLevel']=='Edit'){
							$var_fncParameter['AuthorProfileID']=$param_HostAccount->getAuthorProfileID();
						}
						$this->AddonArray[$param_ClassName]->$var_fncName($var_fncParameter);
					}
				}
			}
		}

	}

	public function saveAddonObjects(Account &$param_HostAccount, iStorable &$param_iStorable) {
		foreach($this->AddonArray as  &$var_ClassObj) {
			if(is_object($var_ClassObj)) {
				$var_ClassObj->saveObject($param_HostAccount, $param_iStorable);
			}
		}
	}

	public function setData(Account &$param_HostAccount, iStorable &$param_iStorable, &$param_Data) {
		if(is_array($this->AddonArray)) {
			foreach($this->AddonArray as &$var_ClassObj) {
				if(is_object($var_ClassObj)) {
					$var_ClassObj->setData($param_HostAccount, $param_iStorable, $param_Data);
				}
			}
		}
	}

	public function getObjDOMElement(Account &$param_HostAccount, iStorable &$param_iStorable, DOMDocument $param_DOMDocument, $param_KindOf) {
		/**
		 * @foreach get the aBasisElementAddon DOMElements
		 */

		if(!empty($this->AddonArray)) {
			$var_BEADOMElement = $param_DOMDocument->createElement('BasisElementAddons');
			foreach($this->AddonArray as &$var_ClassObj) {
				if(is_object($var_ClassObj)) {
					$var_BEADOMElement->appendChild($var_ClassObj->getObjDOMElement($param_HostAccount, $param_iStorable, $param_DOMDocument, $param_KindOf));
				}
			}
			return $var_BEADOMElement;
		}

	}

}

?>