File indexing completed on 2024-12-22 05:33:27

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 Backend_StorecategoriesController extends Local_Controller_Action_Backend
0024 {
0025 
0026     const RESULT_OK = "OK";
0027     const RESULT_ERROR = "ERROR";
0028     const DATA_ID_NAME = 'store_category_id';
0029 
0030     /** @var Default_Model_DbTable_ConfigStoreCategory */
0031     protected $_model;
0032 
0033     protected $_modelName = 'Default_Model_DbTable_ConfigStoreCategory';
0034     protected $_pageTitle = 'Manage Store Categories';
0035 
0036     public function init()
0037     {
0038         $this->_model = new $this->_modelName();
0039 
0040         $this->view->pageTitle = $this->_pageTitle;
0041 
0042         parent::init();
0043     }
0044 
0045     public function indexAction()
0046     {
0047 
0048     }
0049 
0050     public function createAction()
0051     {
0052         $jTableResult = array();
0053         try {
0054             $newRow = $this->_model->createRow($this->prepareEmptyValues($this->getAllParams()));
0055             $result = $newRow->save();
0056 
0057             $this->initCache($newRow->store_id);
0058 
0059             $jTableResult['Result'] = self::RESULT_OK;
0060             $jTableResult['Record'] = $newRow->toArray();
0061         } catch (Exception $e) {
0062             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0063             $translate = Zend_Registry::get('Zend_Translate');
0064             $jTableResult['Result'] = self::RESULT_ERROR;
0065             $jTableResult['Message'] = $translate->_('Error while processing data.');
0066         }
0067 
0068         $this->_helper->json($jTableResult);
0069     }
0070 
0071     /**
0072      * @param $inputParams
0073      *
0074      * @return array
0075      */
0076     protected function prepareEmptyValues($inputParams)
0077     {
0078         return array_map(function ($value) {
0079             return empty($value) ? new Zend_Db_Expr('NULL') : $value;
0080         }, $inputParams);
0081     }
0082 
0083     protected function initCache($store_id)
0084     {
0085         $modelPCat = new Default_Model_ProjectCategory();
0086         $modelPCat->fetchCategoryTreeForStore($store_id, true);
0087 
0088         $modelConfigStore = new Default_Model_DbTable_ConfigStore();
0089         $modelConfigStore->fetchConfigForStore($store_id, true);
0090         $modelConfigStore->fetchAllStoresAndCategories(true);
0091         $modelConfigStore->fetchAllStoresConfigArray(true);
0092     }
0093 
0094     public function initcacheAction()
0095     {
0096         $modelConfigStore = new Default_Model_DbTable_ConfigStore();
0097         $allStoresCat = $modelConfigStore->fetchAllStoresAndCategories(true);
0098         $allStoresConfig = $modelConfigStore->fetchAllStoresConfigArray(true);
0099 
0100         $modelPCat = new Default_Model_ProjectCategory();
0101         foreach ($allStoresConfig as $config) {
0102             $modelPCat->fetchCategoryTreeForStore($config['store_id'], true);
0103             $modelConfigStore->fetchConfigForStore($config['store_id'], true);
0104         }
0105     }
0106 
0107     public function updateAction()
0108     {
0109         $jTableResult = array();
0110         try {
0111             $values = $this->getAllParams();
0112 
0113             foreach ($values as $key => $value) {
0114                 if ($value == '') {
0115                     $values[$key] = new Zend_Db_Expr('NULL');
0116                 }
0117             }
0118 
0119             $record = $this->_model->save($values);
0120 
0121             $this->initCache($record->store_id);
0122 
0123             $jTableResult = array();
0124             $jTableResult['Result'] = self::RESULT_OK;
0125             $jTableResult['Record'] = $record->toArray();
0126         } catch (Exception $e) {
0127             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0128             $translate = Zend_Registry::get('Zend_Translate');
0129             $jTableResult['Result'] = self::RESULT_ERROR;
0130             $jTableResult['Message'] = $translate->_('Error while processing data.');
0131         }
0132 
0133         $this->_helper->json($jTableResult);
0134     }
0135 
0136     public function deleteAction()
0137     {
0138         $dataId = (int)$this->getParam(self::DATA_ID_NAME, null);
0139 
0140         $row = $this->_model->fetchRow(array('store_category_id = ?' => $dataId));
0141         $this->_model->deleteId($dataId);
0142 
0143         $this->initCache($row->store_id);
0144 
0145         $jTableResult = array();
0146         $jTableResult['Result'] = self::RESULT_OK;
0147 
0148         $this->_helper->json($jTableResult);
0149     }
0150 
0151     public function listAction()
0152     {
0153         $startIndex = (int)$this->getParam('jtStartIndex');
0154         $pageSize = (int)$this->getParam('jtPageSize');
0155         $sorting = $this->getParam('jtSorting');
0156         $filter['store_id'] = $this->getParam('filter_hostname');
0157 
0158         $select = $this->_model->select()->limit($pageSize, $startIndex);
0159         if ($sorting) {
0160             $sorting = explode(',', $sorting);
0161         }
0162         $select->order($sorting);
0163         foreach ($filter as $key => $value) {
0164             if (false === empty($value)) {
0165                 $select->where("{$key} like ?", $value);
0166             }
0167         }
0168 
0169         $reports = $this->_model->fetchAll($select);
0170 
0171         $reportsAll = $this->_model->fetchAll($select->limit(null, null)->reset('columns')
0172                                                      ->columns(array('countAll' => new Zend_Db_Expr('count(*)'))));
0173 
0174         $jTableResult = array();
0175         $jTableResult['Result'] = self::RESULT_OK;
0176         $jTableResult['Records'] = $reports->toArray();
0177         $jTableResult['TotalRecordCount'] = $reportsAll->current()->countAll;
0178 
0179         $this->_helper->json($jTableResult);
0180     }
0181 
0182     protected function createJobInitCache($storeId)
0183     {
0184         $queue = Local_Queue_Factory::getQueue();
0185         $command = new Backend_Commands_InitCacheStoreCategories($storeId);
0186         $msg = $queue->send(serialize($command));
0187         Zend_Registry::get('logger')->info(__METHOD__ . ' - ' . print_r($msg, true));
0188     }
0189 
0190     protected function cacheClear($store_id)
0191     {
0192         /** @var Zend_Cache_Core $cache */
0193         $cache = Zend_Registry::get('cache');
0194         $cache->remove(Default_Model_ProjectCategory::CACHE_TREE_STORE . "_{$store_id}");
0195         $cache->remove(Default_Model_DbTable_ConfigStore::CACHE_STORE_CONFIG . "_{$store_id}");
0196         $modelConfigStore = new Default_Model_DbTable_ConfigStore();
0197         $modelConfigStore->fetchAllStoresAndCategories(true);
0198         $modelConfigStore->fetchAllStoresConfigArray(true);
0199     }
0200 
0201     public function treeAction()
0202     {
0203         $result = true;
0204         $model = new Default_Model_DbTable_ProjectCategory();
0205         $cat_id = (int)$this->getParam('c');
0206 
0207         try {
0208             $records = $model->fetchTreeForCategoryStores($cat_id);
0209         } catch (Exception $e) {
0210             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true));
0211             $result = false;
0212             $records = array();
0213         }
0214 
0215         $jTableResult = array();
0216         $jTableResult['Result'] = ($result == true) ? self::RESULT_OK : self::RESULT_ERROR;
0217         $jTableResult['Options'] = $records;
0218 
0219         $this->_helper->json($jTableResult);
0220     }
0221 
0222 }