public static function syncEntriesCategories(entry $entry, $isCategoriesModified)
 {
     self::$skipEntrySave = true;
     if ($entry->getNewCategories() != null && $entry->getNewCategories() !== "") {
         $newCats = explode(entry::ENTRY_CATEGORY_SEPARATOR, $entry->getNewCategories());
     } else {
         $newCats = array();
     }
     if (!$isCategoriesModified) {
         if ($entry->getNewCategoriesIds() != null && $entry->getNewCategoriesIds() !== "") {
             $newCatsIds = explode(entry::ENTRY_CATEGORY_SEPARATOR, $entry->getNewCategoriesIds());
         } else {
             $newCatsIds = array();
         }
         KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
         $dbCategories = categoryPeer::retrieveByPKs($newCatsIds);
         KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
         foreach ($dbCategories as $dbCategory) {
             //skip categoy with privacy contexts.
             if ($dbCategory->getPrivacyContexts() != null && $dbCategory->getPrivacyContexts() != '') {
                 continue;
             }
             $newCats[] = $dbCategory->getFullName();
         }
     }
     $newCats = array_unique($newCats);
     $allIds = array();
     $allCats = array();
     $allIdsWithParents = array();
     $addedCats = array();
     $removedCats = array();
     $remainingCats = array();
     $oldCats = array();
     $oldCatsIds = array();
     $dbOldCategoriesEntry = categoryEntryPeer::selectByEntryId($entry->getId());
     foreach ($dbOldCategoriesEntry as $dbOldCategoryEntry) {
         $oldCatsIds[] = $dbOldCategoryEntry->getCategoryId();
     }
     $oldCategoris = categoryPeer::retrieveByPKsNoFilter($oldCatsIds);
     foreach ($oldCategoris as $category) {
         if ($category->getPrivacyContexts() != '' && $category->getPrivacyContexts() != null) {
             continue;
         }
         $oldCats[] = $category->getFullName();
     }
     foreach ($oldCats as $cat) {
         if (array_search($cat, $newCats) === false) {
             $removedCats[] = $cat;
         }
     }
     foreach ($newCats as $cat) {
         if (array_search($cat, $oldCats) === false) {
             $addedCats[] = $cat;
         } else {
             $remainingCats[] = $cat;
         }
     }
     foreach ($remainingCats as $cat) {
         KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
         $category = categoryPeer::getByFullNameExactMatch($cat);
         KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
         if ($category) {
             if ($category->getPrivacyContext() == '' || $category->getPrivacyContext() == null) {
                 $allCats[] = $category->getFullName();
                 $allIds[] = $category->getId();
             }
             $allIdsWithParents[] = $category->getId();
             $allIdsWithParents = array_merge($allIdsWithParents, $category->getAllParentsIds());
         }
     }
     $alreadyAddedCatIds = $allIdsWithParents;
     foreach ($addedCats as $cat) {
         $category = categoryPeer::getByFullNameExactMatch($cat);
         if (!$category) {
             KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
             $unentitedCategory = categoryPeer::getByFullNameExactMatch($cat);
             KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
             if (!$unentitedCategory) {
                 $category = category::createByPartnerAndFullName($entry->getPartnerId(), $cat);
                 //it is possible to add on an entry a few new categories on the same new parent -
                 //and we need to sync sphinx once we add so the category will not be duplicated
                 kEventsManager::flushEvents();
             }
         } else {
             $categoryKuser = categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($category->getId(), kCurrentContext::$ks_kuser_id);
             if (kEntitlementUtils::getEntitlementEnforcement() && $category->getContributionPolicy() != ContributionPolicyType::ALL && (!$categoryKuser || $categoryKuser->getPermissionLevel() == CategoryKuserPermissionLevel::MEMBER)) {
                 //user is not entitled to add entry to this category
                 $category = null;
             }
         }
         if (!$category) {
             continue;
         }
         //when use caetgoryEntry->add categoryEntry object was alreay created - and no need to create it.
         //when using baseEntry->categories = 'my category' will need to add the new category.
         $categoryEntry = categoryEntryPeer::retrieveByCategoryIdAndEntryId($category->getId(), $entry->getId());
         if (!$categoryEntry) {
             $categoryEntry = new categoryEntry();
             $categoryEntry->setEntryId($entry->getId());
             $categoryEntry->setCategoryId($category->getId());
             $categoryEntry->setEntryCategoriesAddedIds($alreadyAddedCatIds);
             $categoryEntry->setPartnerId($entry->getPartnerId());
             $categoryEntry->setStatus(CategoryEntryStatus::ACTIVE);
             $categoryEntry->save();
         }
         if ($category->getPrivacyContext() == '' || $category->getPrivacyContext() == null) {
             // only categories with no context should be set on entry->categories and entry->categoriesIds
             $allCats[] = $category->getFullName();
             $allIds[] = $category->getId();
         }
         $alreadyAddedCatIds[] = $category->getId();
         $alreadyAddedCatIds = array_merge($alreadyAddedCatIds, $category->getAllParentsIds());
     }
     $alreadyRemovedCatIds = $allIdsWithParents;
     foreach ($removedCats as $cat) {
         $category = categoryPeer::getByFullNameExactMatch($cat);
         if ($category) {
             $categoryEntryToDelete = categoryEntryPeer::retrieveByCategoryIdAndEntryId($category->getId(), $entry->getId());
             if ($categoryEntryToDelete) {
                 $categoryKuser = categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($categoryEntryToDelete->getCategoryId(), kCurrentContext::$ks_kuser_id);
                 if ($category->getPrivacyContexts() && (!$categoryKuser || $categoryKuser->getPermissionLevel() == CategoryKuserPermissionLevel::MEMBER)) {
                     //not entiteld to delete - should be set back on the entry.
                     $allCats[] = $category->getFullName();
                     $allIds[] = $category->getId();
                 } else {
                     $categoryEntryToDelete->setEntryCategoriesRemovedIds($alreadyRemovedCatIds);
                     $categoryEntryToDelete->setStatus(CategoryEntryStatus::DELETED);
                     $categoryEntryToDelete->save();
                 }
             }
             $alreadyRemovedCatIds[] = $category->getId();
             $alreadyRemovedCatIds = array_merge($alreadyRemovedCatIds, $category->getAllParentsIds());
         } else {
             //category was not found - it could be that user is not entitled to remove it
             KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
             $category = categoryPeer::getByFullNameExactMatch($cat);
             KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
             if ($category) {
                 $allCats[] = $category->getFullName();
                 $allIds[] = $category->getId();
             }
         }
     }
     self::$skipEntrySave = false;
     $entry->parentSetCategories(implode(",", $allCats));
     $entry->parentSetCategoriesIds(implode(',', $allIds));
 }
 /**
  * Returns true if kuser or current kuser is entitled to assign entry to categoryId
  * @param int $categoryId
  * @param int $kuser
  * @return bool
  */
 public static function validateEntryAssignToCategory($categoryId, $kuserId = null)
 {
     if (!self::getEntitlementEnforcement()) {
         return true;
     }
     $category = categoryPeer::retrieveByPK($categoryUser->categoryId);
     if (!$category) {
         throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $categoryUser->categoryId);
     }
     if ($category->getContributionPolicy() == ContributionPolicyType::ALL) {
         return true;
     }
     if ($kuserId) {
         $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
         $kuser = kuserPeer::getKuserByPartnerAndUid($partnerId, kCurrentContext::$ks_uid);
         $kuserId = $kuser->getId();
     }
     $currentKuserCategoryKuser = categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($categoryUser->categoryId, $kuserId);
     if ($currentKuserCategoryKuser && ($currentKuserCategoryKuser->getPermissionLevel() == CategoryKuserPermissionLevel::MANAGER || $currentKuserCategoryKuser->getPermissionLevel() == CategoryKuserPermissionLevel::MODERATOR || $currentKuserCategoryKuser->getPermissionLevel() == CategoryKuserPermissionLevel::CONTRIBUTOR)) {
         return true;
     }
     return false;
 }
 public function validateForInsert($propertiesToSkip = array())
 {
     $category = categoryPeer::retrieveByPK($this->categoryId);
     if (!$category) {
         throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $this->categoryId);
     }
     if ($category->getInheritanceType() == InheritanceType::INHERIT) {
         throw new KalturaAPIException(KalturaErrors::CATEGORY_INHERIT_MEMBERS, $this->categoryId);
     }
     $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
     $kuser = kuserPeer::getKuserByPartnerAndUid($partnerId, $this->userId);
     if ($kuser) {
         $categoryKuser = categoryKuserPeer::retrieveByCategoryIdAndKuserId($this->categoryId, $kuser->getId());
         if ($categoryKuser) {
             throw new KalturaAPIException(KalturaErrors::CATEGORY_USER_ALREADY_EXISTS);
         }
     }
     $currentKuserCategoryKuser = categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($this->categoryId, kCurrentContext::$ks_kuser_id);
     if ((!$currentKuserCategoryKuser || $currentKuserCategoryKuser->getPermissionLevel() != CategoryKuserPermissionLevel::MANAGER) && $category->getUserJoinPolicy() == UserJoinPolicyType::NOT_ALLOWED && kEntitlementUtils::getEntitlementEnforcement()) {
         throw new KalturaAPIException(KalturaErrors::CATEGORY_USER_JOIN_NOT_ALLOWED, $this->categoryId);
     }
     //if user doesn't exists - create it
     $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
     $kuser = kuserPeer::getKuserByPartnerAndUid($partnerId, $this->userId);
     if (!$kuser) {
         if (!preg_match(kuser::PUSER_ID_REGEXP, $this->userId)) {
             throw new KalturaAPIException(KalturaErrors::INVALID_FIELD_VALUE, 'userId');
         }
         kuserPeer::createKuserForPartner($partnerId, $this->userId);
     }
     return parent::validateForInsert($propertiesToSkip);
 }
 /**
  * Index CategoryUser by userid and category id
  * 
  * @action index
  * @param string $userId
  * @param int $categoryId
  * @param bool $shouldUpdate
  * @throws KalturaErrors::INVALID_CATEGORY_USER_ID
  * @return int
  */
 public function indexAction($userId, $categoryId, $shouldUpdate = true)
 {
     if (kEntitlementUtils::getEntitlementEnforcement()) {
         throw new KalturaAPIException(KalturaErrors::CANNOT_INDEX_OBJECT_WHEN_ENTITLEMENT_IS_ENABLE);
     }
     $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
     $kuser = kuserPeer::getActiveKuserByPartnerAndUid($partnerId, $userId);
     if (!$kuser) {
         throw new KalturaAPIException(KalturaErrors::INVALID_USER_ID);
     }
     $dbCategoryKuser = categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($categoryId, $kuser->getId());
     if (!$dbCategoryKuser) {
         throw new KalturaAPIException(KalturaErrors::INVALID_CATEGORY_USER_ID);
     }
     if (!$shouldUpdate) {
         $dbCategoryKuser->setUpdatedAt(time());
         $dbCategoryKuser->save();
         return $dbCategoryKuser->getId();
     }
     $dbCategoryKuser->reSetCategoryFullIds();
     $dbCategoryKuser->reSetScreenName();
     $dbCategoryKuser->save();
     return $dbCategoryKuser->getId();
 }
 /**
  * Delete a Category
  * 
  * @action delete
  * @param int $id
  * @param KalturaNullableBoolean $moveEntriesToParentCategory
  */
 function deleteAction($id, $moveEntriesToParentCategory = KalturaNullableBoolean::TRUE_VALUE)
 {
     if ($this->getPartner()->getFeaturesStatusByType(IndexObjectType::LOCK_CATEGORY)) {
         throw new KalturaAPIException(KalturaErrors::CATEGORIES_LOCKED);
     }
     $categoryDb = categoryPeer::retrieveByPK($id);
     if (!$categoryDb) {
         throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $id);
     }
     if (kEntitlementUtils::getEntitlementEnforcement()) {
         $currentKuserCategoryKuser = categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($categoryDb->getId(), kCurrentContext::$ks_kuser_id);
         if (!$currentKuserCategoryKuser || $currentKuserCategoryKuser->getPermissionLevel() != CategoryKuserPermissionLevel::MANAGER) {
             throw new KalturaAPIException(KalturaErrors::NOT_ENTITLED_TO_UPDATE_CATEGORY);
         }
     }
     $this->getPartner()->addFeaturesStatus(IndexObjectType::LOCK_CATEGORY);
     try {
         if ($moveEntriesToParentCategory) {
             $categoryDb->setDeletedAt(time());
         } else {
             $categoryDb->setDeletedAt(time(), 0);
         }
         $categoryDb->save();
         $this->getPartner()->removeFeaturesStatus(IndexObjectType::LOCK_CATEGORY);
     } catch (Exception $ex) {
         $this->getPartner()->removeFeaturesStatus(IndexObjectType::LOCK_CATEGORY);
         throw $ex;
     }
 }
 /**
  * activate CategoryEntry when it is pending moderation
  * 
  * @action reject
  * @param string $entryId
  * @param int $categoryId
  * @throws KalturaErrors::INVALID_ENTRY_ID
  * @throws KalturaErrors::CATEGORY_NOT_FOUND
  * @throws KalturaErrors::ENTRY_IS_NOT_ASSIGNED_TO_CATEGORY
  * @throws KalturaErrors::CANNOT_ACTIVATE_CATEGORY_ENTRY
  */
 function rejectAction($entryId, $categoryId)
 {
     $entry = entryPeer::retrieveByPK($entryId);
     if (!$entry) {
         throw new KalturaAPIException(KalturaErrors::INVALID_ENTRY_ID, $entryId);
     }
     $category = categoryPeer::retrieveByPK($categoryId);
     if (!$category) {
         throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $categoryId);
     }
     $dbCategoryEntry = categoryEntryPeer::retrieveByCategoryIdAndEntryId($categoryId, $entryId);
     if (!$dbCategoryEntry) {
         throw new KalturaAPIException(KalturaErrors::ENTRY_IS_NOT_ASSIGNED_TO_CATEGORY);
     }
     //validate user is entiteld to reject entry from category
     if (kEntitlementUtils::getEntitlementEnforcement()) {
         $categoryKuser = categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($categoryId, kCurrentContext::$ks_kuser_id);
         if (!$categoryKuser || $categoryKuser->getPermissionLevel() != CategoryKuserPermissionLevel::MANAGER && $categoryKuser->getPermissionLevel() != CategoryKuserPermissionLevel::MODERATOR) {
             throw new KalturaAPIException(KalturaErrors::CANNOT_REJECT_CATEGORY_ENTRY);
         }
     }
     if ($dbCategoryEntry->getStatus() != CategoryEntryStatus::PENDING) {
         throw new KalturaAPIException(KalturaErrors::CANNOT_REJECT_CATEGORY_ENTRY_SINCE_IT_IS_NOT_PENDING);
     }
     $dbCategoryEntry->setStatus(CategoryEntryStatus::REJECTED);
     $dbCategoryEntry->save();
 }