File indexing completed on 2024-04-28 05:53:54

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 class Application_Model_DbTable_ConfigStoreCategory extends Local_Model_Table
0023 {
0024 
0025     protected $_keyColumnsForRow = array('store_category_id');
0026     protected $_key = 'store_category_id';
0027     protected $_name = "config_store_category";
0028 
0029     /**
0030      * @param int $dataId
0031      */
0032     public function deleteId($dataId)
0033     {
0034         $sql = "DELETE FROM {$this->_name} WHERE {$this->_key} = ?";
0035         $this->_db->query($sql,$dataId)->execute();
0036     }
0037 
0038     /**
0039      * @param int $storeId
0040      * @return array
0041      */
0042     public function fetchAllCategoriesForStore($storeId)
0043     {
0044         $active = Application_Model_DbTable_ProjectCategory::CATEGORY_ACTIVE;
0045         $notDeleted = Application_Model_DbTable_ProjectCategory::CATEGORY_NOT_DELETED;
0046         $sql = "
0047                 SELECT pc2.project_category_id
0048                 FROM project_category AS pc, project_category AS pc2
0049                 WHERE pc.project_category_id IN (SELECT DISTINCT csc.project_category_id
0050                                                  FROM config_store_category AS csc
0051                                                    JOIN project_category AS pc ON csc.project_category_id = pc.project_category_id AND pc.is_active = 1
0052                                                  WHERE csc.store_id = :storeId)
0053                       AND pc2.lft BETWEEN pc.lft AND pc.rgt
0054                       AND pc2.is_active = {$active} AND pc2.is_deleted = {$notDeleted}
0055                 ORDER BY pc2.lft;
0056                 ";
0057         $results = $this->_db->fetchAll($sql, array('storeId' => $storeId));
0058         $values = array_map(function($row) { return $row['project_category_id']; }, $results);
0059         return $values;
0060     }
0061 
0062     /**
0063      * @param int|array $listCatId
0064      * @return array
0065      */
0066     public function fetchStoresForCatdId($listCatId)
0067     {
0068         $inQuery = '?';
0069         if (is_array($listCatId)) {
0070             $inQuery = implode(',', array_fill(0, count($listCatId), '?'));
0071         }
0072 
0073         $sql = '
0074             SELECT cs.store_id, cs.config_id_name 
0075             FROM config_store_category as csc
0076             join config_store as cs on cs.store_id = csc.store_id 
0077             where csc.project_category_id in ('.$inQuery.')
0078         ';
0079         
0080         $result = $this->_db->query($sql, $listCatId)->fetchAll();
0081         
0082         if (count($result) > 0) {
0083             return $result;
0084         } else {
0085             return array();
0086         }
0087     }
0088 
0089     public function fetchCatIdsForStore($store_id)
0090     {
0091         $sql = "
0092             SELECT csc.project_category_id 
0093             FROM config_store_category AS csc
0094             JOIN project_category AS pc ON pc.project_category_id = csc.project_category_id
0095             WHERE csc.store_id = :store_id
0096             AND csc.deleted_at IS NULL
0097              ORDER BY csc.`order`, pc.title
0098         ";
0099         $results = $this->_db->fetchAll($sql, array('store_id' => $store_id));
0100         $values = array_map(function($row) { return $row['project_category_id']; }, $results);
0101         return $values;
0102     }
0103 
0104 }