Skip to content

Commit

Permalink
refactor: All ajax explorer models use AbstractExplorerItem class t…
Browse files Browse the repository at this point in the history
…o uniformize API properties
  • Loading branch information
ambroisemaupate committed Aug 28, 2024
1 parent c50b430 commit 2728a14
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function indexAction(Request $request): JsonResponse
]
);
$listManager->setDisplayingNotPublishedNodes(true);
$listManager->setItemPerPage(30);
// Use a factor of 12 for a better grid display
$listManager->setItemPerPage(36);
$listManager->handle();

$documents = $listManager->getEntities();
Expand Down
8 changes: 7 additions & 1 deletion lib/Rozier/src/AjaxControllers/AjaxNodeTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Themes\Rozier\Models\NodeTypeModel;

final class AjaxNodeTypesController extends AbstractAjaxController
{
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator
) {
}

/**
* @param Request $request
*
Expand Down Expand Up @@ -106,7 +112,7 @@ private function normalizeNodeType(iterable $nodeTypes): array

/** @var NodeType $nodeType */
foreach ($nodeTypes as $nodeType) {
$nodeModel = new NodeTypeModel($nodeType);
$nodeModel = new NodeTypeModel($nodeType, $this->urlGenerator);
$nodeTypesArray[] = $nodeModel->toArray();
}

Expand Down
42 changes: 30 additions & 12 deletions lib/Rozier/src/Models/CustomFormModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Themes\Rozier\Models;

