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: 11.09.2017
0023  */
0024 
0025 class Application_Model_Tags
0026 {
0027     const TAG_TYPE_PROJECT = 1;
0028     const TAG_TYPE_FILE = 3;
0029 
0030     /**
0031      * Application_Model_Tags constructor.
0032      */
0033     public function __construct()
0034     {
0035 
0036     }
0037 
0038     /**
0039      * @param int    $object_id
0040      * @param string $tags
0041      * @param int    $tag_type
0042      */
0043     public function processTags($object_id, $tags, $tag_type)
0044     {
0045         $this->assignTags($object_id, $tags, $tag_type);
0046         $this->deassignTags($object_id, $tags, $tag_type);
0047     }
0048 
0049     /**
0050      * @param int    $object_id
0051      * @param string $tags
0052      * @param int    $tag_type
0053      */
0054     public function assignTags($object_id, $tags, $tag_type)
0055     {
0056         $new_tags = array_diff(explode(',', $tags), explode(',', $this->getTags($object_id, $tag_type)));
0057 
0058         $tableTags = new Application_Model_DbTable_Tags();
0059         $listIds = $tableTags->storeTags(implode(',', $new_tags));
0060 
0061         $prepared_insert =
0062             array_map(function ($id) use ($object_id, $tag_type) { return "({$id}, {$tag_type}, {$object_id})"; },
0063                 $listIds);
0064         $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id) VALUES " . implode(',',
0065                 $prepared_insert);
0066         $this->getAdapter()->query($sql);
0067     }
0068 
0069     /**
0070      * @param int $object_id
0071      * @param int $tag_type
0072      *
0073      * @return string|null
0074      */
0075     public function getTags($object_id, $tag_type)
0076     {
0077         $sql = "
0078             SELECT GROUP_CONCAT(tag.tag_name) AS tag_names 
0079             FROM tag_object
0080             JOIN tag ON tag.tag_id = tag_object.tag_id
0081             WHERE tag_type_id = :type AND tag_object_id = :object_id
0082             GROUP BY tag_object.tag_object_id
0083         ";
0084 
0085         $result = $this->getAdapter()->fetchRow($sql, array('type' => $tag_type, 'object_id' => $object_id));
0086         if (isset($result['tag_names'])) {
0087             return $result['tag_names'];
0088         }
0089 
0090         return null;
0091     }
0092     
0093     
0094     /**
0095      * @param int $object_id
0096      * @param int $tag_type
0097      *
0098      * @return string|null
0099      */
0100     public function getTagsAsArray($object_id, $tag_type)
0101     {
0102         $sql = "
0103             SELECT tag.tag_name
0104             FROM tag_object
0105             JOIN tag ON tag.tag_id = tag_object.tag_id
0106             WHERE tag_type_id = :type 
0107             AND tag_object_id = :object_id
0108             AND tag_object.is_deleted = 0
0109         ";
0110 
0111         $result = $this->getAdapter()->fetchAll($sql, array('type' => $tag_type,'object_id' => $object_id));
0112         $returnArray = array();
0113         if (isset($result)) {
0114             foreach ($result as $tag) {
0115                 $returnArray[] = $tag['tag_name'];
0116             }
0117             return $returnArray;
0118         }
0119 
0120         return null;
0121     }
0122     
0123     /**
0124      * @return array|null
0125      */
0126     public function getAllFilePackageTypeTags()
0127     {
0128         $sql = "
0129             select t.tag_name from tag t
0130             join tag_group_item tgi on tgi.tag_id = t.tag_id
0131             join tag_group tg on tg.group_id = tgi.tag_group_id
0132             where tgi.tag_group_id = 8
0133         ";
0134 
0135         $result = $this->getAdapter()->fetchAll($sql);
0136         $returnArray = array();
0137         if (isset($result)) {
0138             foreach ($result as $tag) {
0139                 $returnArray[] = $tag['tag_name'];
0140             }
0141             return $returnArray;
0142         }
0143 
0144         return null;
0145     }
0146     
0147     /**
0148      * @return array|null
0149      */
0150     public function getAllFileArchitectureTags()
0151     {
0152         $sql = "
0153             select t.tag_name from tag t
0154             join tag_group_item tgi on tgi.tag_id = t.tag_id
0155             join tag_group tg on tg.group_id = tgi.tag_group_id
0156             where tgi.tag_group_id = 9
0157         ";
0158 
0159         $result = $this->getAdapter()->fetchAll($sql);
0160         $returnArray = array();
0161         if (isset($result)) {
0162             foreach ($result as $tag) {
0163                 $returnArray[] = $tag['tag_name'];
0164             }
0165             return $returnArray;
0166         }
0167 
0168         return null;
0169     }
0170     
0171     
0172     /**
0173      * @return array|null
0174      */
0175     public function getAllFilePlasmaVersionTags()
0176     {
0177         $playsmavesionTagGroup = Zend_Registry::get('config')->settings->client->default->tag_group_plasmaversion_id;
0178         
0179         $sql = "
0180             select t.tag_name from tag t
0181             join tag_group_item tgi on tgi.tag_id = t.tag_id
0182             join tag_group tg on tg.group_id = tgi.tag_group_id
0183             where tgi.tag_group_id = ".$playsmavesionTagGroup."
0184         ";
0185 
0186         $result = $this->getAdapter()->fetchAll($sql);
0187         $returnArray = array();
0188         if (isset($result)) {
0189             foreach ($result as $tag) {
0190                 $returnArray[] = $tag['tag_name'];
0191             }
0192             return $returnArray;
0193         }
0194 
0195         return null;
0196     }
0197     
0198     
0199     /**
0200      * @return array|null
0201      */
0202     public function fetchAllFileTagNamesAsArray()
0203     {
0204         $sql = "
0205             select t.tag_name from tag t
0206             join tag_group_item tgi on tgi.tag_id = t.tag_id
0207             join tag_group tg on tg.group_id = tgi.tag_group_id
0208             where tgi.tag_group_id in (8,9)
0209 
0210         ";
0211 
0212         $result = $this->getAdapter()->fetchAll($sql);
0213         $returnArray = array();
0214         if (isset($result)) {
0215             foreach ($result as $tag) {
0216                 $returnArray[] = $tag['tag_name'];
0217             }
0218             return $returnArray;
0219         }
0220 
0221         return null;
0222     }
0223     
0224     
0225     /**
0226      * @param int $object_id ProjectId
0227      * @param int $whereStatement SQl-Where-Statement
0228      *
0229      * @return string|null
0230      */
0231     public function getFilesForTags($object_id, $whereStatement)
0232     {
0233         $sql = "
0234             select * from stat_file_tags
0235             where project_id = :project_id 
0236         ";
0237         $sql .= $whereStatement;
0238         
0239         //var_dump($sql);
0240         
0241         $result = $this->getAdapter()->fetchAll($sql, array('project_id' => $object_id));
0242         if (isset($result)) {
0243             return $result;
0244         }
0245 
0246         return null;
0247     }
0248     
0249     
0250     /**
0251      * @param int $object_id
0252      * @param int $tag_type
0253      *
0254      * @return string|null
0255      */
0256     public function getTag($tag_id)
0257     {
0258         $sql = "
0259             SELECT *
0260             FROM tag
0261             WHERE tag_id = :tag_id
0262         ";
0263 
0264         $result = $this->getAdapter()->fetchRow($sql, array('tag_id' => $tag_id));
0265         if (isset($result)) {
0266             return $result;
0267         }
0268 
0269         return null;
0270     }
0271 
0272     /**
0273      * @return Zend_Db_Adapter_Abstract
0274      */
0275     private function getAdapter()
0276     {
0277         return Zend_Db_Table::getDefaultAdapter();
0278     }
0279 
0280     /**
0281      * @param int    $object_id
0282      * @param string $tags
0283      * @param int    $tag_type
0284      */
0285     public function deassignTags($object_id, $tags, $tag_type)
0286     {
0287         $removable_tags = array_diff(explode(',', $this->getTags($object_id, $tag_type)), explode(',', $tags));
0288 
0289         $sql = "DELETE tag_object FROM tag_object JOIN tag ON tag.tag_id = tag_object.tag_id WHERE tag.tag_name = :name";
0290         foreach ($removable_tags as $removable_tag) {
0291             $this->getAdapter()->query($sql, array('name' => $removable_tag));
0292         }
0293         $this->updateChanged($object_id, $tag_type);
0294     }
0295 
0296     private function updateChanged($object_id, $tag_type)
0297     {
0298         $sql = "UPDATE tag_object SET tag_changed = NOW() WHERE tag_object_id = :tagObjectId AND tag_type_id = :tagType";
0299         $this->getAdapter()->query($sql, array('tagObjectId' => $object_id, 'tagType' => $tag_type));
0300     }
0301 
0302 }