From 1f6aaf41e1b0d714976fb0771d5ca74993221bf8 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 19 Feb 2022 23:33:19 +0100 Subject: [PATCH] add node view counter option --- core/Entity/Site/Node.php | 54 +++++++++++++++++++ core/Form/Site/NodeType.php | 13 +++++ core/Resources/translations/messages.fr.yaml | 2 + .../views/site/node_admin/_form.html.twig | 1 + 4 files changed, 70 insertions(+) diff --git a/core/Entity/Site/Node.php b/core/Entity/Site/Node.php index ab7d6ef..0c71538 100644 --- a/core/Entity/Site/Node.php +++ b/core/Entity/Site/Node.php @@ -4,6 +4,7 @@ namespace App\Core\Entity\Site; use App\Core\Doctrine\Timestampable; use App\Core\Entity\EntityInterface; +use App\Core\Entity\NodeView; use App\Core\Entity\Site\Page\Page; use App\Core\Repository\Site\NodeRepository; use Doctrine\Common\Collections\ArrayCollection; @@ -139,10 +140,21 @@ class Node implements EntityInterface */ protected $contentType; + /** + * @ORM\Column(type="boolean", options={"default"=0}) + */ + private $enableViewCounter = false; + + /** + * @ORM\OneToMany(targetEntity=NodeView::class, mappedBy="node", orphanRemoval=true) + */ + private $nodeViews; + public function __construct() { $this->children = new ArrayCollection(); $this->aliasNodes = new ArrayCollection(); + $this->nodeViews = new ArrayCollection(); } public function getId(): ?int @@ -548,4 +560,46 @@ class Node implements EntityInterface return $this; } + + public function getEnableViewCounter(): ?bool + { + return $this->enableViewCounter; + } + + public function setEnableViewCounter(bool $enableViewCounter): self + { + $this->enableViewCounter = $enableViewCounter; + + return $this; + } + + /** + * @return Collection|NodeView[] + */ + public function getNodeViews(): Collection + { + return $this->nodeViews; + } + + public function addNodeView(NodeView $nodeView): self + { + if (!$this->nodeViews->contains($nodeView)) { + $this->nodeViews[] = $nodeView; + $nodeView->setNode($this); + } + + return $this; + } + + public function removeNodeView(NodeView $nodeView): self + { + if ($this->nodeViews->removeElement($nodeView)) { + // set the owning side to null (unless already changed) + if ($nodeView->getNode() === $this) { + $nodeView->setNode(null); + } + } + + return $this; + } } diff --git a/core/Form/Site/NodeType.php b/core/Form/Site/NodeType.php index 5c9b314..184cf55 100644 --- a/core/Form/Site/NodeType.php +++ b/core/Form/Site/NodeType.php @@ -60,6 +60,19 @@ class NodeType extends AbstractType ] ); + $builder->add( + 'enableViewCounter', + CheckboxType::class, + [ + 'label' => 'Enable view counter', + 'required' => false, + 'attr' => [ + ], + 'constraints' => [ + ], + ] + ); + $builder->add( 'code', TextType::class, diff --git a/core/Resources/translations/messages.fr.yaml b/core/Resources/translations/messages.fr.yaml index 0204b3e..26a9430 100644 --- a/core/Resources/translations/messages.fr.yaml +++ b/core/Resources/translations/messages.fr.yaml @@ -39,6 +39,8 @@ "Never": "Jamais" "URL": "URL" "Disable URL": "Désactiver l'URL" +"Enable view counter": "Activer le compteur de vues" +"Stats": "Statistiques" "Controller": "Contrôleur" "Leave blank for automatic generation": "Laisser vide pour une génération automatique" "Leave blank to use the default one. Example: App\\Controller\\FooController::barAction": "Laisser vide pour utiliser celui par défaut. Exemple : App\\Controller\\FooController::barAction" diff --git a/core/Resources/views/site/node_admin/_form.html.twig b/core/Resources/views/site/node_admin/_form.html.twig index 9fb460f..b1d7d78 100644 --- a/core/Resources/views/site/node_admin/_form.html.twig +++ b/core/Resources/views/site/node_admin/_form.html.twig @@ -205,6 +205,7 @@ {{ form_row(form.disableUrl) }} + {{ form_row(form.enableViewCounter) }} {{ form_row(form.code) }} {{ form_row(form.contentType) }} {{ form_row(form.controller) }}