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

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  * Created: 11.09.2017
0024  */
0025 class Default_Model_Tags
0026 {
0027     const TAG_TYPE_PROJECT = 1;
0028     const TAG_TYPE_MEMBER = 2;
0029     const TAG_TYPE_FILE = 3;
0030     const TAG_TYPE_OSUSER = 9;
0031 
0032 
0033     const TAG_USER_GROUPID = 5;
0034     const TAG_CATEGORY_GROUPID = 6;
0035 
0036     const TAG_LICENSE_GROUPID = 7;
0037     const TAG_PACKAGETYPE_GROUPID = 8;
0038     const TAG_ARCHITECTURE_GROUPID = 9;
0039     const TAG_GHNS_EXCLUDED_GROUPID = 10;
0040 
0041     const TAG_PRODUCT_ORIGINAL_GROUPID = 11;
0042     const TAG_PRODUCT_ORIGINAL_ID = 2451;
0043    
0044     const TAG_PRODUCT_EBOOK_GROUPID = 14;
0045     const TAG_PRODUCT_EBOOK_AUTHOR_GROUPID = 15;
0046     const TAG_PRODUCT_EBOOK_EDITOR_GROUPID = 16;
0047     const TAG_PRODUCT_EBOOK_ILLUSTRATOR_GROUPID = 17;
0048     const TAG_PRODUCT_EBOOK_TRANSLATOR_GROUPID = 18;
0049     const TAG_PRODUCT_EBOOK_SUBJECT_GROUPID = 19;
0050     const TAG_PRODUCT_EBOOK_SHELF_GROUPID = 20;
0051     const TAG_PRODUCT_EBOOK_LANGUAGE_GROUPID = 21;
0052     const TAG_PRODUCT_EBOOK_TYPE_GROUPID = 22;
0053 
0054 
0055     const TAG_PRODUCT_EBOOK_ID = 2532;
0056 
0057     const TAG_PROJECT_GROUP_IDS = '6,7,10';//type product : category-tags, license-tags,ghns_excluded
0058     const TAG_FILE_GROUP_IDS = '8,9';//file-packagetype-tags,file-architecture-tags
0059     // $tag_project_group_ids ='6,7,10';  
0060     // $tag_file_group_ids ='8,9';  
0061 
0062     /**
0063      * Default_Model_Tags constructor.
0064      */
0065     public function __construct()
0066     {
0067 
0068     }
0069 
0070     /**
0071      * @param int    $object_id
0072      * @param string $tags
0073      * @param int    $tag_type
0074      */
0075     public function processTags($object_id, $tags, $tag_type)
0076     {
0077         $this->assignTags($object_id, $tags, $tag_type);
0078         $this->deassignTags($object_id, $tags, $tag_type);
0079     }
0080 
0081 
0082     /**
0083      * @param int    $object_id
0084      * @param string $tags
0085      * @param int    $tag_type
0086      */
0087     public function assignTags($object_id, $tags, $tag_type)
0088     {
0089         $new_tags = array_diff(explode(',', $tags), explode(',', $this->getTags($object_id, $tag_type)));
0090 
0091 
0092         $tableTags = new Default_Model_DbTable_Tags();
0093         $listIds = $tableTags->storeTags(implode(',', $new_tags));
0094 
0095         $prepared_insert =
0096             array_map(function ($id) use ($object_id, $tag_type) {
0097                 return "({$id}, {$tag_type}, {$object_id})";
0098             },
0099                 $listIds);
0100         $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id) VALUES " . implode(',',
0101                 $prepared_insert);
0102 
0103 
0104         $this->getAdapter()->query($sql);
0105     }
0106 
0107 
0108     /**
0109      * @param int $object_id
0110      * @param int $tag_type
0111      *
0112      * @return string|null
0113      */
0114     public function getTags($object_id, $tag_type)
0115     {
0116         $sql = "
0117             SELECT GROUP_CONCAT(`tag`.`tag_name`) AS `tag_names` 
0118             FROM `tag_object`
0119             JOIN `tag` ON `tag`.`tag_id` = `tag_object`.`tag_id`
0120             JOIN `tag_group_item` ON `tag_object`.`tag_id` = `tag_group_item`.`tag_id`
0121             WHERE `tag_type_id` = :type AND `tag_object_id` = :object_id
0122             AND `tag_group_item`.`tag_group_id` <> :tag_user_groupid
0123             AND `tag_object`.`is_deleted` = 0
0124             GROUP BY `tag_object`.`tag_object_id`
0125         ";
0126 
0127         $result = $this->getAdapter()->fetchRow($sql, array(
0128             'type'             => $tag_type,
0129             'object_id'        => $object_id,
0130             'tag_user_groupid' => Default_Model_Tags::TAG_USER_GROUPID
0131         ));
0132         if (isset($result['tag_names'])) {
0133             return $result['tag_names'];
0134         }
0135 
0136         return null;
0137     }
0138 
0139     /**
0140      * @return Zend_Db_Adapter_Abstract
0141      */
0142     private function getAdapter()
0143     {
0144         return Zend_Db_Table::getDefaultAdapter();
0145     }
0146 
0147     /**
0148      * @param int    $object_id
0149      * @param string $tags
0150      * @param int    $tag_type
0151      */
0152     public function deassignTags($object_id, $tags, $tag_type)
0153     {
0154         $removable_tags = array_diff(explode(',', $this->getTags($object_id, $tag_type)), explode(',', $tags));
0155 
0156         //$sql = "DELETE tag_object FROM tag_object JOIN tag ON tag.tag_id = tag_object.tag_id WHERE tag.tag_name = :name and tag_object.tag_object_id=:object_id";
0157         //$sql = "UPDATE `tag_object` INNER JOIN `tag` ON `tag`.`tag_id` = `tag_object`.`tag_id`  SET `tag_changed` = NOW() , `is_deleted` = 1 WHERE `tag`.`tag_name` = :name AND `tag_object`.`tag_object_id`=:object_id";
0158         $sql = "UPDATE `tag_object` INNER JOIN `tag` ON `tag`.`tag_id` = `tag_object`.`tag_id`  SET `tag_changed` = NOW() , `is_deleted` = 1 
0159         WHERE `tag`.`tag_name` = :name AND `tag_object`.`tag_object_id`=:object_id AND AND `tag_object`.`tag_type_id` = :tagType";
0160         //$this->getAdapter()->query($sql, array('tagObjectId' => $object_id, 'tagType' => $tag_type));
0161 
0162         foreach ($removable_tags as $removable_tag) {
0163             $this->getAdapter()->query($sql, array('name' => $removable_tag, 'object_id' => $object_id,'tagType' => $tag_type));
0164         }
0165         $this->updateChanged($object_id, $tag_type);
0166     }
0167 
0168     private function updateChanged($object_id, $tag_type)
0169     {
0170         $sql = "UPDATE `tag_object` SET `tag_changed` = NOW() WHERE `tag_object_id` = :tagObjectId AND `tag_type_id` = :tagType";
0171         $this->getAdapter()->query($sql, array('tagObjectId' => $object_id, 'tagType' => $tag_type));
0172     }
0173 
0174     /**
0175      * @param int    $project_id
0176      * @param string $tagname
0177      * @return string|null
0178      */
0179     public function isTagsUserExisting($project_id, $tagname)
0180     {
0181         $sql_object = "SELECT count(1) AS `cnt` FROM `tag_object` JOIN `tag` ON `tag`.`tag_id` = `tag_object`.`tag_id` WHERE `tag`.`tag_name` = :tagname AND `tag_object`.`tag_group_id`=:tag_group_id AND `tag_object`.`is_deleted`=0 AND `tag_object`.`tag_object_id`=:project_id AND `tag_object`.`tag_type_id`=:tag_type_id";
0182         $r = $this->getAdapter()->fetchRow($sql_object, array(
0183             'tagname'      => $tagname
0184         ,
0185             'tag_group_id' => Default_Model_Tags::TAG_USER_GROUPID
0186         ,
0187             'project_id'   => $project_id
0188         ,
0189             'tag_type_id'  => Default_Model_Tags::TAG_TYPE_PROJECT
0190         ));
0191         if ($r['cnt'] == 0) {
0192             return false;
0193         } else {
0194             return true;
0195         }
0196     }
0197 
0198     /**
0199      * @param int $object_id
0200      * @param int $tag_type
0201      *
0202      * @return string|null
0203      */
0204     public function getTagsCategory($object_id, $tag_type)
0205     {
0206         $tag_group_ids = $this::TAG_CATEGORY_GROUPID;
0207         $tags = $this->getTagsArray($object_id, $tag_type, $tag_group_ids);
0208 
0209         $tag_names = '';
0210         foreach ($tags as $tag) {
0211             $tag_names = $tag_names . $tag['tag_name'] . ',';
0212         }
0213         $len = strlen($tag_names);
0214         if ($len > 0) {
0215             return substr($tag_names, 0, ($len - 1));
0216         }
0217 
0218         return null;
0219     }
0220 
0221     /**
0222      * @param int    $object_id
0223      * @param int    $tag_type
0224      * @param String $tag_group_ids
0225      *
0226      * @return array
0227      */
0228     public function getTagsArray($object_id, $tag_type, $tag_group_ids)
0229     {
0230         $sql = "
0231                         SELECT tag.tag_id,tag.tag_name,tag_group_item.tag_group_id,tag.tag_fullname, tag.tag_description
0232                         FROM tag_object
0233                         JOIN tag ON tag.tag_id = tag_object.tag_id
0234                         join tag_group_item on tag_object.tag_id = tag_group_item.tag_id and tag_object.tag_group_id = tag_group_item.tag_group_id
0235                         WHERE tag_type_id = :type AND tag_object_id = :object_id
0236                         and tag_object.tag_group_id in  ({$tag_group_ids} )     
0237                         and tag_object.is_deleted = 0  
0238                         order by tag_group_item.tag_group_id desc , tag.tag_name asc
0239             ";
0240 
0241         $result = $this->getAdapter()->fetchAll($sql, array('type' => $tag_type, 'object_id' => $object_id));
0242 
0243         return $result;
0244     }
0245 
0246     /**
0247      * @param int $object_id
0248      * @param int $tag_type
0249      *
0250      * @return string|null
0251      */
0252     public function getTagsSystem($object_id, $tag_type)
0253     {
0254         $tag_group_ids = '6,7,10';
0255         $tags = $this->getTagsArray($object_id, $tag_type, $tag_group_ids);
0256 
0257         $tag_names = '';
0258         foreach ($tags as $tag) {
0259             $tag_names = $tag_names . $tag['tag_name'] . ',';
0260         }
0261         $len = strlen($tag_names);
0262         if ($len > 0) {
0263             return substr($tag_names, 0, ($len - 1));
0264         }
0265 
0266         return null;
0267     }
0268 
0269     /**
0270      * @param int $project_id
0271      * @return array
0272      */
0273     public function getTagsSystemList($project_id)
0274     {
0275         $tag_project_group_ids = self::TAG_PROJECT_GROUP_IDS;
0276         $tag_file_group_ids = self::TAG_FILE_GROUP_IDS;
0277         $sql = "
0278                 SELECT tag.tag_id,tag.tag_name,tag_object.tag_group_id
0279                 FROM tag_object
0280                 JOIN tag ON tag.tag_id = tag_object.tag_id                
0281                 WHERE tag_type_id = :type_project AND tag_object_id = :project_id
0282                 and tag_object.tag_group_id in  ({$tag_project_group_ids} )     
0283                 and tag_object.is_deleted = 0  
0284                 union all
0285                 SELECT distinct t.tag_id,t.tag_name,o.tag_group_id
0286                 FROM tag_object o
0287                 JOIN tag t ON t.tag_id = o.tag_id  
0288                 inner join project p on o.tag_parent_object_id = p.project_id                 
0289                 inner join ppload.ppload_files f on p.ppload_collection_id = f.collection_id and o.tag_object_id=f.id and f.active = 1
0290                 WHERE o.tag_type_id = :type_file AND p.project_id = :project_id
0291                 and o.tag_group_id in  ({$tag_file_group_ids} )       
0292                 and o.is_deleted = 0  
0293                 order by tag_group_id  , tag_name                 
0294         ";
0295         $result = $this->getAdapter()->fetchAll($sql, array(
0296             'type_project' => Default_Model_Tags::TAG_TYPE_PROJECT
0297         ,
0298             'project_id'   => $project_id
0299         ,
0300             'type_file'    => Default_Model_Tags::TAG_TYPE_FILE
0301         ));
0302 
0303 
0304         return $result;
0305     }
0306 
0307     /**
0308      * @param int $object_id
0309      * @param int $tag_type
0310      *
0311      * @return string|null
0312      */
0313     public function getTagsUserCount($object_id, $tag_type)
0314     {
0315         $sql = "
0316             SELECT count(*) AS `cnt`
0317             FROM `tag_object`
0318             JOIN `tag` ON `tag`.`tag_id` = `tag_object`.`tag_id`
0319             JOIN `tag_group_item` ON `tag_object`.`tag_id` = `tag_group_item`.`tag_id` AND `tag_object`.`tag_group_id` = `tag_group_item`.`tag_group_id`
0320             WHERE `tag_type_id` = :type AND `tag_object_id` = :object_id      
0321             AND `tag_object`.`is_deleted` = 0      
0322             AND `tag_group_item`.`tag_group_id` = :tag_user_groupid     
0323 
0324         ";
0325 
0326         $result = $this->getAdapter()->fetchRow($sql, array(
0327             'type'             => $tag_type,
0328             'object_id'        => $object_id,
0329             'tag_user_groupid' => Default_Model_Tags::TAG_USER_GROUPID
0330         ));
0331         if (isset($result['cnt'])) {
0332             return $result['cnt'];
0333         }
0334 
0335         return 0;
0336     }
0337 
0338     public function filterTagsUser($filter, $limit)
0339     {
0340         $sql = "
0341              select 
0342              tag.tag_id 
0343              ,tag.tag_name
0344              from tag
0345              join tag_group_item on tag.tag_id = tag_group_item.tag_id and tag_group_item.tag_group_id = :tag_user_groupid
0346              where tag.tag_name like '%" . $filter . "%'
0347                ";
0348         if (isset($limit)) {
0349             $sql .= ' limit ' . $limit;
0350         }
0351 
0352         $result = $this->getAdapter()->fetchAll($sql,
0353             array('tag_user_groupid' => Default_Model_Tags::TAG_USER_GROUPID));
0354 
0355         return $result;
0356     }
0357 
0358     /**
0359      * @param int    $object_id
0360      * @param string $tags
0361      * @param int    $tag_type
0362      */
0363     public function processTagsUser($object_id, $tags, $tag_type)
0364     {
0365         if ($tags) {
0366             $this->assignTagsUser($object_id, $tags, $tag_type);
0367         }
0368         $this->deassignTagsUser($object_id, $tags, $tag_type);
0369     }
0370 
0371     /**
0372      * @param int    $object_id
0373      * @param string $tags
0374      * @param int    $tag_type
0375      */
0376     public function assignTagsUser($object_id, $tags, $tag_type)
0377     {
0378         $tags = strtolower($tags);
0379         $tag_group_id = 5;
0380         $new_tags = array_diff(explode(',', $tags), explode(',', $this->getTagsUser($object_id, $tag_type)));
0381         if (sizeof($new_tags) > 0) {
0382             $tableTags = new Default_Model_DbTable_Tags();
0383             $listIds = $tableTags->storeTagsUser(implode(',', $new_tags));
0384 
0385             $prepared_insert =
0386                 array_map(function ($id) use ($object_id, $tag_type, $tag_group_id) {
0387                     return "({$id}, {$tag_type}, {$object_id},{$tag_group_id})";
0388                 },
0389                     $listIds);
0390             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id,tag_group_id) VALUES " . implode(',',
0391                     $prepared_insert);
0392             $this->getAdapter()->query($sql);
0393         }
0394     }
0395 
0396     /**
0397      * @param int $object_id
0398      * @param int $tag_type
0399      *
0400      * @return string|null
0401      */
0402     public function getTagsUser($object_id, $tag_type)
0403     {
0404         $tag_group_ids = $this::TAG_USER_GROUPID;
0405         $tags = $this->getTagsArray($object_id, $tag_type, $tag_group_ids);
0406 
0407         $tag_names = '';
0408         foreach ($tags as $tag) {
0409             $tag_names = $tag_names . $tag['tag_name'] . ',';
0410         }
0411         $len = strlen($tag_names);
0412         if ($len > 0) {
0413             return substr($tag_names, 0, ($len - 1));
0414         }
0415 
0416         return null;
0417     }
0418 
0419     public function deassignTagsUser($object_id, $tags, $tag_type)
0420     {
0421         if ($tags) {
0422             $tags = strtolower($tags);
0423             $removable_tags = array_diff(explode(',', $this->getTagsUser($object_id, $tag_type)), explode(',', $tags));
0424         } else {
0425             $removable_tags = explode(',', $this->getTagsUser($object_id, $tag_type));
0426         }
0427 
0428         //$sql = "DELETE tag_object FROM tag_object JOIN tag ON tag.tag_id = tag_object.tag_id WHERE tag_group_id = ".Default_Model_Tags::TAG_USER_GROUPID." and tag.tag_name = :name and tag_object.tag_object_id=:object_id";
0429         $sql = "UPDATE tag_object inner join tag ON tag.tag_id = tag_object.tag_id set tag_changed = NOW() , is_deleted = 1 
0430                     WHERE tag_group_id = " . Default_Model_Tags::TAG_USER_GROUPID . " and tag.tag_name = :name and tag_object.tag_object_id=:object_id";
0431 
0432         foreach ($removable_tags as $removable_tag) {
0433             $this->getAdapter()->query($sql, array('name' => $removable_tag, 'object_id' => $object_id));
0434             // if Tag is the only one in Tag_object table then delete this tag for user_groupid = 5
0435 
0436             $sql_object = "SELECT count(1)  AS `cnt` FROM `tag_object` JOIN `tag` ON `tag`.`tag_id` = `tag_object`.`tag_id` WHERE `tag`.`tag_name` = :name";
0437             $r = $this->getAdapter()->fetchRow($sql_object, array('name' => $removable_tag));
0438             if ($r['cnt'] == 0) {
0439                 // then remove tag if not existing in Tag_object
0440                 $sql_delete_tag = "delete from tag where tag_name=:name";
0441                 $this->getAdapter()->query($sql_delete_tag, array('name' => $removable_tag));
0442             }
0443         }
0444         $this->updateChanged($object_id, $tag_type);
0445     }
0446 
0447     public function isProductOriginal($project_id)
0448     {
0449         $sql_object = "select tag_item_id  from tag_object WHERE tag_id = :tag_id and tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
0450                                     and tag_type_id = :tag_type_id and is_deleted = 0";
0451         $r = $this->getAdapter()->fetchRow($sql_object, array(
0452             'tag_id'        => self::TAG_PRODUCT_ORIGINAL_ID,
0453             'tag_object_id' => $project_id,
0454             'tag_group_id'  => self::TAG_PRODUCT_ORIGINAL_GROUPID,
0455             'tag_type_id'   => self::TAG_TYPE_PROJECT
0456         ));
0457         if ($r) {
0458             return true;
0459         } else {
0460             return false;
0461         }
0462     }
0463     
0464     public function isProductModification($project_id)
0465     {
0466         $tag_modification_id = Zend_Registry::get('config')->settings->client->default->tag_modification_id;
0467         
0468         $sql_object = "select tag_item_id  from tag_object WHERE tag_id = :tag_id and tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
0469                                     and tag_type_id = :tag_type_id and is_deleted = 0";
0470         $r = $this->getAdapter()->fetchRow($sql_object, array(
0471             'tag_id'        => $tag_modification_id,
0472             'tag_object_id' => $project_id,
0473             'tag_group_id'  => self::TAG_PRODUCT_ORIGINAL_GROUPID,
0474             'tag_type_id'   => self::TAG_TYPE_PROJECT
0475         ));
0476         if ($r) {
0477             return true;
0478         } else {
0479             return false;
0480         }
0481     }
0482     
0483     public function isProductDangerous($project_id)
0484     {
0485         $tag_dangerous_group_id = Zend_Registry::get('config')->settings->client->default->tag_group_dangerous_id;
0486         $tag_dangerous_id = Zend_Registry::get('config')->settings->client->default->tag_dangerous_id;
0487         
0488         $sql_object = "select tag_item_id  from tag_object WHERE tag_id = :tag_id and tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
0489                                     and tag_type_id = :tag_type_id and is_deleted = 0";
0490         $r = $this->getAdapter()->fetchRow($sql_object, array(
0491             'tag_id'        => $tag_dangerous_id,
0492             'tag_object_id' => $project_id,
0493             'tag_group_id'  => $tag_dangerous_group_id,
0494             'tag_type_id'   => self::TAG_TYPE_PROJECT
0495         ));
0496         if ($r) {
0497             return true;
0498         } else {
0499             return false;
0500         }
0501     }
0502 
0503     public function isProductDeprecatedModerator($project_id)
0504     {
0505         $tag_group_id = Zend_Registry::get('config')->settings->client->default->tag_group_deprecated_id;
0506         $tag_id = Zend_Registry::get('config')->settings->client->default->tag_deprecated_id;        
0507         return $this->isTagObjectExistingForProject($project_id, $tag_group_id,$tag_id);
0508     }
0509 
0510     protected function isTagObjectExistingForProject($project_id,$tag_group_id,$tag_id)
0511     {
0512         $sql_object = "select tag_item_id  from tag_object WHERE tag_id = :tag_id and tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
0513                                     and tag_type_id = :tag_type_id and is_deleted = 0";
0514         $r = $this->getAdapter()->fetchRow($sql_object, array(
0515             'tag_id'        => $tag_id,
0516             'tag_object_id' => $project_id,
0517             'tag_group_id'  => $tag_group_id,
0518             'tag_type_id'   => self::TAG_TYPE_PROJECT
0519         ));
0520         if ($r) {
0521             return true;
0522         } else {
0523             return false;
0524         }
0525 
0526     }
0527 
0528 
0529     public function isProductEbook($project_id)
0530     {
0531         $ebookTagGroupId = Zend_Registry::get('config')->settings->client->default->tag_group_ebook;
0532         $ebookTagId = Zend_Registry::get('config')->settings->client->default->tag_is_ebook;
0533 
0534         $sql_object = "select tag_item_id  from tag_object WHERE tag_id = :tag_id and tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
0535                                 and tag_type_id = :tag_type_id and is_deleted = 0";
0536         $r = $this->getAdapter()->fetchRow($sql_object, array(
0537             'tag_id'        => $ebookTagId,
0538             'tag_object_id' => $project_id,
0539             'tag_group_id'  => $ebookTagGroupId,
0540             'tag_type_id'   => self::TAG_TYPE_PROJECT
0541         ));
0542         if ($r) {
0543             return true;
0544         } else {
0545             return false;
0546         }
0547     }
0548 
0549     /**
0550      * @param int $object_id
0551      * @param int $is_original
0552      */
0553     public function processTagProductOriginal($object_id, $is_original)
0554     {
0555         $sql_object = "select tag_item_id  from tag_object WHERE tag_id = :tag_id and tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
0556                                 and tag_type_id = :tag_type_id and is_deleted = 0";
0557         $r = $this->getAdapter()->fetchRow($sql_object, array(
0558             'tag_id'        => self::TAG_PRODUCT_ORIGINAL_ID,
0559             'tag_object_id' => $object_id,
0560             'tag_group_id'  => self::TAG_PRODUCT_ORIGINAL_GROUPID,
0561             'tag_type_id'   => self::TAG_TYPE_PROJECT
0562         ));
0563 
0564         if ($is_original == '1') {
0565             if (!$r) {
0566                 $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
0567                 $this->getAdapter()->query($sql, array(
0568                     'tag_id'        => self::TAG_PRODUCT_ORIGINAL_ID,
0569                     'tag_type_id'   => self::TAG_TYPE_PROJECT,
0570                     'tag_object_id' => $object_id,
0571                     'tag_group_id'  => self::TAG_PRODUCT_ORIGINAL_GROUPID
0572                 ));
0573             }
0574         } else {
0575 
0576             if ($r) {
0577                 $sql = "UPDATE tag_object set tag_changed = NOW() , is_deleted = 1  WHERE tag_item_id = :tagItemId";
0578                 $this->getAdapter()->query($sql, array('tagItemId' => $r['tag_item_id']));
0579             }
0580         }
0581 
0582     }
0583     
0584     /**
0585      * @param int $object_id
0586      * @param int $original (null, 1 or 2)
0587      */
0588     public function processTagProductOriginalOrModification($object_id, $original)
0589     {
0590         $tag_original_id = Zend_Registry::get('config')->settings->client->default->tag_original_id;
0591         $tag_modification_id = Zend_Registry::get('config')->settings->client->default->tag_modification_id;
0592         
0593         $sql_object = "select tag_item_id  from tag_object WHERE tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
0594                                 and tag_type_id = :tag_type_id and is_deleted = 0";
0595         $r = $this->getAdapter()->fetchAll($sql_object, array(
0596             'tag_object_id' => $object_id,
0597             'tag_group_id'  => self::TAG_PRODUCT_ORIGINAL_GROUPID,
0598             'tag_type_id'   => self::TAG_TYPE_PROJECT
0599         ));
0600 
0601         if ($original == '1' || $original == '2') {
0602             if ($r) {
0603                 //delte all old tags
0604                 foreach ($r as $tag) {
0605                     $sql = "UPDATE tag_object set tag_changed = NOW() , is_deleted = 1  WHERE tag_item_id = :tagItemId";
0606                     $this->getAdapter()->query($sql, array('tagItemId' => $tag['tag_item_id']));
0607                 }
0608             }
0609             
0610             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
0611             if ($original == '1') {
0612                 $this->getAdapter()->query($sql, array(
0613                     'tag_id'        => $tag_original_id,
0614                     'tag_type_id'   => self::TAG_TYPE_PROJECT,
0615                     'tag_object_id' => $object_id,
0616                     'tag_group_id'  => self::TAG_PRODUCT_ORIGINAL_GROUPID
0617                 ));
0618             } else {
0619                 $this->getAdapter()->query($sql, array(
0620                     'tag_id'        => $tag_modification_id,
0621                     'tag_type_id'   => self::TAG_TYPE_PROJECT,
0622                     'tag_object_id' => $object_id,
0623                     'tag_group_id'  => self::TAG_PRODUCT_ORIGINAL_GROUPID
0624                 ));
0625             }
0626             
0627 
0628         } else {
0629 
0630             if ($r) {
0631                 foreach ($r as $tag) {
0632                     $sql = "UPDATE tag_object set tag_changed = NOW() , is_deleted = 1  WHERE tag_item_id = :tagItemId";
0633                     $this->getAdapter()->query($sql, array('tagItemId' => $tag['tag_item_id']));
0634                 }
0635             }
0636         }
0637 
0638     }
0639     
0640 
0641     /**
0642      * @param int $object_id
0643      */
0644     public function processTagCollection($object_id)
0645     {
0646         $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
0647         $this->getAdapter()->query($sql, array(
0648             'tag_id'        => self::TAG_COLLECTION_ID_ID,
0649             'tag_type_id'   => self::TAG_TYPE_PROJECT,
0650             'tag_object_id' => $object_id,
0651             'tag_group_id'  => self::TAG_COLLECTION_GROUPID
0652         ));
0653     }
0654 
0655     /**
0656      * @param int    $object_id
0657      * @param string $tag
0658      * @param int    $tag_type
0659      */
0660     public function addTagUser($object_id, $tag, $tag_type)
0661     {
0662         $tableTags = new Default_Model_DbTable_Tags();
0663         $listIds = $tableTags->storeTagsUser($tag);
0664         $tag_group_id = $this::TAG_USER_GROUPID;
0665         $prepared_insert =
0666             array_map(function ($id) use ($object_id, $tag_type, $tag_group_id) {
0667                 return "({$id}, {$tag_type}, {$object_id},{$tag_group_id})";
0668             },
0669                 $listIds);
0670         $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id,tag_group_id) VALUES " . implode(',',
0671                 $prepared_insert);
0672 
0673 
0674         $this->getAdapter()->query($sql);
0675     }
0676 
0677     public function deleteTagUser($object_id, $tag, $tag_type)
0678     {
0679         $removable_tag = $tag;
0680         // $sql = "DELETE tag_object FROM tag_object JOIN tag ON tag.tag_id = tag_object.tag_id WHERE tag_group_id = ".Default_Model_Tags::TAG_USER_GROUPID." and  tag.tag_name = :name and tag_object.tag_object_id=:object_id
0681         //             and tag_group_id =".Default_Model_Tags::TAG_USER_GROUPID;
0682 
0683         $sql = "UPDATE `tag_object` INNER JOIN `tag` ON `tag`.`tag_id` = `tag_object`.`tag_id` SET `tag_changed` = NOW() , `is_deleted` = 1 
0684                     WHERE `tag_group_id` = " . Default_Model_Tags::TAG_USER_GROUPID . " AND `tag`.`tag_name` = :name AND `tag_object`.`tag_object_id`=:object_id";
0685 
0686         $this->getAdapter()->query($sql, array('name' => $removable_tag, 'object_id' => $object_id));
0687         // if Tag is the only one in Tag_object table then delete this tag for user_groupid = 5
0688 
0689         $sql_object = "select count(1)  as cnt from tag_object JOIN tag ON tag.tag_id = tag_object.tag_id WHERE tag.tag_name = :name ";
0690         $r = $this->getAdapter()->fetchRow($sql_object, array('name' => $removable_tag));
0691         if ($r['cnt'] == 0) {
0692             // then remove tag if not existing in Tag_object
0693             $sql_delete_tag = "DELETE FROM `tag` WHERE `tag_name`=:name";
0694             $this->getAdapter()->query($sql_delete_tag, array('name' => $removable_tag));
0695         }
0696         $this->updateChanged($object_id, $tag_type);
0697     }
0698 
0699     public function getTagsPerCategory($cat_id)
0700     {
0701         $sql = "select t.*  from  category_tag as c ,tag as t where c.tag_id = t.tag_id and c.category_id = :cat_id";
0702         $r = $this->getAdapter()->fetchAll($sql, array('cat_id' => $cat_id));
0703 
0704         return $r;
0705     }
0706 
0707     public function validateCategoryTags($cat_id, $tags)
0708     {
0709         if ($tags == null) {
0710             return true;
0711         }
0712         //check if $cat_id children has tag already
0713         $sql = '
0714             select * from category_tag where tag_id in (' . $tags . ') and category_id in
0715             (
0716                 select c.project_category_id 
0717                 from project_category c 
0718                 join project_category d where d.project_category_id = ' . $cat_id . ' and c.lft> d.lft and c.rgt<d.rgt    
0719             )
0720         ';
0721 
0722 
0723         $r = $this->getAdapter()->fetchAll($sql);
0724         if (sizeof($r) > 0) {
0725             return false;
0726         }
0727 
0728         // check parent
0729         $sql = ' select ancestor_id_path from stat_cat_tree where project_category_id = ' . $cat_id;
0730         $r = $this->getAdapter()->fetchRow($sql);
0731         $sql = '
0732             select * from category_tag where category_id in (' . $r['ancestor_id_path'] . ') and category_id <> ' . $cat_id . ' and tag_id in (' . $tags . ')';
0733 
0734 
0735         $r = $this->getAdapter()->fetchAll($sql);
0736         if (sizeof($r) > 0) {
0737             return false;
0738         }
0739 
0740         return true;
0741     }
0742 
0743     public function updateTagsPerCategory($cat_id, $tags)
0744     {
0745         $sql = "delete from category_tag  where category_id=:cat_id";
0746         $this->getAdapter()->query($sql, array('cat_id' => $cat_id));
0747 
0748         if ($tags) {
0749             $tags_id = explode(',', $tags);
0750             $prepared_insert =
0751                 array_map(function ($id) use ($cat_id) {
0752                     return "({$cat_id},{$id})";
0753                 },
0754                     $tags_id);
0755             $sql = "INSERT IGNORE INTO category_tag (category_id, tag_id) VALUES " . implode(',',
0756                     $prepared_insert);
0757 
0758             $this->getAdapter()->query($sql);
0759         }
0760     }
0761 
0762     public function updateTagsPerStore($store_id, $tags)
0763     {
0764         $sql = "delete from config_store_tag  where store_id=:store_id";
0765         $this->getAdapter()->query($sql, array('store_id' => $store_id));
0766 
0767         if ($tags) {
0768             $tags_id = explode(',', $tags);
0769             $prepared_insert =
0770                 array_map(function ($id) use ($store_id) {
0771                     return "({$store_id},{$id})";
0772                 },
0773                     $tags_id);
0774             $sql = "INSERT IGNORE INTO config_store_tag (store_id, tag_id) VALUES " . implode(',',
0775                     $prepared_insert);
0776 
0777             $this->getAdapter()->query($sql);
0778         }
0779     }
0780 
0781     public function getTagsPerGroup($groupid)
0782     {
0783         $sql = "
0784                          select 
0785                          tag.tag_id 
0786                          ,tag.tag_name
0787                          ,tag_fullname
0788                          ,tag_description
0789                          from tag
0790                          join tag_group_item on tag.tag_id = tag_group_item.tag_id and tag_group_item.tag_group_id = :groupid                         
0791                          order by tag_name
0792                     ";
0793         $result = $this->getAdapter()->fetchAll($sql, array('groupid' => $groupid));
0794 
0795         return $result;
0796     }
0797 
0798     public function getAllTagsForStoreFilter()
0799     {
0800         $sql = "
0801                          select 
0802                          tag.tag_id, 
0803                          CASE WHEN tag.tag_fullname IS NULL THEN tag_name ELSE tag.tag_fullname END as tag_name
0804                          from tag
0805                          where tag.is_active = 1
0806                          order by tag_name
0807                     ";
0808         $result = $this->getAdapter()->fetchAll($sql);
0809 
0810         return $result;
0811     }
0812 
0813 
0814     public function getAllTagGroupsForStoreFilter()
0815     {
0816         $sql = "
0817              SELECT 
0818                 `tag_group`.`group_id`, 
0819                 `tag_group`.`group_name` AS `group_name`
0820                 FROM `tag_group`
0821                 ORDER BY `tag_group`.`group_name`
0822                ";
0823         $result = $this->getAdapter()->fetchAll($sql);
0824 
0825         return $result;
0826     }
0827 
0828 
0829     public function saveLicenseTagForProject($object_id, $tag_id)
0830     {
0831 
0832         $tableTags = new Default_Model_DbTable_Tags();
0833 
0834         $tags = $tableTags->fetchLicenseTagsForProject($object_id);
0835         if (count($tags) == 0) {
0836             //insert new tag
0837             if ($tag_id) {
0838                 $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
0839                 $this->getAdapter()->query($sql, array(
0840                     'tag_id'        => $tag_id,
0841                     'tag_type_id'   => $this::TAG_TYPE_PROJECT,
0842                     'tag_object_id' => $object_id,
0843                     'tag_group_id'  => $this::TAG_LICENSE_GROUPID
0844                 ));
0845             }
0846         } else {
0847             $tag = $tags[0];
0848 
0849             //remove tag license
0850             if (!$tag_id) {
0851                 //$sql = "DELETE FROM tag_object WHERE tag_item_id = :tagItemId";
0852                 $sql = "UPDATE `tag_object` SET `tag_changed` = NOW() , `is_deleted` = 1  WHERE `tag_item_id` = :tagItemId";
0853                 $this->getAdapter()->query($sql, array('tagItemId' => $tag['tag_item_id']));
0854             } else {
0855                 //Update old tag
0856                 if ($tag_id <> $tag['tag_id']) {
0857                     $sql = "UPDATE tag_object SET tag_changed = NOW(),tag_id = :tag_id WHERE tag_item_id = :tagItemId";
0858                     $this->getAdapter()->query($sql, array('tagItemId' => $tag['tag_item_id'], 'tag_id' => $tag_id));
0859                 }
0860             }
0861 
0862         }
0863     }
0864     
0865     
0866     public function saveGhnsExcludedTagForProject($object_id, $tag_value)
0867     {
0868 
0869         $tableTags = new Default_Model_DbTable_Tags();
0870         $ghnsExcludedTagId = $tableTags->fetchGhnsExcludedTagId();
0871 
0872         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id";
0873         $this->getAdapter()->query($sql, array(
0874             'tag_group_id'  => $this::TAG_GHNS_EXCLUDED_GROUPID,
0875             'tag_type_id'   => $this::TAG_TYPE_PROJECT,
0876             'tag_object_id' => $object_id
0877         ));
0878 
0879         if ($tag_value == 1) {
0880             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
0881             $this->getAdapter()->query($sql, array(
0882                 'tag_id'        => $ghnsExcludedTagId,
0883                 'tag_type_id'   => $this::TAG_TYPE_PROJECT,
0884                 'tag_object_id' => $object_id,
0885                 'tag_group_id'  => $this::TAG_GHNS_EXCLUDED_GROUPID
0886             ));
0887         }
0888 
0889 
0890     }
0891 
0892 
0893     public function saveDangerosuTagForProject($object_id, $tag_value)
0894     {
0895 
0896         $tableTags = new Default_Model_DbTable_Tags();
0897         $tag_dangerous_group_id = Zend_Registry::get('config')->settings->client->default->tag_group_dangerous_id;
0898         $tag_dangerous_id = Zend_Registry::get('config')->settings->client->default->tag_dangerous_id;
0899 
0900         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id";
0901         $this->getAdapter()->query($sql, array(
0902             'tag_group_id'  => $tag_dangerous_group_id,
0903             'tag_type_id'   => $this::TAG_TYPE_PROJECT,
0904             'tag_object_id' => $object_id
0905         ));
0906 
0907         if ($tag_value == 1) {
0908             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
0909             $this->getAdapter()->query($sql, array(
0910                 'tag_id'        => $tag_dangerous_id,
0911                 'tag_type_id'   => $this::TAG_TYPE_PROJECT,
0912                 'tag_object_id' => $object_id,
0913                 'tag_group_id'  => $tag_dangerous_group_id
0914             ));
0915         }
0916 
0917 
0918     }
0919 
0920     public function saveDeprecatedModeratorTagForProject($object_id, $tag_value)
0921     {        
0922         $tag_group_id = Zend_Registry::get('config')->settings->client->default->tag_group_deprecated_id;
0923         $tag_id = Zend_Registry::get('config')->settings->client->default->tag_deprecated_id;
0924         $this->saveOrUpdateTagObjectProject($object_id,$tag_value,$tag_group_id,$tag_id);        
0925     }
0926 
0927     protected function saveOrUpdateTagObjectProject($object_id,$tag_value,$tag_group_id,$tag_id)
0928     { 
0929         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id";
0930         $this->getAdapter()->query($sql, array(
0931             'tag_group_id'  => $tag_group_id,
0932             'tag_type_id'   => $this::TAG_TYPE_PROJECT,
0933             'tag_object_id' => $object_id
0934         ));
0935 
0936         if ($tag_value == 1) {
0937             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
0938             $this->getAdapter()->query($sql, array(
0939                 'tag_id'        => $tag_id,
0940                 'tag_type_id'   => $this::TAG_TYPE_PROJECT,
0941                 'tag_object_id' => $object_id,
0942                 'tag_group_id'  => $tag_group_id
0943             ));
0944         }
0945     }
0946 
0947 
0948     public function saveArchitectureTagForProject($project_id, $file_id, $tag_id)
0949     {
0950 
0951         //first delete old
0952         //$sql = "DELETE FROM tag_object WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id AND tag_parent_object_id = :tag_parent_object_id";
0953         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id AND tag_parent_object_id = :tag_parent_object_id";
0954         $this->getAdapter()->query($sql, array(
0955             'tag_group_id'         => $this::TAG_ARCHITECTURE_GROUPID,
0956             'tag_type_id'          => $this::TAG_TYPE_FILE,
0957             'tag_object_id'        => $file_id,
0958             'tag_parent_object_id' => $project_id
0959         ));
0960 
0961         if ($tag_id) {
0962             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_parent_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_parent_object_id, :tag_group_id)";
0963             $this->getAdapter()->query($sql, array(
0964                 'tag_id'               => $tag_id,
0965                 'tag_type_id'          => $this::TAG_TYPE_FILE,
0966                 'tag_object_id'        => $file_id,
0967                 'tag_parent_object_id' => $project_id,
0968                 'tag_group_id'         => $this::TAG_ARCHITECTURE_GROUPID
0969             ));
0970         }
0971 
0972     }
0973 
0974     public function saveFileTagForProjectAndTagGroup($project_id, $file_id, $tag_id, $tag_group_id)
0975     {
0976 
0977         //first delete old
0978         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id AND tag_parent_object_id = :tag_parent_object_id";
0979 
0980         $this->getAdapter()->query($sql, array(
0981             'tag_group_id'         => $tag_group_id,
0982             'tag_type_id'          => $this::TAG_TYPE_FILE,
0983             'tag_object_id'        => $file_id,
0984             'tag_parent_object_id' => $project_id
0985         ));
0986 
0987         if (!empty($tag_id)) {
0988             if (is_array($tag_id)) {
0989                 foreach ($tag_id as $tag) {
0990                     $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_parent_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_parent_object_id, :tag_group_id)";
0991                     $this->getAdapter()->query($sql, array(
0992                         'tag_id'               => $tag,
0993                         'tag_type_id'          => $this::TAG_TYPE_FILE,
0994                         'tag_object_id'        => $file_id,
0995                         'tag_parent_object_id' => $project_id,
0996                         'tag_group_id'         => $tag_group_id
0997                     ));
0998                 }
0999 
1000             } else {
1001                 $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_parent_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_parent_object_id, :tag_group_id)";
1002                 $this->getAdapter()->query($sql, array(
1003                     'tag_id'               => $tag_id,
1004                     'tag_type_id'          => $this::TAG_TYPE_FILE,
1005                     'tag_object_id'        => $file_id,
1006                     'tag_parent_object_id' => $project_id,
1007                     'tag_group_id'         => $tag_group_id
1008                 ));
1009             }
1010 
1011 
1012         }
1013     }
1014 
1015 
1016     public function deleteFileTagForProject($project_id, $file_id, $tag_id)
1017     {
1018 
1019         //first delete old
1020         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_id= :tag_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id AND tag_parent_object_id = :tag_parent_object_id";
1021 
1022         $this->getAdapter()->query($sql, array(
1023             'tag_id'               => $tag_id,
1024             'tag_type_id'          => $this::TAG_TYPE_FILE,
1025             'tag_object_id'        => $file_id,
1026             'tag_parent_object_id' => $project_id
1027         ));
1028 
1029     }
1030 
1031 
1032     public function savePackageTagForProject($project_id, $file_id, $tag_id)
1033     {
1034 
1035         //first delete old
1036         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id AND tag_parent_object_id = :tag_parent_object_id";
1037 
1038         $this->getAdapter()->query($sql, array(
1039             'tag_group_id'         => $this::TAG_PACKAGETYPE_GROUPID,
1040             'tag_type_id'          => $this::TAG_TYPE_FILE,
1041             'tag_object_id'        => $file_id,
1042             'tag_parent_object_id' => $project_id
1043         ));
1044 
1045         if ($tag_id) {
1046             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_parent_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_parent_object_id, :tag_group_id)";
1047             $this->getAdapter()->query($sql, array(
1048                 'tag_id'               => $tag_id,
1049                 'tag_type_id'          => $this::TAG_TYPE_FILE,
1050                 'tag_object_id'        => $file_id,
1051                 'tag_parent_object_id' => $project_id,
1052                 'tag_group_id'         => $this::TAG_PACKAGETYPE_GROUPID
1053             ));
1054         }
1055 
1056 
1057     }
1058 
1059 
1060     public function getProjectPackageTypesString($projectId)
1061     {
1062         $sql = 'SELECT DISTINCT ta.tag_fullname as name FROM tag_object t INNER JOIN tag ta on ta.tag_id = t.tag_id WHERE t.tag_group_id = :tag_group_id AND t.tag_parent_object_id = :project_id AND t.is_deleted = 0';
1063         $resultSet = $this->getAdapter()->fetchAll($sql,
1064             array('tag_group_id' => $this::TAG_PACKAGETYPE_GROUPID, 'project_id' => $projectId));
1065         $resultString = '';
1066         if (count($resultSet) > 0) {
1067             foreach ($resultSet as $item) {
1068                 $resultString = $resultString . ' <span class="packagetypeos" > ' . stripslashes($item['name']) . '</span>';
1069             }
1070 
1071             return $resultString;
1072         }
1073 
1074         return '';
1075     }
1076 
1077     public function getProjectPackageTypesPureStrings($projectId)
1078     {
1079         $sql = 'SELECT DISTINCT ta.tag_fullname as name FROM tag_object t INNER JOIN tag ta on ta.tag_id = t.tag_id WHERE t.tag_group_id = :tag_group_id AND t.tag_parent_object_id = :project_id AND t.is_deleted = 0';
1080         $resultSet = $this->getAdapter()->fetchAll($sql,
1081             array('tag_group_id' => $this::TAG_PACKAGETYPE_GROUPID, 'project_id' => $projectId));
1082         $resultString = '';
1083         if (count($resultSet) > 0) {
1084             foreach ($resultSet as $item) {
1085                 $resultString = $resultString . '  ' . stripslashes($item['name']);
1086             }
1087 
1088             return $resultString;
1089         }
1090 
1091         return '';
1092     }
1093 
1094     public function deleteFileTagsOnProject($projectId, $fileId)
1095     {
1096         $sql = "UPDATE tag_object inner join tag ON tag.tag_id = tag_object.tag_id set tag_changed = NOW() , is_deleted = 1 
1097                     WHERE tag_type_id = :tag_type_id and tag_object.tag_object_id=:object_id and tag_object.tag_parent_object_id=:parent_object_id";
1098 
1099         $this->getAdapter()->query($sql,
1100             array('tag_type_id' => $this::TAG_TYPE_FILE, 'object_id' => $fileId, 'parent_object_id' => $projectId));
1101     }
1102 
1103 
1104     public function deletePackageTypeOnProject($projectId, $fileId)
1105     {
1106         $sql = "UPDATE tag_object inner join tag ON tag.tag_id = tag_object.tag_id set tag_changed = NOW() , is_deleted = 1 
1107                     WHERE tag_group_id = :tag_group_id and tag_object.tag_object_id=:object_id and tag_object.tag_parent_object_id=:parent_object_id";
1108 
1109         $this->getAdapter()->query($sql, array(
1110             'tag_group_id'     => $this::TAG_PACKAGETYPE_GROUPID,
1111             'object_id'        => $fileId,
1112             'parent_object_id' => $projectId
1113         ));
1114     }
1115 
1116 
1117     public function deleteArchitectureOnProject($projectId, $fileId)
1118     {
1119         $sql = "UPDATE tag_object inner join tag ON tag.tag_id = tag_object.tag_id set tag_changed = NOW() , is_deleted = 1 
1120                     WHERE tag_group_id = :tag_group_id and tag_object.tag_object_id=:object_id and tag_object.tag_parent_object_id=:parent_object_id";
1121 
1122         $this->getAdapter()->query($sql, array(
1123             'tag_group_id'     => $this::TAG_ARCHITECTURE_GROUPID,
1124             'object_id'        => $fileId,
1125             'parent_object_id' => $projectId
1126         ));
1127     }
1128 
1129 
1130     /**
1131      * @param int $projectId
1132      * @param int $fileId
1133      * @return string
1134      */
1135     public function getPackageType($projectId, $fileId)
1136     {
1137         $sql = 'SELECT ta.tag_fullname as name FROM tag_object t INNER JOIN tag ta on ta.tag_id = t.tag_id WHERE t.tag_group_id = :tag_group_id AND t.tag_parent_object_id = :project_id AND t.tag_object_id = :file_id AND t.is_deleted = 0';
1138         $resultSet = $this->getAdapter()->fetchAll($sql,
1139             array('tag_group_id' => $this::TAG_PACKAGETYPE_GROUPID, 'project_id' => $projectId, 'file_id' => $fileId));
1140 
1141         if (count($resultSet) > 0) {
1142             return $resultSet[0]['name'];
1143         } else {
1144             return '';
1145         }
1146     }
1147 
1148 
1149     /**
1150      * @param int $fileId
1151      * @return array
1152      */
1153     public function getFileTags($fileId)
1154     {
1155         $sql = 'SELECT ta.tag_id, ta.tag_fullname as name FROM tag_object t INNER JOIN tag ta on ta.tag_id = t.tag_id WHERE t.tag_type_id = :tag_type_id AND t.tag_object_id = :file_id AND t.is_deleted = 0';
1156         $resultSet = $this->getAdapter()->fetchAll($sql,
1157             array('tag_type_id' => $this::TAG_TYPE_FILE, 'file_id' => $fileId));
1158 
1159         return $resultSet;
1160     }
1161 
1162 
1163     /**
1164      * @param int $projectId
1165      * @param int $fileId
1166      * @param int $tagGroup
1167      * @return array
1168      */
1169     public function getTagsForFileAndTagGroup($projectId, $fileId, $tagGroup)
1170     {
1171         $sql = 'SELECT ta.tag_fullname as name FROM tag_object t INNER JOIN tag ta on ta.tag_id = t.tag_id WHERE t.tag_group_id = :tag_group_id AND t.tag_parent_object_id = :project_id AND t.tag_object_id = :file_id AND t.is_deleted = 0';
1172         $resultSet = $this->getAdapter()->fetchAll($sql,
1173             array('tag_group_id' => $tagGroup, 'project_id' => $projectId, 'file_id' => $fileId));
1174 
1175         return $resultSet;
1176     }
1177 
1178 
1179     /**
1180      * @param int $object_id
1181      *
1182      * @return array
1183      * @throws Zend_Exception
1184      */
1185     public function getTagsEbookSubject($object_id)
1186     {
1187         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_subject;
1188         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1189 
1190         return $tags;
1191     }
1192 
1193     /**
1194      * @param int $object_id
1195      *
1196      * @return array
1197      * @throws Zend_Exception
1198      */
1199     public function getTagsEbookAuthor($object_id)
1200     {
1201         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_author;
1202         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1203 
1204         return $tags;
1205     }
1206 
1207 
1208     /**
1209      * @param int $object_id
1210      *
1211      * @return array
1212      * @throws Zend_Exception
1213      */
1214     public function getTagsEbookEditor($object_id)
1215     {
1216         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_editor;
1217         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1218 
1219         return $tags;
1220     }
1221 
1222 
1223     /**
1224      * @param int $object_id
1225      *
1226      * @return array
1227      * @throws Zend_Exception
1228      */
1229     public function getTagsEbookIllustrator($object_id)
1230     {
1231         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_illustrator;
1232         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1233 
1234         return $tags;
1235     }
1236 
1237 
1238     /**
1239      * @param int $object_id
1240      *
1241      * @return array
1242      * @throws Zend_Exception
1243      */
1244     public function getTagsEbookTranslator($object_id)
1245     {
1246         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_translator;
1247         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1248 
1249         return $tags;
1250     }
1251 
1252 
1253     /**
1254      * @param int $object_id
1255      *
1256      * @return array
1257      * @throws Zend_Exception
1258      */
1259     public function getTagsEbookShelf($object_id)
1260     {
1261         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_shelf;
1262         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1263 
1264         return $tags;
1265     }
1266 
1267 
1268     /**
1269      * @param int $object_id
1270      *
1271      * @return array
1272      * @throws Zend_Exception
1273      */
1274     public function getTagsEbookLanguage($object_id)
1275     {
1276         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_language;
1277         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1278 
1279         return $tags;
1280     }
1281 
1282 
1283     /**
1284      * @param int $object_id
1285      *
1286      * @return array
1287      * @throws Zend_Exception
1288      */
1289     public function getTagsEbookType($object_id)
1290     {
1291         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_ebook_type;
1292         $tags = $this->getTagsArray($object_id, $this::TAG_TYPE_PROJECT, $tag_group_ids);
1293 
1294         return $tags;
1295     }
1296 
1297 
1298     public function saveCollectionTypeTagForProject($object_id, $tag_id)
1299     {
1300 
1301         $tableTags = new Default_Model_DbTable_Tags();
1302 
1303         $collectionTagGroup = Zend_Registry::get('config')->settings->client->default->tag_group_collection_type_id;
1304 
1305 
1306         $tags = $tableTags->fetchTagsForProject($object_id, $collectionTagGroup);
1307         if (count($tags) == 0) {
1308             //insert new tag
1309             if ($tag_id) {
1310                 $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_object_id, tag_group_id) VALUES (:tag_id, :tag_type_id, :tag_object_id, :tag_group_id)";
1311                 $this->getAdapter()->query($sql, array(
1312                     'tag_id'        => $tag_id,
1313                     'tag_type_id'   => $this::TAG_TYPE_PROJECT,
1314                     'tag_object_id' => $object_id,
1315                     'tag_group_id'  => $collectionTagGroup
1316                 ));
1317             }
1318         } else {
1319             $tag = $tags[0];
1320 
1321             //remove tag license
1322             if (!$tag_id) {
1323                 //$sql = "DELETE FROM tag_object WHERE tag_item_id = :tagItemId";
1324                 $sql = "UPDATE tag_object set tag_changed = NOW() , is_deleted = 1  WHERE tag_item_id = :tagItemId";
1325                 $this->getAdapter()->query($sql, array('tagItemId' => $tag['tag_item_id']));
1326             } else {
1327                 //Update old tag
1328                 if ($tag_id <> $tag['tag_id']) {
1329                     $sql = "UPDATE tag_object SET tag_changed = NOW(),tag_id = :tag_id WHERE tag_item_id = :tagItemId";
1330                     $this->getAdapter()->query($sql, array('tagItemId' => $tag['tag_item_id'], 'tag_id' => $tag_id));
1331                 }
1332             }
1333 
1334         }
1335 
1336 
1337     }
1338 
1339     //========================== generic methods =============================
1340 
1341     public function fetchTagObject($tag_id, $tag_object_id, $tag_group_id, $tag_type_id)
1342     {
1343         $sql = $sql_object = "select tag_item_id  from tag_object WHERE tag_id = :tag_id and tag_object_id=:tag_object_id and tag_group_id=:tag_group_id  
1344                                 and tag_type_id = :tag_type_id and is_deleted = 0";
1345         $r = $this->getAdapter()->fetchRow($sql_object, array(
1346             'tag_id'        => $tag_id,
1347             'tag_object_id' => $tag_object_id,
1348             'tag_group_id'  => $tag_group_id,
1349             'tag_type_id'   => $tag_type_id
1350         ));
1351 
1352         return $r;
1353     }
1354 
1355     /*
1356     * $tag_ids array tag ids 
1357     */
1358 
1359     /**
1360      * @return array
1361      * @throws Zend_Exception
1362      */
1363     public function getTagGroupsOSUser()
1364     {
1365         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_osuser;
1366 
1367         return $this->getTagGroups($tag_group_ids);
1368     }
1369 
1370     public function getTagGroups($tag_group_ids)
1371     {
1372         $sql = 'select g.group_id
1373                 ,g.group_name
1374                 ,g.group_display_name
1375                 ,g.is_multi_select
1376                 ,i.tag_group_item_id
1377                 ,i.tag_id
1378                 ,t.tag_name
1379                 ,t.tag_fullname
1380                 ,t.tag_description    
1381              from 
1382              tag_group g,
1383              tag_group_item i,
1384              tag t
1385              where g.group_id=i.tag_group_id
1386              and i.tag_id = t.tag_id 
1387              and g.group_id in (' . $tag_group_ids . ')';
1388         $resultSet = $this->getAdapter()->fetchAll($sql);
1389 
1390         return $resultSet;
1391     }
1392 
1393     public function saveOSTagForUser($tag_id, $tag_group_id, $member_id)
1394     {
1395         $tag_type_id = Zend_Registry::get('config')->settings->client->default->tag_type_osuser;
1396         $this->deleteTagForTabObject($member_id, $tag_group_id, $tag_type_id);
1397         $this->insertTagObject($tag_id, $tag_type_id, $tag_group_id, $member_id, null);
1398     }
1399 
1400     //========================== generic methods end =============================
1401 
1402 
1403     // ======================== settings profile user os ==========================================
1404 
1405     public function deleteTagForTabObject($tag_object_id, $tag_group_id, $tag_type_id)
1406     {
1407 
1408         $sql = "UPDATE tag_object SET tag_changed = NOW() , is_deleted = 1  WHERE tag_group_id = :tag_group_id AND tag_type_id = :tag_type_id AND tag_object_id = :tag_object_id";
1409         $this->getAdapter()->query($sql,
1410             array('tag_group_id' => $tag_group_id, 'tag_type_id' => $tag_type_id, 'tag_object_id' => $tag_object_id));
1411 
1412     }
1413 
1414     public function insertTagObject($tag_ids, $tag_type_id, $tag_group_id, $tag_object_id, $tag_parent_object_id)
1415     {
1416         if ($tag_ids == null || sizeof($tag_ids) == 0) {
1417             return;
1418         }
1419         if (!is_array($tag_ids)) {
1420             $tag_ids = array($tag_ids);
1421         }
1422 
1423         if ($tag_parent_object_id) {
1424             $prepared_insert =
1425                 array_map(function ($id) use ($tag_type_id, $tag_group_id, $tag_object_id, $tag_parent_object_id) {
1426                     return "({$id}, {$tag_type_id},{$tag_group_id},{$tag_object_id},{$tag_parent_object_id})";
1427                 },
1428                     $tag_ids);
1429             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_group_id,tag_object_id,tag_parent_object_id) VALUES " . implode(',',
1430                     $prepared_insert);
1431 
1432             $this->getAdapter()->query($sql);
1433         } else {
1434             $prepared_insert =
1435                 array_map(function ($id) use ($tag_type_id, $tag_group_id, $tag_object_id) {
1436                     return "({$id}, {$tag_type_id},{$tag_group_id},{$tag_object_id})";
1437                 },
1438                     $tag_ids);
1439             $sql = "INSERT IGNORE INTO tag_object (tag_id, tag_type_id, tag_group_id,tag_object_id) VALUES " . implode(',',
1440                     $prepared_insert);
1441             $this->getAdapter()->query($sql);
1442         }
1443     }
1444 
1445     public function getTagsOSUser($member_id)
1446     {
1447         $tag_group_ids = Zend_Registry::get('config')->settings->client->default->tag_group_osuser;
1448         $tag_type_id = Zend_Registry::get('config')->settings->client->default->tag_type_osuser;
1449 
1450         return $this->getTagsArray($member_id, $tag_type_id,$tag_group_ids);
1451     }
1452 
1453 }