public function putAction() { // Get the content-type of the request $contentType = $this->getRequest()->getHeader('Content-Type'); $contentType = explode(';', $contentType, 2); $fileType = explode('/', $contentType[0], 2); // Only image and audio file are supported if ($fileType[0] != SourceItem::IMAGE_TYPE && $fileType[0] != SourceItem::AUDIO_TYPE) { $this->_buildResponse(Api_BaseController::HTTP_FAILED, 'Unsupported media type'); return; } if (!$this->_authenticateUser()) { return; } // Get the Item from IRI $item = $this->_getItemByIri($this->_getItemIri()); if (!$this->_isItemExists($item)) { return; } // Only stuffpress media item can be edited if ($item->getPrefix() != 'stuffpress') { $this->_buildResponse(Api_BaseController::HTTP_FAILED, 'Uneditable media resource' . $item->getPrefix()); return; } // Get the raw body of the request $rawBody = $this->getRequest()->getRawBody(); $data = $this->_putMedia($item, $rawBody); // Check if the put process is successful if ($data != false) { // Update the data $item = $this->_updateItem($data, $item->getSource(), $item->getID()); // Build the entry for the response $atomProcessor = new AtomProcessor(); $newEntry = $atomProcessor->buildItemEntry($item); // Build the response $this->_buildResponse(Api_BaseController::HTTP_SUCCESS, $newEntry->getXml(), Api_BaseController::CONTENT_TYPE_ATOM); } }
protected function _postCommentEntry($rawBody, $item) { // Get the Atom Adapter $entry = AtomDocumentAdapterFactory::getInstance()->adapt($rawBody); /* @var $entry AtomEntryAdapter */ // Check whether the entry is an atom entry or not if ($entry->getDocumentType() == Atomns::ENTRY_ELEMENT) { $atomProcessor = new AtomProcessor(); // Read the Entry $data = $atomProcessor->readCommentEntry($entry); if ($data['timestamp'] == '') { $data['timestamp'] = time(); } $data['source_id'] = $item->getSource(); $data['item_id'] = $item->getID(); // Add new comment $comments = new Comments(); $comments->setUser($this->_application->user); $data['id'] = $comments->addComment($data['source_id'], $data['item_id'], $data['comment'], $data['name'], $data['email'], $data['website'], $data['timestamp'], 0); return new Comment($data); } else { $this->_buildResponse(Api_BaseController::HTTP_FAILED, 'Invalid Data!!! Atom Entry Document is required!!'); return false; } }
protected function _putEntry($item, $rawBody) { // Get the Atom Adapter $entry = AtomDocumentAdapterFactory::getInstance()->adapt($rawBody); /* @var $entry AtomEntryAdapter */ if ($entry->getDocumentType() == Atomns::ENTRY_ELEMENT) { // Read the Entry $atomProcessor = new AtomProcessor(); $data = $atomProcessor->readEntry($entry); if ($data['published'] == '') { $data['published'] = $item->getTimestamp(); } // Check whether the new resource has different type if ($data['type'] != $item->getType()) { $this->_buildResponse(Api_BaseController::HTTP_FAILED, 'The updated resource has different type than the old one!!!'); return false; } // Check whether the new resource has the same iri with the old one - it will be implemented later if the unit testing is ready (I think it works) // if ($this->_getItemIri() != $data['iri']) { // $this->_buildResponse(Api_BaseController::HTTP_FAILED, 'The updated resource has different IRI than the old one!!!'); // return false; // } unset($data['iri']); // If an image (should be audio as well) and the URL was given and it is a stuffpress item, we change the image if ($item->getPrefix() == 'stuffpress' && ($data['type'] == SourceItem::IMAGE_TYPE && $data['type'] == SourceItem::AUDIO_TYPE) && ($url = $data['url']) && strlen($url) > 0) { // Check whether the url is the same as the old one - not tested yet if ($item->getImageUrl(ImageItem::SIZE_LARGE) != $url) { $this->_deleteFileFromDb($item->getFile()); $key = $this->_saveFileToDb($data['type'], $url); if ($key == false) { return false; } $data['file'] = $key; } } return $data; } else { $this->_buildResponse(Api_BaseController::HTTP_FAILED, 'Invalid Data!!! Atom Entry Document is required!!'); return false; } }