Skip to content

Commit

Permalink
fixed some errors (#90)
Browse files Browse the repository at this point in the history
* fixed some errors

* check for null

* changed return type of method getOutputDefinitionForObjectAndChannel to "OutputDefinition|null"

* fix call of OutputDefinition::getById

* fixed the value of the attribute "is_inherited"

* fixed deletion of an entry

* fixed some errors with operators like "concatenator"

* merged migration "Version20230502065100" with "Version20221130082116" and removed "Version20230502065100"
  • Loading branch information
aweichler authored May 3, 2023
1 parent 30d3fa9 commit f0e4827
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/ConfigElement/AbstractConfigElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/ConfigElement/Operator/AbstractOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public function getChilds()
{
return $this->childs;
}

public function __construct($config, $context = null)
{
parent::__construct($config, $context);
$this->childs = $config->childs ?? [];
}
}
19 changes: 11 additions & 8 deletions src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}
}
Expand All @@ -127,7 +130,7 @@ public function getOutputConfigsAction(Request $request)
* @param $classId
* @param $channel
*
* @return OutputDefinition
* @return OutputDefinition|null
*/
private function getOutputDefinitionForObjectAndChannel($object, $classId, $channel)
{
Expand All @@ -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]);
Expand All @@ -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());
Expand All @@ -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'))) {
Expand All @@ -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) {
Expand All @@ -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]);
Expand Down Expand Up @@ -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)) {
Expand Down
10 changes: 10 additions & 0 deletions src/Migrations/Version20221130082116.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'));
}
}
}
6 changes: 3 additions & 3 deletions src/OutputDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class OutputDefinition extends \Pimcore\Model\AbstractModel
{
public $id;
public $o_id;
public $objectId;
public $classId;
public $channel;
public $configuration;
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions src/OutputDefinition/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/OutputDefinition/Listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/OutputDefinition/Listing/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Tools/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
');

Expand Down

0 comments on commit f0e4827

Please sign in to comment.