File indexing completed on 2024-12-15 05:21:37

0001 <?php
0002 
0003 /**
0004  *  ocs-apiserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-apiserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Application_Model_ProjectCategory
0024 {
0025     const CACHE_TREE_STORE = 'store_cat_tree';
0026 
0027     /** @var string */
0028     protected $_dataTableName;
0029     /** @var  Application_Model_DbTable_ProjectCategory */
0030     protected $_dataTable;
0031 
0032     /**
0033      * PHP 5 allows developers to declare constructor methods for classes.
0034      * Classes which have a constructor method call this method on each newly-created object,
0035      * so it is suitable for any initialization that the object may need before it is used.
0036      *
0037      * Note: Parent constructors are not called implicitly if the child class defines a constructor.
0038      * In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
0039      *
0040      * param [ mixed $args [, $... ]]
0041      *
0042      * @param string $_dataTableName
0043      *
0044      * @link http://php.net/manual/en/language.oop5.decon.php
0045      */
0046     public function __construct($_dataTableName = 'Application_Model_DbTable_ProjectCategory')
0047     {
0048         $this->_dataTableName = $_dataTableName;
0049         $this->_dataTable = new $this->_dataTableName;
0050     }
0051 
0052     private function buildTree($list, $parent_id = null, $store_id = null)
0053     {
0054         if (false === is_array($list)) {
0055             $list = array($list);
0056         }
0057         $modelCategories = new Application_Model_DbTable_ProjectCategory();
0058         $result = array();
0059         foreach ($list as $cat_id) {
0060             $currentCategory = $modelCategories->fetchElement($cat_id);
0061 
0062             $result_element = array(
0063                 'id'            => $cat_id,
0064                 'title'         => $currentCategory['title'],
0065                 'xdg_type'      => $currentCategory['xdg_type'],
0066                 'name_legacy'   => $currentCategory['name_legacy'],
0067                 'has_children'  => false
0068             );
0069 
0070             if (isset($parent_id)) {
0071                 $result_element['parent_id'] = $parent_id;
0072             }
0073 
0074             //has children?
0075             if (($currentCategory['rgt'] - $currentCategory['lft']) > 1) {
0076                 $result_element['has_children'] = true;
0077                 $ids = $modelCategories->fetchImmediateChildrenIds($currentCategory['project_category_id'],
0078                     $modelCategories::ORDERED_TITLE);
0079                 $result_element['children'] = $this->buildTree($ids, $currentCategory['project_category_id'], $store_id);
0080             }
0081             $result[] = $result_element;
0082         }
0083 
0084         return $result;
0085     }
0086 
0087     public function fetchCategoryTreeCurrentStore($clearCache = false)
0088     {
0089         $store_config = Zend_Registry::get('store_config');
0090         $store_id = $store_config->store_id;
0091 
0092         /** @var Zend_Cache_Core $cache */
0093         $cache = Zend_Registry::get('cache');
0094         $cache_id = self::CACHE_TREE_STORE . "_{$store_id}";
0095 
0096         if ($clearCache) {
0097             $cache->remove($cache_id);
0098         }
0099 
0100         if (false === ($tree = $cache->load($cache_id))) {
0101             $list_cat_id = self::fetchCatIdsForCurrentStore();
0102             $tree = $this->buildTree($list_cat_id);
0103             $cache->save($tree, $cache_id, array(), 120);
0104         }
0105 
0106         return $tree;
0107     }
0108 
0109     public static function fetchCatIdsForCurrentStore()
0110     {
0111         return Zend_Registry::isRegistered('store_category_list') ? Zend_Registry::get('store_category_list') : null;
0112     }
0113 
0114 }