public function handleItemUpdated(KalturaObjectBase $object, SimpleXMLElement $item)
 {
     if (!$object instanceof KalturaBaseEntry) {
         return;
     }
     if (empty($item->scenes)) {
         return;
     }
     $action = KBulkUploadEngine::$actionsMap[KalturaBulkUploadAction::UPDATE];
     if (isset($item->scenes->action)) {
         $action = strtolower($item->scenes->action);
     }
     switch ($action) {
         case KBulkUploadEngine::$actionsMap[KalturaBulkUploadAction::UPDATE]:
             break;
         default:
             throw new KalturaBatchException("scenes->action: {$action} is not supported", KalturaBatchJobAppErrors::BULK_ACTION_NOT_SUPPORTED);
     }
     $this->entryId = $object->id;
     $this->cuePointPlugin = KalturaCuePointClientPlugin::get($this->xmlBulkUploadEngine->getClient());
     $this->xmlBulkUploadEngine->impersonate();
     $this->getExistingCuePointsBySystemName($this->entryId);
     $this->xmlBulkUploadEngine->getClient()->startMultiRequest();
     $items = array();
     foreach ($item->scenes->children() as $scene) {
         if ($this->updateCuePoint($scene)) {
             $items[] = $scene;
         }
     }
     $results = $this->xmlBulkUploadEngine->getClient()->doMultiRequest();
     $this->xmlBulkUploadEngine->unimpersonate();
     if (is_array($results) && is_array($items)) {
         $this->handleResults($results, $items);
     }
 }
 public function handleCustomData($objectId, SimpleXMLElement $customData)
 {
     $action = KBulkUploadEngine::$actionsMap[KalturaBulkUploadAction::REPLACE];
     if (isset($customData->action)) {
         $action = strtolower($customData->action);
     }
     $metadataProfileId = null;
     if (!empty($customData['metadataProfileId'])) {
         $metadataProfileId = (int) $customData['metadataProfileId'];
     }
     if (!$metadataProfileId && !empty($customData['metadataProfile'])) {
         $metadataProfileId = $this->getMetadataProfileId($customData['metadataProfile']);
     }
     if (!$metadataProfileId) {
         throw new KalturaBatchException("Missing custom data metadataProfile attribute", KalturaBatchJobAppErrors::BULK_MISSING_MANDATORY_PARAMETER);
     }
     $metadataPlugin = KalturaMetadataClientPlugin::get($this->xmlBulkUploadEngine->getClient());
     $metadataFilter = new KalturaMetadataFilter();
     $metadataFilter->metadataObjectTypeEqual = $this->objectType;
     $metadataFilter->objectIdEqual = $objectId;
     $metadataFilter->metadataProfileIdEqual = $metadataProfileId;
     $pager = new KalturaFilterPager();
     $pager->pageSize = 1;
     $metadataListResponse = $metadataPlugin->metadata->listAction($metadataFilter, $pager);
     $metadataId = null;
     $metadata = null;
     if (is_array($metadataListResponse->objects) && count($metadataListResponse->objects) > 0) {
         $metadata = reset($metadataListResponse->objects);
         $metadataId = $metadata->id;
     }
     switch ($action) {
         case KBulkUploadEngine::$actionsMap[KalturaBulkUploadAction::TRANSFORM_XSLT]:
             if (!isset($customData->xslt)) {
                 throw new KalturaBatchException($this->containerName . '->' . $this->nodeName . "->xslt element is missing", KalturaBatchJobAppErrors::BULK_ELEMENT_NOT_FOUND);
             }
             if ($metadata) {
                 $metadataXml = $metadata->xml;
             } else {
                 $metadataXml = '<metadata></metadata>';
             }
             $decodedXslt = kXml::decodeXml($customData->xslt);
             $metadataXml = kXml::transformXmlUsingXslt($metadataXml, $decodedXslt);
             break;
         case KBulkUploadEngine::$actionsMap[KalturaBulkUploadAction::REPLACE]:
             if (!isset($customData->xmlData)) {
                 throw new KalturaBatchException($this->containerName . '->' . $this->nodeName . "->xmlData element is missing", KalturaBatchJobAppErrors::BULK_ELEMENT_NOT_FOUND);
             }
             $metadataXmlObject = $customData->xmlData->children();
             $metadataXml = $metadataXmlObject->asXML();
             break;
         default:
             throw new KalturaBatchException($this->containerName . '->' . $this->nodeName . "->action: {$action} is not supported", KalturaBatchJobAppErrors::BULK_ACTION_NOT_SUPPORTED);
     }
     if ($metadataId) {
         $metadataPlugin->metadata->update($metadataId, $metadataXml);
     } else {
         $metadataPlugin->metadata->add($metadataProfileId, $this->objectType, $objectId, $metadataXml);
     }
 }
 public function handleDistribution($entryId, SimpleXMLElement $distribution)
 {
     $distributionProfileId = null;
     if (!empty($distribution->distributionProfileId)) {
         $distributionProfileId = (int) $distribution->distributionProfileId;
     }
     if (!$distributionProfileId && (!empty($distribution->distributionProfile) || !empty($distribution->distributionProvider))) {
         $distributionProfileId = $this->getDistributionProfileId($distribution->distributionProfile, $distribution->distributionProvider);
     }
     if (!$distributionProfileId) {
         throw new KalturaBatchException("Missing custom data distributionProfile attribute", KalturaBatchJobAppErrors::BULK_MISSING_MANDATORY_PARAMETER);
     }
     $distributionPlugin = KalturaContentDistributionClientPlugin::get($this->xmlBulkUploadEngine->getClient());
     $entryDistributionFilter = new KalturaEntryDistributionFilter();
     $entryDistributionFilter->distributionProfileIdEqual = $distributionProfileId;
     $entryDistributionFilter->entryIdEqual = $entryId;
     $pager = new KalturaFilterPager();
     $pager->pageSize = 1;
     $entryDistributionResponse = $distributionPlugin->entryDistribution->listAction($entryDistributionFilter, $pager);
     $entryDistribution = new KalturaEntryDistribution();
     $entryDistributionId = null;
     if (is_array($entryDistributionResponse->objects) && count($entryDistributionResponse->objects) > 0) {
         $existingEntryDistribution = reset($entryDistributionResponse->objects);
         $entryDistributionId = $existingEntryDistribution->id;
     } else {
         $entryDistribution->entryId = $entryId;
         $entryDistribution->distributionProfileId = $distributionProfileId;
     }
     if (!empty($distribution->sunrise) && KBulkUploadEngine::isFormatedDate($distribution->sunrise)) {
         $entryDistribution->sunrise = KBulkUploadEngine::parseFormatedDate($distribution->sunrise);
     }
     if (!empty($distribution->sunset) && KBulkUploadEngine::isFormatedDate($distribution->sunset)) {
         $entryDistribution->sunset = KBulkUploadEngine::parseFormatedDate($distribution->sunset);
     }
     if (!empty($distribution->flavorAssetIds)) {
         $entryDistribution->flavorAssetIds = $distribution->flavorAssetIds;
     }
     if (!empty($distribution->thumbAssetIds)) {
         $entryDistribution->thumbAssetIds = $distribution->thumbAssetIds;
     }
     $submitWhenReady = false;
     if ($distribution['submitWhenReady']) {
         $submitWhenReady = true;
     }
     $this->xmlBulkUploadEngine->impersonate();
     if ($entryDistributionId) {
         $updatedEntryDistribution = $distributionPlugin->entryDistribution->update($entryDistributionId, $entryDistribution);
         if ($submitWhenReady && $updatedEntryDistribution->dirtyStatus == KalturaEntryDistributionFlag::UPDATE_REQUIRED) {
             $distributionPlugin->entryDistribution->submitUpdate($entryDistributionId);
         }
     } else {
         $createdEntryDistribution = $distributionPlugin->entryDistribution->add($entryDistribution);
         $distributionPlugin->entryDistribution->submitAdd($createdEntryDistribution->id, $submitWhenReady);
     }
     $this->xmlBulkUploadEngine->unimpersonate();
 }
 private function getCurrentCaptionAssets($entryId)
 {
     $filter = new KalturaCaptionAssetItemFilter();
     $filter->entryIdEqual = $entryId;
     $pager = new KalturaFilterPager();
     $pager->pageSize = 500;
     $captionAssetPlugin = KalturaCaptionClientPlugin::get($this->xmlBulkUploadEngine->getClient());
     $captions = $captionAssetPlugin->captionAsset->listAction($filter, $pager);
     $this->currentCaptionAssets = array();
     if (!isset($captions->objects)) {
         return;
     }
     foreach ($captions->objects as $caption) {
         if ($caption->captionParamsId != 0) {
             //there could be multiple captions with captionParamsId=0
             $this->currentCaptionAssets[$caption->captionParamsId] = $caption->id;
         }
     }
 }
 private function handleAttachmentAsset($entryId, SimpleXMLElement $attachment)
 {
     $attachmentPlugin = KalturaAttachmentClientPlugin::get($this->xmlBulkUploadEngine->getClient());
     $attachmentAsset = new KalturaAttachmentAsset();
     $attachmentAsset->tags = $this->xmlBulkUploadEngine->implodeChildElements($attachment->tags);
     $this->xmlBulkUploadEngine->impersonate();
     //needed since $this->xmlBulkUploadEngine->getAssetParamsId calles to unimpersonate
     if (isset($attachment->fileExt)) {
         $attachmentAsset->fileExt = $attachment->fileExt;
     }
     if (isset($attachment->description)) {
         $attachmentAsset->partnerDescription = $attachment->description;
     }
     if (isset($attachment->filename)) {
         $attachmentAsset->filename = $attachment->filename;
     }
     if (isset($attachment->title)) {
         $attachmentAsset->title = $attachment->title;
     }
     if (isset($attachment['format'])) {
         $attachmentAsset->format = $attachment['format'];
     }
     $attachmentAssetId = null;
     if (isset($attachment['attachmentAssetId'])) {
         $attachmentAssetId = $attachment['attachmentAssetId'];
     }
     if ($attachmentAssetId) {
         $attachmentPlugin->attachmentAsset->update($attachmentAssetId, $attachmentAsset);
     } else {
         $attachmentAsset = $attachmentPlugin->attachmentAsset->add($entryId, $attachmentAsset);
         $attachmentAssetId = $attachmentAsset->id;
     }
     $attachmentAssetResource = $this->xmlBulkUploadEngine->getResource($attachment, 0);
     if ($attachmentAssetResource) {
         $attachmentPlugin->attachmentAsset->setContent($attachmentAssetId, $attachmentAssetResource);
     }
 }