/** * @param Object\Concrete $object * @return array */ public function load(Object\Concrete $object) { $fieldDef = $object->getClass()->getFieldDefinition($this->model->getFieldname()); $values = array(); foreach ($fieldDef->getAllowedTypes() as $type) { try { $definition = Object\Objectbrick\Definition::getByKey($type); } catch (\Exception $e) { continue; } $tableName = $definition->getTableName($object->getClass(), false); try { $results = $this->db->fetchAll("SELECT * FROM " . $tableName . " WHERE o_id = ? AND fieldname = ?", array($object->getId(), $this->model->getFieldname())); } catch (\Exception $e) { $results = array(); } //$allRelations = $this->db->fetchAll("SELECT * FROM object_relations_" . $object->getO_classId() . " WHERE src_id = ? AND ownertype = 'objectbrick' AND ownername = ?", array($object->getO_id(), $this->model->getFieldname())); $fieldDefinitions = $definition->getFieldDefinitions(); $brickClass = "\\Pimcore\\Model\\Object\\Objectbrick\\Data\\" . ucfirst($type); foreach ($results as $result) { $brick = new $brickClass($object); $brick->setFieldname($result["fieldname"]); $brick->setObject($object); foreach ($fieldDefinitions as $key => $fd) { if (method_exists($fd, "load")) { // datafield has it's own loader $value = $fd->load($brick); if ($value === 0 || !empty($value)) { $brick->setValue($key, $value); } } else { if (is_array($fd->getColumnType())) { $multidata = array(); foreach ($fd->getColumnType() as $fkey => $fvalue) { $multidata[$key . "__" . $fkey] = $result[$key . "__" . $fkey]; } $brick->setValue($key, $fd->getDataFromResource($multidata)); } else { $brick->setValue($key, $fd->getDataFromResource($result[$key])); } } } $setter = "set" . ucfirst($type); $this->model->{$setter}($brick); $values[] = $brick; } } return $values; }
/** * Check if Plugin is installed * * @return bool */ public static function isInstalled() { $p = PIMCORE_PLUGINS_PATH . '/CoreShop/plugin.xml'; if (!file_exists($p)) { return false; } $config = new \Zend_Config_Xml($p); if ((int) $config->plugin->pluginRevision < self::$requiredCoreShopBuild) { return false; } try { \Pimcore\Model\Object\Objectbrick\Definition::getByKey("CoreShopPaymentPaypal"); return true; } catch (\Exception $e) { } return false; }
/** * @throws \Exception * @param Object\Concrete $object * @return void */ public function delete(Object\Concrete $object) { // this is to clean up also the inherited values $fieldDef = $object->getClass()->getFieldDefinition($this->model->getFieldname()); foreach ($fieldDef->getAllowedTypes() as $type) { try { $definition = Object\Objectbrick\Definition::getByKey($type); } catch (\Exception $e) { continue; } $tableName = $definition->getTableName($object->getClass(), true); $this->db->delete($tableName, "o_id = " . $object->getId()); } }
/** * Create a new definition * * @param $name * @return AbstractModel */ protected function createDefinition($name) { $definition = new Definition(); $definition->setKey($name); return $definition; }
<?php $c++; } ?> <?php } ?> <?php } else { if ($definition instanceof Object\ClassDefinition\Data\ObjectBricks) { ?> <?php foreach ($definition->getAllowedTypes() as $asAllowedType) { ?> <?php $collectionDef = Object\Objectbrick\Definition::getByKey($asAllowedType); foreach ($collectionDef->getFieldDefinitions() as $lfd) { ?> <?php $v1 = null; $bricks1 = $this->object1->{"get" . ucfirst($fieldName)}(); if ($bricks1) { $brick1Value = $bricks1->{"get" . $asAllowedType}(); if ($brick1Value) { $v1 = $lfd->getVersionPreview($brick1Value->getValueForFieldName($lfd->getName())); } } $v2 = null; $bricks2 = $this->object2->{"get" . ucfirst($fieldName)}(); if ($bricks2) { $brick2Value = $bricks2->{"get" . $asAllowedType}();
/** * @todo */ private static function installObjectBricks() { $sourceFiles = scandir(PIMCORE_PLUGINS_PATH . '/OnlineShop/install/objectbrick_sources'); foreach ($sourceFiles as $filename) { if (!is_dir($filename)) { preg_match('/_(.*)_/', $filename, $matches); $key = $matches[1]; try { $brick = \Pimcore\Model\Object\Objectbrick\Definition::getByKey($key); } catch (Exception $e) { $brick = new \Pimcore\Model\Object\Objectbrick\Definition(); $brick->setKey($key); } $data = file_get_contents(PIMCORE_PLUGINS_PATH . '/OnlineShop/install/objectbrick_sources/' . $filename); $success = \Pimcore\Model\Object\ClassDefinition\Service::importObjectBrickFromJson($brick, $data); if (!$success) { Logger::err("Could not import {$key} ObjectBrick."); } } } }
/** * @return mixed */ public function getDefinition() { $definition = Object\Objectbrick\Definition::getByKey($this->getType()); return $definition; }
/** * @param $type * @throws \Exception */ public function addObjectbrick($type) { if (empty($type)) { throw new \Exception("No objectbrick given"); } Object\Objectbrick\Definition::getByKey($type); if (!in_array($type, $this->objectBrickConfigs)) { $this->objectBrickConfigs[] = $type; } }
/** * * @param string $filterJson * @param ClassDefinition $class * @return string */ public static function getFilterCondition($filterJson, $class) { $systemFields = array("o_path", "o_key", "o_id", "o_published", "o_creationDate", "o_modificationDate", "o_fullpath"); // create filter condition $conditionPartsFilters = array(); if ($filterJson) { $db = \Pimcore\Db::get(); $filters = \Zend_Json::decode($filterJson); foreach ($filters as $filter) { $operator = "="; /** * @extjs */ $filterField = $filter["field"]; $filterOperator = $filter["comparison"]; if (\Pimcore\Tool\Admin::isExtJS6()) { $filterField = $filter["property"]; $filterOperator = $filter["operator"]; } if ($filter["type"] == "string") { $operator = "LIKE"; } else { if ($filter["type"] == "numeric") { if ($filterOperator == "lt") { $operator = "<"; } else { if ($filterOperator == "gt") { $operator = ">"; } else { if ($filterOperator == "eq") { $operator = "="; } } } } else { if ($filter["type"] == "date") { if ($filterOperator == "lt") { $operator = "<"; } else { if ($filterOperator == "gt") { $operator = ">"; } else { if ($filterOperator == "eq") { $operator = "="; } } } $filter["value"] = strtotime($filter["value"]); } else { if ($filter["type"] == "list") { $operator = "="; } else { if ($filter["type"] == "boolean") { $operator = "="; $filter["value"] = (int) $filter["value"]; } } } } } $field = $class->getFieldDefinition($filterField); $brickField = null; $brickType = null; if (!$field) { // if the definition doesn't exist check for a localized field $localized = $class->getFieldDefinition("localizedfields"); if ($localized instanceof ClassDefinition\Data\Localizedfields) { $field = $localized->getFieldDefinition($filterField); } //if the definition doesn't exist check for object brick $keyParts = explode("~", $filterField); if (substr($filterField, 0, 1) == "~") { // not needed for now // $type = $keyParts[1]; // $field = $keyParts[2]; // $keyid = $keyParts[3]; } else { if (count($keyParts) > 1) { $brickType = $keyParts[0]; $brickKey = $keyParts[1]; $key = self::getFieldForBrickType($class, $brickType); $field = $class->getFieldDefinition($key); $brickClass = Objectbrick\Definition::getByKey($brickType); $brickField = $brickClass->getFieldDefinition($brickKey); } } } if ($field instanceof ClassDefinition\Data\Objectbricks) { // custom field $db = \Pimcore\Db::get(); if (is_array($filter["value"])) { $fieldConditions = array(); foreach ($filter["value"] as $filterValue) { $fieldConditions[] = $db->getQuoteIdentifierSymbol() . $brickType . $db->getQuoteIdentifierSymbol() . "." . $brickField->getFilterCondition($filterValue, $operator); } $conditionPartsFilters[] = "(" . implode(" OR ", $fieldConditions) . ")"; } else { $conditionPartsFilters[] = $db->getQuoteIdentifierSymbol() . $brickType . $db->getQuoteIdentifierSymbol() . "." . $brickField->getFilterCondition($filter["value"], $operator); } } else { if ($field instanceof ClassDefinition\Data) { // custom field if (is_array($filter["value"])) { $fieldConditions = array(); foreach ($filter["value"] as $filterValue) { $fieldConditions[] = $field->getFilterCondition($filterValue, $operator); } $conditionPartsFilters[] = "(" . implode(" OR ", $fieldConditions) . ")"; } else { $conditionPartsFilters[] = $field->getFilterCondition($filter["value"], $operator); } } else { if (in_array("o_" . $filterField, $systemFields)) { // system field if ($filterField == "fullpath") { $conditionPartsFilters[] = "concat(o_path, o_key) " . $operator . " " . $db->quote("%" . $filter["value"] . "%"); } else { $conditionPartsFilters[] = "`o_" . $filterField . "` " . $operator . " " . $db->quote($filter["value"]); } } } } } } $conditionFilters = "1 = 1"; if (count($conditionPartsFilters) > 0) { $conditionFilters = "(" . implode(" AND ", $conditionPartsFilters) . ")"; } \Logger::log("ObjectController filter condition:" . $conditionFilters); return $conditionFilters; }
/** end point for the object-brick definition * GET http://[YOUR-DOMAIN]/webservice/rest/object-brick/id/abt1?apikey=[API-KEY] * returns the class definition for the given class * */ public function objectBrickAction() { $this->checkUserPermission("classes"); try { $fc = Object\Objectbrick\Definition::getByKey($this->getParam("id")); $this->_helper->json(array("success" => true, "data" => $fc)); } catch (\Exception $e) { \Logger::error($e); $this->encoder->encode(array("success" => false, "msg" => (string) $e)); } $this->encoder->encode(array("success" => false)); }
public function batchAction() { $success = true; try { $object = Object::getById($this->getParam("job")); if ($object) { $className = $object->getClassName(); $class = Object\ClassDefinition::getByName($className); $value = $this->getParam("value"); if ($this->getParam("valueType") == "object") { $value = \Zend_Json::decode($value); } $name = $this->getParam("name"); $parts = explode("~", $name); if (substr($name, 0, 1) == "~") { $type = $parts[1]; $field = $parts[2]; $keyid = $parts[3]; $getter = "get" . ucfirst($field); $setter = "set" . ucfirst($field); $keyValuePairs = $object->{$getter}(); if (!$keyValuePairs) { $keyValuePairs = new Object\Data\KeyValue(); $keyValuePairs->setObjectId($object->getId()); $keyValuePairs->setClass($object->getClass()); } $keyValuePairs->setPropertyWithId($keyid, $value, true); $object->{$setter}($keyValuePairs); } else { if (count($parts) > 1) { // check for bricks $brickType = $parts[0]; $brickKey = $parts[1]; $brickField = Object\Service::getFieldForBrickType($object->getClass(), $brickType); $fieldGetter = "get" . ucfirst($brickField); $brickGetter = "get" . ucfirst($brickType); $valueSetter = "set" . ucfirst($brickKey); $brick = $object->{$fieldGetter}()->{$brickGetter}(); if (empty($brick)) { $classname = "\\Pimcore\\Model\\Object\\Objectbrick\\Data\\" . ucfirst($brickType); $brickSetter = "set" . ucfirst($brickType); $brick = new $classname($object); $object->{$fieldGetter}()->{$brickSetter}($brick); } $brickClass = Object\Objectbrick\Definition::getByKey($brickType); $field = $brickClass->getFieldDefinition($brickKey); $brick->{$valueSetter}($field->getDataFromEditmode($value, $object)); } else { // everything else $field = $class->getFieldDefinition($name); if ($field) { $object->setValue($name, $field->getDataFromEditmode($value, $object)); } else { // check if it is a localized field if ($this->getParam("language")) { $localizedField = $class->getFieldDefinition("localizedfields"); if ($localizedField) { $field = $localizedField->getFieldDefinition($name); if ($field) { $object->{"set" . $name}($value, $this->getParam("language")); } } } // seems to be a system field, this is actually only possible for the "published" field yet if ($name == "published") { if ($value == "false" || empty($value)) { $object->setPublished(false); } else { $object->setPublished(true); } } } } } try { // don't check for mandatory fields here $object->setOmitMandatoryCheck(true); $object->setUserModification($this->getUser()->getId()); $object->save(); $success = true; } catch (\Exception $e) { $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } } else { \Logger::debug("ObjectController::batchAction => There is no object left to update."); $this->_helper->json(array("success" => false, "message" => "ObjectController::batchAction => There is no object left to update.")); } } catch (\Exception $e) { \Logger::err($e); $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } $this->_helper->json(array("success" => $success)); }
public function getNicePathAction() { $source = \Zend_Json::decode($this->getParam("source")); if ($source["type"] != "object") { throw new \Exception("currently only objects as source elements are supported"); } $result = []; $id = $source["id"]; $source = Object\Concrete::getById($id); if ($this->getParam("context")) { $context = \Zend_Json::decode($this->getParam("context")); } else { $context = []; } $ownerType = $context["containerType"]; $fieldname = $context["fieldname"]; if ($ownerType == "object") { $fd = $source->getClass()->getFieldDefinition($fieldname); } elseif ($ownerType == "localizedfield") { $fd = $source->getClass()->getFieldDefinition("localizedfields")->getFieldDefinition($fieldname); } elseif ($ownerType == "objectbrick") { $fdBrick = Object\Objectbrick\Definition::getByKey($context["containerKey"]); $fd = $fdBrick->getFieldDefinition($fieldname); } elseif ($ownerType == "fieldcollection") { $containerKey = $context["containerKey"]; $fdCollection = Object\Fieldcollection\Definition::getByKey($containerKey); if ($context["subContainerType"] == "localizedfield") { $fdLocalizedFields = $fdCollection->getFieldDefinition("localizedfields"); $fd = $fdLocalizedFields->getFieldDefinition($fieldname); } else { $fd = $fdCollection->getFieldDefinition($fieldname); } } if (method_exists($fd, "getPathFormatterClass")) { $formatterClass = $fd->getPathFormatterClass(); if (Pimcore\Tool::classExists($formatterClass)) { $targets = \Zend_Json::decode($this->getParam("targets")); $result = call_user_func($formatterClass . "::formatPath", $result, $source, $targets, ["fd" => $fd, "context" => $context]); } else { Logger::error("Formatter Class does not exist: " . $formatterClass); } } $this->_helper->json(["success" => true, "data" => $result]); }
public function removeObjectBrick($name) { try { $brick = Object\Objectbrick\Definition::getByKey($name); if ($brick) { $brick->delete(); } } catch (\Exception $e) { return false; } return true; }
/** * Rewrites id from source to target, $idMapping contains * array( * "document" => array( * SOURCE_ID => TARGET_ID, * SOURCE_ID => TARGET_ID * ), * "object" => array(...), * "asset" => array(...) * ) * @param mixed $object * @param array $idMapping * @param array $params * @return Element\ElementInterface */ public function rewriteIds($object, $idMapping, $params = array()) { $data = $this->getDataFromObjectParam($object, $params); if ($data instanceof Object\Objectbrick) { $items = $data->getItems(); foreach ($items as $item) { if (!$item instanceof Object\Objectbrick\Data\AbstractData) { continue; } try { $collectionDef = Object\Objectbrick\Definition::getByKey($item->getType()); } catch (\Exception $e) { continue; } foreach ($collectionDef->getFieldDefinitions() as $fd) { if (method_exists($fd, "rewriteIds")) { $d = $fd->rewriteIds($item, $idMapping, $params); $setter = "set" . ucfirst($fd->getName()); $item->{$setter}($d); } } } } return $data; }
/** * This method is called in Object|Class::save() and is used to create the database table for the localized data * @return void */ public function classSaved($class) { if (is_array($this->allowedTypes)) { foreach ($this->allowedTypes as $allowedType) { try { $definition = Object\Objectbrick\Definition::getByKey($allowedType); } catch (\Exception $e) { \Logger::info("Unknown allowed type [ {$allowedType} ] ignored."); } if ($definition) { $fieldDefinition = $definition->getFieldDefinitions(); foreach ($fieldDefinition as $fd) { if (method_exists($fd, "classSaved")) { $fd->classSaved($class); } } } } } }
/** * This method is called in Object|Class::save() and is used to create the database table for the localized data * @return void */ public function classSaved($class) { if (is_array($this->allowedTypes)) { foreach ($this->allowedTypes as $allowedType) { $definition = Object\Objectbrick\Definition::getByKey($allowedType); if ($definition) { $fieldDefinition = $definition->getFieldDefinitions(); foreach ($fieldDefinition as $fd) { if (method_exists($fd, "classSaved")) { $fd->classSaved($class); } } } } } }
/** * See http://www.pimcore.org/issues/browse/PIMCORE-2358 * Add option to export/import all class definitions/brick definitions etc. at once */ public function bulkCommitAction() { $filename = $this->getParam("filename"); $data = json_decode($this->getParam("data"), true); $json = @file_get_contents($filename); $json = json_decode($json, true); $type = $data["type"]; $name = $data["name"]; $list = $json[$type]; foreach ($list as $item) { unset($item["creationDate"]); unset($item["modificationDate"]); unset($item["userOwner"]); unset($item["userModification"]); unset($item["id"]); if ($type == "class" && $item["name"] == $name) { $class = Object\ClassDefinition::getByName($name); if (!$class) { $class = new Object\ClassDefinition(); $class->setName($name); } $success = Object\ClassDefinition\Service::importClassDefinitionFromJson($class, json_encode($item), true); $this->_helper->json(["success" => $success !== false]); } elseif ($type == "objectbrick" && $item["key"] == $name) { try { $brick = Object\Objectbrick\Definition::getByKey($name); } catch (\Exception $e) { $brick = new Object\Objectbrick\Definition(); $brick->setKey($name); } $success = Object\ClassDefinition\Service::importObjectBrickFromJson($brick, json_encode($item), true); $this->_helper->json(["success" => $success !== false]); } elseif ($type == "fieldcollection" && $item["key"] == $name) { try { $fieldCollection = Object\Fieldcollection\Definition::getByKey($name); } catch (\Exception $e) { $fieldCollection = new Object\Fieldcollection\Definition(); $fieldCollection->setKey($name); } $success = Object\ClassDefinition\Service::importFieldCollectionFromJson($fieldCollection, json_encode($item), true); $this->_helper->json(["success" => $success !== false]); } elseif ($type == "customlayout") { $layoutData = unserialize($data["name"]); $className = $layoutData["className"]; $layoutName = $layoutData["name"]; if ($item["name"] == $layoutName && $item["className"] == $className) { $class = Object\ClassDefinition::getByName($className); if (!$class) { throw new \Exception("Class does not exist"); } $classId = $class->getId(); $layoutList = new Object\ClassDefinition\CustomLayout\Listing(); $db = \Pimcore\Db::get(); $layoutList->setCondition("name = " . $db->quote($layoutName) . " AND classId = " . $classId); $layoutList = $layoutList->load(); $layoutDefinition = null; if ($layoutList) { $layoutDefinition = $layoutList[0]; } if (!$layoutDefinition) { $layoutDefinition = new Object\ClassDefinition\CustomLayout(); $layoutDefinition->setName($layoutName); $layoutDefinition->setClassId($classId); } try { $layoutDefinition->setDescription($item["description"]); $layoutDef = Object\ClassDefinition\Service::generateLayoutTreeFromArray($item["layoutDefinitions"], true); $layoutDefinition->setLayoutDefinitions($layoutDef); $layoutDefinition->save(); } catch (\Exception $e) { Logger::error($e->getMessage()); $this->_helper->json(["success" => false, "message" => $e->getMessage()]); } } } } $this->_helper->json(["success" => true]); }