Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions core/components/minishop3/src/Processors/Category/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,20 @@

namespace MiniShop3\Processors\Category;

use MODX\Revolution\Processors\Resource\Update as UpdateProcessor;
use MiniShop3\Model\msCategory;
use MODX\Revolution\modResource;
use MiniShop3\Processors\Resource\EnsureTargetClassKeyTrait;
use MODX\Revolution\Processors\Resource\Update as UpdateProcessor;

class Update extends UpdateProcessor
{
use EnsureTargetClassKeyTrait;

public $classKey = msCategory::class;
public $languageTopics = ['resource', 'minishop3:default'];
public $permission = 'mscategory_save';
public $beforeSaveEvent = 'OnBeforeDocFormSave';
public $afterSaveEvent = 'OnDocFormSave';

/**
* @return bool|null|string
*/
public function initialize()
{
$primaryKey = $this->getProperty($this->primaryKeyField, false);
if (empty($primaryKey)) {
return $this->modx->lexicon($this->classKey . '_err_ns');
}

if (!$this->modx->getCount($this->classKey, ['id' => $primaryKey, 'class_key' => $this->classKey])) {
$res = $this->modx->getObject(modResource::class, ['id' => $primaryKey]);
if ($res) {
$res->set('class_key', $this->classKey);
$res->save();
}
}

return parent::initialize();
}


/**
* @return int|mixed|string
*/
Expand Down
2 changes: 2 additions & 0 deletions core/components/minishop3/src/Processors/Product/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use MiniShop3\Model\msCategory;
use MiniShop3\Model\msProduct;
use MiniShop3\Processors\Resource\EnsureTargetClassKeyTrait;
use MiniShop3\Utils\Utils;
use MODX\Revolution\modX;
use MODX\Revolution\Processors\Processor;
use MODX\Revolution\Processors\Resource\Update as UpdateProcessor;

class Update extends UpdateProcessor
{
use EnsureTargetClassKeyTrait;
use ProductDataPayloadTrait;

public $classKey = msProduct::class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace MiniShop3\Processors\Resource;

use MODX\Revolution\modResource;

/**
* Allows Resource Update processors to load the object when class_key is being changed
* (e.g. modDocument → msProduct) before parent::initialize() runs getObject($classKey).
*
* @property \MODX\Revolution\modX $modx
* @property string $classKey
* @property string $primaryKeyField
*/
trait EnsureTargetClassKeyTrait
{
/**
* @return bool|null|string
*/
public function initialize()
{
$err = $this->ensureTargetClassKeyExists();
if ($err !== null) {
return $err;
}

return parent::initialize();
}

/**
* @return string|null Error lexicon string or null on success
*/
protected function ensureTargetClassKeyExists(): ?string
{
$primaryKey = $this->getProperty($this->primaryKeyField, false);
if (empty($primaryKey)) {
return $this->modx->lexicon($this->classKey . '_err_ns');
}

if (!$this->modx->getCount($this->classKey, ['id' => $primaryKey, 'class_key' => $this->classKey])) {
$res = $this->modx->getObject(modResource::class, ['id' => $primaryKey]);
if ($res) {
$res->set('class_key', $this->classKey);
$res->save();
}
}

return null;
}
}