src/Entity/Content/Content.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Content;
  3. use App\Entity\EntityId;
  4. use App\Entity\ObjectFileHandlingInterface;
  5. use App\Entity\Redaktor\RedaktorPortraitImage;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Filesystem\Filesystem;
  8. use Symfony\Component\Finder\Finder;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Webmozart\Assert\Assert;
  11. #[ORM\Entity]
  12. #[ORM\Table(name: 'content')]
  13. class Content implements ObjectFileHandlingInterface
  14. {
  15. public const int MAX_LENGTH_CONTENT_DESCRIPTION = 300;
  16. public const int MAX_LENGTH_CODE = 32;
  17. public const int MAX_WIDTH_IMAGE = 2048;
  18. #[ORM\Column(name: 'code', type: 'string', length: 32, unique: true)]
  19. private string $code;
  20. #[ORM\Column(name: 'contentHtml', type: 'text', nullable: true)]
  21. private ?string $contentHtml = null;
  22. #[ORM\Column(name: 'content_html_editable', type: 'boolean', nullable: true)]
  23. private bool $contentHtmlEditable = true;
  24. #[ORM\Column(name: 'content_text', type: 'text', nullable: true)]
  25. private ?string $contentText = null;
  26. #[ORM\Column(name: 'content_text_editable', type: 'boolean', nullable: true)]
  27. private ?bool $contentTextEditable = false;
  28. #[ORM\Column(name: 'content_description', type: 'text', length: 300, nullable: true)]
  29. private ?string $contentDescription = null;
  30. #[ORM\Column(name: 'content_image', type: 'json', nullable: true)]
  31. private ?array $contentImage = [];
  32. #[ORM\Column(name: 'content_image_editable', type: 'boolean', nullable: true)]
  33. private bool $contentImageEditable = false;
  34. #[ORM\Column(name: 'edit_sysadmin_only', type: 'boolean', nullable: true)]
  35. private bool $editSysadminOnly = true;
  36. #[ORM\Column(name: 'is_visible_on_frontend', type: 'boolean', nullable: true)]
  37. private bool $isVisibleOnFrontend = true;
  38. #[ORM\Column(name: 'edit_at', type: 'datetime', nullable: false)]
  39. private ?\DateTimeInterface $editAt = null;
  40. public function __construct(
  41. #[ORM\Id]
  42. #[ORM\Column(name: 'id', type: 'entityId')]
  43. #[ORM\GeneratedValue(strategy: 'NONE')]
  44. private readonly EntityId $id)
  45. {
  46. $this->code = 'NEW_'.date('Ymd_His');
  47. $this->setEditAtNow();
  48. }
  49. public function id(): EntityId
  50. {
  51. return $this->id;
  52. }
  53. public function changeConfig(array $options): void
  54. {
  55. $resolver = new OptionsResolver();
  56. $resolver->setRequired([
  57. 'code',
  58. 'content_description',
  59. 'edit_sysadmin_only',
  60. 'content_image_editable',
  61. 'content_html_editable',
  62. 'content_text_editable',
  63. ]);
  64. $options = $resolver->resolve($options);
  65. $this->code = $options['code'];
  66. $this->setContentDescription($options['content_description']);
  67. $this->setEditSysadminOnly($options['edit_sysadmin_only']);
  68. $this->setContentImageEditable($options['content_image_editable']);
  69. $this->setContentHtmlEditable($options['content_html_editable']);
  70. $this->setContentTextEditable($options['content_text_editable']);
  71. $this->setEditAtNow();
  72. }
  73. public function change(array $options): void
  74. {
  75. $resolver = new OptionsResolver();
  76. $resolver->setRequired('is_visible_on_frontend');
  77. $resolver->setDefined(
  78. [
  79. 'content_image',
  80. 'content_html',
  81. 'content_text',
  82. ]
  83. );
  84. $options = $resolver->resolve($options);
  85. $this->setIsVisibleOnFrontend($options['is_visible_on_frontend']);
  86. if (array_key_exists('content_image', $options)) {
  87. $this->setContentImage($options['content_image']);
  88. }
  89. if (array_key_exists('content_html', $options)) {
  90. $this->setContentHtml($options['content_html']);
  91. }
  92. if (array_key_exists('content_text', $options)) {
  93. $this->setContentText($options['content_text']);
  94. }
  95. $this->setEditAtNow();
  96. }
  97. public function code(): string
  98. {
  99. return $this->code;
  100. }
  101. public function contentHtml(): ?string
  102. {
  103. return $this->contentHtml;
  104. }
  105. public function contentText(): ?string
  106. {
  107. return $this->contentText;
  108. }
  109. public function contentDescription(): ?string
  110. {
  111. return $this->contentDescription;
  112. }
  113. public function editSysadminOnly(): bool
  114. {
  115. return $this->editSysadminOnly;
  116. }
  117. public function contentImageEditable(): bool
  118. {
  119. return $this->contentImageEditable;
  120. }
  121. public function contentHtmlEditable(): bool
  122. {
  123. return $this->contentHtmlEditable;
  124. }
  125. public function contentTextEditable(): ?bool
  126. {
  127. return $this->contentTextEditable;
  128. }
  129. public function isVisibleOnFrontend(): bool
  130. {
  131. return $this->isVisibleOnFrontend;
  132. }
  133. public function editAt(): ?\DateTimeInterface
  134. {
  135. return $this->editAt;
  136. }
  137. private function setContentDescription(?string $contentDescription): void
  138. {
  139. Assert::maxLength($contentDescription, self::MAX_LENGTH_CONTENT_DESCRIPTION);
  140. $this->contentDescription = $contentDescription;
  141. }
  142. private function setContentHtml(?string $contentHtml): void
  143. {
  144. $this->contentHtml = $contentHtml;
  145. }
  146. private function setContentHtmlEditable(bool $bool): void
  147. {
  148. Assert::boolean($bool);
  149. $this->contentHtmlEditable = $bool;
  150. }
  151. private function setContentText(?string $contentText): void
  152. {
  153. $this->contentText = $contentText;
  154. }
  155. private function setContentTextEditable(bool $bool): void
  156. {
  157. Assert::boolean($bool);
  158. $this->contentTextEditable = $bool;
  159. }
  160. private function setEditSysadminOnly(bool $editSysadminOnly): void
  161. {
  162. Assert::boolean($editSysadminOnly);
  163. $this->editSysadminOnly = $editSysadminOnly;
  164. }
  165. private function setContentImageEditable(bool $bool): void
  166. {
  167. Assert::boolean($bool);
  168. $this->contentImageEditable = $bool;
  169. }
  170. private function setIsVisibleOnFrontend(bool $isVisibleOnFrontend): void
  171. {
  172. Assert::boolean($isVisibleOnFrontend);
  173. $this->isVisibleOnFrontend = $isVisibleOnFrontend;
  174. }
  175. private function setEditAtNow(): void
  176. {
  177. $this->editAt = new \DateTime();
  178. }
  179. private function setContentImage(ContentImage $image): void
  180. {
  181. $this->contentImage['filename'] = $image->getFilename();
  182. $this->contentImage['alt'] = $image->getAlt();
  183. $this->contentImage['caption'] = $image->getCaption();
  184. $this->contentImage['click_url'] = $image->getClickUrl();
  185. }
  186. public function contentImage(): RedaktorPortraitImage|ContentImage
  187. {
  188. if ($this->contentImage && array_key_exists('filename', $this->contentImage)) {
  189. $fileName = $this->contentImage['filename'];
  190. $alt = array_key_exists('alt', $this->contentImage) ? $this->contentImage['alt'] : '';
  191. $caption = array_key_exists('caption', $this->contentImage) ? $this->contentImage['caption'] : '';
  192. $clickUrl = array_key_exists('click_url', $this->contentImage) ? $this->contentImage['click_url'] : '';
  193. return ContentImage::create($fileName, $alt, $caption, $clickUrl, $this->baseObjectServerDirPath(), $this->baseObjectWebDirPath());
  194. }
  195. return ContentImage::createNonExistImage();
  196. }
  197. #[\Override]
  198. public function baseObjectServerDirPath(): string
  199. {
  200. return __DIR__.'/../../../var/data/public/content/'.$this->id;
  201. }
  202. #[\Override]
  203. public function baseObjectWebDirPath(): string
  204. {
  205. return '/data/content/'.$this->id;
  206. }
  207. #[\Override]
  208. public function removeUnusedFiles(): void
  209. {
  210. $serverPath = $this->baseObjectServerDirPath();
  211. // Auflistung aller effektiv benötigter Filenamen dieses Objects, damit diese erhalten bleiben
  212. $usedFiles_ = [];
  213. if ($this->contentImage()->getFilename()) {
  214. $usedFiles_[] = $this->contentImage()->getFilename();
  215. }
  216. // Ende
  217. // Lösche alle Files aus dem Objektverzeichnis, welche nicht im Objekt vorhanden sind
  218. $fs = new Filesystem();
  219. if ($fs->exists($serverPath)) {
  220. $finder = new Finder();
  221. $finder->files()->in($serverPath);
  222. foreach ($finder as $file) {
  223. if (!in_array($file->getFilename(), $usedFiles_)) {
  224. $fs->remove($file->getRealPath());
  225. }
  226. }
  227. }
  228. // Ende
  229. }
  230. }