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

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
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 Default_Model_DbTable_SectionCategory extends Local_Model_Table
0023 {
0024 
0025     protected $_keyColumnsForRow = array('section_category_id');
0026     protected $_key = 'section_category_id';
0027     protected $_name = "section_store_category";
0028     
0029 
0030     /**
0031      * @param int $dataId
0032      */
0033     public function deleteId($dataId)
0034     {
0035         $sql = "DELETE FROM {$this->_name} WHERE {$this->_key} = ?";
0036         $this->_db->query($sql,$dataId)->execute();
0037     }
0038 
0039     /**
0040      * @param int $storeId
0041      * @return array
0042      */
0043     public function fetchAllCategoriesForSection($sectionId)
0044     {
0045         $active = Default_Model_DbTable_ProjectCategory::CATEGORY_ACTIVE;
0046         $notDeleted = Default_Model_DbTable_ProjectCategory::CATEGORY_NOT_DELETED;
0047         $sql = "
0048                 SELECT pc2.project_category_id
0049                 FROM project_category AS pc, project_category AS pc2
0050                 WHERE pc.project_category_id IN (SELECT DISTINCT csc.project_category_id
0051                                                  FROM section_category AS csc
0052                                                    JOIN project_category AS pc ON csc.project_category_id = pc.project_category_id AND pc.is_active = 1
0053                                                  WHERE csc.section_id = :sectionId)
0054                       AND pc2.lft BETWEEN pc.lft AND pc.rgt
0055                       AND pc2.is_active = {$active} AND pc2.is_deleted = {$notDeleted}
0056                 ORDER BY pc2.lft;
0057                 ";
0058         $results = $this->_db->fetchAll($sql, array('sectionId' => $sectionId));
0059         $values = array_map(function($row) { return $row['project_category_id']; }, $results);
0060         return $values;
0061     }
0062 
0063     /**
0064      * @param int|array $listCatId
0065      * @return array
0066      */
0067     public function fetchSectionForCatdId($listCatId)
0068     {
0069         $inQuery = '?';
0070         if (is_array($listCatId)) {
0071             $inQuery = implode(',', array_fill(0, count($listCatId), '?'));
0072         }
0073 
0074         $sql = '
0075             SELECT cs.section_id, cs.section_id_name 
0076             FROM section_category as csc
0077             join section as cs on cs.section_id = csc.section_id 
0078             where csc.project_category_id in ('.$inQuery.')
0079         ';
0080         
0081         $result = $this->_db->query($sql, $listCatId)->fetchAll();
0082         
0083         if (count($result) > 0) {
0084             return $result;
0085         } else {
0086             return array();
0087         }
0088     }
0089 
0090     public function fetchCatIdsForSection($section_id)
0091     {
0092         $sql = "
0093             SELECT csc.project_category_id 
0094             FROM section_category AS csc
0095             JOIN project_category AS pc ON pc.project_category_id = csc.project_category_id
0096             WHERE csc.section_id = :section_id
0097             AND csc.deleted_at IS NULL
0098              ORDER BY csc.`order`, pc.title
0099         ";
0100         $results = $this->_db->fetchAll($sql, array('section_id' => $section_id));
0101         $values = array_map(function($row) { return $row['project_category_id']; }, $results);
0102         return $values;
0103     }
0104     
0105     public function updateSectionPerCategory($cat_id,$section_id=null)
0106     {
0107         $sql = "delete from section_category where project_category_id=:cat_id";
0108         $this->getAdapter()->query($sql, array('cat_id' => $cat_id));
0109 
0110         if(!empty($section_id)) {
0111             $sql = "INSERT IGNORE INTO section_category (project_category_id, section_id) VALUES ($cat_id,$section_id)";
0112             $this->getAdapter()->query($sql);
0113         }
0114 
0115         
0116     }
0117 
0118 
0119 }