File indexing completed on 2025-02-09 07:14:33
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_ConfigStore extends Local_Model_Table 0024 { 0025 0026 const CACHE_STORES_CATEGORIES = 'stores_categories_list'; 0027 const CACHE_STORES_CONFIGS = 'stores_configs_list'; 0028 const CACHE_STORE_CONFIG = 'store_config'; 0029 const CACHE_STORES_CONFIGS_BY_ID = 'stores_configs_id_list'; 0030 0031 protected $_keyColumnsForRow = array('store_id'); 0032 protected $_key = 'store_id'; 0033 protected $_name = "config_store"; 0034 0035 /** 0036 * @param null $id 0037 * 0038 * @return array 0039 * @throws Zend_Db_Select_Exception 0040 */ 0041 public function fetchHostnamesForJTable($id = null) 0042 { 0043 $select = $this->select()->from($this->_name)->columns('host')->group('host'); 0044 0045 $resultRows = $this->fetchAll($select); 0046 0047 $resultForSelect = array(); 0048 foreach ($resultRows as $row) { 0049 $resultForSelect[] = array('DisplayText' => $row['host'], 'Value' => $row['store_id']); 0050 } 0051 0052 return $resultForSelect; 0053 } 0054 0055 /** 0056 * @param bool $clearCache 0057 * @return array 0058 * @throws Zend_Cache_Exception 0059 * @throws Zend_Exception 0060 */ 0061 public function fetchAllStoresAndCategories($clearCache = false) 0062 { 0063 /** @var Zend_Cache_Core $cache */ 0064 $cache = Zend_Registry::get('cache'); 0065 $cacheName = self::CACHE_STORES_CATEGORIES; 0066 0067 if ($clearCache) { 0068 $cache->remove($cacheName); 0069 } 0070 0071 if (false == ($configArray = $cache->load($cacheName))) { 0072 $resultSet = $this->queryAllStoresAndCategories(); 0073 $configArray = $this->createArrayAllStoresAndCategories($resultSet); 0074 $cache->save($configArray, $cacheName, array(), 28800); 0075 } 0076 0077 return $configArray; 0078 } 0079 0080 /** 0081 * @return array 0082 */ 0083 private function queryAllStoresAndCategories() 0084 { 0085 $sql = " 0086 SELECT 0087 `config_store`.`host`, 0088 `config_store_category`.`store_id`, 0089 `config_store_category`.`project_category_id` 0090 FROM 0091 `config_store` 0092 JOIN 0093 `config_store_category` ON `config_store`.`store_id` = `config_store_category`.`store_id` 0094 JOIN 0095 `project_category` ON `project_category`.`project_category_id` = `config_store_category`.`project_category_id` 0096 ORDER BY `config_store`.`host`,`config_store_category`.`order`,`project_category`.`title`; 0097 "; 0098 $resultSet = $this->_db->fetchAll($sql); 0099 0100 return $resultSet; 0101 } 0102 0103 /** 0104 * @param array $resultSetConfig 0105 * 0106 * @return array 0107 */ 0108 private function createArrayAllStoresAndCategories($resultSetConfig) 0109 { 0110 $result = array(); 0111 foreach ($resultSetConfig as $element) { 0112 $result[$element['host']][] = $element['project_category_id']; 0113 } 0114 array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;')); 0115 0116 return $result; 0117 } 0118 0119 /** 0120 * @return array 0121 * @throws Zend_Cache_Exception 0122 * @throws Zend_Exception 0123 */ 0124 public function fetchDomainConfigIdList() 0125 { 0126 if (Zend_Registry::isRegistered('cache')) { 0127 /** @var Zend_Cache_Core $cache */ 0128 $cache = Zend_Registry::get('cache'); 0129 $cacheName = __FUNCTION__; 0130 if (false == ($configArray = $cache->load($cacheName))) { 0131 $resultSet = $this->queryDomainConfigIdList(); 0132 $configArray = $this->createDomainStoreIdArray($resultSet); 0133 $cache->save($configArray, $cacheName); 0134 } 0135 } else { 0136 $resultSet = $this->queryDomainConfigIdList(); 0137 $configArray = $this->createDomainStoreIdArray($resultSet); 0138 } 0139 0140 return $configArray; 0141 } 0142 0143 /** 0144 * @return array 0145 */ 0146 private function queryDomainConfigIdList() 0147 { 0148 $sql = "SELECT `host`, `config_id_name` FROM `config_store` ORDER BY `host`;"; 0149 $resultSet = $this->_db->fetchAll($sql); 0150 0151 return $resultSet; 0152 } 0153 0154 /** 0155 * @param array $resultSetConfig 0156 * 0157 * @return array 0158 */ 0159 private function createDomainStoreIdArray($resultSetConfig) 0160 { 0161 $result = array(); 0162 foreach ($resultSetConfig as $element) { 0163 $result[$element['host']] = $element['config_id_name']; 0164 } 0165 0166 return $result; 0167 } 0168 0169 /** 0170 * @return array 0171 * @throws Zend_Cache_Exception 0172 * @throws Zend_Exception 0173 */ 0174 public function fetchDomainsStoreNameList() 0175 { 0176 if (Zend_Registry::isRegistered('cache')) { 0177 /** @var Zend_Cache_Core $cache */ 0178 $cache = Zend_Registry::get('cache'); 0179 $cacheName = __FUNCTION__; 0180 if (false == ($configArray = $cache->load($cacheName))) { 0181 $resultSet = $this->queryDomains(); 0182 $configArray = $this->createDomainsArray($resultSet); 0183 $cache->save($configArray, $cacheName); 0184 } 0185 } else { 0186 $resultSet = $this->queryDomains(); 0187 $configArray = $this->createDomainsArray($resultSet); 0188 } 0189 0190 return $configArray; 0191 } 0192 0193 /** 0194 * @return array 0195 */ 0196 private function queryDomains() 0197 { 0198 $sql = "SELECT `host`, `name` FROM `config_store` ORDER BY `order`;"; 0199 $resultSet = $this->_db->fetchAll($sql); 0200 0201 return $resultSet; 0202 } 0203 0204 /** 0205 * @param array $resultSetConfig 0206 * 0207 * @return array 0208 */ 0209 private function createDomainsArray($resultSetConfig) 0210 { 0211 $result = array(); 0212 foreach ($resultSetConfig as $element) { 0213 $result[$element['host']] = $element['name']; 0214 } 0215 0216 return $result; 0217 } 0218 0219 /** 0220 * @return array 0221 */ 0222 public function fetchDomainObjects() 0223 { 0224 $sql = "SELECT * FROM `config_store` ORDER BY `order`;"; 0225 $resultSet = $this->_db->fetchAll($sql); 0226 0227 return $resultSet; 0228 } 0229 0230 /** 0231 * @return array 0232 */ 0233 public function fetchDomainObjectsByName($name) 0234 { 0235 $sql = "SELECT * FROM `config_store` WHERE `name`='" . $name . "'"; 0236 $resultSet = $this->_db->fetchAll($sql); 0237 0238 return array_pop($resultSet); 0239 } 0240 0241 0242 public function deleteId($dataId) 0243 { 0244 $sql = "DELETE FROM `config_store` WHERE {$this->_key} = ?"; 0245 $this->_db->query($sql, $dataId)->execute(); 0246 // return $this->delete(array('store_id = ?' => (int)$dataId)); 0247 } 0248 0249 public function delete($where) 0250 { 0251 $where = parent::_whereExpr($where); 0252 0253 /** 0254 * Build the DELETE statement 0255 */ 0256 $sql = "UPDATE " . parent::getAdapter()->quoteIdentifier($this->_name, 0257 true) . " SET `deleted_at` = NOW() " . (($where) ? " WHERE $where" : ''); 0258 0259 /** 0260 * Execute the statement and return the number of affected rows 0261 */ 0262 $stmt = parent::getAdapter()->query($sql); 0263 $result = $stmt->rowCount(); 0264 0265 return $result; 0266 } 0267 0268 /** 0269 * @param bool $clearCache 0270 * 0271 * @return array 0272 * @throws Zend_Cache_Exception 0273 * @throws Zend_Exception 0274 */ 0275 public function fetchAllStoresConfigArray($clearCache = false) 0276 { 0277 /** @var Zend_Cache_Core $cache */ 0278 $cache = Zend_Registry::get('cache'); 0279 $cacheName = self::CACHE_STORES_CONFIGS; 0280 0281 if ($clearCache) { 0282 $cache->remove($cacheName); 0283 } 0284 0285 if (false == ($configArray = $cache->load($cacheName))) { 0286 $resultSet = $this->queryStoreConfigArray(); 0287 $configArray = $this->createStoreConfigArray($resultSet); 0288 $cache->save($configArray, $cacheName, array(), 28800); 0289 } 0290 0291 return $configArray; 0292 } 0293 0294 /** 0295 * @return array 0296 */ 0297 private function queryStoreConfigArray() 0298 { 0299 $sql = "SELECT * FROM `config_store` ORDER BY `order`;"; 0300 $resultSet = $this->_db->fetchAll($sql); 0301 0302 return $resultSet; 0303 } 0304 0305 /** 0306 * @param array $resultSetConfig 0307 * @param string $key 0308 * 0309 * @return array 0310 */ 0311 private function createStoreConfigArray($resultSetConfig, $key = 'host') 0312 { 0313 $result = array(); 0314 foreach ($resultSetConfig as $element) { 0315 $result[$element[$key]] = $element; 0316 } 0317 0318 return $result; 0319 } 0320 0321 /** 0322 * @param bool $clearCache 0323 * 0324 * @return array 0325 * @throws Zend_Cache_Exception 0326 * @throws Zend_Exception 0327 */ 0328 public function fetchAllStoresConfigByIdArray($clearCache = false) 0329 { 0330 /** @var Zend_Cache_Core $cache */ 0331 $cache = Zend_Registry::get('cache'); 0332 $cacheName = self::CACHE_STORES_CONFIGS_BY_ID; 0333 0334 if ($clearCache) { 0335 $cache->remove($cacheName); 0336 } 0337 0338 if (false == ($configArray = $cache->load($cacheName))) { 0339 $resultSet = $this->queryStoreConfigArray(); 0340 $configArray = $this->createStoreConfigArray($resultSet, 'store_id'); 0341 $cache->save($configArray, $cacheName, array(), 28800); 0342 } 0343 0344 return $configArray; 0345 } 0346 0347 public function fetchConfigForStore($store_id, $clearCache = false) 0348 { 0349 /** @var Zend_Cache_Core $cache */ 0350 $cache = Zend_Registry::get('cache'); 0351 $cacheName = self::CACHE_STORE_CONFIG . "_{$store_id}"; 0352 0353 if ($clearCache) { 0354 $cache->remove($cacheName); 0355 } 0356 0357 if (false == ($config = $cache->load($cacheName))) { 0358 $config = $this->queryStoreConfig($store_id); 0359 $cache->save($config, $cacheName, array(), 28800); 0360 } 0361 0362 return $config; 0363 } 0364 0365 private function queryStoreConfig($store_id) 0366 { 0367 $sql = "SELECT * FROM `config_store` WHERE `store_id` = :store;"; 0368 $resultSet = $this->_db->fetchRow($sql, array('store' => (int)$store_id)); 0369 0370 return $resultSet; 0371 } 0372 0373 /** 0374 * @return stdClass|bool 0375 */ 0376 public function fetchDefaultStoreId() 0377 { 0378 $sql = "SELECT `store_id`, `package_type` FROM `config_store` WHERE `default` = 1;"; 0379 $resultSet = $this->_db->fetchRow($sql); 0380 if (count($resultSet) > 0) { 0381 return (object)$resultSet; 0382 } else { 0383 return false; 0384 } 0385 } 0386 0387 }