File indexing completed on 2024-10-13 13:29:13

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  * Created: 13.09.2017
0023  */
0024 
0025 class Application_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     /**
0054      * @return Zend_Db_Adapter_Abstract
0055      */
0056     private function getAdapter()
0057     {
0058         return Zend_Db_Table::getDefaultAdapter();
0059     }
0060 
0061     /**
0062      * @param int $group_id
0063      *
0064      * @return array
0065      */
0066     public function fetchGroupItems($group_id)
0067     {
0068         $sql = "SELECT tag_group_item.tag_group_item_id, tag_group_item.tag_group_id, tag.tag_id, tag.tag_name 
0069              FROM tag_group_item 
0070              JOIN tag ON tag.tag_id = tag_group_item.tag_id 
0071              WHERE tag_group_id = :group_id";
0072         $resultSet = $this->getAdapter()->fetchAll($sql, array('group_id' => $group_id));
0073 
0074         return $resultSet;
0075     }
0076 
0077     /**
0078      * @param int    $group_id
0079      * @param string $tag_name
0080      *
0081      * @return array
0082      */
0083     public function assignGroupTag($group_id, $tag_name)
0084     {
0085         $tag_id = $this->saveTag($tag_name);
0086         $group_tag_id = $this->saveGroupTag($group_id, $tag_id);
0087         $resultSet = $this->fetchOneGroupItem($group_tag_id);
0088 
0089         return $resultSet;
0090     }
0091 
0092     /**
0093      * @param string $tag_name
0094      *
0095      * @return int
0096      */
0097     public function saveTag($tag_name)
0098     {
0099         $sql = "SELECT tag_id FROM tag WHERE tag_name = :tagName";
0100         $resultSet = $this->getAdapter()->fetchRow($sql, array('tagName' => $tag_name));
0101         if (empty($resultSet)) {
0102             $this->getAdapter()->insert('tag', array('tag_name' => $tag_name));
0103             $resultId = $this->getAdapter()->lastInsertId();
0104         } else {
0105             $resultId = $resultSet['tag_id'];
0106         }
0107 
0108         return $resultId;
0109     }
0110 
0111     /**
0112      * @param int $group_id
0113      * @param int $tag_id
0114      *
0115      * @return int
0116      */
0117     public function saveGroupTag($group_id, $tag_id)
0118     {
0119         $sql = "SELECT tag_group_item_id FROM tag_group_item WHERE tag_group_id = :group_id AND tag_id = :tag_id";
0120         $resultSet = $this->getAdapter()->fetchRow($sql, array('group_id' => $group_id, 'tag_id' => $tag_id));
0121         if (empty($resultSet)) {
0122             $this->getAdapter()->insert('tag_group_item', array('tag_group_id' => $group_id, 'tag_id' => $tag_id));
0123             $resultId = $this->getAdapter()->lastInsertId();
0124         } else {
0125             $resultId = $resultSet['tag_group_item_id'];
0126         }
0127 
0128         return $resultId;
0129     }
0130 
0131     /**
0132      * @param int $group_item_id
0133      *
0134      * @return array|false
0135      */
0136     public function fetchOneGroupItem($group_item_id)
0137     {
0138         $sql = "SELECT tag_group_item.tag_group_item_id, tag_group_item.tag_group_id, tag.tag_id, tag.tag_name 
0139              FROM tag_group_item 
0140              JOIN tag ON tag.tag_id = tag_group_item.tag_id 
0141              WHERE tag_group_item_id = :group_item_id";
0142         $resultSet = $this->getAdapter()->fetchRow($sql, array('group_item_id' => $group_item_id));
0143 
0144         return $resultSet;
0145     }
0146 
0147     public function updateGroupTag($tag_id, $tag_name)
0148     {
0149         $this->getAdapter()->update('tag', array('tag_name' => $tag_name), array('tag_id = ?' => $tag_id));
0150     }
0151 
0152     public function deleteGroupTag($groupItemId)
0153     {
0154         $this->getAdapter()->delete('tag_group_item', array('tag_group_item_id = ?' => $groupItemId));
0155     }
0156 
0157 }