/**
  * List all categoryEntry
  * 
  * @action list
  * @param KalturaCategoryEntryFilter $filter
  * @param KalturaFilterPager $pager
  * @throws KalturaErrors::MUST_FILTER_ENTRY_ID_EQUAL
  * @throws KalturaErrors::MUST_FILTER_ON_ENTRY_OR_CATEGORY
  * @return KalturaCategoryEntryListResponse
  */
 function listAction(KalturaCategoryEntryFilter $filter = null, KalturaFilterPager $pager = null)
 {
     if ($filter === null) {
         $filter = new KalturaCategoryEntryFilter();
     }
     if ($pager == null) {
         $pager = new KalturaFilterPager();
     }
     if ($filter->entryIdEqual == null && $filter->categoryIdIn == null && $filter->categoryIdEqual == null && (kEntitlementUtils::getEntitlementEnforcement() || !kCurrentContext::$is_admin_session)) {
         throw new KalturaAPIException(KalturaErrors::MUST_FILTER_ON_ENTRY_OR_CATEGORY);
     }
     if (kEntitlementUtils::getEntitlementEnforcement()) {
         //validate entitl for entry
         if ($filter->entryIdEqual != null) {
             $entry = entryPeer::retrieveByPK($filter->entryIdEqual);
             if (!$entry) {
                 throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $filter->entryIdEqual);
             }
         }
         //validate entitl for entryIn
         if ($filter->entryIdIn != null) {
             $entry = entryPeer::retrieveByPKs($filter->entryIdIn);
             if (!$entry) {
                 throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $filter->entryIdIn);
             }
         }
         //validate entitl categories
         if ($filter->categoryIdIn != null) {
             $categoryIdInArr = explode(',', $filter->categoryIdIn);
             $categoryIdInArr = array_unique($categoryIdInArr);
             $entitledCategories = categoryPeer::retrieveByPKs($categoryIdInArr);
             if (!count($entitledCategories) || count($entitledCategories) != count($categoryIdInArr)) {
                 throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $filter->categoryIdIn);
             }
             $categoriesIdsUnlisted = array();
             foreach ($entitledCategories as $category) {
                 if ($category->getDisplayInSearch() == DisplayInSearchType::CATEGORY_MEMBERS_ONLY) {
                     $categoriesIdsUnlisted[] = $category->getId();
                 }
             }
             if (count($categoriesIdsUnlisted)) {
                 $categoriesUnlistWithMembership = categoryKuserPeer::retrieveByCategoriesIdsAndActiveKuserId($categoriesIdsUnlisted, kCurrentContext::$kuser_id);
                 if (count($categoriesIdsUnlisted) != count($categoriesUnlistWithMembership)) {
                     throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $filter->categoryIdIn);
                 }
             }
         }
         //validate entitl category
         if ($filter->categoryIdEqual != null) {
             $category = categoryPeer::retrieveByPK($filter->categoryIdEqual);
             if (!$category && kCurrentContext::$master_partner_id != Partner::BATCH_PARTNER_ID) {
                 throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $filter->categoryIdEqual);
             }
             if ($category->getDisplayInSearch() == DisplayInSearchType::CATEGORY_MEMBERS_ONLY && !categoryKuserPeer::retrieveByCategoryIdAndActiveKuserId($category->getId(), kCurrentContext::$ks_kuser_id)) {
                 throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $filter->categoryIdEqual);
             }
         }
     }
     $categoryEntryFilter = new categoryEntryFilter();
     $filter->toObject($categoryEntryFilter);
     $c = KalturaCriteria::create(categoryEntryPeer::OM_CLASS);
     $categoryEntryFilter->attachToCriteria($c);
     $totalCount = categoryEntryPeer::doCount($c);
     if (!kEntitlementUtils::getEntitlementEnforcement() || $filter->entryIdEqual == null && $filter->entryIdEqual == null) {
         $pager->attachToCriteria($c);
     }
     $dbCategoriesEntry = categoryEntryPeer::doSelect($c);
     if (kEntitlementUtils::getEntitlementEnforcement() && count($dbCategoriesEntry) && $filter->entryIdEqual != null) {
         //remove unlisted categories: display in search is set to members only
         $categoriesIds = array();
         foreach ($dbCategoriesEntry as $dbCategoryEntry) {
             $categoriesIds[] = $dbCategoryEntry->getCategoryId();
         }
         $c = KalturaCriteria::create(categoryPeer::OM_CLASS);
         $c->add(categoryPeer::ID, $categoriesIds, Criteria::IN);
         $pager->attachToCriteria($c);
         $c->applyFilters();
         $categoryIds = $c->getFetchedIds();
         foreach ($dbCategoriesEntry as $key => $dbCategoryEntry) {
             if (!in_array($dbCategoryEntry->getCategoryId(), $categoryIds)) {
                 KalturaLog::debug('Category [' . print_r($dbCategoryEntry->getCategoryId(), true) . '] is not listed to user');
                 unset($dbCategoriesEntry[$key]);
             }
         }
         $totalCount = $c->getRecordsCount();
     }
     $categoryEntrylist = KalturaCategoryEntryArray::fromCategoryEntryArray($dbCategoriesEntry);
     $response = new KalturaCategoryEntryListResponse();
     $response->objects = $categoryEntrylist;
     $response->totalCount = $totalCount;
     // no pager since category entry is limited to ENTRY::MAX_CATEGORIES_PER_ENTRY
     return $response;
 }