Skip to content

Commit

Permalink
refactor: Deprecated Themes\Rozier\Models\ModelInterface, changed `…
Browse files Browse the repository at this point in the history
…NodeModel` and `NodeSourceModel` to use AbstractExplorerItem
  • Loading branch information
ambroisemaupate committed Aug 27, 2024
1 parent 1eba519 commit c50b430
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract protected function getDefaultOrdering(): array;
* @param array $options
*
* @return EntityListManagerInterface
* @throws \ReflectionException
*/
protected function doFetchItems(array $options = []): EntityListManagerInterface
{
Expand Down
18 changes: 16 additions & 2 deletions lib/RoadizCoreBundle/src/Explorer/AbstractExplorerItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

namespace RZ\Roadiz\CoreBundle\Explorer;

use RZ\Roadiz\Documents\Models\DocumentInterface;

abstract class AbstractExplorerItem implements ExplorerItemInterface
{
protected function getEditItemPath(): ?string
{
return null;
}

protected function getThumbnail(): ?array
protected function getThumbnail(): DocumentInterface|array|null
{
return null;
}

protected function isPublished(): bool
{
return true;
}

protected function getColor(): ?string
{
return null;
}
Expand All @@ -26,7 +38,9 @@ public function toArray(): array
'classname' => $this->getAlternativeDisplayable() ?? '',
'displayable' => $this->getDisplayable(),
'editItem' => $this->getEditItemPath(),
'thumbnail' => $this->getThumbnail()
'thumbnail' => $this->getThumbnail(),
'published' => $this->isPublished(),
'color' => $this->getColor(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\EntityApi\NodeTypeApi;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use RZ\Roadiz\CoreBundle\SearchEngine\ClientRegistry;
use RZ\Roadiz\CoreBundle\SearchEngine\NodeSourceSearchHandlerInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\SolrSearchResultItem;
Expand Down Expand Up @@ -251,7 +252,7 @@ public function listAction(Request $request): JsonResponse
* Normalize response Node list result.
*
* @param iterable<Node|NodesSources|SolrSearchResultItem> $nodes
* @return array
* @return array<AbstractExplorerItem>
*/
private function normalizeNodes(iterable $nodes): array
{
Expand Down
3 changes: 3 additions & 0 deletions lib/Rozier/src/Models/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Themes\Rozier\Models;

/**
* @deprecated Use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemInterface instead.
*/
interface ModelInterface
{
/**
Expand Down
126 changes: 77 additions & 49 deletions lib/Rozier/src/Models/NodeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace Themes\Rozier\Models;

use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Groups;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodesSourcesDocuments;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use RZ\Roadiz\CoreBundle\Security\Authorization\Voter\NodeVoter;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use RZ\Roadiz\Documents\Models\DocumentInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* @Serializer\ExclusionPolicy("all")
*/
final class NodeModel implements ModelInterface
final class NodeModel extends AbstractExplorerItem
{
public function __construct(
private readonly Node $node,
Expand All @@ -25,70 +25,98 @@ public function __construct(
) {
}

public function toArray(): array
#[Groups(['model', 'node'])]
public function getId(): string|int
{
return $this->node->getId();
}

#[Groups(['model', 'node'])]
public function getAlternativeDisplayable(): ?string
{
$parent = $this->node->getParent();

if (!($parent instanceof Node)) {
return null;
}

$items = [];
$items[] = $parent->getNodeSources()->first() ?
$parent->getNodeSources()->first()->getTitle() :
$parent->getNodeName();

$subParent = $parent->getParent();
if ($subParent instanceof Node) {
$items[] = $subParent->getNodeSources()->first() ?
$subParent->getNodeSources()->first()->getTitle() :
$subParent->getNodeName();
}

return implode(' / ', array_reverse($items));
}

#[Groups(['model', 'node'])]
public function getDisplayable(): string
{
/** @var NodesSources|false $nodeSource */
$nodeSource = $this->node->getNodeSources()->first();
return false !== $nodeSource ?
($nodeSource->getTitle() ?? $this->node->getNodeName()) :
$this->node->getNodeName();
}

#[Exclude]
public function getOriginal(): Node
{
return $this->node;
}

#[Groups(['model', 'node'])]
public function getEditItemPath(): ?string
{
/** @var NodesSources|false $nodeSource */
$nodeSource = $this->node->getNodeSources()->first();

if (false === $nodeSource) {
$result = [
'id' => $this->node->getId(),
'title' => $this->node->getNodeName(),
'nodeName' => $this->node->getNodeName(),
'isPublished' => $this->node->isPublished(),
'nodeType' => [
'color' => $this->node->getNodeType()->getColor() ?? '#000000',
]
];
if ($this->security->isGranted(NodeVoter::EDIT_SETTING, $this->node)) {
$result['nodesEditPage'] = $this->urlGenerator->generate('nodesEditPage', [
return $this->urlGenerator->generate('nodesEditPage', [
'nodeId' => $this->node->getId(),
]);
}
return $result;
return null;
}

/** @var NodesSourcesDocuments|false $thumbnail */
$thumbnail = $nodeSource->getDocumentsByFields()->first();
/** @var Translation $translation */
$translation = $nodeSource->getTranslation();

$result = [
'id' => $this->node->getId(),
'title' => $nodeSource->getTitle() ?? $this->node->getNodeName(),
'thumbnail' => $thumbnail ? $thumbnail->getDocument() : null,
'nodeName' => $this->node->getNodeName(),
'isPublished' => $this->node->isPublished(),
'nodeType' => [
'color' => $this->node->getNodeType()->getColor() ?? '#000000',
]
];

if ($this->security->isGranted(NodeVoter::EDIT_CONTENT, $nodeSource)) {
$result['nodesEditPage'] = $this->urlGenerator->generate('nodesEditSourcePage', [
return $this->urlGenerator->generate('nodesEditSourcePage', [
'nodeId' => $this->node->getId(),
'translationId' => $translation->getId(),
]);
}
return null;
}

$parent = $this->node->getParent();
#[Groups(['model', 'node'])]
public function getThumbnail(): ?DocumentInterface
{
/** @var NodesSources|false $nodeSource */
$nodeSource = $this->node->getNodeSources()->first();
/** @var NodesSourcesDocuments|false $thumbnail */
$thumbnail = false !== $nodeSource ? $nodeSource->getDocumentsByFields()->first() : false;
return $thumbnail ? $thumbnail->getDocument() : null;
}

if ($parent instanceof Node) {
$result['parent'] = [
'title' => $parent->getNodeSources()->first() ?
$parent->getNodeSources()->first()->getTitle() :
$parent->getNodeName()
];
$subParent = $parent->getParent();
if ($subParent instanceof Node) {
$result['subparent'] = [
'title' => $subParent->getNodeSources()->first() ?
$subParent->getNodeSources()->first()->getTitle() :
$subParent->getNodeName()
];
}
}
#[Groups(['model', 'node'])]
public function isPublished(): bool
{
return $this->node->isPublished();
}

return $result;
#[Groups(['model', 'node'])]
public function getColor(): string
{
return $this->node->getNodeType()->getColor() ?? '#000000';
}
}
103 changes: 66 additions & 37 deletions lib/Rozier/src/Models/NodeSourceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

namespace Themes\Rozier\Models;

use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Groups;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Entity\NodesSourcesDocuments;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use RZ\Roadiz\CoreBundle\Security\Authorization\Voter\NodeVoter;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use RZ\Roadiz\Documents\Models\DocumentInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* @Serializer\ExclusionPolicy("all")
*/
final class NodeSourceModel implements ModelInterface
final class NodeSourceModel extends AbstractExplorerItem
{
public function __construct(
private readonly NodesSources $nodeSource,
Expand All @@ -24,47 +24,76 @@ public function __construct(
) {
}

public function toArray(): array
#[Groups(['model', 'node'])]
public function getId(): string|int
{
$node = $this->nodeSource->getNode();
return $this->nodeSource->getNode()->getId();
}

/** @var NodesSourcesDocuments|false $thumbnail */
$thumbnail = $this->nodeSource->getDocumentsByFields()->first();
#[Groups(['model', 'node'])]
public function getAlternativeDisplayable(): ?string
{
$parent = $this->nodeSource->getParent();

if (!($parent instanceof NodesSources)) {
return null;
}

$items = [];
$items[] = $parent->getTitle();

$subParent = $parent->getParent();
if ($subParent instanceof NodesSources) {
$items[] = $subParent->getTitle();
}

return implode(' / ', array_reverse($items));
}

#[Groups(['model', 'node'])]
public function getDisplayable(): string
{
return $this->nodeSource->getTitle() ?? $this->nodeSource->getNode()->getNodeName();
}

#[Exclude]
public function getOriginal(): NodesSources
{
return $this->nodeSource;
}

#[Groups(['model', 'node'])]
public function getEditItemPath(): ?string
{
/** @var Translation $translation */
$translation = $this->nodeSource->getTranslation();

$result = [
'id' => $node->getId(),
'title' => $this->nodeSource->getTitle(),
'nodeName' => $node->getNodeName(),
'thumbnail' => $thumbnail ? $thumbnail->getDocument() : null,
'isPublished' => $node->isPublished(),
'nodeType' => [
'color' => $node->getNodeType()->getColor() ?? '#000000',
]
];

if ($this->security->isGranted(NodeVoter::EDIT_CONTENT, $node)) {
$result['nodesEditPage'] = $this->urlGenerator->generate('nodesEditSourcePage', [
'nodeId' => $node->getId(),
if ($this->security->isGranted(NodeVoter::EDIT_CONTENT, $this->nodeSource)) {
return $this->urlGenerator->generate('nodesEditSourcePage', [
'nodeId' => $this->nodeSource->getNode()->getId(),
'translationId' => $translation->getId(),
]);
}
return null;
}

$parent = $this->nodeSource->getParent();
#[Groups(['model', 'node'])]
public function getThumbnail(): ?DocumentInterface
{
/** @var NodesSourcesDocuments|false $thumbnail */
$thumbnail = $this->nodeSource->getDocumentsByFields()->first();
return $thumbnail ? $thumbnail->getDocument() : null;
}

if ($parent instanceof NodesSources) {
$result['parent'] = [
'title' => $parent->getTitle()
];
$subparent = $parent->getParent();
if ($subparent instanceof NodesSources) {
$result['subparent'] = [
'title' => $subparent->getTitle()
];
}
}
#[Groups(['model', 'node'])]
public function isPublished(): bool
{
return $this->nodeSource->getNode()->isPublished();
}

return $result;
#[Groups(['model', 'node'])]
public function getColor(): string
{
return $this->nodeSource->getNode()->getNodeType()->getColor() ?? '#000000';
}
}
Loading

0 comments on commit c50b430

Please sign in to comment.