File indexing completed on 2024-05-12 05:58:47

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  * Created: 13.09.2017
0023  */
0024 
0025 class Default_Model_TagGroup
0026 {
0027 
0028     /**
0029      * @inheritDoc
0030      */
0031     public function __construct()
0032     {
0033 
0034     }
0035 
0036     public function fetchGroupHierarchy()
0037     {
0038         $sql = "
0039             SELECT tag_group.group_name, tag.tag_id, tag.tag_name
0040             FROM tag_group_item
0041             JOIN tag_group ON tag_group.group_id = tag_group_item.tag_group_id
0042             JOIN tag ON tag.tag_id = tag_group_item.tag_id
0043         ";
0044         $resultSet = $this->getAdapter()->fetchAll($sql);
0045         $optgroup = array();
0046         foreach ($resultSet as $item) {
0047             $optgroup[$item['group_name']][$item['tag_id']] = $item['tag_name'];
0048         }
0049 
0050         return $optgroup;
0051     }
0052     
0053     public function fetchAllGroups()
0054     {
0055         $sql = "
0056             SELECT tag_group.group_name, tag_group.group_id
0057             FROM tag_group
0058         ";
0059         $resultSet = $this->getAdapter()->fetchAll($sql);
0060 
0061         return $resultSet;
0062     }
0063 
0064     /**
0065      * @return Zend_Db_Adapter_Abstract
0066      */
0067     private function getAdapter()
0068     {
0069         return Zend_Db_Table::getDefaultAdapter();
0070     }
0071 
0072     /**
0073      * @param int $group_id
0074      *
0075      * @return array
0076      */
0077     public function fetchGroupItems($group_id)
0078     {
0079         $sql = "SELECT tag_group_item.tag_group_item_id
0080                     , tag_group_item.tag_group_id
0081                     , tag.tag_id, tag.tag_name
0082                     , tag.tag_fullname
0083                     , tag.tag_description
0084                     , tag.is_active
0085              FROM tag_group_item 
0086              JOIN tag ON tag.tag_id = tag_group_item.tag_id 
0087              WHERE tag_group_id = :group_id";
0088         $resultSet = $this->getAdapter()->fetchAll($sql, array('group_id' => $group_id));
0089 
0090         return $resultSet;
0091     }
0092 
0093     /**
0094      * @param int    $group_id
0095      * @param string $tag_name
0096      *
0097      * @return array
0098      */
0099     public function assignGroupTag($group_id, $tag_name,$tag_fullname, $tag_description,$is_active=1)
0100     {
0101         $tag_id = $this->saveTag($tag_name,$tag_fullname, $tag_description,$is_active);
0102         $group_tag_id = $this->saveGroupTag($group_id, $tag_id);
0103         $resultSet = $this->fetchOneGroupItem($group_tag_id);
0104 
0105         return $resultSet;
0106     }
0107 
0108 
0109 
0110     /**
0111      * @param string $tag_name
0112      *
0113      * @return int
0114      */
0115     public function saveTag($tag_name,$tag_fullname, $tag_description,$is_active=1)
0116     {
0117         $tag_name = strtolower($tag_name);
0118         $sql = "SELECT tag_id FROM tag WHERE tag_name = :tagName";
0119         $resultSet = $this->getAdapter()->fetchRow($sql, array('tagName' => $tag_name));
0120         if (empty($resultSet)) {
0121             $this->getAdapter()->insert('tag', array('tag_name' => $tag_name, 'tag_fullname' => $tag_fullname, 'tag_description' => $tag_description,'is_active' => $is_active));
0122             $resultId = $this->getAdapter()->lastInsertId();
0123         } else {
0124             $resultId = $resultSet['tag_id'];
0125         }
0126 
0127         return $resultId;
0128     }
0129 
0130     /**
0131      * @param int $group_id
0132      * @param int $tag_id
0133      *
0134      * @return int
0135      */
0136     public function saveGroupTag($group_id, $tag_id)
0137     {
0138         $sql = "SELECT tag_group_item_id FROM tag_group_item WHERE tag_group_id = :group_id AND tag_id = :tag_id";
0139         $resultSet = $this->getAdapter()->fetchRow($sql, array('group_id' => $group_id, 'tag_id' => $tag_id));
0140         if (empty($resultSet)) {
0141             $this->getAdapter()->insert('tag_group_item', array('tag_group_id' => $group_id, 'tag_id' => $tag_id));
0142             $resultId = $this->getAdapter()->lastInsertId();
0143         } else {
0144             $resultId = $resultSet['tag_group_item_id'];
0145         }
0146 
0147         return $resultId;
0148     }
0149 
0150     /**
0151      * @param int $group_item_id
0152      *
0153      * @return array|false
0154      */
0155     public function fetchOneGroupItem($group_item_id)
0156     {
0157         $sql = "SELECT tag_group_item.tag_group_item_id
0158                     , tag_group_item.tag_group_id
0159                     , tag.tag_id, tag.tag_name
0160                     , tag.tag_fullname
0161                     , tag.tag_description
0162                     , tag.is_active
0163              FROM tag_group_item 
0164              JOIN tag ON tag.tag_id = tag_group_item.tag_id 
0165              WHERE tag_group_item_id = :group_item_id";
0166         $resultSet = $this->getAdapter()->fetchRow($sql, array('group_item_id' => $group_item_id));
0167 
0168         return $resultSet;
0169     }
0170 
0171     public function updateGroupTag($tag_id, $tag_name,$tag_fullname, $tag_description,$is_active=1)
0172     {        
0173             $updateValues = array(
0174                 'tag_name' =>$tag_name,
0175                 'tag_fullname' => $tag_fullname,
0176                 'tag_description' => $tag_description,
0177                 'is_active' => $is_active
0178             );
0179         
0180             $this->getAdapter()->update('tag', $updateValues, array('tag_id = ?' => $tag_id));        
0181     }
0182 
0183     public function deleteGroupTag($groupItemId)
0184     {
0185         $this->getAdapter()->delete('tag_group_item', array('tag_group_item_id = ?' => $groupItemId));
0186     }
0187     
0188     
0189     public function fetchTagGroupsForCategory($cat_id) {
0190         $sql = " SELECT category_tag_group.tag_group_id
0191                         , tag_group.group_name
0192                         , tag_group.group_display_name
0193                         , tag_group.group_legacy_name
0194                         , tag_group.is_multi_select
0195                         , category_tag_group.category_id
0196                         , project_category.title
0197                  FROM category_tag_group
0198                  JOIN tag_group ON tag_group.group_id = category_tag_group.tag_group_id
0199                  JOIN project_category ON project_category.project_category_id = category_tag_group.category_id
0200                  WHERE category_tag_group.category_id = :cat_id";
0201         $resultSet = $this->getAdapter()->fetchAll($sql, array('cat_id' => $cat_id));
0202         
0203         return $resultSet;
0204     }
0205     
0206     
0207     public function updateTagGroupsPerCategory($cat_id,$taggroups)
0208     {
0209         $sql = "delete from category_tag_group where category_id=:cat_id";
0210         $this->getAdapter()->query($sql, array('cat_id' => $cat_id));
0211 
0212         if($taggroups){
0213             $taggroup_id =explode(',', $taggroups);
0214             $prepared_insert =
0215                 array_map(function ($id) use ($cat_id) { return "({$cat_id},{$id})"; },
0216                     $taggroup_id);
0217             $sql = "INSERT IGNORE INTO category_tag_group (category_id, tag_group_id) VALUES " . implode(',',
0218                     $prepared_insert);
0219           
0220             $this->getAdapter()->query($sql);
0221         }
0222     }
0223     
0224     public function updateTagGroupsPerStore($store_id,$taggroups)
0225     {
0226         $sql = "delete from config_store_tag_group where store_id=:store_id";
0227         $this->getAdapter()->query($sql, array('store_id' => $store_id));
0228 
0229         if($taggroups){
0230             $taggroup_id =explode(',', $taggroups);
0231             $prepared_insert =
0232                 array_map(function ($id) use ($store_id) { return "({$store_id},{$id})"; },
0233                     $taggroup_id);
0234             $sql = "INSERT IGNORE INTO config_store_tag_group (store_id, tag_group_id) VALUES " . implode(',',
0235                     $prepared_insert);
0236           
0237             $this->getAdapter()->query($sql);
0238         }
0239     }
0240 
0241 }