class_Category.inc.php 12.6 KB
<?php
/**
 * @file	class_Category.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 Category extends aBasisElementAddon {
	protected $MText = null;
	protected $BasisClipboard = null;
	protected $CategoryChoiceArray;

	/**
	 *
	 * @var array the Categorys which are link with the self::$MText / self::$BasisClipboard Object
	 */
	protected $CategoryArray =null;

	public function __construct(iStorable &$param_DB, &$param_Obj) {
		if(is_subclass_of($param_Obj, 'BasisElement')) {
			$this->MText =& $param_Obj;
		} else if(is_subclass_of($param_Obj, 'BasisClipboard')) {
			$this->BasisClipboard =& $param_Obj;
		}

		$this->DB =& $param_DB;
		$this->depth = CONFIG::getConfig('CategoryDepth');
		$this->ThisClassName = get_class($this);

	}

	public function delete(){
		$var_BEADB = &$this->DB->getBasisElementAddonDataBase($this->ThisClassName);
		if($var_BEADB) {
			$var_BEADB->deleteCategorysForBE($this->DB, $this->MText->getObjVar('BECategoryListID'));
		}
	}


	private static $DefaultCategoryArray = null ;
	private static function &getDefaultCategoryArray(Account &$param_HostAccount, iStorable &$param_iStorable) {
		if(!Category::$DefaultCategoryArray) {
			$var_CategoryBEADB = & $param_iStorable->getBasisElementAddonDataBase('Category');
			/**
			 * @todo Achtung dadurch das RequestProfileID=null ist, wird einmal CategoryBEADB::getCategoryChoice zuviel aufgerufen, da es ansonsten gecachet werden würde ;) ...
			 */
			Category::$DefaultCategoryArray = $var_CategoryBEADB->getCategoryChoice($param_HostAccount, $param_iStorable);
		}

		return Category::$DefaultCategoryArray;
	}

	private static $DefaultCategoryDOMElement = null;
	private static function getDefaultCategoryDOMElement(Account &$param_HostAccount, iStorable &$param_iStorable, DOMDocument $param_DOMDocument,  $param_Selected=null) {
		if(!Category::$DefaultCategoryDOMElement) {
			$var_DefaultCategoryArray = &Category::getDefaultCategoryArray($param_HostAccount, $param_iStorable);
			Category::$DefaultCategoryDOMElement =	$param_DOMDocument->createElement('DefaultCategoryList');
			Category::setCategoryChoiceArrayDOMElement($param_DOMDocument, Category::$DefaultCategoryDOMElement, $var_DefaultCategoryArray, $param_Selected);
		}
		return Category::$DefaultCategoryDOMElement;
	}


	public function setData(Account &$param_HostAccount, iStorable &$param_iStorable, array $param_Data) {
		if(array_key_exists($this->ThisClassName, $param_Data)) {
			$this->CategoryArray = array();

			if(array_key_exists('form_newCategory', $param_Data[$this->ThisClassName]) && !empty($param_Data[$this->ThisClassName]['form_newCategory'])) {

				$Cats = Form::varcharToArray($param_Data[$this->ThisClassName]['form_newCategory']);
				$var_BEADB =  $param_iStorable->getBasisElementAddonDataBase('Category');
				$var_result = $var_BEADB->insertCategory($param_iStorable, $Cats);

				foreach($var_result as $var_key => $var_value) {
					if(array_key_exists('form_CategoryArray', $param_Data[$this->ThisClassName])) {
						$param_Data[$this->ThisClassName]['form_CategoryArray'][] = $var_key;
					}
					$this->CategoryArray[$var_key] = array('BECategoryName' => $var_value, 'BECategoryID' => $var_key);
				}
			}

			if(!array_key_exists('form_CategoryArray', $param_Data[$this->ThisClassName]) || !is_array($param_Data[$this->ThisClassName]['form_CategoryArray'])) {
				$param_Data[$this->ThisClassName]['form_CategoryArray'] = array();
			}


			//D::show($this->CategoryArray, '2');

			$var_CategoryArray = $this->CategoryArray;
			$this->CategoryArray = array();


			if(is_array($param_Data[$this->ThisClassName]['form_CategoryArray'])) {
				foreach( $param_Data[$this->ThisClassName]['form_CategoryArray'] as $CatKey) {
					if(array_key_exists($CatKey, $var_CategoryArray)) {
						$this->CategoryArray[$CatKey] = $var_CategoryArray[$CatKey];

						//@um vorläufige automatische setzung der hauptkategotien, muss noch generalisert werden
						if($CatKey<400 && $CatKey>99){
							$this->CategoryArray[] = floor($CatKey/100);
						}
					}
					else{
						$this->CategoryArray[$CatKey] = null;
					}

					unset($var_CategoryArray[$CatKey]);
				}
				foreach($var_CategoryArray as $var_key => $var_value) {
					$this->CategoryArray[$var_key] = $var_value;
				}

			}

			/*set Default Category*/
			if(array_key_exists('Category',TreeConfig::$AddonPreferencs) && array_key_exists('Default',TreeConfig::$AddonPreferencs['Category'])){
				foreach(TreeConfig::$AddonPreferencs['Category']['Default'] as $key => $value){
					if(!array_key_exists($key,$this->CategoryArray)){
						foreach($this->CategoryArray as $Catkey => $Catvalue){
							if($Catkey <= $value[1] && $Catkey >= $value[0]){
								$this->CategoryArray[$key] = '';
								break;
							}
						}
					}
				}
			}

		}
	}

	private static $Cache_CategoryArray = array();

	/**
	 *	importiert SimpleXMLElement,
	 *allerdings nur "selected" Categorien
	 */
	public function import( $param_ImportData) {
	//	D::show($param_ImportData, $this);
		if(get_class($param_ImportData)=='SimpleXMLElement') {
			foreach($param_ImportData->children() as $var_key => $var_value) {

				$var_attr=$var_value->attributes();

				if(!array_key_exists((string)$var_attr['BECategoryID'], self::$Cache_CategoryArray)) {
					self::$Cache_CategoryArray[(string)$var_attr['BECategoryID']] = array(
            	 			'BECategoryName' => (string)$var_attr['BECategoryName'],
							'BECategoryID' => (string)$var_attr['BECategoryID'],
							'BECategoryGroupID' =>'',
					);
				}

				if((string)$var_attr['selected']){
					//					D::ulli($this.' ## '.$var_attr['BECategoryName'].' -- BECategoryID: '.$var_attrc['BECategoryID']);
					$this->CategoryArray[(string)$var_attr['BECategoryID']] = self::$Cache_CategoryArray[(string)$var_attr['BECategoryID']];
					$this->CategoryArray[(string)$var_attr['BECategoryID']]['selected']=(string)$var_attr['selected'];
					//            		$this->CategoryArray[(string)$var_attr['BECategoryID']]=array(
					//            	 			'BECategoryName' => (string)$var_attr['BECategoryName'],
					//							'BECategoryID' => (string)$var_attr['BECategoryID'],
					//							'BECategoryGroupID' =>'',
					//							'selected'=>(string)$var_attr['selected']
					//            	 			);
					//D::show($this->CategoryArray[(string)$var_attr['BECategoryID']], $var_key.' ## '.$this);
				}


				if(array_key_exists('Category',$var_value)){
					foreach($var_value->children() as $var_ckey => $var_cvalue) {
						$var_attrc=$var_cvalue->attributes();

						if(!array_key_exists((string)$var_attrc['BECategoryID'], self::$Cache_CategoryArray)) {
							self::$Cache_CategoryArray[(string)$var_attrc['BECategoryID']] = array(
									'BECategoryName' => (string)$var_attrc['BECategoryName'],
									'BECategoryID' => (string)$var_attrc['BECategoryID'],
									'BECategoryGroupID' =>'',
							);
						}

						if((string)$var_attrc['selected']){
							//								D::ulli($this.' ## '.$var_attrc['BECategoryName'].' -- BECategoryID: '.$var_attrc['BECategoryID']);
							$this->CategoryArray[(string)$var_attrc['BECategoryID']] = self::$Cache_CategoryArray[(string)$var_attrc['BECategoryID']];
							$this->CategoryArray[(string)$var_attrc['BECategoryID']]['selected']=(string)$var_attrc['selected'];
							//							$this->CategoryArray[(string)$var_attrc['BECategoryID']]=array(
							//									'BECategoryName' => (string)$var_attrc['BECategoryName'],
							//									'BECategoryID' => (string)$var_attrc['BECategoryID'],
							//									'BECategoryGroupID' =>'',
							//									'selected'=>(string)$var_attrc['selected']
							//
							//									);
							//D::show($this->CategoryArray[(string)$var_attrc['BECategoryID']], $var_ckey.' ## '.$this);
						}
					}
				}

			}
			//D::show($this->CategoryArray, "$this ->CategoryArray")			;
			return true;
		} else {
			D::li('IMPORT DATA nur für SimpleXMLElement implementiert');
			return false;
		}
	}
	public function getObjDOMElement(Account &$param_HostAccount, iStorable &$param_iStorable, DOMDocument $param_DOMDocument, $param_KindOf=null, array $param_DOMElemente=null) {
		$DOMElement = parent::getObjDOMElement($param_HostAccount, $param_iStorable, $param_DOMDocument, $param_KindOf, $param_DOMElemente);
		if(is_object($this->MText)) {
			$DOMElement->setAttribute('MTextID', $this->MText->getObjVar('ID'));
		}

		$DOMElement->setAttribute('BasisClipboard', 'CategoryContentList');

		if($param_KindOf=='form') {
			$var_CategoryArray = array();
			$DOMElement->appendChild(BasisElement::getElementDOMElement($param_DOMDocument, $param_label=Language::getWord('newCategory'), $param_objvar=$this->ThisClassName.'][form_newCategory', $param_value=Form::arrayToVarchar($var_CategoryArray), $param_edit=true, $param_type='varchar', $param_length='64', $param_show=true));
			$DOMElement->appendChild(Category::getDefaultCategoryDOMElement($param_HostAccount, $param_iStorable, $param_DOMDocument, $this->CategoryArray));
		}

		{
			{
				$DOMList =	$DOMElement->appendChild($param_DOMDocument->createElement('List'));
				if(is_object($this->MText)) {
					$DOMList->setAttribute('class', 'BasisElement');
					$DOMList->setAttribute('ID', $this->MText->getObjVar('ID'));
					$DOMList->setAttribute('BECategoryListID', $this->MText->getObjVar('BECategoryListID'));
				} else if(is_object($this->BasisClipboard)){
					$DOMList->setAttribute('class', 'BasisClipboard');
					$DOMList->setAttribute('PartOfID', $this->BasisClipboard->getObjVar('PartOfID'));
				}
				$DOMList->setAttribute('label', Language::getWord($this->ThisClassName));
				$DOMList->setAttribute('objvar', $this->ThisClassName.'][form_CategoryArray');

			}

			//D::show($this->CategoryChoiceArray, '$this->CategoryChoiceArray');
			$tmp_CategoryArray = $this->CategoryArray;
			if(is_array($this->CategoryChoiceArray)) {
				if(!$tmp_CategoryArray) {
					$tmp_CategoryArray=null;
				}
				Category::setCategoryChoiceArrayDOMElement($param_DOMDocument, $DOMList, $this->CategoryChoiceArray, $tmp_CategoryArray);
			}

			if(is_array($tmp_CategoryArray)) {
				foreach($tmp_CategoryArray as $var_BECategoryID => $var_BECategoryArray) {
					$DOMElement_Child =DOMElementHelper::getDOMElementWithParameter($param_DOMDocument, $param_ElementName='Category', $var_BECategoryArray);
					$DOMList->appendChild($DOMElement_Child);
				}
			}
		}
		return $DOMElement;
	}

	private static function setCategoryChoiceArrayDOMElement(DOMDocument $param_DOMDocument, DOMElement &$param_DOMList, array $param_CategoryChoiceArray,  &$param_CategorySelectedArray=null) {
		foreach($param_CategoryChoiceArray as $var_Key => $var_Value ) {

			if(is_array($param_CategorySelectedArray) && array_key_exists($var_Key, $param_CategorySelectedArray)) {
				$var_Value['selected'] = 'selected';
				unset($param_CategorySelectedArray[$var_Key]);
			}
			$DOMElement_Child =DOMElementHelper::getDOMElementWithParameter($param_DOMDocument, $param_ElementName='Category', $var_Value);

			if(array_key_exists('SubCategory', $var_Value)) {
				foreach($var_Value['SubCategory'] as $var_SubKey => $var_SubValue) {

					if(is_array($param_CategorySelectedArray) && array_key_exists($var_SubKey, $param_CategorySelectedArray)) {
						$var_SubValue['selected'] = 'selected';
						unset($param_CategorySelectedArray[$var_SubKey]);
					}
					$DOMElement_SubChild=DOMElementHelper::getDOMElementWithParameter($param_DOMDocument, $param_ElementName='Category', $var_SubValue);
					$DOMElement_Child->appendChild($DOMElement_SubChild);
				}
			}
			$param_DOMList->appendChild($DOMElement_Child);
		}
	}

	public static function getRequestMTextList(Account &$param_HostAccount, iStorable &$param_iStorable, $param_RequestValue, MultimediaText $param_PartOf, $param_BasisClipboardName=false, array $param_ConditionArray=null) {
		$var_BEADB = $param_iStorable->getBasisElementAddonDataBase('Category');D::show($param_PartOf->getObjVar('ID'),'PartOf');
		return $var_BEADB->getContList($param_HostAccount, $param_iStorable,$param_RequestValue, $param_PartOf->getObjVar('ID'), $param_Depth=null, $param_BasisClipboardName);
	}

}

?>