/** * Add a media file to the library */ public function LibraryMediaAdd() { // Does this user have permission to call this webservice method? if (!$this->user->PageAuth('content')) { return $this->Error(1, 'Access Denied'); } Kit::ClassLoader('Media'); // Create a media object and gather the required parameters. $media = new Media(); $fileId = $this->GetParam('fileId', _INT); $type = $this->GetParam('type', _WORD); $name = $this->GetParam('name', _STRING); $duration = $this->GetParam('duration', _INT); $fileName = $this->GetParam('fileName', _FILENAME); // Check permissions if (!$this->user->FileAuth($fileId)) { return $this->Error(1, 'Access Denied'); } // Add the media. if (!($mediaId = $media->Add($fileId, $type, $name, $duration, $fileName, $this->user->userid))) { return $this->Error($media->GetErrorNumber(), $media->GetErrorMessage()); } // Return the mediaId. return $this->Respond($this->ReturnId('media', $mediaId)); }
/** * Adds Library Media * called from inside the FileUpload Handler * @param [type] $fileId [description] * @param [type] $mediaName [description] * @param [type] $duration [description] * @param [type] $fileName [description] * @return [int] [The ID of the Media Added] */ public function AddLibraryMedia($fileId, $mediaName, $duration, $fileName) { $this->response = new ResponseManager(); $db =& $this->db; $layoutid = $this->layoutid; $regionid = $this->regionid; // The media name might be empty here, because the user isn't forced to select it if ($mediaName == '') { $mediaName = $fileName; } // Hand off to the media module Kit::ClassLoader('media'); $mediaObject = new Media($db); if (!($mediaid = $mediaObject->Add($fileId, $this->type, $mediaName, $duration, $fileName, $this->user->userid))) { return $this->SetError($mediaObject->GetErrorMessage()); } Debug::LogEntry('audit', 'Returned MediaId: ' . $mediaid, 'module', 'AddLibraryMedia'); // Required Attributes $this->mediaid = $mediaid; $this->duration = $duration; try { $dbh = PDOConnect::init(); $sth = $dbh->prepare('SELECT StoredAs FROM `media` WHERE mediaid = :mediaid'); $sth->execute(array('mediaid' => $mediaid)); if (!($row = $sth->fetch())) { return $this->SetError(__('Unable to get the storage name')); } // Find out what we stored this item as $storedAs = Kit::ValidateParam($row['StoredAs'], _STRING); } catch (Exception $e) { Debug::LogEntry('error', $e->getMessage()); if (!$this->IsError()) { $this->SetError(1, __('Unknown Error')); } return false; } // Any Options $this->SetOption('uri', $storedAs); // Should have built the media object entirely by this time if ($regionid != '') { // This saves the Media Object to the Region if (!$this->UpdateRegion()) { return false; } } // Return the ID of this media return $mediaid; }
function Import($zipFile, $layout, $userId, $template, $replaceExisting, $importTags, $delete = true) { // I think I might add a layout and then import if (!file_exists($zipFile)) { return $this->SetError(__('File does not exist')); } // Open the Zip file $zip = new ZipArchive(); if (!$zip->open($zipFile)) { return $this->SetError(__('Unable to open ZIP')); } try { $dbh = PDOConnect::init(); $sth = $dbh->prepare('SELECT mediaid, storedAs FROM `media` WHERE name = :name AND IsEdited = 0'); // Get the layout details $layoutDetails = json_decode($zip->getFromName('layout.json'), true); // Set the layout name $layout = $layout != '' ? $layout : $layoutDetails['layout']; $description = isset($layoutDetails['description']) ? $layoutDetails['description'] : ''; // Get the layout xml $xml = $zip->getFromName('layout.xml'); // Add the layout if (!($layoutId = $this->Add($layout, $description, NULL, $userId, NULL, NULL, $xml))) { return false; } // Either remove out the tags, or add them to the DB if ($importTags) { // Pull the tags out of the XML $xmlDoc = new DOMDocument(); $xmlDoc->loadXML($xml); $xpath = new DOMXPath($xmlDoc); $tagsNode = $xpath->query("//tags"); foreach ($tagsNode as $tag) { $this->tag($tag->nodeValue, $layoutId); } } else { $this->EditTags($layoutId, array()); } // Are we a template? if ($template) { $this->tag('template', $layoutId); } // Tag as imported $this->tag('imported', $layoutId); // Set the DOM XML $this->SetDomXml($layoutId); // Set the user on each region foreach ($this->DomXml->getElementsByTagName('region') as $region) { $region->setAttribute('userId', $userId); } // Set the user on each media node foreach ($this->DomXml->getElementsByTagName('media') as $media) { $media->setAttribute('userId', $userId); } // We will need a file object and a media object $fileObject = new File(); $mediaObject = new Media(); $currentType = ''; // Go through each region and add the media (updating the media ids) $mappings = json_decode($zip->getFromName('mapping.json'), true); foreach ($mappings as $file) { Debug::LogEntry('audit', 'Found file ' . json_encode($file)); // Do we need to recharge our media object if ($currentType != '' && $file['type'] != $currentType) { $mediaObject = new Media(); } // Set the current type $currentType = $file['type']; // Does a media item with this name already exist? $sth->execute(array('name' => $file['name'])); $rows = $sth->fetchAll(); if (count($rows) > 0) { if ($replaceExisting) { // Alter the name of the file and add it $file['name'] = 'import_' . $layout . '_' . uniqid(); // Add the file if (!($fileId = $fileObject->NewFile($zip->getFromName('library/' . $file['file']), $userId))) { return $this->SetError(__('Unable to add a media item')); } // Add this media to the library if (!($mediaId = $mediaObject->Add($fileId, $file['type'], $file['name'], $file['duration'], $file['file'], $userId))) { return $this->SetError($mediaObject->GetErrorMessage()); } // Tag it $mediaObject->tag('imported', $mediaId); } else { // Don't add the file, use the one that already exists $mediaObject->mediaId = $rows[0]['mediaid']; $mediaObject->storedAs = $rows[0]['storedAs']; } } else { // Add the file if (!($fileId = $fileObject->NewFile($zip->getFromName('library/' . $file['file']), $userId))) { return $this->SetError(__('Unable to add a media item')); } // Add this media to the library if (!($mediaId = $mediaObject->Add($fileId, $file['type'], $file['name'], $file['duration'], $file['file'], $userId))) { return $this->SetError($mediaObject->GetErrorMessage()); } // Tag it $mediaObject->tag('imported', $mediaId); } Debug::LogEntry('audit', 'Post File Import Fix', get_class(), __FUNCTION__); // Get this media node from the layout using the old media id if (!$this->PostImportFix($userId, $layoutId, $file['mediaid'], $mediaObject->mediaId, $mediaObject->storedAs, $file['background'])) { return false; } } Debug::LogEntry('audit', 'Saving XLF', get_class(), __FUNCTION__); // Save the updated XLF if (!$this->SetLayoutXml($layoutId, $this->DomXml->saveXML())) { return false; } $this->SetValid($layoutId); // Finished, so delete $zip->close(); if ($delete) { @unlink($zipFile); } return true; } catch (Exception $e) { Debug::LogEntry('error', $e->getMessage()); if (!$this->IsError()) { $this->SetError(1, __('Unknown Error')); } return false; } }