Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data Object Update] Add tasks "save" and "publish" #805

Merged
merged 2 commits into from
Feb 18, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/DataIndex/Query/DataObjectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

final class DataObjectQuery implements DataObjectQueryInterface
{
public const DATA_OBJECT_QUERY_ID = 'data_object_query';
public const string DATA_OBJECT_QUERY_ID = 'data_object_query';

public function __construct(
private readonly DataObjectSearch $search,
Expand Down
36 changes: 33 additions & 3 deletions src/Element/Service/ElementSaveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\SynchronousProcessingServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementPermissions;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementSaveTasks;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Document;
use Pimcore\Model\Element\DuplicateFullPathException;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\UserInterface;
Expand All @@ -30,15 +34,17 @@
*/
final readonly class ElementSaveService implements ElementSaveServiceInterface
{
public function __construct(private SynchronousProcessingServiceInterface $synchronousProcessingService)
{
public function __construct(
private SynchronousProcessingServiceInterface $synchronousProcessingService,
private SecurityServiceInterface $securityService
) {
}

/**
* @throws DuplicateFullPathException
* @throws Exception
*/
public function save(ElementInterface $element, UserInterface $user, ?string $task): void
public function save(ElementInterface $element, UserInterface $user, ?string $task = null): void
{
$this->synchronousProcessingService->enable();
$element->setUserModification($user->getId());
Expand All @@ -59,6 +65,16 @@ public function save(ElementInterface $element, UserInterface $user, ?string $ta
*/
private function processTask(ElementInterface $element, UserInterface $user, string $task): void
{
if ($task === ElementSaveTasks::PUBLISH->value) {
$this->processPublishTask($element, $user);
}

if ($task === ElementSaveTasks::SAVE->value || $task === ElementSaveTasks::PUBLISH->value) {
$element->save();

return;
}

/**
* @var Concrete $element
*/
Expand All @@ -74,4 +90,18 @@ private function processTask(ElementInterface $element, UserInterface $user, str

$element->deleteAutoSaveVersions($user->getId());
}

/**
* @throws ForbiddenException
*/
private function processPublishTask(ElementInterface $element, UserInterface $user): void
{
if (!$element instanceof Concrete && !$element instanceof Document) {
return;
}

$this->securityService->hasElementPermission($element, $user, ElementPermissions::PUBLISH_PERMISSION);
$element->deleteAutoSaveVersions($user->getId());
$element->setPublished(true);
}
}
2 changes: 1 addition & 1 deletion src/Element/Service/ElementSaveServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ interface ElementSaveServiceInterface
* @throws DuplicateFullPathException
* @throws Exception
*/
public function save(ElementInterface $element, UserInterface $user, ?string $task): void;
public function save(ElementInterface $element, UserInterface $user, ?string $task = null): void;
}
4 changes: 3 additions & 1 deletion src/Util/Constant/ElementSaveTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum ElementSaveTasks: string
{
use EnumToValueArrayTrait;

case VERSION = 'version';
case AUTOSAVE = 'autoSave';
case PUBLISH = 'publish';
case SAVE = 'save';
case VERSION = 'version';
}
Loading