/** * For event in shared context: * Node.@images.add.post * * @param NodeRef $nodeRef NodeRef Being Added * @param Node $node Node Being added * @param null $notUsed Paramater passed in via Events::trigger, but set to null, and not used. * @param bool $asyncRebuild Wether to rebuildThumbnails asyncronously or not (defaults to true) * * @return void */ public function processAdd(NodeRef $nodeRef, Node $node, $notUsed = null, $asyncRebuild = true) { $node = $this->RegulatedNodeService->getByNodeRef($nodeRef, new NodePartials('', '#original.fields')); //----------- // Build CMS thumbnail separately so that it's ready when this action completes. $file = $this->FileService->retrieveFileFromNode($node->getElement(), $node->getOutTag('#original')->TagLinkNodeRef); if ($file === null) { throw new Exception('File cannot be retrieved for Node'); } $cmsThumb = $this->ImageService->createAndStoreThumbnail($node->getElement(), $this->imagesThumbnailCmsSize, $file->getLocalPath(), $this->ImageService->filenameForNode($node)); $tag = new Tag($cmsThumb->getElement()->getSlug(), $cmsThumb->Slug, '#thumbnails', $this->imagesThumbnailCmsSize, $this->imagesThumbnailCmsSize); $node->replaceOutTags('#thumbnails', array($tag)); $this->RegulatedNodeService->edit($node); if ($asyncRebuild) { // commit so the node is ready for the worker $this->TransactionManager->commit()->begin(); //----------- // Rebuild thumbnails asynchronously $workerParams = array('nodeRef' => '' . $node->getNodeRef(), 'forceRebuildExisting' => false); $this->GearmanService->doBackgroundJob('ImagesWorker', 'rebuildThumbnails', $workerParams, 'high'); } else { $this->ImageService->rebuildMissingThumbnails($node->getNodeRef(), false); } // remove storage facility generated temp file @unlink($file->getLocalPath()); }
public static function commit($datasource = 'default') { $result = parent::commit($datasource); if ($result) { self::$commited[] = $datasource; } return $result; }
public function buildThumbnails(NodeRef $nodeRef, Node $node) { // commit so the node is ready for the worker $this->TransactionManager->commit()->begin(); //----------- // Rebuild thumbnails asynchronously $workerParams = array('nodeRef' => '' . $nodeRef, 'forceRebuildExisting' => false); $this->GearmanService->doBackgroundJob('ImagesWorker', 'rebuildThumbnails', $workerParams, 'high'); }
/** * Replaces the file associated with the file node then updates the node. * * @param Node $fileNode * @param Element $parentElement * @param string $newFile Full path to the new file * @param string $newFileName the new desired filename * @return Node */ protected function replaceNodeFile(Node $fileNode, Element $parentElement, $newFile, $newFileName) { // get storage facility $fileElement = $fileNode->getNodeRef()->getElement(); list($storageFacility, $sfParams) = $this->deriveStorageFacility($fileElement); $oldId = $fileNode->Title; // add the new file $newId = $storageFacility->findUniqueFileID($sfParams, $this->buildFilename($newFileName)); $file = new StorageFacilityFile($newId, $newFile); $file = $storageFacility->putFile($sfParams, $file); // set the new id as title $fileNode->Title = $file->getID(); // get metrics list($width, $height) = ImageUtils::getImageDimensions($newFile); // and mime-type $mimetype = FileSystemUtils::getMimetype($file->getExtension()); // update the meta fields $fileNode->setMeta('#url', $file->getURL()); $fileNode->setMeta('#mimetype', $mimetype); $fileNode->setMeta('#size', filesize($newFile)); $fileNode->setMeta('#width', $width); $fileNode->setMeta('#height', $height); $fileNode->setMeta('#parent-element', $parentElement->Slug); // do the update $this->RegulatedNodeService->edit($fileNode); // if different ID, we need to remove old file, // but first make sure new record is committed if ($oldId != $newId) { // force commit changes $this->TransactionManager->commit()->begin(); // delete existing file $storageFacility->deleteFile($sfParams, $oldId); } return $fileNode; }
protected function _transaction($model, $start = true) { $this->started[$model->useDbConfig] = $start; return $start ? TransactionManager::begin($model->useDbConfig) : TransactionManager::commit($model->useDbConfig); }