public function save()
{
$isUpdate = false;
if ($this->getId()) {
$isUpdate = true;
\Pimcore::getEventManager()->trigger("object.class.preUpdate", $this);
} else {
\Pimcore::getEventManager()->trigger("object.class.preAdd", $this);
}
$this->setModificationDate(time());
$this->getDao()->save();
$infoDocBlock = $this->getInfoDocBlock();
// save definition as a php file
$definitionFile = $this->getDefinitionFile();
if (!is_writable(dirname($definitionFile)) || is_file($definitionFile) && !is_writable($definitionFile)) {
throw new \Exception("Cannot write definition file in: " . $definitionFile . " please check write permission on this directory.");
}
$clone = clone $this;
$clone->setDao(null);
unset($clone->id);
unset($clone->fieldDefinitions);
self::cleanupForExport($clone->layoutDefinitions);
$exportedClass = var_export($clone, true);
$data = '<?php ';
$data .= "\n\n";
$data .= $infoDocBlock;
$data .= "\n\n";
$data .= "\nreturn " . $exportedClass . ";\n";
\Pimcore\File::put($definitionFile, $data);
// create class for object
$extendClass = "Concrete";
if ($this->getParentClass()) {
$extendClass = $this->getParentClass();
$extendClass = "\\" . ltrim($extendClass, "\\");
}
// create directory if not exists
if (!is_dir(PIMCORE_CLASS_DIRECTORY . "/Object")) {
File::mkdir(PIMCORE_CLASS_DIRECTORY . "/Object");
}
$cd = '<?php ';
$cd .= "\n\n";
$cd .= $infoDocBlock;
$cd .= "\n\n";
$cd .= "namespace Pimcore\\Model\\Object;";
$cd .= "\n\n";
$cd .= "\n\n";
$cd .= "/**\n";
if (is_array($this->getFieldDefinitions()) && count($this->getFieldDefinitions())) {
foreach ($this->getFieldDefinitions() as $key => $def) {
if (!(method_exists($def, "isRemoteOwner") and $def->isRemoteOwner())) {
if ($def instanceof Object\ClassDefinition\Data\Localizedfields) {
$cd .= "* @method static \\Pimcore\\Model\\Object\\" . ucfirst($this->getName()) . '\\Listing getBy' . ucfirst($def->getName()) . ' ($field, $value, $locale = null, $limit = 0) ' . "\n";
} else {
$cd .= "* @method static \\Pimcore\\Model\\Object\\" . ucfirst($this->getName()) . '\\Listing getBy' . ucfirst($def->getName()) . ' ($value, $limit = 0) ' . "\n";
}
}
}
}
$cd .= "*/\n\n";
$cd .= "class " . ucfirst($this->getName()) . " extends " . $extendClass . " {";
$cd .= "\n\n";
if ($this->getUseTraits()) {
$cd .= 'use ' . $this->getUseTraits() . ";\n";
$cd .= "\n";
}
$cd .= 'public $o_classId = ' . $this->getId() . ";\n";
$cd .= 'public $o_className = "' . $this->getName() . '"' . ";\n";
if (is_array($this->getFieldDefinitions()) && count($this->getFieldDefinitions())) {
foreach ($this->getFieldDefinitions() as $key => $def) {
if (!(method_exists($def, "isRemoteOwner") && $def->isRemoteOwner()) && !$def instanceof Object\ClassDefinition\Data\CalculatedValue) {
$cd .= "public \$" . $key . ";\n";
}
}
}
$cd .= "\n\n";
$cd .= '/**' . "\n";
$cd .= '* @param array $values' . "\n";
$cd .= '* @return \\Pimcore\\Model\\Object\\' . ucfirst($this->getName()) . "\n";
$cd .= '*/' . "\n";
$cd .= 'public static function create($values = array()) {';
$cd .= "\n";
$cd .= "\t" . '$object = new static();' . "\n";
$cd .= "\t" . '$object->setValues($values);' . "\n";
$cd .= "\t" . 'return $object;' . "\n";
$cd .= "}";
$cd .= "\n\n";
if (is_array($this->getFieldDefinitions()) && count($this->getFieldDefinitions())) {
$relationTypes = [];
foreach ($this->getFieldDefinitions() as $key => $def) {
if (method_exists($def, "isRemoteOwner") and $def->isRemoteOwner()) {
continue;
}
// get setter and getter code
$cd .= $def->getGetterCode($this);
$cd .= $def->getSetterCode($this);
// call the method "classSaved" if exists, this is used to create additional data tables or whatever which depends on the field definition, for example for localizedfields
if (method_exists($def, "classSaved")) {
$def->classSaved($this);
}
if ($def->isRelationType()) {
$relationTypes[$key] = ["type" => $def->getFieldType()];
}
// collect lazyloaded fields
if (method_exists($def, "getLazyLoading") and $def->getLazyLoading()) {
$lazyLoadedFields[] = $key;
}
}
$cd .= 'protected static $_relationFields = ' . var_export($relationTypes, true) . ";\n\n";
$cd .= 'public $lazyLoadedFields = ' . var_export($lazyLoadedFields, true) . ";\n\n";
}
$cd .= "}\n";
$cd .= "\n";
$classFile = PIMCORE_CLASS_DIRECTORY . "/Object/" . ucfirst($this->getName()) . ".php";
if (!is_writable(dirname($classFile)) || is_file($classFile) && !is_writable($classFile)) {
throw new \Exception("Cannot write class file in " . $classFile . " please check the rights on this directory");
}
File::put($classFile, $cd);
// create list class
$cd = '<?php ';
$cd .= "\n\n";
$cd .= "namespace Pimcore\\Model\\Object\\" . ucfirst($this->getName()) . ";";
$cd .= "\n\n";
$cd .= "use Pimcore\\Model\\Object;";
$cd .= "\n\n";
$cd .= "/**\n";
$cd .= " * @method Object\\" . ucfirst($this->getName()) . " current()\n";
$cd .= " */";
$cd .= "\n\n";
$cd .= "class Listing extends Object\\Listing\\Concrete {";
$cd .= "\n\n";
$cd .= 'public $classId = ' . $this->getId() . ";\n";
$cd .= 'public $className = "' . $this->getName() . '"' . ";\n";
$cd .= "\n\n";
$cd .= "}\n";
File::mkdir(PIMCORE_CLASS_DIRECTORY . "/Object/" . ucfirst($this->getName()));
$classListFile = PIMCORE_CLASS_DIRECTORY . "/Object/" . ucfirst($this->getName()) . "/Listing.php";
if (!is_writable(dirname($classListFile)) || is_file($classListFile) && !is_writable($classListFile)) {
throw new \Exception("Cannot write class file in " . $classListFile . " please check the rights on this directory");
}
File::put($classListFile, $cd);
// empty object cache
try {
Cache::clearTag("class_" . $this->getId());
} catch (\Exception $e) {
}
if ($isUpdate) {
\Pimcore::getEventManager()->trigger("object.class.postUpdate", $this);
} else {
\Pimcore::getEventManager()->trigger("object.class.postAdd", $this);
}
}