diff --git a/src/ConfigElement/AbstractConfigElement.php b/src/ConfigElement/AbstractConfigElement.php index 431caca..49960d4 100644 --- a/src/ConfigElement/AbstractConfigElement.php +++ b/src/ConfigElement/AbstractConfigElement.php @@ -24,7 +24,7 @@ abstract class AbstractConfigElement implements IConfigElement public function __construct($config, $context = null) { - $this->attribute = $config->attribute; + $this->attribute = $config->attribute ?? null; $this->label = $config->label; $this->context = $context; diff --git a/src/ConfigElement/Operator/AbstractOperator.php b/src/ConfigElement/Operator/AbstractOperator.php index 2f94edf..7c7b2a4 100644 --- a/src/ConfigElement/Operator/AbstractOperator.php +++ b/src/ConfigElement/Operator/AbstractOperator.php @@ -32,4 +32,10 @@ public function getChilds() { return $this->childs; } + + public function __construct($config, $context = null) + { + parent::__construct($config, $context); + $this->childs = $config->childs ?? []; + } } diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index f64ed2b..34862d3 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -109,12 +109,15 @@ public function getOutputConfigsAction(Request $request) foreach ($classList as $class) { foreach ($channels as $channel) { $def = $this->getOutputDefinitionForObjectAndChannel($object, $class->getId(), $channel); + if ($def === null) { + continue; + } $outputDefinitions[] = [ 'id' => $def->getId(), 'classname' => $this->translator->trans($class->getName(), [], 'admin'), 'channel' => $this->translator->trans($channel, [], 'admin'), 'object_id' => $def->getObjectId(), - 'is_inherited' => $def->getId() != $objectId + 'is_inherited' => $def->getObjectId() != $objectId ]; } } @@ -127,7 +130,7 @@ public function getOutputConfigsAction(Request $request) * @param $classId * @param $channel * - * @return OutputDefinition + * @return OutputDefinition|null */ private function getOutputDefinitionForObjectAndChannel($object, $classId, $channel) { @@ -152,7 +155,7 @@ private function getOutputDefinitionForObjectAndChannel($object, $classId, $chan public function resetOutputConfigAction(Request $request) { try { - $config = OutputDefinition::getByID($request->get('config_id')); + $config = OutputDefinition::getById($request->get('config_id')); $config->delete(); return $this->jsonResponse(['success' => true]); @@ -173,7 +176,7 @@ public function resetOutputConfigAction(Request $request) public function getOutputConfigAction(Request $request) { try { - $config = OutputDefinition::getByID($request->get('config_id')); + $config = OutputDefinition::getById($request->get('config_id')); $objectClass = ClassDefinition::getById($config->getClassId()); $configuration = json_decode($config->getConfiguration()); @@ -199,7 +202,7 @@ public function getOutputConfigAction(Request $request) public function getOrCreateOutputConfigAction(Request $request) { try { - $config = OutputDefinition::getByID($request->get('config_id')); + $config = OutputDefinition::getById($request->get('config_id')); $class = null; if (!$config) { if (is_numeric($request->get('class_id'))) { @@ -211,7 +214,7 @@ public function getOrCreateOutputConfigAction(Request $request) throw new \Exception('Class ' . $request->get('class_id') . ' not found.'); } - $config = OutputDefinition::getByObjectIdClassIdChannel($request->get('o_id'), $class->getId(), $request->get('channel')); + $config = OutputDefinition::getByObjectIdClassIdChannel($request->get('objectId'), $class->getId(), $request->get('channel')); } if ($config) { @@ -225,7 +228,7 @@ public function getOrCreateOutputConfigAction(Request $request) $config = new OutputDefinition(); $config->setChannel($request->get('channel')); $config->setClassId($class->getId()); - $config->setObjectId($request->get('o_id')); + $config->setObjectId($request->get('objectId')); $config->save(); return $this->jsonResponse(['success' => true, 'outputConfig' => $config]); @@ -409,7 +412,7 @@ public function getFieldDefinitionAction(Request $request) public function saveOutputConfigAction(Request $request, EventDispatcherInterface $eventDispatcher) { try { - $config = OutputDefinition::getByID($request->get('config_id')); + $config = OutputDefinition::getById($request->get('config_id')); $object = AbstractObject::getById($request->get('object_id')); if (empty($object)) { diff --git a/src/Migrations/Version20221130082116.php b/src/Migrations/Version20221130082116.php index 9e5c313..863a444 100644 --- a/src/Migrations/Version20221130082116.php +++ b/src/Migrations/Version20221130082116.php @@ -38,6 +38,11 @@ public function up(Schema $schema): void $query = 'ALTER TABLE `%s` RENAME COLUMN `%s` TO `%s`'; $this->addSql(sprintf($query, $table->getName(), 'o_classId', 'classId')); } + + if ($table->hasColumn('o_id')) { + $query = 'ALTER TABLE `%s` RENAME COLUMN `%s` TO `%s`'; + $this->addSql(sprintf($query, $table->getName(), 'o_id', 'objectId')); + } } public function down(Schema $schema): void @@ -48,5 +53,10 @@ public function down(Schema $schema): void $query = 'ALTER TABLE `%s` RENAME COLUMN `%s` TO `%s`'; $this->addSql(sprintf($query, $table->getName(), 'classId', 'o_classId')); } + + if ($table->hasColumn('objectId')) { + $query = 'ALTER TABLE `%s` RENAME COLUMN `%s` TO `%s`'; + $this->addSql(sprintf($query, $table->getName(), 'objectId', 'o_id')); + } } } diff --git a/src/OutputDefinition.php b/src/OutputDefinition.php index 7594186..56273fd 100644 --- a/src/OutputDefinition.php +++ b/src/OutputDefinition.php @@ -25,7 +25,7 @@ class OutputDefinition extends \Pimcore\Model\AbstractModel { public $id; - public $o_id; + public $objectId; public $classId; public $channel; public $configuration; @@ -142,12 +142,12 @@ public function getClassId() public function setObjectId($id) { - $this->o_id = $id; + $this->objectId = $id; } public function getObjectId() { - return $this->o_id; + return $this->objectId; } public function setId($id) diff --git a/src/OutputDefinition/Dao.php b/src/OutputDefinition/Dao.php index 0da2a7c..7e7cb39 100644 --- a/src/OutputDefinition/Dao.php +++ b/src/OutputDefinition/Dao.php @@ -49,7 +49,7 @@ public function init() */ public function getByObjectIdClassIdChannel($id, $classId, $channel) { - $outputDefinitionRaw = $this->db->fetchAssociative('SELECT * FROM ' . self::TABLE_NAME . ' WHERE o_id=? AND o_classId = ? AND channel = ?', [$id, $classId, $channel]); + $outputDefinitionRaw = $this->db->fetchAssociative('SELECT * FROM ' . self::TABLE_NAME . ' WHERE objectId=? AND classId = ? AND channel = ?', [$id, $classId, $channel]); if (empty($outputDefinitionRaw)) { throw new \Exception('OutputDefinition ' . $id . ' - ' . $classId . ' - ' . $channel . ' not found.'); } @@ -77,10 +77,21 @@ public function getById($id) */ public function create() { - $this->db->insert(self::TABLE_NAME, []); - $this->model->setId($this->db->lastInsertId()); + $class = get_object_vars($this->model); + $data = []; - $this->save(); + foreach ($class as $key => $value) { + if (in_array($key, $this->validColumns)) { + if (is_array($value) || is_object($value)) { + $value = serialize($value); + } elseif (is_bool($value)) { + $value = (int)$value; + } + $data[$key] = $value; + } + } + $this->db->insert(self::TABLE_NAME, self::quoteDataIdentifiers($this->db, $data)); + $this->model->setId($this->db->lastInsertId()); } /** @@ -130,7 +141,7 @@ public function update() */ public function delete() { - $this->db->delete(self::TABLE_NAME, ['id' => $this->db->quote($this->model->getId())]); + $this->db->delete(self::TABLE_NAME, ['id' => (int) $this->model->getId()]); } public static function quoteDataIdentifiers(Connection $db, array $data): array diff --git a/src/OutputDefinition/Listing.php b/src/OutputDefinition/Listing.php index d836137..75c9d57 100644 --- a/src/OutputDefinition/Listing.php +++ b/src/OutputDefinition/Listing.php @@ -36,7 +36,7 @@ class Listing extends \Pimcore\Model\Listing\AbstractListing */ public function isValidOrderKey($key): bool { - if ($key == 'o_id' || $key == 'classId' || $key == 'channel') { + if ($key == 'objectId' || $key == 'classId' || $key == 'channel') { return true; } diff --git a/src/OutputDefinition/Listing/Dao.php b/src/OutputDefinition/Listing/Dao.php index 24c4b13..e566b09 100644 --- a/src/OutputDefinition/Listing/Dao.php +++ b/src/OutputDefinition/Listing/Dao.php @@ -37,13 +37,13 @@ public function load(): array $params = array_column($this->model->getConditionParams() ?: [], 'value'); $unitIds = $this->db->fetchAllAssociative( - 'SELECT o_id, id, classId, channel FROM ' . OutputDefinition\Dao::TABLE_NAME . + 'SELECT objectId, id, classId, channel FROM ' . OutputDefinition\Dao::TABLE_NAME . $this->getCondition() . $this->getOrder() . $this->getOffsetLimit(), $params ); foreach ($unitIds as $row) { - $configs[] = OutputDefinition::getByObjectIdClassIdChannel($row['o_id'], $row['classId'], $row['channel']); + $configs[] = OutputDefinition::getByObjectIdClassIdChannel($row['objectId'], $row['classId'], $row['channel']); } $this->model->setOutputDefinitions($configs); diff --git a/src/Tools/Installer.php b/src/Tools/Installer.php index bdd9333..6f544a2 100644 --- a/src/Tools/Installer.php +++ b/src/Tools/Installer.php @@ -34,12 +34,12 @@ public function install(): void $db = \Pimcore\Db::get(); $db->executeQuery('CREATE TABLE `' . Dao::TABLE_NAME . '` ( `id` int(11) NOT NULL AUTO_INCREMENT, - `o_id` int(11) NOT NULL, + `objectId` int(11) NOT NULL, `classId` varchar(50) NOT NULL, `channel` varchar(255) COLLATE utf8_bin NOT NULL, `configuration` longtext CHARACTER SET latin1, PRIMARY KEY (`id`), - UNIQUE KEY `Unique` (`o_id`,`classId`,`channel`) + UNIQUE KEY `Unique` (`objectId`,`classId`,`channel`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; ');