diff options
Diffstat (limited to 'src/model/entities/Node.php')
| -rw-r--r-- | src/model/entities/Node.php | 166 |
1 files changed, 0 insertions, 166 deletions
diff --git a/src/model/entities/Node.php b/src/model/entities/Node.php deleted file mode 100644 index 7932efc..0000000 --- a/src/model/entities/Node.php +++ /dev/null | |||
| @@ -1,166 +0,0 @@ | |||
| 1 | <?php | ||
| 2 | // src/model/entities/Node.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | namespace App\Entity; | ||
| 7 | |||
| 8 | use Doctrine\ORM\Mapping as ORM; | ||
| 9 | |||
| 10 | #[ORM\Entity] | ||
| 11 | #[ORM\Table(name: TABLE_PREFIX . "node")] | ||
| 12 | class Node | ||
| 13 | { | ||
| 14 | use \Position; | ||
| 15 | |||
| 16 | #[ORM\Id] | ||
| 17 | #[ORM\GeneratedValue] | ||
| 18 | #[ORM\Column(type: "integer")] | ||
| 19 | private ?int $id_node = null; | ||
| 20 | |||
| 21 | #[ORM\Column(type: "string", length: 255)] | ||
| 22 | private string $name_node; | ||
| 23 | |||
| 24 | #[ORM\Column(type: "integer")] | ||
| 25 | private int $position; | ||
| 26 | |||
| 27 | #[ORM\ManyToOne(targetEntity: self::class)] | ||
| 28 | //#[ORM\ManyToOne(targetEntity: self::class, fetch: 'EAGER')] // fetch: 'eager' désactive le mode fainéant de doctrine | ||
| 29 | #[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id_node", onDelete: "SET NULL", nullable: true)] | ||
| 30 | private ?self $parent = null; | ||
| 31 | |||
| 32 | // un onDelete: "CASCADE" serait logique ici mais dangereux, on pourrait aussi faire en sorte que les noeuds soient récupérables si la page est brutalement supprimée | ||
| 33 | #[ORM\ManyToOne(targetEntity: Page::class)] | ||
| 34 | #[ORM\JoinColumn(name: "page_id", referencedColumnName: "id_page", nullable: true)] | ||
| 35 | private ?Page $page; | ||
| 36 | |||
| 37 | #[ORM\ManyToOne(targetEntity: Article::class, cascade: ['persist'])] | ||
| 38 | #[ORM\JoinColumn(name: "article_id", referencedColumnName: "id_article", onDelete: "CASCADE", nullable: true)] // supprimer le contenu d'un article supprime les noeuds associés, faux dans l'autre sens | ||
| 39 | private ?Article $article = null; | ||
| 40 | |||
| 41 | // propriété non mappée dans la table "node", la jointure est décrite dans NodeData | ||
| 42 | // elle sert à persister ou supprimer des données par cascade | ||
| 43 | // "mappedBy" permet de cibler $node dans l'autre classe, qui elle possède un "inversedBy" | ||
| 44 | #[ORM\OneToOne(targetEntity: NodeData::class, mappedBy: "node", cascade: ['persist', 'remove'])] | ||
| 45 | private ?NodeData $node_data = null; | ||
| 46 | |||
| 47 | #[ORM\OneToOne(targetEntity: EmailForm::class, mappedBy: "node", cascade: ['persist'])] // pas de remove, les e-mails sont associés au EmailForm | ||
| 48 | private ?EmailForm $email_form = null; | ||
| 49 | |||
| 50 | #[ORM\OneToOne(targetEntity: Presentation::class, mappedBy: "node", cascade: ['persist', 'remove'])] | ||
| 51 | private ?Presentation $presentation = null; | ||
| 52 | |||
| 53 | // attributs non destinés à doctrine | ||
| 54 | private array $children = []; // tableau de Node | ||
| 55 | private ?self $adopted = null; // = "new" est un enfant de "main" lorsque la page est "article" | ||
| 56 | |||
| 57 | public function __construct(string $name = '', int $position = 0, ?self $parent = null, ?Page $page = null, ?Article $article = null){ | ||
| 58 | $this->name_node = $name; | ||
| 59 | $this->position = $position; | ||
| 60 | $this->parent = $parent; | ||
| 61 | $this->page = $page; | ||
| 62 | $this->article = $article; | ||
| 63 | } | ||
| 64 | |||
| 65 | // pfff... | ||
| 66 | public function getId(): ?int | ||
| 67 | { | ||
| 68 | return $this->id_node; | ||
| 69 | } | ||
| 70 | public function getName(): string | ||
| 71 | { | ||
| 72 | return $this->name_node; | ||
| 73 | } | ||
| 74 | /*public function setName(string $name): void | ||
| 75 | { | ||
| 76 | $this->name_node = $name; | ||
| 77 | }*/ | ||
| 78 | |||
| 79 | public function getParent(): ?self | ||
| 80 | { | ||
| 81 | return $this->parent; | ||
| 82 | } | ||
| 83 | /*public function setParent(?self $parent): void | ||
| 84 | { | ||
| 85 | $this->parent = $parent; | ||
| 86 | }*/ | ||
| 87 | public function getPosition(): int | ||
| 88 | { | ||
| 89 | return $this->position; | ||
| 90 | } | ||
| 91 | public function setPosition(int $position): void | ||
| 92 | { | ||
| 93 | $this->position = $position; | ||
| 94 | } | ||
| 95 | public function getPage(): Page | ||
| 96 | { | ||
| 97 | return $this->page; | ||
| 98 | } | ||
| 99 | /*public function setPage(Page $page): void | ||
| 100 | { | ||
| 101 | $this->page = $page; | ||
| 102 | }*/ | ||
| 103 | |||
| 104 | // utiliser l'interface de NodeData et EmailForm | ||
| 105 | public function getArticle(): Article | ||
| 106 | { | ||
| 107 | return $this->article; | ||
| 108 | } | ||
| 109 | /*public function setArticle(Article $article): void | ||
| 110 | { | ||
| 111 | $this->article = $article; | ||
| 112 | }*/ | ||
| 113 | |||
| 114 | // une interface serait cool! | ||
| 115 | // OU possible un "héritage doctrine", faire en sorte que Node soit la mère de NodeData, EmailForm, Article... | ||
| 116 | // une seule instance est matérialisée par plusieurs tables en même temps | ||
| 117 | public function getNodeData(): NodeData|EmailForm|Presentation|null | ||
| 118 | { | ||
| 119 | // pour du polymorphisme (une unique variable $node_data): opter pour l'héritage | ||
| 120 | return match($this->name_node){ | ||
| 121 | 'form' => $this->email_form, | ||
| 122 | 'post_block', 'news_block' => $this->presentation, | ||
| 123 | default => $this->node_data, // inclut 'calendar' | ||
| 124 | }; | ||
| 125 | } | ||
| 126 | public function getChildren(): array | ||
| 127 | { | ||
| 128 | return $this->children; | ||
| 129 | } | ||
| 130 | public function getNodeByName(string $name): ?Node | ||
| 131 | { | ||
| 132 | foreach($this->children as $child){ | ||
| 133 | if($child->getName() === $name){ | ||
| 134 | return $child; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | return null; | ||
| 138 | } | ||
| 139 | public function addChild(self $child): void | ||
| 140 | { | ||
| 141 | $this->children[] = $child; | ||
| 142 | |||
| 143 | if(!Presentation::hasPresentation($this->getName())){ // post_block et news_block ont leurs enfants ordonnés avec ORDER BY | ||
| 144 | $this->sortChildren(false); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | public function removeChild(self $child): void | ||
| 148 | { | ||
| 149 | foreach($this->children as $key => $object){ | ||
| 150 | if($object->getId() === $child->getId()){ | ||
| 151 | unset($this->children[$key]); | ||
| 152 | } | ||
| 153 | break; | ||
| 154 | } | ||
| 155 | $this->children = array_values($this->children); // réindexer pour supprimer la case vide | ||
| 156 | } | ||
| 157 | |||
| 158 | public function getAdoptedChild(): ?self // peut renvoyer null | ||
| 159 | { | ||
| 160 | return $this->adopted; | ||
| 161 | } | ||
| 162 | public function setAdoptedChild(self $child): void | ||
| 163 | { | ||
| 164 | $this->adopted = $child; | ||
| 165 | } | ||
| 166 | } | ||
