/**
  * merge duplicate shared tags
  * 
  * @param string $model record model for which tags should be merged
  * @param boolean $deleteObsoleteTags
  * @param boolean $ignoreAcl
  * 
  * @see 0007354: function for merging duplicate tags
  */
 public function mergeDuplicateSharedTags($model, $deleteObsoleteTags = TRUE, $ignoreAcl = FALSE)
 {
     $select = $this->_db->select()->from(array('tags' => SQL_TABLE_PREFIX . 'tags'), 'name')->where($this->_db->quoteIdentifier('type') . ' = ?', Tinebase_Model_Tag::TYPE_SHARED)->where($this->_db->quoteIdentifier('is_deleted') . ' = 0')->group('name')->having('COUNT(' . $this->_db->quoteIdentifier('name') . ') > 1');
     $queryResult = $this->_db->fetchAll($select);
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Found ' . count($queryResult) . ' duplicate tag names.');
     }
     $controller = Tinebase_Core::getApplicationInstance($model);
     if ($ignoreAcl) {
         $containerChecks = $controller->doContainerACLChecks(FALSE);
     }
     $recordFilterModel = $model . 'Filter';
     foreach ($queryResult as $duplicateTag) {
         $filter = new Tinebase_Model_TagFilter(array('name' => $duplicateTag['name'], 'type' => Tinebase_Model_Tag::TYPE_SHARED));
         $paging = new Tinebase_Model_Pagination(array('sort' => 'creation_time'));
         $tagsWithSameName = $this->searchTags($filter, $paging);
         $targetTag = $tagsWithSameName->getFirstRecord();
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Merging tag ' . $duplicateTag['name'] . '. Found ' . count($tagsWithSameName) . ' tags with this name.');
         }
         foreach ($tagsWithSameName as $tag) {
             if ($tag->getId() === $targetTag->getId()) {
                 // skip target (oldest) tag
                 continue;
             }
             $recordFilter = new $recordFilterModel(array(array('field' => 'tag', 'operator' => 'in', 'value' => array($tag->getId()))));
             $recordIdsWithTagToMerge = $controller->search($recordFilter, NULL, FALSE, TRUE);
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Found ' . count($recordIdsWithTagToMerge) . ' ' . $model . '(s) with tags to be merged.');
             }
             if (!empty($recordIdsWithTagToMerge)) {
                 $recordFilter = new $recordFilterModel(array(array('field' => 'id', 'operator' => 'in', 'value' => $recordIdsWithTagToMerge)));
                 $this->attachTagToMultipleRecords($recordFilter, $targetTag);
                 $this->detachTagsFromMultipleRecords($recordFilter, $tag->getId());
             }
             // check occurrence of the merged tag and remove it if obsolete
             $tag = $this->get($tag);
             if ($deleteObsoleteTags && $tag->occurrence == 0) {
                 $this->deleteTags($tag->getId(), $ignoreAcl);
             }
         }
     }
     if ($ignoreAcl) {
         /** @noinspection PhpUndefinedVariableInspection */
         $controller->doContainerACLChecks($containerChecks);
     }
 }
示例#2
0
 /**
  * Gets tags of a given records where user has the required right to
  * The tags are stored in the records $_tagsProperty.
  *
  * @param Tinebase_Record_RecordSet  $_records       the recordSet
  * @param string                     $_tagsProperty  the property in the record where the tags are in (defaults: 'tags')
  * @param string                     $_right         the required right current user must have on the tags
  * @return Tinebase_Record_RecordSet tags of record
  */
 public function getMultipleTagsOfRecords($_records, $_tagsProperty = 'tags', $_right = Tinebase_Model_TagRight::VIEW_RIGHT)
 {
     if (count($_records) == 0) {
         // do nothing
         return;
     }
     $recordIds = $_records->getArrayOfIds();
     if (count($recordIds) == 0) {
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Can\'t get tags for records without ids');
         }
         // do nothing
         return;
     }
     // get first record to determine application
     $first = $_records->getFirstRecord();
     $appId = Tinebase_Application::getInstance()->getApplicationByName($first->getApplication())->getId();
     $select = $this->_getSelect($recordIds, $appId);
     $select->group(array('tagging.tag_id', 'tagging.record_id'));
     Tinebase_Model_TagRight::applyAclSql($select, $_right, $this->_db->quoteIdentifier('tagging.tag_id'));
     Tinebase_Backend_Sql_Abstract::traitGroup($this->_db, SQL_TABLE_PREFIX, $select);
     $queryResult = $this->_db->fetchAll($select);
     //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($queryResult, TRUE));
     // argh: Tinebase_Model_Tag has no record_id
     /*
     $tagsOfRecords = new Tinebase_Record_RecordSet('Tinebase_Model_Tag', $queryResult);
     $tagsOfRecords->addIndices(array('record_id'));
     */
     // build array with tags (record_id => array of Tinebase_Model_Tag)
     $tagsOfRecords = array();
     foreach ($queryResult as $result) {
         $tagsOfRecords[$result['record_id']][] = new Tinebase_Model_Tag($result, true);
     }
     Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Getting ' . count($tagsOfRecords) . ' tags for ' . count($_records) . ' records.');
     foreach ($_records as $record) {
         //$record->{$_tagsProperty} = $tagsOfRecords->filter('record_id', $record->getId());
         $record->{$_tagsProperty} = new Tinebase_Record_RecordSet('Tinebase_Model_Tag', isset($tagsOfRecords[$record->getId()]) ? $tagsOfRecords[$record->getId()] : array());
     }
 }
 /**
  * Fetches elements according to limit
  *
  * @param int $offset
  * @param int $itemCountPerPage
  * @return array 
  */
 public function fetchAll($offset, $itemCountPerPage)
 {
     return $this->adapter->fetchAll($this->query . ' LIMIT ' . $itemCountPerPage . ' OFFSET ' . $offset);
 }