use RZ\Roadiz\CoreBundle\Entity\CustomForm;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class CustomFormModel implements ModelInterface
final class CustomFormModel extends AbstractExplorerItem
{
public function __construct(
private readonly CustomForm $customForm,
Expand All @@ -17,23 +18,40 @@ public function __construct(
) {
}

public function toArray(): array
public function getId(): string|int
{
$countFields = strip_tags($this->translator->trans(
return $this->customForm->getId();
}

public function getAlternativeDisplayable(): ?string
{
return strip_tags($this->translator->trans(
'{0} no.customFormField|{1} 1.customFormField|]1,Inf] %count%.customFormFields',
[
'%count%' => $this->customForm->getFields()->count()
]
));
}

public function getDisplayable(): string
{
return $this->customForm->getDisplayName();
}

return [
'id' => $this->customForm->getId(),
'name' => $this->customForm->getDisplayName(),
'countFields' => $countFields,
'color' => $this->customForm->getColor(),
'customFormsEditPage' => $this->urlGenerator->generate('customFormsEditPage', [
'id' => $this->customForm->getId()
]),
];
public function getOriginal(): CustomForm
{
return $this->customForm;
}

protected function getEditItemPath(): ?string
{
return $this->urlGenerator->generate('customFormsEditPage', [
'id' => $this->customForm->getId()
]);
}

protected function getColor(): ?string
{
return $this->customForm->getColor();
}
}
83 changes: 54 additions & 29 deletions lib/Rozier/src/Models/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Entity\Document;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use RZ\Roadiz\Documents\MediaFinders\EmbedFinderFactory;
use RZ\Roadiz\Documents\Models\AdvancedDocumentInterface;
use RZ\Roadiz\Documents\Models\DocumentInterface;
use RZ\Roadiz\Documents\Models\HasThumbnailInterface;
use RZ\Roadiz\Documents\Renderer\RendererInterface;
use RZ\Roadiz\Documents\UrlGenerators\DocumentUrlGeneratorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class DocumentModel implements ModelInterface
final class DocumentModel extends AbstractExplorerItem
{
public static array $thumbnail80Array = [
"fit" => "80x80",
Expand All @@ -25,6 +27,7 @@ final class DocumentModel implements ModelInterface
"width" => 1440,
"quality" => 80,
"inline" => false,
"picture" => true,
"embed" => true,
];

Expand All @@ -37,19 +40,58 @@ public function __construct(
) {
}

public function toArray(): array
public function getId(): string|int
{
$name = (string) $this->document;
$thumbnail80Url = null;
$previewUrl = null;
if ($this->document instanceof PersistableInterface) {
return $this->document->getId();
}
return 0;
}

public function getAlternativeDisplayable(): ?string
{
return (string) $this->document;
}

public function getDisplayable(): string
{
if (
$this->document instanceof Document &&
$this->document->getDocumentTranslations()->first() &&
$this->document->getDocumentTranslations()->first()->getName()
) {
$name = $this->document->getDocumentTranslations()->first()->getName();
return $this->document->getDocumentTranslations()->first()->getName();
}
return (string) $this->document;
}

public function getOriginal(): DocumentInterface
{
return $this->document;
}

protected function getEditItemPath(): ?string
{
if (!($this->document instanceof PersistableInterface)) {
return null;
}
return $this->urlGenerator->generate('documentsEditPage', [
'documentId' => $this->document->getId()
]);
}

protected function getColor(): ?string
{
if ($this->document instanceof AdvancedDocumentInterface) {
return $this->document->getImageAverageColor();
}
return null;
}


public function toArray(): array
{
$thumbnail80Url = null;

$this->documentUrlGenerator->setDocument($this->document);
$hasThumbnail = false;
Expand All @@ -65,21 +107,9 @@ public function toArray(): array
}

if (!$this->document->isPrivate() && !empty($this->document->getRelativePath())) {
$this->documentUrlGenerator->setOptions(DocumentModel::$thumbnail80Array);
$this->documentUrlGenerator->setOptions(self::$thumbnail80Array);
$thumbnail80Url = $this->documentUrlGenerator->getUrl();
$this->documentUrlGenerator->setOptions(DocumentModel::$previewArray);
$previewUrl = $this->documentUrlGenerator->getUrl();
}

if ($this->document instanceof PersistableInterface) {
$id = $this->document->getId();
$editUrl = $this->urlGenerator
->generate('documentsEditPage', [
'documentId' => $this->document->getId()
]);
} else {
$id = null;
$editUrl = null;
$this->documentUrlGenerator->setOptions(self::$previewArray);
}

$embedFinder = $this->embedFinderFactory?->createForPlatform(
Expand All @@ -88,9 +118,7 @@ public function toArray(): array
) ?? null;

return [
'id' => $id,
'filename' => (string) $this->document,
'name' => $name,
...parent::toArray(),
'hasThumbnail' => $hasThumbnail,
'isImage' => $this->document->isImage(),
'isWebp' => $this->document->getMimeType() === 'image/webp',
Expand All @@ -102,18 +130,15 @@ public function toArray(): array
'shortType' => $this->document->getShortType(),
'processable' => $this->document->isProcessable(),
'relativePath' => $this->document->getRelativePath(),
'editUrl' => $editUrl,
'preview' => $previewUrl,
'preview_html' => !$this->document->isPrivate() ?
$this->renderer->render($this->document, DocumentModel::$previewArray) :
'previewHtml' => !$this->document->isPrivate() ?
$this->renderer->render($this->document, self::$previewArray) :
null,
'embedPlatform' => $this->document->getEmbedPlatform(),
'icon' => null !== $embedFinder
? $embedFinder->getShortType()
: $this->document->getShortType(),
'shortMimeType' => $this->document->getShortMimeType(),
'thumbnail_80' => $thumbnail80Url,
'url' => $previewUrl ?? $thumbnail80Url,
'thumbnail80' => $thumbnail80Url,
];
}
}
44 changes: 35 additions & 9 deletions lib/Rozier/src/Models/NodeTypeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,46 @@
namespace Themes\Rozier\Models;

use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class NodeTypeModel implements ModelInterface
final class NodeTypeModel extends AbstractExplorerItem
{
public function __construct(private readonly NodeType $nodeType)
public function __construct(
private readonly NodeType $nodeType,
private readonly UrlGeneratorInterface $urlGenerator
) {
}

public function getId(): string|int
{
return $this->nodeType->getId();
}

public function getAlternativeDisplayable(): ?string
{
return $this->nodeType->getName();
}

public function getDisplayable(): string
{
return $this->nodeType->getDisplayName();
}

public function getOriginal(): NodeType
{
return $this->nodeType;
}

protected function getEditItemPath(): ?string
{
return $this->urlGenerator->generate('nodeTypesEditPage', [
'nodeTypeId' => $this->nodeType->getId()
]);
}

public function toArray(): array
protected function getColor(): ?string
{
return [
'id' => $this->nodeType->getId(),
'nodeName' => $this->nodeType->getName(),
'name' => $this->nodeType->getDisplayName(),
'color' => $this->nodeType->getColor(),
];
return $this->nodeType->getColor();
}
}
43 changes: 31 additions & 12 deletions lib/Rozier/src/Models/TagModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,40 @@
namespace Themes\Rozier\Models;

use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Explorer\AbstractExplorerItem;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class TagModel implements ModelInterface
final class TagModel extends AbstractExplorerItem
{
public function __construct(
private readonly Tag $tag,
private readonly UrlGeneratorInterface $urlGenerator
) {
}

public function toArray(): array
protected function getEditItemPath(): ?string
{
return $this->urlGenerator->generate('tagsEditPage', [
'tagId' => $this->tag->getId()
]);
}

protected function getColor(): ?string
{
return $this->tag->getColor();
}

public function getId(): string|int
{
return $this->tag->getId();
}

public function getAlternativeDisplayable(): ?string
{
return $this->getTagParents($this->tag);
}

public function getDisplayable(): string
{
$firstTrans = $this->tag->getTranslatedTags()->first();
$name = $this->tag->getTagName();
Expand All @@ -24,16 +47,12 @@ public function toArray(): array
$name = $firstTrans->getName();
}

return [
'id' => $this->tag->getId(),
'name' => $name,
'tagName' => $this->tag->getTagName(),
'color' => $this->tag->getColor(),
'parent' => $this->getTagParents($this->tag),
'editUrl' => $this->urlGenerator->generate('tagsEditPage', [
'tagId' => $this->tag->getId()
]),
];
return $name;
}

public function getOriginal(): Tag
{
return $this->tag;
}

/**
Expand Down
Loading

0 comments on commit 2728a14

Please sign in to comment.