public static importClassDefinitionFromJson ( $class, $json, $throwException = false ) : boolean | ||
$class | ||
$json | ||
return | boolean |
public function createClass($className) { $class = Object\ClassDefinition::getByName($className); if (!$class) { $result = Plugin::getEventManager()->trigger("install.class.getClass.{$className}", $this, array("className" => $className, "json" => $json), function ($v) { return $v instanceof Object\ClassDefinition; }); if ($result->stopped()) { return $result->last(); } $jsonFile = PIMCORE_PLUGINS_PATH . "/CoreShop/install/class-{$className}.json"; $class = Object\ClassDefinition::create(); $class->setName($className); $class->setUserOwner($this->_getUser()->getId()); $json = file_get_contents($jsonFile); $result = Plugin::getEventManager()->trigger('install.class.preCreate', $this, array("className" => $className, "json" => $json), function ($v) { return !preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/', preg_replace('/"(\\.|[^"\\\\])*"/', '', $v)); }); if ($result->stopped()) { $resultJson = $result->last(); if ($resultJson) { $json = $resultJson; } } Object\ClassDefinition\Service::importClassDefinitionFromJson($class, $json, true); return $class; } return $class; }
/** * @todo refactor steps into proper methods instead of doing it all in the action * @throws Exception */ public function indexAction() { // reachable via http://your.domain/plugin/Prototyper/admin/index $this->view->previewData = null; $this->view->csvText = $this->getParam('csv'); $this->view->classname = $this->getParam('classname'); $csvData = array(); $rowNr = 0; $rows = str_getcsv($this->view->csvText, "\n"); //parse the rows foreach ($rows as $row) { $rowNr++; $rowData = str_getcsv($row, ";"); $csvData[] = $rowData; if ($rowNr == 1) { $fieldNames = array(); $fieldTitles = array(); foreach ($rowData as $cell) { $fieldTitles[] = $cell; $fieldNames[] = $this->getFieldName($cell); } $csvData[] = $fieldNames; } if ($rowNr > 10) { break; } } $this->view->previewTable = $csvData; $fieldList = array(); foreach ($fieldNames as $fieldName) { $fieldTitle = array_shift($fieldTitles); ob_start(); include __DIR__ . "/../classes/field.json.php"; $fieldList[] = ob_get_clean(); } $fields = implode(',', $fieldList); ob_start(); include __DIR__ . "/../classes/object.json.php"; $jsonText = ob_get_clean(); if ($this->getParam('generate') != '') { $class = Object\ClassDefinition::getByName($this->correctClassname($this->getParam("classname"))); if ($class == null) { $class = Object\ClassDefinition::create(array('name' => $this->correctClassname($this->getParam("classname")), 'userOwner' => $this->user->getId())); $class->save(); } $class = Object\ClassDefinition::getById($class->getId()); $success = Object\ClassDefinition\Service::importClassDefinitionFromJson($class, $jsonText); if ($success) { $this->view->successMessage = '<strong>Class generation successful.</strong>'; } } }
/** * Process import * * @param AbstractModel $definition * @param string $json * @return bool */ protected function import(AbstractModel $definition, $json) { return Service::importClassDefinitionFromJson($definition, $json); }
/** * @param $classname * @param $filepath * @throws \Exception */ private static function createClass($classname, $filepath) { $class = \Pimcore\Model\Object\ClassDefinition::getByName($classname); if (!$class) { $class = new \Pimcore\Model\Object\ClassDefinition(); $class->setName($classname); } $json = file_get_contents($filepath); $success = \Pimcore\Model\Object\ClassDefinition\Service::importClassDefinitionFromJson($class, $json); if (!$success) { throw new \Exception("Could not import {$classname} 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]); }