File indexing completed on 2025-02-02 05:43:40
0001 <?php 0002 /** 0003 * ocs-apiserver 0004 * 0005 * Copyright 2016 by pling GmbH. 0006 * 0007 * This file is part of ocs-apiserver. 0008 * 0009 * This program is free software: you can redistribute it and/or modify 0010 * it under the terms of the GNU Affero General Public License as 0011 * published by the Free Software Foundation, either version 3 of the 0012 * License, or (at your option) any later version. 0013 * 0014 * This program is distributed in the hope that it will be useful, 0015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 * GNU Affero General Public License for more details. 0018 * 0019 * You should have received a copy of the GNU Affero General Public License 0020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 * 0022 * Created: 01.12.2017 0023 */ 0024 0025 class Application_Model_DbTable_ConfigStore extends Local_Model_Table 0026 { 0027 const CACHE_STORES_CATEGORIES = 'stores_categories_list'; 0028 const CACHE_STORES_CONFIGS = 'stores_configs_list'; 0029 const CACHE_STORE_CONFIG = 'store_config'; 0030 const CACHE_STORES_CONFIGS_BY_ID = 'stores_configs_id_list'; 0031 0032 protected $_keyColumnsForRow = array('store_id'); 0033 protected $_key = 'store_id'; 0034 protected $_name = "config_store"; 0035 0036 /** 0037 * @param null $id 0038 * 0039 * @return array 0040 * @throws Zend_Db_Select_Exception 0041 */ 0042 public function fetchHostnamesForJTable($id = null) 0043 { 0044 $select = $this->select()->from($this->_name)->columns('host')->group('host'); 0045 0046 $resultRows = $this->fetchAll($select); 0047 0048 $resultForSelect = array(); 0049 foreach ($resultRows as $row) { 0050 $resultForSelect[] = array('DisplayText' => $row['host'], 'Value' => $row['store_id']); 0051 } 0052 0053 return $resultForSelect; 0054 } 0055 0056 /** 0057 * @param bool $clearCache 0058 * 0059 * @return array 0060 * @throws Zend_Cache_Exception 0061 * @throws Zend_Exception 0062 */ 0063 public function fetchAllStoresAndCategories($clearCache = false) 0064 { 0065 /** @var Zend_Cache_Core $cache */ 0066 $cache = Zend_Registry::get('cache'); 0067 $cacheName = self::CACHE_STORES_CATEGORIES; 0068 0069 if ($clearCache) { 0070 $cache->remove($cacheName); 0071 } 0072 0073 if (false == ($configArray = $cache->load($cacheName))) { 0074 $resultSet = $this->queryAllStoresAndCategories(); 0075 $configArray = $this->createArrayAllStoresAndCategories($resultSet); 0076 $cache->save($configArray, $cacheName, array(), 28800); 0077 } 0078 0079 return $configArray; 0080 } 0081 0082 /** 0083 * @return array 0084 */ 0085 private function queryAllStoresAndCategories() 0086 { 0087 $sql = " 0088 SELECT 0089 config_store.`host`, 0090 config_store_category.store_id, 0091 config_store_category.project_category_id 0092 FROM 0093 config_store 0094 JOIN 0095 config_store_category ON config_store.store_id = config_store_category.store_id 0096 JOIN 0097 project_category ON project_category.project_category_id = config_store_category.project_category_id 0098 ORDER BY config_store.`host`,config_store_category.`order`,project_category.title; 0099 "; 0100 $resultSet = $this->_db->fetchAll($sql); 0101 0102 return $resultSet; 0103 } 0104 0105 /** 0106 * @param array $resultSetConfig 0107 * 0108 * @return array 0109 */ 0110 private function createArrayAllStoresAndCategories($resultSetConfig) 0111 { 0112 $result = array(); 0113 foreach ($resultSetConfig as $element) { 0114 $result[$element['host']][] = $element['project_category_id']; 0115 } 0116 array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;')); 0117 0118 return $result; 0119 } 0120 0121 /** 0122 * @return array 0123 * @throws Zend_Cache_Exception 0124 * @throws Zend_Exception 0125 */ 0126 public function fetchDomainConfigIdList() 0127 { 0128 if (Zend_Registry::isRegistered('cache')) { 0129 /** @var Zend_Cache_Core $cache */ 0130 $cache = Zend_Registry::get('cache'); 0131 $cacheName = __FUNCTION__; 0132 if (false == ($configArray = $cache->load($cacheName))) { 0133 $resultSet = $this->queryDomainConfigIdList(); 0134 $configArray = $this->createDomainStoreIdArray($resultSet); 0135 $cache->save($configArray, $cacheName); 0136 } 0137 } else { 0138 $resultSet = $this->queryDomainConfigIdList(); 0139 $configArray = $this->createDomainStoreIdArray($resultSet); 0140 } 0141 0142 return $configArray; 0143 } 0144 0145 /** 0146 * @return array 0147 */ 0148 private function queryDomainConfigIdList() 0149 { 0150 $sql = "SELECT host, config_id_name FROM config_store ORDER BY host;"; 0151 $resultSet = $this->_db->fetchAll($sql); 0152 0153 return $resultSet; 0154 } 0155 0156 /** 0157 * @param array $resultSetConfig 0158 * 0159 * @return array 0160 */ 0161 private function createDomainStoreIdArray($resultSetConfig) 0162 { 0163 $result = array(); 0164 foreach ($resultSetConfig as $element) { 0165 $result[$element['host']] = $element['config_id_name']; 0166 } 0167 0168 return $result; 0169 } 0170 0171 /** 0172 * @return array 0173 * @throws Zend_Cache_Exception 0174 * @throws Zend_Exception 0175 */ 0176 public function fetchDomainsStoreNameList() 0177 { 0178 if (Zend_Registry::isRegistered('cache')) { 0179 /** @var Zend_Cache_Core $cache */ 0180 $cache = Zend_Registry::get('cache'); 0181 $cacheName = __FUNCTION__; 0182 if (false == ($configArray = $cache->load($cacheName))) { 0183 $resultSet = $this->queryDomains(); 0184 $configArray = $this->createDomainsArray($resultSet); 0185 $cache->save($configArray, $cacheName); 0186 } 0187 } else { 0188 $resultSet = $this->queryDomains(); 0189 $configArray = $this->createDomainsArray($resultSet); 0190 } 0191 0192 return $configArray; 0193 } 0194 0195 /** 0196 * @return array 0197 */ 0198 private function queryDomains() 0199 { 0200 $sql = "SELECT host, name FROM config_store ORDER BY `order`;"; 0201 $resultSet = $this->_db->fetchAll($sql); 0202 0203 return $resultSet; 0204 } 0205 0206 /** 0207 * @param array $resultSetConfig 0208 * 0209 * @return array 0210 */ 0211 private function createDomainsArray($resultSetConfig) 0212 { 0213 $result = array(); 0214 foreach ($resultSetConfig as $element) { 0215 $result[$element['host']] = $element['name']; 0216 } 0217 0218 return $result; 0219 } 0220 0221 /** 0222 * @return array 0223 */ 0224 public function fetchDomainObjects() 0225 { 0226 $sql = "SELECT * FROM config_store ORDER BY `order`;"; 0227 $resultSet = $this->_db->fetchAll($sql); 0228 0229 return $resultSet; 0230 } 0231 0232 /** 0233 * @param int $dataId 0234 * 0235 * @throws Zend_Db_Statement_Exception 0236 */ 0237 public function deleteId($dataId) 0238 { 0239 $sql = "DELETE FROM config_store WHERE {$this->_key} = ?"; 0240 $this->_db->query($sql, $dataId)->execute(); 0241 } 0242 0243 /** 0244 * @param bool $clearCache 0245 * 0246 * @return array 0247 * @throws Zend_Cache_Exception 0248 * @throws Zend_Exception 0249 */ 0250 public function fetchAllStoresConfigArray($clearCache = false) 0251 { 0252 if (Zend_Registry::isRegistered('cache')) { 0253 /** @var Zend_Cache_Core $cache */ 0254 $cache = Zend_Registry::get('cache'); 0255 $cacheName = self::CACHE_STORES_CONFIGS; 0256 0257 if ($clearCache) { 0258 $cache->remove($cacheName); 0259 } 0260 0261 if (false == ($configArray = $cache->load($cacheName))) { 0262 $resultSet = $this->queryStoreConfigArray(); 0263 $configArray = $this->createStoreConfigArray($resultSet); 0264 $cache->save($configArray, $cacheName, array(), 28800); 0265 } 0266 } else { 0267 $resultSet = $this->queryStoreConfigArray(); 0268 $configArray = $this->createStoreConfigArray($resultSet); 0269 } 0270 0271 return $configArray; 0272 } 0273 0274 /** 0275 * @return array 0276 */ 0277 private function queryStoreConfigArray() 0278 { 0279 $sql = "SELECT * FROM config_store ORDER BY `order`;"; 0280 $resultSet = $this->_db->fetchAll($sql); 0281 0282 return $resultSet; 0283 } 0284 0285 /** 0286 * @param array $resultSetConfig 0287 * @param string $key 0288 * 0289 * @return array 0290 */ 0291 private function createStoreConfigArray($resultSetConfig, $key = 'host') 0292 { 0293 $result = array(); 0294 foreach ($resultSetConfig as $element) { 0295 $result[$element[$key]] = $element; 0296 } 0297 0298 return $result; 0299 } 0300 0301 /** 0302 * @param bool $clearCache 0303 * 0304 * @return array 0305 * @throws Zend_Cache_Exception 0306 * @throws Zend_Exception 0307 */ 0308 public function fetchAllStoresConfigByIdArray($clearCache = false) 0309 { 0310 if (Zend_Registry::isRegistered('cache')) { 0311 /** @var Zend_Cache_Core $cache */ 0312 $cache = Zend_Registry::get('cache'); 0313 $cacheName = self::CACHE_STORES_CONFIGS_BY_ID; 0314 0315 if ($clearCache) { 0316 $cache->remove($cacheName); 0317 } 0318 0319 if (false == ($configArray = $cache->load($cacheName))) { 0320 $resultSet = $this->queryStoreConfigArray(); 0321 $configArray = $this->createStoreConfigArray($resultSet, 'store_id'); 0322 $cache->save($configArray, $cacheName, array(), 28800); 0323 } 0324 } else { 0325 $resultSet = $this->queryStoreConfigArray(); 0326 $configArray = $this->createStoreConfigArray($resultSet, 'store_id'); 0327 } 0328 0329 return $configArray; 0330 } 0331 0332 /** 0333 * @param int $store_id 0334 * @param bool $clearCache 0335 * 0336 * @return false|mixed 0337 * @throws Zend_Cache_Exception 0338 * @throws Zend_Exception 0339 */ 0340 public function fetchConfigForStore($store_id, $clearCache = false) 0341 { 0342 if (Zend_Registry::isRegistered('cache')) { 0343 /** @var Zend_Cache_Core $cache */ 0344 $cache = Zend_Registry::get('cache'); 0345 $cacheName = self::CACHE_STORE_CONFIG . "_{$store_id}"; 0346 0347 if ($clearCache) { 0348 $cache->remove($cacheName); 0349 } 0350 0351 if (false == ($config = $cache->load($cacheName))) { 0352 $config = $this->queryStoreConfig($store_id); 0353 $cache->save($config, $cacheName, array(), 28800); 0354 } 0355 } else { 0356 $config = $this->queryStoreConfig($store_id); 0357 } 0358 0359 return $config; 0360 } 0361 0362 /** 0363 * @param int $store_id 0364 * 0365 * @return mixed 0366 */ 0367 private function queryStoreConfig($store_id) 0368 { 0369 $sql = "SELECT * FROM config_store WHERE store_id = :store;"; 0370 $resultSet = $this->_db->fetchRow($sql, array('store' => (int)$store_id)); 0371 0372 return $resultSet; 0373 } 0374 0375 }