diff options
Diffstat (limited to 'src/Model/Entity/Page.php')
| -rw-r--r-- | src/Model/Entity/Page.php | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/src/Model/Entity/Page.php b/src/Model/Entity/Page.php new file mode 100644 index 0000000..7cca3dc --- /dev/null +++ b/src/Model/Entity/Page.php | |||
| @@ -0,0 +1,222 @@ | |||
| 1 | <?php | ||
| 2 | // src/Model/Entity/Page.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | namespace App\Entity; | ||
| 7 | |||
| 8 | use Doctrine\ORM\Mapping as ORM; | ||
| 9 | use Doctrine\Common\Collections\Collection; | ||
| 10 | use Doctrine\Common\Collections\ArrayCollection; | ||
| 11 | use App\Model\Position; | ||
| 12 | |||
| 13 | #[ORM\Entity] | ||
| 14 | #[ORM\Table(name: TABLE_PREFIX . "page")] | ||
| 15 | class Page | ||
| 16 | { | ||
| 17 | use Position; | ||
| 18 | |||
| 19 | #[ORM\Id] | ||
| 20 | #[ORM\GeneratedValue] | ||
| 21 | #[ORM\Column(type: "integer")] | ||
| 22 | private int $id_page; | ||
| 23 | |||
| 24 | #[ORM\Column(type: "string", length: 255)] | ||
| 25 | private string $name_page; | ||
| 26 | |||
| 27 | #[ORM\Column(type: "string", length: 255)] | ||
| 28 | private string $end_of_path; // morceau d'URL plus exactement | ||
| 29 | |||
| 30 | private string $page_path = ''; | ||
| 31 | |||
| 32 | #[ORM\Column(type: "text")] | ||
| 33 | private string $description; | ||
| 34 | |||
| 35 | #[ORM\Column(type: "json", nullable: true)] | ||
| 36 | private ?array $css = null; | ||
| 37 | |||
| 38 | #[ORM\Column(type: "json", nullable: true)] | ||
| 39 | private ?array $js = null; | ||
| 40 | |||
| 41 | static private array $default_css = ['body', 'head', 'nav', 'foot']; | ||
| 42 | static private array $default_js = ['main']; | ||
| 43 | |||
| 44 | #[ORM\Column(type: "boolean")] | ||
| 45 | private bool $reachable; | ||
| 46 | |||
| 47 | #[ORM\Column(type: "boolean")] | ||
| 48 | private bool $in_menu; | ||
| 49 | |||
| 50 | #[ORM\Column(type: "boolean")] | ||
| 51 | private bool $hidden; | ||
| 52 | |||
| 53 | #[ORM\Column(type: "integer", nullable: true)] // null si hors menu | ||
| 54 | private ?int $position; | ||
| 55 | |||
| 56 | #[ORM\ManyToOne(targetEntity: self::class, inversedBy: "children")] | ||
| 57 | #[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id_page", onDelete: "SET NULL", nullable: true)] | ||
| 58 | private ?self $parent = null; | ||
| 59 | |||
| 60 | #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')] | ||
| 61 | protected Collection $children; | ||
| 62 | |||
| 63 | /*#[ORM\Column(type: "json", nullable: true)] | ||
| 64 | private ?array $metadata = null;*/ | ||
| 65 | |||
| 66 | public function __construct(string $name, string $eop, string $description, bool $reachable, bool $in_menu, bool $hidden, ?int $position, ?Page $parent) | ||
| 67 | { | ||
| 68 | $this->name_page = $name; | ||
| 69 | $this->end_of_path = $eop; | ||
| 70 | $this->description = $description; | ||
| 71 | $this->reachable = $reachable; | ||
| 72 | $this->in_menu = $in_menu; | ||
| 73 | $this->hidden = $hidden; | ||
| 74 | $this->position = $position; | ||
| 75 | $this->parent = $parent; | ||
| 76 | $this->children = new ArrayCollection(); | ||
| 77 | } | ||
| 78 | |||
| 79 | // getters/setters | ||
| 80 | public function getId(): int | ||
| 81 | { | ||
| 82 | return $this->id_page; | ||
| 83 | } | ||
| 84 | public function getPageName(): string | ||
| 85 | { | ||
| 86 | return $this->name_page; | ||
| 87 | } | ||
| 88 | public function setPageName(string $name): void | ||
| 89 | { | ||
| 90 | $this->name_page = $name; | ||
| 91 | } | ||
| 92 | public function getPagePath(): string | ||
| 93 | { | ||
| 94 | return $this->page_path; | ||
| 95 | } | ||
| 96 | public function setPagePath(string $path):void | ||
| 97 | { | ||
| 98 | $this->page_path = $path; | ||
| 99 | } | ||
| 100 | public function getEndOfPath(): string | ||
| 101 | { | ||
| 102 | return $this->end_of_path; | ||
| 103 | } | ||
| 104 | public function setEndOfPath(string $end_of_path):void | ||
| 105 | { | ||
| 106 | $this->end_of_path = $end_of_path; | ||
| 107 | } | ||
| 108 | public function getDescription(): string | ||
| 109 | { | ||
| 110 | return $this->description; | ||
| 111 | } | ||
| 112 | public function setDescription(string $description): void | ||
| 113 | { | ||
| 114 | $this->description = $description; | ||
| 115 | } | ||
| 116 | |||
| 117 | public function getCSS(): array | ||
| 118 | { | ||
| 119 | return array_merge(self::$default_css, $this->css ?? []); | ||
| 120 | } | ||
| 121 | public function addCSS(string $css): void | ||
| 122 | { | ||
| 123 | if(!in_array($css, $this->css ?? [])){ | ||
| 124 | $this->css[] = $css; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | public function removeCSS(string $css): void | ||
| 128 | { | ||
| 129 | // array_diff renvoie une copie du 1er tableau alégée des élements existants aussi dans le 2è, array_values réindexe | ||
| 130 | $this->css = array_values(array_diff($this->css, [$css])); | ||
| 131 | } | ||
| 132 | public function getJS(): array | ||
| 133 | { | ||
| 134 | return array_merge(self::$default_js, $this->js ?? []); | ||
| 135 | //UPDATE `nb_page` SET `js` = NULL WHERE JSON_EQUALS(`js`, '["main"]'); | ||
| 136 | } | ||
| 137 | public function addJS(string $js): void | ||
| 138 | { | ||
| 139 | if(!in_array($js, $this->js ?? [])){ | ||
| 140 | $this->js[] = $js; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | public function removeJS(string $js): void | ||
| 144 | { | ||
| 145 | $this->js = array_values(array_diff($this->js, [$js])); | ||
| 146 | } | ||
| 147 | |||
| 148 | public function isReachable(): bool | ||
| 149 | { | ||
| 150 | return $this->reachable; | ||
| 151 | } | ||
| 152 | public function isInMenu(): bool | ||
| 153 | { | ||
| 154 | return $this->in_menu; | ||
| 155 | } | ||
| 156 | public function isHidden(): bool | ||
| 157 | { | ||
| 158 | return $this->hidden; | ||
| 159 | } | ||
| 160 | public function setHidden(bool $hidden): void | ||
| 161 | { | ||
| 162 | $this->hidden = $hidden; | ||
| 163 | } | ||
| 164 | public function getPosition(): ?int | ||
| 165 | { | ||
| 166 | return $this->position; | ||
| 167 | } | ||
| 168 | public function setPosition(int $position): void | ||
| 169 | { | ||
| 170 | $this->position = $position; | ||
| 171 | } | ||
| 172 | public function getParent(): ?Page | ||
| 173 | { | ||
| 174 | return $this->parent; | ||
| 175 | } | ||
| 176 | public function setParent(?Page $parent): void | ||
| 177 | { | ||
| 178 | $this->parent = $parent; | ||
| 179 | } | ||
| 180 | public function getChildren(): Collection | ||
| 181 | { | ||
| 182 | return $this->children; | ||
| 183 | } | ||
| 184 | |||
| 185 | // utilisée par $menu_path | ||
| 186 | public function fillChildrenPagePath(string $parent_path = ''): void | ||
| 187 | { | ||
| 188 | $this->page_path = $parent_path != '' ? $parent_path . '/' . $this->end_of_path : $this->end_of_path; | ||
| 189 | foreach($this->getChildren() as $page){ | ||
| 190 | $page->fillChildrenPagePath($this->page_path); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | public function addChild(self $child): void | ||
| 195 | { | ||
| 196 | $this->children[] = $child; | ||
| 197 | $this->sortChildren(false); | ||
| 198 | } | ||
| 199 | public function removeChild(self $child): void | ||
| 200 | { | ||
| 201 | $this->children->removeElement($child); | ||
| 202 | $this->children = new ArrayCollection(array_values($this->children->toArray())); // réindexer en passant par un tableau | ||
| 203 | $this->sortChildren(false); | ||
| 204 | } | ||
| 205 | |||
| 206 | public function findPageById(int $id): ?Page | ||
| 207 | { | ||
| 208 | $target = null; | ||
| 209 | foreach($this->children as $page){ | ||
| 210 | if($page->getId() === $id){ | ||
| 211 | return $page; | ||
| 212 | } | ||
| 213 | if(count($page->getChildren()) > 0){ | ||
| 214 | $target = $page->findPageById($id); | ||
| 215 | if($target !== null){ | ||
| 216 | return $target; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | return $target; | ||
| 221 | } | ||
| 222 | } | ||
