Exemplo n.º 1
0
 /**
  * Add new Entry Distribution
  * 
  * @action add
  * @param KalturaEntryDistribution $entryDistribution
  * @return KalturaEntryDistribution
  * @throws ContentDistributionErrors::DISTRIBUTION_PROFILE_NOT_FOUND
  * @throws ContentDistributionErrors::ENTRY_DISTRIBUTION_ALREADY_EXISTS
  * @throws KalturaErrors::ENTRY_ID_NOT_FOUND
  */
 function addAction(KalturaEntryDistribution $entryDistribution)
 {
     $entryDistribution->validateForInsert();
     $dbDistributionProfile = DistributionProfilePeer::retrieveByPK($entryDistribution->distributionProfileId);
     if (!$dbDistributionProfile) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_NOT_FOUND, $entryDistribution->distributionProfileId);
     }
     if ($dbDistributionProfile->getStatus() == DistributionProfileStatus::DISABLED) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_DISABLED, $entryDistribution->distributionProfileId);
     }
     $dbEntry = entryPeer::retrieveByPK($entryDistribution->entryId);
     if (!$dbEntry) {
         throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryDistribution->entryId);
     }
     $dbEntryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entryDistribution->entryId, $entryDistribution->distributionProfileId);
     if ($dbEntryDistribution) {
         throw new KalturaAPIException(ContentDistributionErrors::ENTRY_DISTRIBUTION_ALREADY_EXISTS, $entryDistribution->entryId, $entryDistribution->distributionProfileId);
     }
     $dbEntryDistribution = kContentDistributionManager::addEntryDistribution($dbEntry, $dbDistributionProfile);
     $entryDistribution->toInsertableObject($dbEntryDistribution);
     $dbEntryDistribution->setPartnerId($this->getPartnerId());
     $dbEntryDistribution->save();
     $entryDistribution = new KalturaEntryDistribution();
     $entryDistribution->fromObject($dbEntryDistribution, $this->getResponseProfile());
     return $entryDistribution;
 }
 /**
  * @action getFeed
  * @disableTags TAG_WIDGET_SESSION,TAG_ENTITLEMENT_ENTRY,TAG_ENTITLEMENT_CATEGORY
  * @param int $distributionProfileId
  * @param string $hash
  * @return file
  */
 public function getFeedAction($distributionProfileId, $hash)
 {
     if (!$this->getPartnerId() || !$this->getPartner()) {
         throw new KalturaAPIException(KalturaErrors::INVALID_PARTNER_ID, $this->getPartnerId());
     }
     $profile = DistributionProfilePeer::retrieveByPK($distributionProfileId);
     if (!$profile || !$profile instanceof SynacorHboDistributionProfile) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_NOT_FOUND, $distributionProfileId);
     }
     if ($profile->getStatus() != KalturaDistributionProfileStatus::ENABLED) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_DISABLED, $distributionProfileId);
     }
     if ($profile->getUniqueHashForFeedUrl() != $hash) {
         throw new KalturaAPIException(SynacorHboDistributionErrors::INVALID_FEED_URL);
     }
     // "Creates advanced filter on distribution profile
     $distributionAdvancedSearch = new ContentDistributionSearchFilter();
     $distributionAdvancedSearch->setDistributionProfileId($profile->getId());
     $distributionAdvancedSearch->setDistributionSunStatus(EntryDistributionSunStatus::AFTER_SUNRISE);
     $distributionAdvancedSearch->setEntryDistributionStatus(EntryDistributionStatus::READY);
     $distributionAdvancedSearch->setEntryDistributionFlag(EntryDistributionDirtyStatus::NONE);
     $distributionAdvancedSearch->setHasEntryDistributionValidationErrors(false);
     //Creates entry filter with advanced filter
     $entryFilter = new entryFilter();
     $entryFilter->setStatusEquel(entryStatus::READY);
     $entryFilter->setModerationStatusNot(entry::ENTRY_MODERATION_STATUS_REJECTED);
     $entryFilter->setPartnerSearchScope($this->getPartnerId());
     $entryFilter->setAdvancedSearch($distributionAdvancedSearch);
     $baseCriteria = KalturaCriteria::create(entryPeer::OM_CLASS);
     $baseCriteria->add(entryPeer::DISPLAY_IN_SEARCH, mySearchUtils::DISPLAY_IN_SEARCH_SYSTEM, Criteria::NOT_EQUAL);
     $entryFilter->attachToCriteria($baseCriteria);
     $entries = entryPeer::doSelect($baseCriteria);
     $feed = new SynacorHboFeed('synacor_hbo_feed_template.xml');
     $feed->setDistributionProfile($profile);
     $counter = 0;
     foreach ($entries as $entry) {
         /* @var $entry entry */
         $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $profile->getId());
         if (!$entryDistribution) {
             KalturaLog::err('Entry distribution was not found for entry [' . $entry->getId() . '] and profile [' . $profile->getId() . ']');
             continue;
         }
         $fields = $profile->getAllFieldValues($entryDistribution);
         $flavorAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getFlavorAssetIds()));
         $thumbAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getThumbAssetIds()));
         $additionalAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getAssetIds()));
         $feed->addItem($fields, $entry, $flavorAssets, $thumbAssets, $additionalAssets);
         $counter++;
         //to avoid the cache exceeding the memory size
         if ($counter >= 100) {
             kMemoryManager::clearMemory();
             $counter = 0;
         }
     }
     header('Content-Type: text/xml');
     echo $feed->getXml();
     die;
 }
 /**
  * @action getFeed
  * @disableTags TAG_WIDGET_SESSION,TAG_ENTITLEMENT_ENTRY,TAG_ENTITLEMENT_CATEGORY
  * @param int $distributionProfileId
  * @param string $hash
  * @return file
  */
 public function getFeedAction($distributionProfileId, $hash)
 {
     if (!$this->getPartnerId() || !$this->getPartner()) {
         throw new KalturaAPIException(KalturaErrors::INVALID_PARTNER_ID, $this->getPartnerId());
     }
     $profile = DistributionProfilePeer::retrieveByPK($distributionProfileId);
     if (!$profile || !$profile instanceof UverseDistributionProfile) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_NOT_FOUND, $distributionProfileId);
     }
     if ($profile->getStatus() != KalturaDistributionProfileStatus::ENABLED) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_DISABLED, $distributionProfileId);
     }
     if ($profile->getUniqueHashForFeedUrl() != $hash) {
         throw new KalturaAPIException(UverseDistributionErrors::INVALID_FEED_URL);
     }
     // "Creates advanced filter on distribution profile
     $distributionAdvancedSearch = new ContentDistributionSearchFilter();
     $distributionAdvancedSearch->setDistributionProfileId($profile->getId());
     $distributionAdvancedSearch->setDistributionSunStatus(EntryDistributionSunStatus::AFTER_SUNRISE);
     $distributionAdvancedSearch->setEntryDistributionStatus(EntryDistributionStatus::READY);
     $distributionAdvancedSearch->setHasEntryDistributionValidationErrors(false);
     //Creates entry filter with advanced filter
     $entryFilter = new entryFilter();
     $entryFilter->setStatusEquel(entryStatus::READY);
     $entryFilter->setModerationStatusNot(entry::ENTRY_MODERATION_STATUS_REJECTED);
     $entryFilter->setPartnerSearchScope($this->getPartnerId());
     $entryFilter->setAdvancedSearch($distributionAdvancedSearch);
     $baseCriteria = KalturaCriteria::create(entryPeer::OM_CLASS);
     $baseCriteria->add(entryPeer::DISPLAY_IN_SEARCH, mySearchUtils::DISPLAY_IN_SEARCH_SYSTEM, Criteria::NOT_EQUAL);
     $entryFilter->attachToCriteria($baseCriteria);
     $entries = entryPeer::doSelect($baseCriteria);
     $feed = new UverseFeed('uverse_template.xml');
     $feed->setDistributionProfile($profile);
     $feed->setChannelFields();
     $lastBuildDate = $profile->getUpdatedAt(null);
     foreach ($entries as $entry) {
         /* @var $entry entry */
         $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $profile->getId());
         if (!$entryDistribution) {
             KalturaLog::err('Entry distribution was not found for entry [' . $entry->getId() . '] and profile [' . $profile->getId() . ']');
             continue;
         }
         $fields = $profile->getAllFieldValues($entryDistribution);
         $flavorAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getFlavorAssetIds()));
         $flavorAsset = reset($flavorAssets);
         $flavorAssetRemoteUrl = $entryDistribution->getFromCustomData(UverseEntryDistributionCustomDataField::REMOTE_ASSET_URL);
         $thumbAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getThumbAssetIds()));
         $feed->addItem($fields, $flavorAsset, $flavorAssetRemoteUrl, $thumbAssets);
         // we want to find the newest update time between all entries
         if ($entry->getUpdatedAt(null) > $lastBuildDate) {
             $lastBuildDate = $entry->getUpdatedAt(null);
         }
     }
     $feed->setChannelLastBuildDate($lastBuildDate);
     header('Content-Type: text/xml');
     echo $feed->getXml();
     die;
 }
 protected function handleEntries($context, $feed, array $entries)
 {
     $protocol = infraRequestUtils::getProtocol();
     $cachePrefix = "dist_" . $this->profile->getId() . "/{$protocol}/entry_";
     $profileUpdatedAt = $this->profile->getUpdatedAt(null);
     $extendItems = $this->profile->getItemXpathsToExtend();
     $enableCache = empty($extendItems);
     $cacheStore = null;
     if ($enableCache) {
         $cacheStore = kCacheManager::getSingleLayerCache(kCacheManager::CACHE_TYPE_FEED_ENTRY);
         if (is_null($cacheStore)) {
             $enableCache = false;
         }
     }
     $counter = 0;
     foreach ($entries as $entry) {
         $xml = null;
         $cacheKey = $cachePrefix . str_replace("_", "-", $entry->getId());
         // replace _ with - so cache folders will be created with random entry id and not 0_/1_
         if ($enableCache) {
             $cacheTime = $cacheStore->get($cacheKey . self::CACHE_CREATION_TIME_SUFFIX);
             $updatedAt = max($profileUpdatedAt, $entry->getUpdatedAt(null));
             if ($updatedAt + self::CACHE_CREATION_MARGIN < $cacheTime) {
                 $xml = $cacheStore->get($cacheKey);
             }
         }
         if (!$xml) {
             $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $this->profile->getId());
             if (!$entryDistribution) {
                 KalturaLog::err('Entry distribution was not found for entry [' . $entry->getId() . '] and profile [' . $this->profile->getId() . ']');
                 continue;
             }
             $xml = $this->handleEntry($context, $feed, $entry, $entryDistribution);
             if (!is_null($xml) && $enableCache) {
                 $cacheStore->set($cacheKey . self::CACHE_CREATION_TIME_SUFFIX, time());
                 $cacheStore->set($cacheKey, $xml);
             }
         }
         $feed->addItemXml($xml);
         $counter++;
         //to avoid the cache exceeding the memory size
         if ($counter % self::CACHE_SIZE == 0) {
             kMemoryManager::clearMemory();
         }
     }
 }
 /**
  * @action getFeed
  * @disableTags TAG_WIDGET_SESSION,TAG_ENTITLEMENT_ENTRY,TAG_ENTITLEMENT_CATEGORY
  * @param int $distributionProfileId
  * @param string $hash
  * @return file
  */
 public function getFeedAction($distributionProfileId, $hash)
 {
     if (!$this->getPartnerId() || !$this->getPartner()) {
         throw new KalturaAPIException(KalturaErrors::INVALID_PARTNER_ID, $this->getPartnerId());
     }
     $profile = DistributionProfilePeer::retrieveByPK($distributionProfileId);
     if (!$profile || !$profile instanceof AttUverseDistributionProfile) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_NOT_FOUND, $distributionProfileId);
     }
     if ($profile->getStatus() != KalturaDistributionProfileStatus::ENABLED) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_DISABLED, $distributionProfileId);
     }
     if ($profile->getUniqueHashForFeedUrl() != $hash) {
         throw new KalturaAPIException(AttUverseDistributionErrors::INVALID_FEED_URL);
     }
     // "Creates advanced filter on distribution profile
     $distributionAdvancedSearch = new ContentDistributionSearchFilter();
     $distributionAdvancedSearch->setDistributionProfileId($profile->getId());
     $distributionAdvancedSearch->setDistributionSunStatus(EntryDistributionSunStatus::AFTER_SUNRISE);
     $distributionAdvancedSearch->setEntryDistributionStatus(EntryDistributionStatus::READY);
     $distributionAdvancedSearch->setEntryDistributionFlag(EntryDistributionDirtyStatus::NONE);
     $distributionAdvancedSearch->setHasEntryDistributionValidationErrors(false);
     //Creates entry filter with advanced filter
     $entryFilter = new entryFilter();
     $entryFilter->setStatusEquel(entryStatus::READY);
     $entryFilter->setModerationStatusNot(entry::ENTRY_MODERATION_STATUS_REJECTED);
     $entryFilter->setPartnerSearchScope($this->getPartnerId());
     $entryFilter->setAdvancedSearch($distributionAdvancedSearch);
     $baseCriteria = KalturaCriteria::create(entryPeer::OM_CLASS);
     $baseCriteria->add(entryPeer::DISPLAY_IN_SEARCH, mySearchUtils::DISPLAY_IN_SEARCH_SYSTEM, Criteria::NOT_EQUAL);
     $entryFilter->attachToCriteria($baseCriteria);
     $entries = entryPeer::doSelect($baseCriteria);
     $feed = new AttUverseDistributionFeedHelper('feed_template.xml', $profile);
     $channelTitle = $profile->getChannelTitle();
     $counter = 0;
     foreach ($entries as $entry) {
         /* @var $entry entry */
         /* @var $entryDistribution Entrydistribution */
         $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $profile->getId());
         if (!$entryDistribution) {
             KalturaLog::err('Entry distribution was not found for entry [' . $entry->getId() . '] and profile [' . $profile->getId() . ']');
             continue;
         }
         $fields = $profile->getAllFieldValues($entryDistribution);
         //flavors assets and remote flavor asset file urls
         $flavorAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getFromCustomData(AttUverseEntryDistributionCustomDataField::DISTRIBUTED_FLAVOR_IDS)));
         $remoteAssetFileUrls = unserialize($entryDistribution->getFromCustomData(AttUverseEntryDistributionCustomDataField::REMOTE_ASSET_FILE_URLS));
         //thumb assets and remote thumb asset file urls
         $thumbAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getFromCustomData(AttUverseEntryDistributionCustomDataField::DISTRIBUTED_THUMBNAIL_IDS)));
         $remoteThumbailFileUrls = unserialize($entryDistribution->getFromCustomData(AttUverseEntryDistributionCustomDataField::REMOTE_THUMBNAIL_FILE_URLS));
         //thumb assets and remote thumb asset file urls
         $captionAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getFromCustomData(AttUverseEntryDistributionCustomDataField::DISTRIBUTED_CAPTION_IDS)));
         $feed->addItem($fields, $flavorAssets, $remoteAssetFileUrls, $thumbAssets, $remoteThumbailFileUrls, $captionAssets);
         $counter++;
         //to avoid the cache exceeding the memory size
         if ($counter >= 100) {
             kMemoryManager::clearMemory();
             $counter = 0;
         }
     }
     //set channel title
     if (isset($fields)) {
         $channelTitle = $fields[AttUverseDistributionField::CHANNEL_TITLE];
     }
     $feed->setChannelTitle($channelTitle);
     header('Content-Type: text/xml');
     echo str_replace('&', '&amp;', html_entity_decode($feed->getXml(), ENT_QUOTES, 'UTF-8'));
     die;
 }
 /**
  * @param entry $entry
  */
 public static function onEntryReady(entry $entry)
 {
     if (!ContentDistributionPlugin::isAllowedPartner($entry->getPartnerId())) {
         return true;
     }
     //no temp entries should be handled
     if ($entry->getDisplayInSearch() == mySearchUtils::DISPLAY_IN_SEARCH_SYSTEM && $entry->getReplacedEntryId()) {
         return true;
     }
     $distributionProfiles = DistributionProfilePeer::retrieveByPartnerId($entry->getPartnerId());
     foreach ($distributionProfiles as $distributionProfile) {
         $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $distributionProfile->getId());
         if ($entryDistribution) {
             KalturaLog::info("Found entry distribution object with id [" . $entryDistribution->getId() . "] for distrinution profle [" . $distributionProfile->getId() . "]");
             self::onEntryDistributionUpdateRequired($entryDistribution);
             continue;
         }
         if ($distributionProfile->getSubmitEnabled() == DistributionProfileActionStatus::AUTOMATIC) {
             self::addEntryDistribution($entry, $distributionProfile, true);
         }
     }
     return true;
 }
 /**
  * @action getFeedByEntryId
  * @disableTags TAG_WIDGET_SESSION,TAG_ENTITLEMENT_ENTRY,TAG_ENTITLEMENT_CATEGORY
  * @param int $distributionProfileId
  * @param string $hash
  * @param string $entryId
  * @return file
  */
 public function getFeedByEntryIdAction($distributionProfileId, $hash, $entryId)
 {
     if (!$this->getPartnerId() || !$this->getPartner()) {
         throw new KalturaAPIException(KalturaErrors::INVALID_PARTNER_ID, $this->getPartnerId());
     }
     $profile = DistributionProfilePeer::retrieveByPK($distributionProfileId);
     if (!$profile || !$profile instanceof DoubleClickDistributionProfile) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_NOT_FOUND, $distributionProfileId);
     }
     if ($profile->getStatus() != KalturaDistributionProfileStatus::ENABLED) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_DISABLED, $distributionProfileId);
     }
     if ($profile->getUniqueHashForFeedUrl() != $hash) {
         throw new KalturaAPIException(DoubleClickDistributionErrors::INVALID_FEED_URL);
     }
     // Creates entry filter with advanced filter
     $entry = entryPeer::retrieveByPK($entryId);
     if (!$entry || $entry->getPartnerId() != $this->getPartnerId()) {
         throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId);
     }
     // Construct the feed
     $feed = new DoubleClickFeed('doubleclick_template.xml', $profile);
     $feed->setTotalResult(1);
     $feed->setStartIndex(1);
     $profileUpdatedAt = $profile->getUpdatedAt(null);
     $cacheDir = kConf::get("global_cache_dir") . "feeds/dist_{$distributionProfileId}/";
     // check cache
     $cacheFileName = $cacheDir . myContentStorage::dirForId($entry->getIntId(), $entry->getId() . ".xml");
     $updatedAt = max($profileUpdatedAt, $entry->getUpdatedAt(null));
     if (file_exists($cacheFileName) && $updatedAt < filemtime($cacheFileName)) {
         $xml = file_get_contents($cacheFileName);
     } else {
         $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $profile->getId());
         if (!$entryDistribution) {
             throw new KalturaAPIException(ContentDistributionErrors::ENTRY_DISTRIBUTION_NOT_FOUND, '');
         }
         $fields = $profile->getAllFieldValues($entryDistribution);
         $flavorAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getFlavorAssetIds()));
         $thumbAssets = assetPeer::retrieveByIds(explode(',', $entryDistribution->getThumbAssetIds()));
         $cuePoints = $this->getCuePoints($entry->getPartnerId(), $entry->getId());
         $xml = $feed->getItemXml($fields, $flavorAssets, $thumbAssets, $cuePoints);
         mkdir(dirname($cacheFileName), 0777, true);
         file_put_contents($cacheFileName, $xml);
     }
     $feed->addItemXml($xml);
     header('Content-Type: text/xml');
     echo $feed->getXml();
     die;
 }
