/**
  * Function which checks the object tags agains DB
  * and returns an array of tags ids which need to be deleted.
  * @param BaseObject $object
  * @return array
  */
 protected function checkExistForDelete(BaseObject $object, $tagsToCheck = null)
 {
     $objectTags = $tagsToCheck ? $this->trimObjectTags($tagsToCheck) : $this->trimObjectTags($object->getTags());
     $objectTags = str_replace(self::$specialCharacters, self::$specialCharactersReplacement, $objectTags);
     $tagsToKeep = array();
     foreach ($objectTags as $objectTag) {
         $peer = $object->getPeer();
         $c = KalturaCriteria::create(get_class($object));
         $c->addAnd(self::PARTNER_ID_FIELD, $object->getPartnerId(), KalturaCriteria::EQUAL);
         $c->addAnd($peer::TAGS, $objectTag, KalturaCriteria::LIKE);
         $c->addAnd($peer::ID, array($object->getId()), KalturaCriteria::NOT_IN);
         $selectResults = $peer->doSelect($c);
         foreach ($selectResults as $selectResult) {
             $resultTags = $this->trimObjectTags($selectResult->getTags());
             if (in_array($objectTag, $resultTags)) {
                 //    	            if(isset($tagsToKeep[$objectTag]))
                 //    	                $tagsToKeep[$objectTag]++;
                 //    	            else
                 //    	                $tagsToKeep[$objectTag] = 1;
                 if (!in_array($objectTag, $tagsToKeep)) {
                     $tagsToKeep[] = $objectTag;
                 }
             }
         }
     }
     KalturaLog::debug("tags to keep: " . print_r($tagsToKeep, true));
     if (count($tagsToKeep)) {
         //Decrement instance count for the tags that we keep
         $c = self::getTagObjectsByTagStringsCriteria($tagsToKeep, $this->getObjectIdByClassName(get_class($object)), $object->getPartnerId());
         $tagsToKeepObjects = TagPeer::doSelect($c);
         foreach ($tagsToKeepObjects as $tagToKeepObject) {
             /* @var $tagToKeepObject Tag */
             $tagToKeepObject->decrementInstanceCount();
         }
     }
     //Return the IDs of the rest of the tags for removal.
     $tagsToRemove = array_diff($objectTags, $tagsToKeep);
     KalturaLog::debug("tags to delete: " . print_r($tagsToRemove, true));
     if ($tagsToRemove) {
         $c = self::getTagObjectsByTagStringsCriteria($tagsToRemove, $this->getObjectIdByClassName(get_class($object)), $object->getPartnerId());
         $c->applyFilters();
         $recordsToRemove = $c->getRecordsCount();
         return $c->getFetchedIds();
     }
     return array();
 }
Exemplo n.º 2
0
 public function objectChanged(BaseObject $object, array $modifiedColumns)
 {
     $privacyContexts = null;
     if ($object instanceof entry) {
         $criteria = new Criteria();
         $criteria->add(categoryEntryPeer::ENTRY_ID, $object->getId());
         $categoryEntries = categoryEntryPeer::doSelect($criteria);
         $privacyContexts = array(self::NULL_PC);
         if (count($categoryEntries)) {
             foreach ($categoryEntries as $categoryEntry) {
                 /* @var $categoryEntry categoryEntry */
                 if ($categoryEntry->getPrivacyContext() != "") {
                     $privacyContexts = array_merge($privacyContexts, self::trimObjectTags($categoryEntry->getPrivacyContext()));
                 } else {
                     $privacyContexts[] = kEntitlementUtils::DEFAULT_CONTEXT;
                 }
             }
             $privacyContexts = array_unique($privacyContexts);
         }
     }
     $oldTags = $object->getColumnsOldValue(self::getClassConstValue(get_class($object->getPeer()), self::TAGS_FIELD_NAME));
     $newTags = $object->getTags();
     $tagsForDelete = implode(',', array_diff(explode(',', $oldTags), explode(',', $newTags)));
     $tagsForUpdate = implode(',', array_diff(explode(',', $newTags), explode(',', $oldTags)));
     if ($oldTags && $oldTags != "") {
         self::decrementExistingTagsInstanceCount($tagsForDelete, $object->getPartnerId(), get_class($object), $privacyContexts);
     }
     self::addOrIncrementTags($tagsForUpdate, $object->getPartnerId(), get_class($object), $privacyContexts);
 }