File indexing completed on 2025-02-09 07:14:35

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
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 Default_Model_DbTable_Section extends Local_Model_Table
0024 {
0025 
0026     const CACHE_STORES_CATEGORIES = 'section_categories_list';
0027     const CACHE_STORES_CONFIGS = 'section_list';
0028     const CACHE_STORE_CONFIG = 'section';
0029     const CACHE_STORES_CONFIGS_BY_ID = 'section_id_list';
0030 
0031     protected $_keyColumnsForRow = array('section_id');
0032     protected $_key = 'section_id';
0033     protected $_name = "section";
0034     
0035 
0036     /**
0037      * @param null $id
0038      *
0039      * @return array
0040      * @throws Zend_Db_Select_Exception
0041      */
0042     public function fetchNamesForJTable($id = null)
0043     {
0044         $select = $this->select()->from($this->_name)->columns('name')->group('name');
0045 
0046         $resultRows = $this->fetchAll($select);
0047 
0048         $resultForSelect = array();
0049         foreach ($resultRows as $row) {
0050             $resultForSelect[] = array('DisplayText' => $row['name'], 'Value' => $row['section_id']);
0051         }
0052 
0053         return $resultForSelect;
0054     }
0055 
0056     /**
0057      * @return array
0058      */
0059     public function fetchAllSectionsAndCategories($clearCache = false)
0060     {
0061         /** @var Zend_Cache_Core $cache */
0062         $cache = Zend_Registry::get('cache');
0063         $cacheName = self::CACHE_STORES_CATEGORIES;
0064 
0065         if ($clearCache) {
0066             $cache->remove($cacheName);
0067         }
0068 
0069         if (false == ($configArray = $cache->load($cacheName))) {
0070             $resultSet = $this->queryAllSectionsAndCategories();
0071             $configArray = $this->createArrayAllSectionsAndCategories($resultSet);
0072             $cache->save($configArray, $cacheName, array(), 28800);
0073         }
0074 
0075         return $configArray;
0076     }
0077 
0078     /**
0079      * @return array
0080      */
0081     private function queryAllSectionsAndCategories()
0082     {
0083         $sql = "
0084                 SELECT
0085                     `section`.`name`,
0086                     `section_category`.`section_id`,
0087                     `section_category`.`project_category_id`
0088                 FROM
0089                     `section`
0090                 JOIN
0091                     `section_category` ON `section`.`section_id` = `section_category`.`section_id`
0092                 JOIN
0093                     `project_category` ON `project_category`.`project_category_id` = `section_category`.`project_category_id`
0094                 ORDER BY `section`.`name`,`project_category`.`title`;
0095         ";
0096         $resultSet = $this->_db->fetchAll($sql);
0097 
0098         return $resultSet;
0099     }
0100 
0101     /**
0102      * @param array $resultSetConfig
0103      *
0104      * @return array
0105      */
0106     private function createArrayAllSectionsAndCategories($resultSetConfig)
0107     {
0108         $result = array();
0109         foreach ($resultSetConfig as $element) {
0110             $result[$element['name']][] = $element['project_category_id'];
0111         }
0112         array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;'));
0113 
0114         return $result;
0115     }
0116 
0117 
0118     public function deleteId($dataId)
0119     {
0120         $sql = "DELETE FROM `section` WHERE {$this->_key} = ?";
0121         $this->_db->query($sql, $dataId)->execute();
0122 //        return $this->delete(array('store_id = ?' => (int)$dataId));
0123     }
0124 
0125     public function delete($where)
0126     {
0127         $where = parent::_whereExpr($where);
0128 
0129         /**
0130          * Build the DELETE statement
0131          */
0132         $sql = "UPDATE " . parent::getAdapter()->quoteIdentifier($this->_name, true) . " SET is_active = 1, `deleted_at` = NOW() " . (($where) ? " WHERE $where" : '');
0133 
0134         /**
0135          * Execute the statement and return the number of affected rows
0136          */
0137         $stmt = parent::getAdapter()->query($sql);
0138         $result = $stmt->rowCount();
0139 
0140         return $result;
0141     }
0142 
0143     /**
0144      * @param bool $clearCache
0145      *
0146      * @return array
0147      * @throws Zend_Cache_Exception
0148      * @throws Zend_Exception
0149      */
0150     public function fetchAllSectionsArray($clearCache = false)
0151     {
0152         if (Zend_Registry::isRegistered('cache')) {
0153             /** @var Zend_Cache_Core $cache */
0154             $cache = Zend_Registry::get('cache');
0155             $cacheName = self::CACHE_STORES_CONFIGS;
0156 
0157             if ($clearCache) {
0158                 $cache->remove($cacheName);
0159             }
0160 
0161             if (false == ($configArray = $cache->load($cacheName))) {
0162                 $resultSet = $this->querySectionArray();
0163                 $configArray = $this->createSectionArray($resultSet);
0164                 $cache->save($configArray, $cacheName, array(), 28800);
0165             }
0166         } else {
0167             $resultSet = $this->querySectionArray();
0168             $configArray = $this->createSectionArray($resultSet);
0169         }
0170 
0171         return $configArray;
0172     }
0173 
0174     /**
0175      * @return array
0176      */
0177     private function querySectionArray()
0178     {
0179         $sql = "SELECT * FROM `section` ORDER BY `name`;";
0180         $resultSet = $this->_db->fetchAll($sql);
0181 
0182         return $resultSet;
0183     }
0184 
0185     /**
0186      * @param array  $resultSetConfig
0187      * @param string $key
0188      *
0189      * @return array
0190      */
0191     private function createSectionArray($resultSetConfig, $key = 'name')
0192     {
0193         $result = array();
0194         foreach ($resultSetConfig as $element) {
0195             $result[$element[$key]] = $element;
0196         }
0197 
0198         return $result;
0199     }
0200 
0201     /**
0202      * @param bool $clearCache
0203      *
0204      * @return array
0205      */
0206     public function fetchAllSectionByIdArray($clearCache = false)
0207     {
0208         if (Zend_Registry::isRegistered('cache')) {
0209             /** @var Zend_Cache_Core $cache */
0210             $cache = Zend_Registry::get('cache');
0211             $cacheName = self::CACHE_STORES_CONFIGS_BY_ID;
0212 
0213             if ($clearCache) {
0214                 $cache->remove($cacheName);
0215             }
0216 
0217             if (false == ($configArray = $cache->load($cacheName))) {
0218                 $resultSet = $this->querySectionArray();
0219                 $configArray = $this->createSectionArray($resultSet, 'section_id');
0220                 $cache->save($configArray, $cacheName, array(), 28800);
0221             }
0222         } else {
0223             $resultSet = $this->querySectionArray();
0224             $configArray = $this->createSectionArray($resultSet, 'section_id');
0225         }
0226 
0227         return $configArray;
0228     }
0229     
0230     
0231     
0232 }