Exemplo n.º 8
0
 protected function handleEntries($context, $feed, array $entries)
 {
     //getting array of all related entries (categories that will appear in the xml)
     $relatedEntriesArray = array();
     //going through all entries and preparing an array with all related entries (categories) directing to the entires
     foreach ($entries as $entry) {
         $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $this->profile->getId());
         if (!$entryDistribution) {
             KalturaLog::err('Entry distribution was not found for entry [' . $entry->getId() . '] and profile [' . $this->profile->getId() . ']');
             continue;
         }
         $fields = $this->profile->getAllFieldValues($entryDistribution);
         $relatedEntryId = $fields[UverseClickToOrderDistributionField::CATEGORY_ENTRY_ID];
         if (!isset($relatedEntriesArray[$relatedEntryId])) {
             $relatedEntry = entryPeer::retrieveByPK($relatedEntryId);
             $relatedEntrySortValue = $this->getRelatedEntrySortValue($this->profile, $relatedEntryId);
             $relatedEntriesArray[$relatedEntryId] = array();
             $relatedEntriesArray[$relatedEntryId]['sortValue'] = $relatedEntrySortValue;
             $relatedEntriesArray[$relatedEntryId]['updatedAt'] = $relatedEntry->getUpdatedAt();
             $relatedEntriesArray[$relatedEntryId]['relatedEntryId'] = $relatedEntryId;
         }
         $flavorAssets = array_map('trim', explode(',', $entryDistribution->getFlavorAssetIds()));
         $flavorAssetId = isset($flavorAssets[0]) ? $flavorAssets[0] : null;
         $flavorAsset = assetPeer::retrieveById($flavorAssetId);
         $flavorUrl = $flavorAsset ? $flavorAsset->getDownloadUrl() : $entry->getDownloadUrl();
         $thumbAssets = array_map('trim', explode(',', $entryDistribution->getThumbAssetIds()));
         $thumbAssetId = isset($thumbAssets[0]) ? $thumbAssets[0] : null;
         $thumbAsset = assetPeer::retrieveById($thumbAssetId);
         $thumbUrl = $thumbAsset ? $thumbAsset->getDownloadUrl() : $entry->getThumbnailUrl();
         $relatedEntriesArray[$relatedEntryId][] = array('id' => $entry->getId(), 'thumbnailUrl' => $thumbUrl, 'downloadUrl' => $flavorUrl, 'updatedAt' => $entry->getUpdatedAt(), 'sortValue' => $this->profile->getFieldValue($entryDistribution, UverseClickToOrderDistributionField::SORT_ITEMS_BY_FIELD));
     }
     //sorting the related entries.
     usort($relatedEntriesArray, array($this, 'sortItems'));
     //removing the values that where used for sorting.
     foreach ($relatedEntriesArray as $key => $relatedEntry) {
         $relatedEntryId = $relatedEntry['relatedEntryId'];
         unset($relatedEntry['relatedEntryId']);
         unset($relatedEntry['sortValue']);
         unset($relatedEntry['updatedAt']);
         unset($relatedEntriesArray[$key]);
         $relatedEntriesArray[$relatedEntryId] = $relatedEntry;
     }
     //retreive each category and add it to the xml
     foreach ($relatedEntriesArray as $relatedEntryId => $entriesUnderCategory) {
         //getting the related entry id object
         $relatedEntryObject = entryPeer::retrieveByPK($relatedEntryId);
         if (!$relatedEntryObject) {
             KalturaLog::err('Related Entry [' . $relatedEntryId . '] was not found');
             continue;
         }
         $categoryName = $relatedEntryObject->getName();
         $categoryFile = $relatedEntryObject->getThumbnailUrl() . '/width/' . $fields[UverseClickToOrderDistributionField::CATEGORY_IMAGE_WIDTH] . '/height/' . $fields[UverseClickToOrderDistributionField::CATEGORY_IMAGE_HEIGHT];
         $categoryNode = $feed->addCategory($categoryName, $categoryFile);
         usort($entriesUnderCategory, array($this, 'sortItems'));
         //getting all entries under a category
         foreach ($entriesUnderCategory as $entryInfo) {
             $entryId = $entryInfo['id'];
             $thumbnailFile = $entryInfo['thumbnailUrl'];
             $flavorFile = $entryInfo['downloadUrl'];
             //getting entry's fileds array
             $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entryId, $this->profile->getId());
             $fields = $this->profile->getAllFieldValues($entryDistribution);
             $feed->addItem($fields, $categoryNode, $thumbnailFile, $flavorFile);
         }
     }
 }
 /**
  * @param entry $entry
  * @param DistributionProfile $distributionProfile
  * @return EntryDistribution or null if failed to create.
  */
 public static function createEntryDistribution(entry $entry, DistributionProfile $distributionProfile)
 {
     $illegalEntryDistributionStatus = array(EntryDistributionStatus::SUBMITTING, EntryDistributionStatus::UPDATING, EntryDistributionStatus::DELETING, EntryDistributionStatus::IMPORT_SUBMITTING, EntryDistributionStatus::IMPORT_UPDATING);
     $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $distributionProfile->getId());
     if (!$entryDistribution || $entryDistribution->getStatus() == EntryDistributionStatus::DELETED) {
         $entryDistribution = new EntryDistribution();
     } else {
         if (in_array($entryDistribution->getStatus(), $illegalEntryDistributionStatus)) {
             KalturaLog::err("Entry distribution already exist. entry [" . $entry->getId() . "] distribution profile [" . $distributionProfile->getId() . "] status [" . $entryDistribution->getStatus() . "]");
             return null;
         }
     }
     $entryDistribution->setEntryId($entry->getId());
     $entryDistribution->setPartnerId($entry->getPartnerId());
     $entryDistribution->setDistributionProfileId($distributionProfile->getId());
     $entryDistribution->setStatus(EntryDistributionStatus::PENDING);
     self::assignFlavorAssets($entryDistribution, $entry, $distributionProfile);
     self::assignThumbAssets($entryDistribution, $entry, $distributionProfile);
     self::assignAssets($entryDistribution, $entry, $distributionProfile);
     $entryDistribution->save();
     // need to save before checking validations
     $validationErrors = $distributionProfile->validateForSubmission($entryDistribution, DistributionAction::SUBMIT);
     $entryDistribution->setValidationErrorsArray($validationErrors);
     return $entryDistribution;
 }
 /**
  * @action getFeed
  * @disableTags TAG_WIDGET_SESSION,TAG_ENTITLEMENT_ENTRY,TAG_ENTITLEMENT_CATEGORY
  * @param int $distributionProfileId
  * @param string $hash
  * @return file
  */
 public function getFeedAction($distributionProfileId, $hash)
 {
     if (!$this->getPartnerId() || !$this->getPartner()) {
         throw new KalturaAPIException(KalturaErrors::INVALID_PARTNER_ID, $this->getPartnerId());
     }
     $profile = DistributionProfilePeer::retrieveByPK($distributionProfileId);
     if (!$profile || !$profile instanceof UverseClickToOrderDistributionProfile) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_NOT_FOUND, $distributionProfileId);
     }
     if ($profile->getStatus() != KalturaDistributionProfileStatus::ENABLED) {
         throw new KalturaAPIException(ContentDistributionErrors::DISTRIBUTION_PROFILE_DISABLED, $distributionProfileId);
     }
     if ($profile->getUniqueHashForFeedUrl() != $hash) {
         throw new KalturaAPIException(UverseClickToOrderDistributionErrors::INVALID_FEED_URL);
     }
     // "Creates advanced filter on distribution profile
     $distributionAdvancedSearch = new ContentDistributionSearchFilter();
     $distributionAdvancedSearch->setDistributionProfileId($profile->getId());
     $distributionAdvancedSearch->setDistributionSunStatus(EntryDistributionSunStatus::AFTER_SUNRISE);
     $distributionAdvancedSearch->setEntryDistributionStatus(EntryDistributionStatus::READY);
     $distributionAdvancedSearch->setEntryDistributionFlag(EntryDistributionDirtyStatus::NONE);
     $distributionAdvancedSearch->setHasEntryDistributionValidationErrors(false);
     //Creates entry filter with advanced filter
     $entryFilter = new entryFilter();
     $entryFilter->setStatusEquel(entryStatus::READY);
     $entryFilter->setModerationStatusNot(entry::ENTRY_MODERATION_STATUS_REJECTED);
     $entryFilter->setPartnerSearchScope($this->getPartnerId());
     $entryFilter->setAdvancedSearch($distributionAdvancedSearch);
     $baseCriteria = KalturaCriteria::create(entryPeer::OM_CLASS);
     $baseCriteria->add(entryPeer::DISPLAY_IN_SEARCH, mySearchUtils::DISPLAY_IN_SEARCH_SYSTEM, Criteria::NOT_EQUAL);
     $entryFilter->attachToCriteria($baseCriteria);
     $baseCriteria->addDescendingOrderByColumn(entryPeer::UPDATED_AT);
     $entries = entryPeer::doSelect($baseCriteria);
     $feed = new UverseClickToOrderFeed('feed_template.xml');
     $feed->setDistributionProfile($profile);
     //setting background images
     $wideBackgroundImageEntryId = $profile->getBackgroundImageWide();
     $standardBackgroundImageEntryId = $profile->getBackgroundImageStandard();
     $widedBackgroundImageUrl = $this->getImageUrl($wideBackgroundImageEntryId, '854', '480');
     $standardBackgroundImageUrl = $this->getImageUrl($standardBackgroundImageEntryId, '640', '480');
     $feed->setBackgroudImage($widedBackgroundImageUrl, $standardBackgroundImageUrl);
     //getting array of all related entries (categories that will appear in the xml)
     $relatedEntriesArray = array();
     //going through all entries and preparing an array with all related entries (categories) directing to the entires
     foreach ($entries as $entry) {
         $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entry->getId(), $profile->getId());
         if (!$entryDistribution) {
             KalturaLog::err('Entry distribution was not found for entry [' . $entry->getId() . '] and profile [' . $profile->getId() . ']');
             continue;
         }
         $fields = $profile->getAllFieldValues($entryDistribution);
         $relatedEntryId = $fields[UverseClickToOrderDistributionField::CATEGORY_ENTRY_ID];
         if (!isset($relatedEntriesArray[$relatedEntryId])) {
             $relatedEntry = entryPeer::retrieveByPK($relatedEntryId);
             $relatedEntrySortValue = $this->getRelatedEntrySortValue($profile, $relatedEntryId);
             $relatedEntriesArray[$relatedEntryId] = array();
             $relatedEntriesArray[$relatedEntryId]['sortValue'] = $relatedEntrySortValue;
             $relatedEntriesArray[$relatedEntryId]['updatedAt'] = $relatedEntry->getUpdatedAt();
             $relatedEntriesArray[$relatedEntryId]['relatedEntryId'] = $relatedEntryId;
         }
         $flavorAssets = array_map('trim', explode(',', $entryDistribution->getFlavorAssetIds()));
         $flavorAssetId = isset($flavorAssets[0]) ? $flavorAssets[0] : null;
         $flavorAsset = assetPeer::retrieveById($flavorAssetId);
         $flavorUrl = $flavorAsset ? $flavorAsset->getDownloadUrl() : $entry->getDownloadUrl();
         $thumbAssets = array_map('trim', explode(',', $entryDistribution->getThumbAssetIds()));
         $thumbAssetId = isset($thumbAssets[0]) ? $thumbAssets[0] : null;
         $thumbAsset = assetPeer::retrieveById($thumbAssetId);
         $thumbUrl = $thumbAsset ? $thumbAsset->getDownloadUrl() : $entry->getThumbnailUrl();
         $relatedEntriesArray[$relatedEntryId][] = array('id' => $entry->getId(), 'thumbnailUrl' => $thumbUrl, 'downloadUrl' => $flavorUrl, 'updatedAt' => $entry->getUpdatedAt(), 'sortValue' => $profile->getFieldValue($entryDistribution, UverseClickToOrderDistributionField::SORT_ITEMS_BY_FIELD));
     }
     //sorting the related entries.
     usort($relatedEntriesArray, array($this, 'sortItems'));
     //removing the values that where used for sorting.
     foreach ($relatedEntriesArray as $key => $relatedEntry) {
         $relatedEntryId = $relatedEntry['relatedEntryId'];
         unset($relatedEntry['relatedEntryId']);
         unset($relatedEntry['sortValue']);
         unset($relatedEntry['updatedAt']);
         unset($relatedEntriesArray[$key]);
         $relatedEntriesArray[$relatedEntryId] = $relatedEntry;
     }
     //retreive each category and add it to the xml
     foreach ($relatedEntriesArray as $relatedEntryId => $entriesUnderCategory) {
         //getting the related entry id object
         $relatedEntryObject = entryPeer::retrieveByPK($relatedEntryId);
         if (!$relatedEntryObject) {
             KalturaLog::err('Related Entry [' . $relatedEntryId . '] was not found');
             continue;
         }
         $categoryName = $relatedEntryObject->getName();
         $categoryFile = $relatedEntryObject->getThumbnailUrl() . '/width/' . $fields[UverseClickToOrderDistributionField::CATEGORY_IMAGE_WIDTH] . '/height/' . $fields[UverseClickToOrderDistributionField::CATEGORY_IMAGE_HEIGHT];
         $categoryNode = $feed->addCategory($categoryName, $categoryFile);
         usort($entriesUnderCategory, array($this, 'sortItems'));
         //getting all entries under a category
         foreach ($entriesUnderCategory as $entryInfo) {
             $entryId = $entryInfo['id'];
             $thumbnailFile = $entryInfo['thumbnailUrl'];
             $flavorFile = $entryInfo['downloadUrl'];
             //getting entry's fileds array
             $entryDistribution = EntryDistributionPeer::retrieveByEntryAndProfileId($entryId, $profile->getId());
             $fields = $profile->getAllFieldValues($entryDistribution);
             $feed->addItem($fields, $categoryNode, $thumbnailFile, $flavorFile);
         }
     }
     header('Content-Type: text/xml');
     echo $feed->getXml();
     die;
 }