/** * @param $type * @param $realName * @param array $options * @return Model\Document\Tag */ public function tag($type, $realName, $options = array()) { $type = strtolower($type); $document = $this->document; $name = Model\Document\Tag::buildTagName($type, $realName, $document); try { if ($document instanceof Model\Document\PageSnippet) { $tag = $document->getElement($name); if ($tag instanceof Model\Document\Tag && $tag->getType() == $type) { // call the load() method if it exists to reinitialize the data (eg. from serializing, ...) if (method_exists($tag, "load")) { $tag->load(); } // set view & controller, editmode $tag->setController($this->controller); $tag->setView($this); $tag->setEditmode($this->editmode); $tag->setOptions($options); } else { $tag = Model\Document\Tag::factory($type, $name, $document->getId(), $options, $this->controller, $this, $this->editmode); $document->setElement($name, $tag); } // set the real name of this editable, without the prefixes and suffixes from blocks and areablocks $tag->setRealName($realName); } return $tag; } catch (\Exception $e) { \Logger::warning($e); } }
/** * @param Model\Document $document * @throws \Zend_Json_Exception */ protected function addPropertiesToDocument(Model\Document $document) { // properties if ($this->getParam("properties")) { $properties = []; // assign inherited properties foreach ($document->getProperties() as $p) { if ($p->isInherited()) { $properties[$p->getName()] = $p; } } $propertiesData = \Zend_Json::decode($this->getParam("properties")); if (is_array($propertiesData)) { foreach ($propertiesData as $propertyName => $propertyData) { $value = $propertyData["data"]; try { $property = new Property(); $property->setType($propertyData["type"]); $property->setName($propertyName); $property->setCtype("document"); $property->setDataFromEditmode($value); $property->setInheritable($propertyData["inheritable"]); $properties[$propertyName] = $property; } catch (\Exception $e) { Logger::warning("Can't add " . $propertyName . " to document " . $document->getRealFullPath()); } } } if ($document->isAllowed("properties")) { $document->setProperties($properties); } } // force loading of properties $document->getProperties(); }
/** * @param $name */ public static function getByName($name) { try { $config = new self(); $config->setName($name); $config->getDao()->getByName(); return $config; } catch (\Exception $e) { Logger::warning($e); } }
/** * @return bool */ public function isAvailable() { try { $ghostscript = self::getGhostscriptCli(); $phpCli = Console::getPhpCli(); if ($ghostscript && $phpCli) { return true; } } catch (\Exception $e) { Logger::warning($e); } return false; }
/** * @return bool */ public function isAvailable() { try { $lo = self::getLibreOfficeCli(); if ($lo && parent::isAvailable()) { // LibreOffice and GhostScript is necessary return true; } } catch (\Exception $e) { Logger::warning($e); } return false; }
/** * @return bool */ public function isAvailable() { try { $ffmpeg = self::getFfmpegCli(); $phpCli = Console::getPhpCli(); if ($ffmpeg && $phpCli) { return true; } } catch (\Exception $e) { Logger::warning($e); } return false; }
/** * Get the data for the object from database for the given id * * @param integer $id * @return void */ public function getById($id) { try { $data = $this->db->fetchRow("SELECT objects.*, tree_locks.locked as o_locked FROM objects\n LEFT JOIN tree_locks ON objects.o_id = tree_locks.id AND tree_locks.type = 'object'\n WHERE o_id = ?", $id); if ($data["o_id"]) { $this->assignVariablesToModel($data); $this->getData(); } else { throw new \Exception("Object with the ID " . $id . " doesn't exists"); } } catch (\Exception $e) { Logger::warning($e); } }
public static function getDataFromEditmode($data, $pimcoreTagName) { $tagClass = '\\Pimcore\\Model\\Object\\ClassDefinition\\Data\\' . ucfirst($pimcoreTagName); if (\Pimcore\Tool::classExists($tagClass)) { /** * @var \Pimcore\Model\Object\ClassDefinition\Data $tag */ $tag = new $tagClass(); return $tag->getDataFromEditmode($data); } //purposely return null if there is no valid class, log a warning Logger::warning("No valid pimcore tag found for fieldType ({$pimcoreTagName}), check 'fieldType' exists, and 'type' is not being used in config"); return null; }
/** * @return bool */ public static function getDefaultAdapter() { $adapters = ["Ffmpeg"]; foreach ($adapters as $adapter) { $adapterClass = "\\Pimcore\\Video\\Adapter\\" . $adapter; if (Tool::classExists($adapterClass)) { try { $adapter = new $adapterClass(); if ($adapter->isAvailable()) { return $adapter; } } catch (\Exception $e) { Logger::warning($e); } } } return null; }
/** * Returns the children of the asset if the asset is a folder * * @return array */ public function getChildren() { $children = []; if ($this->asset->hasChilds()) { foreach ($this->asset->getChilds() as $child) { if ($child->isAllowed("view")) { try { if ($child = $this->getChild($child)) { $children[] = $child; } } catch (\Exception $e) { Logger::warning($e); } } } } return $children; }
/** * @return bool */ public static function getDefaultAdapter() { $adapters = array("LibreOffice", "Ghostscript"); foreach ($adapters as $adapter) { $adapterClass = "\\Pimcore\\Document\\Adapter\\" . $adapter; if (Tool::classExists($adapterClass)) { try { $adapter = new $adapterClass(); if ($adapter->isAvailable()) { return $adapter; } } catch (\Exception $e) { \Logger::warning($e); } } } return null; }
/** * @param Object\Concrete $object * @throws \Exception */ public function save(Object\Concrete $object) { // HACK: set the pimcore admin mode to false to get the inherited values from parent if this source one is empty $inheritedValues = Object\AbstractObject::doGetInheritedValues(); $storetable = $this->model->getDefinition()->getTableName($object->getClass(), false); $querytable = $this->model->getDefinition()->getTableName($object->getClass(), true); $this->inheritanceHelper = new Object\Concrete\Dao\InheritanceHelper($object->getClassId(), "o_id", $storetable, $querytable); Object\AbstractObject::setGetInheritedValues(false); $fieldDefinitions = $this->model->getDefinition()->getFieldDefinitions(); $data = []; $data["o_id"] = $object->getId(); $data["fieldname"] = $this->model->getFieldname(); // remove all relations try { $this->db->delete("object_relations_" . $object->getClassId(), "src_id = " . $object->getId() . " AND ownertype = 'objectbrick' AND ownername = '" . $this->model->getFieldname() . "' AND (position = '" . $this->model->getType() . "' OR position IS NULL OR position = '')"); } catch (\Exception $e) { Logger::warning("Error during removing old relations: " . $e); } foreach ($fieldDefinitions as $key => $fd) { $getter = "get" . ucfirst($fd->getName()); if (method_exists($fd, "save")) { // for fieldtypes which have their own save algorithm eg. objects, multihref, ... $fd->save($this->model); } elseif ($fd->getColumnType()) { if (is_array($fd->getColumnType())) { $insertDataArray = $fd->getDataForResource($this->model->{$getter}(), $object, ['context' => $this->model]); $data = array_merge($data, $insertDataArray); } else { $insertData = $fd->getDataForResource($this->model->{$getter}(), $object, ['context' => $this->model]); $data[$key] = $insertData; } } } $this->db->insertOrUpdate($storetable, $data); // get data for query table // $tableName = $this->model->getDefinition()->getTableName($object->getClass(), true); // this is special because we have to call each getter to get the inherited values from a possible parent object $data = []; $data["o_id"] = $object->getId(); $data["fieldname"] = $this->model->getFieldname(); $this->inheritanceHelper->resetFieldsToCheck(); $oldData = $this->db->fetchRow("SELECT * FROM " . $querytable . " WHERE o_id = ?", $object->getId()); $inheritanceEnabled = $object->getClass()->getAllowInherit(); $parentData = null; if ($inheritanceEnabled) { // get the next suitable parent for inheritance $parentForInheritance = $object->getNextParentForInheritance(); if ($parentForInheritance) { // we don't use the getter (built in functionality to get inherited values) because we need to avoid race conditions // we cannot Object\AbstractObject::setGetInheritedValues(true); and then $this->model->$method(); // so we select the data from the parent object using FOR UPDATE, which causes a lock on this row // so the data of the parent cannot be changed while this transaction is on progress $parentData = $this->db->fetchRow("SELECT * FROM " . $querytable . " WHERE o_id = ? FOR UPDATE", $parentForInheritance->getId()); } } foreach ($fieldDefinitions as $key => $fd) { if ($fd->getQueryColumnType()) { //exclude untouchables if value is not an array - this means data has not been loaded $method = "get" . $key; $fieldValue = $this->model->{$method}(); $insertData = $fd->getDataForQueryResource($fieldValue, $object); $isEmpty = $fd->isEmpty($fieldValue); if (is_array($insertData)) { $columnNames = array_keys($insertData); $data = array_merge($data, $insertData); } else { $columnNames = [$key]; $data[$key] = $insertData; } // if the current value is empty and we have data from the parent, we just use it if ($isEmpty && $parentData) { foreach ($columnNames as $columnName) { if (array_key_exists($columnName, $parentData)) { $data[$columnName] = $parentData[$columnName]; if (is_array($insertData)) { $insertData[$columnName] = $parentData[$columnName]; } else { $insertData = $parentData[$columnName]; } } } } if ($inheritanceEnabled) { //get changed fields for inheritance if ($fd instanceof Object\ClassDefinition\Data\CalculatedValue) { // nothing to do, see https://github.com/pimcore/pimcore/issues/727 continue; } elseif ($fd->isRelationType()) { if (is_array($insertData)) { $doInsert = false; foreach ($insertData as $insertDataKey => $insertDataValue) { if ($isEmpty && $oldData[$insertDataKey] == $parentData[$insertDataKey]) { // do nothing, ... value is still empty and parent data is equal to current data in query table } elseif ($oldData[$insertDataKey] != $insertDataValue) { $doInsert = true; break; } } if ($doInsert) { $this->inheritanceHelper->addRelationToCheck($key, $fd, array_keys($insertData)); } } else { if ($isEmpty && $oldData[$key] == $parentData[$key]) { // do nothing, ... value is still empty and parent data is equal to current data in query table } elseif ($oldData[$key] != $insertData) { $this->inheritanceHelper->addRelationToCheck($key, $fd); } } } else { if (is_array($insertData)) { foreach ($insertData as $insertDataKey => $insertDataValue) { if ($isEmpty && $oldData[$insertDataKey] == $parentData[$insertDataKey]) { // do nothing, ... value is still empty and parent data is equal to current data in query table } elseif ($oldData[$insertDataKey] != $insertDataValue) { $this->inheritanceHelper->addFieldToCheck($insertDataKey, $fd); } } } else { if ($isEmpty && $oldData[$key] == $parentData[$key]) { // do nothing, ... value is still empty and parent data is equal to current data in query table } elseif ($oldData[$key] != $insertData) { // data changed, do check and update $this->inheritanceHelper->addFieldToCheck($key, $fd); } } } } } } $this->db->insertOrUpdate($querytable, $data); if ($inheritanceEnabled) { $this->inheritanceHelper->doUpdate($object->getId(), true); } $this->inheritanceHelper->resetFieldsToCheck(); // HACK: see a few lines above! Object\AbstractObject::setGetInheritedValues($inheritedValues); }
/** * @param string $id * @param array $tags * @return void */ protected function saveTags($id, $tags) { //$this->getDb()->beginTransaction(); try { while ($tag = array_shift($tags)) { try { $this->getDb()->insertOrUpdate("cache_tags", ["id" => $id, "tag" => $tag]); } catch (\Exception $e) { if (strpos(strtolower($e->getMessage()), "is full") !== false) { Logger::warning($e); if ($this->_options["tags_do_not_switch_to_innodb"]) { $this->clean(); } else { // it seems that the MEMORY table is on the limit an full // change the storage engine of the cache tags table to InnoDB $this->getDb()->query("ALTER TABLE `cache_tags` ENGINE=InnoDB"); } // try it again $tags[] = $tag; } else { // it seems that the item does already exist throw $e; } } } //$this->getDb()->commit(); } catch (\Exception $e) { Logger::error($e); } }
/** * @param string $path * @return self */ public static function getByPath($path) { $path = Model\Element\Service::correctPath($path); try { $object = new self(); if (Tool::isValidPath($path)) { $object->getDao()->getByPath($path); return self::getById($object->getId()); } } catch (\Exception $e) { Logger::warning($e->getMessage()); } return null; }
/** * Static helper to get an asset by the passed ID * @param integer $id * @return Asset|Asset\Archive|Asset\Audio|Asset\Document|Asset\Folder|Asset\Image|Asset\Text|Asset\Unknown|Asset\Video */ public static function getById($id) { $id = intval($id); if ($id < 1) { return null; } $cacheKey = "asset_" . $id; try { $asset = \Zend_Registry::get($cacheKey); if (!$asset) { throw new \Exception("Asset in registry is null"); } } catch (\Exception $e) { try { if (!($asset = \Pimcore\Cache::load($cacheKey))) { $asset = new Asset(); $asset->getDao()->getById($id); $className = "Pimcore\\Model\\Asset\\" . ucfirst($asset->getType()); $asset = \Pimcore::getDiContainer()->make($className); \Zend_Registry::set($cacheKey, $asset); $asset->getDao()->getById($id); \Pimcore\Cache::save($asset, $cacheKey); } else { \Zend_Registry::set($cacheKey, $asset); } } catch (\Exception $e) { Logger::warning($e->getMessage()); return null; } } if (!$asset) { return null; } return $asset; }
/** * @param $method * @param $args * @param $exception * @param bool $logError * @throws \Exception */ public static function errorHandler($method, $args, $exception, $logError = true) { if ($logError) { \Logger::error($exception); \Logger::error(array("message" => $exception->getMessage(), "method" => $method, "arguments" => $args)); } $lowerErrorMessage = strtolower($exception->getMessage()); // check if the mysql-connection is the problem (timeout issues, ...) if (strpos($lowerErrorMessage, "mysql server has gone away") !== false || strpos($lowerErrorMessage, "lost connection") !== false) { // wait a few seconds sleep(5); // the connection to the server has probably been lost, try to reconnect and call the method again try { \Logger::warning("The connection to the MySQL-Server has probably been lost, try to reconnect..."); self::reset(); \Logger::warning("Reconnecting to the MySQL-Server was successful, sending the command again to the server."); $r = self::get()->callResourceMethod($method, $args); \Logger::warning("Resending the command was successful"); return $r; } catch (\Exception $e) { \Logger::error($e); throw $e; } } // no handling throw it again throw $exception; }
/** * Static helper to get a Document by it's ID * @param integer $id * @return Document|Document\Email|Document\Folder|Document\Hardlink|Document\Link|Document\Page|Document\Printcontainer|Document\Printpage|Document\Snippet|Document\Newsletter */ public static function getById($id) { $id = intval($id); if ($id < 1) { return null; } $cacheKey = "document_" . $id; try { $document = \Zend_Registry::get($cacheKey); if (!$document) { throw new \Exception("Document in registry is null"); } } catch (\Exception $e) { try { if (!($document = \Pimcore\Cache::load($cacheKey))) { $document = new Document(); $document->getDao()->getById($id); $className = "Pimcore\\Model\\Document\\" . ucfirst($document->getType()); // this is the fallback for custom document types using prefixes // so we need to check if the class exists first if (!Tool::classExists($className)) { $oldStyleClass = "Document_" . ucfirst($document->getType()); if (Tool::classExists($oldStyleClass)) { $className = $oldStyleClass; } } $document = \Pimcore::getDiContainer()->make($className); \Zend_Registry::set($cacheKey, $document); $document->getDao()->getById($id); \Pimcore\Cache::save($document, $cacheKey); } else { \Zend_Registry::set($cacheKey, $document); } } catch (\Exception $e) { Logger::warning($e->getMessage()); return null; } } if (!$document) { return null; } return $document; }
/** * Shows the email logs and returns the Json object for the dynamic params */ public function showEmailLogAction() { if (!$this->getUser()->isAllowed("emails")) { throw new \Exception("Permission denied, user needs 'emails' permission."); } $type = $this->getParam('type'); $emailLog = Tool\Email\Log::getById($this->getParam('id')); if ($this->getParam('type') == 'text') { $this->disableViewAutoRender(); echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style>body{background-color:#fff;}</style></head><body><pre>' . $emailLog->getTextLog() . '</pre></body></html>'; } elseif ($this->getParam('type') == 'html') { $this->disableViewAutoRender(); echo $emailLog->getHtmlLog(); } elseif ($this->getParam('type') == 'params') { $this->disableViewAutoRender(); try { $params = \Zend_Json::decode($emailLog->getParams()); } catch (\Exception $e) { Logger::warning("Could not decode JSON param string"); $params = []; } foreach ($params as &$entry) { $this->enhanceLoggingData($entry); } $this->_helper->json($params); } else { die('No Type specified'); } }
/** * gets workflow config for element. always returns first valid workflow config * * @param Asset|Document|ConcreteObject $element * @return array */ public static function getElementWorkflowConfig(AbstractElement $element) { $config = self::getWorkflowManagementConfig(); if (!is_array($config)) { return null; } $elementType = Service::getElementType($element); $elementSubType = $element->getType(); foreach ($config['workflows'] as $workflow) { //workflow is not enabled, continue with next if (isset($workflow['enabled']) && !$workflow['enabled']) { continue; } if (isset($workflow['workflowSubject']) && in_array($elementType, $workflow['workflowSubject']['types'])) { switch ($elementType) { case 'asset': if (isset($workflow['workflowSubject']['assetTypes']) && is_array($workflow['workflowSubject']['assetTypes'])) { if (in_array($elementSubType, $workflow['workflowSubject']['assetTypes'])) { return $workflow; } } else { Logger::warning('WorkflowManagement::getClassWorkflowConfig workflow does not feature a valid array of available asset types'); } break; case 'document': if (isset($workflow['workflowSubject']['documentTypes']) && is_array($workflow['workflowSubject']['documentTypes'])) { if (in_array($elementSubType, $workflow['workflowSubject']['documentTypes'])) { return $workflow; } } else { Logger::warning('WorkflowManagement::getClassWorkflowConfig workflow does not feature a valid array of available document types'); } break; case 'object': if ($element instanceof ConcreteObject) { if (isset($workflow['workflowSubject']['classes']) && is_array($workflow['workflowSubject']['classes'])) { $classId = $element->getClassId(); if (in_array($classId, $workflow['workflowSubject']['classes'])) { return $workflow; } } else { Logger::warning('WorkflowManagement::getClassWorkflowConfig workflow does not feature a valid array of available class ID\'s'); } } break; default: //unknown element type, return null return null; } } } return null; }
/** * Set raw data of an element (eg. for editmode) * * @param string $name * @param string $type * @param string $data * @return $this */ public function setRawElement($name, $type, $data) { try { if ($type) { $class = "\\Pimcore\\Model\\Document\\Tag\\" . ucfirst($type); // this is the fallback for custom document tags using prefixes // so we need to check if the class exists first if (!\Pimcore\Tool::classExists($class)) { $oldStyleClass = "\\Document_Tag_" . ucfirst($type); if (\Pimcore\Tool::classExists($oldStyleClass)) { $class = $oldStyleClass; } } $this->elements[$name] = new $class(); $this->elements[$name]->setDataFromEditmode($data); $this->elements[$name]->setName($name); $this->elements[$name]->setDocumentId($this->getId()); } } catch (\Exception $e) { Logger::warning("can't set element " . $name . " with the type " . $type . " to the document: " . $this->getRealFullPath()); } return $this; }