| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace internship\model;
- use n2n\web\dispatch\Dispatchable;
- use internship\bo\NewsCategory;
- use internship\bo\NewsTag;
- use n2n\web\dispatch\map\bind\BindingDefinition;
- use n2n\impl\web\dispatch\map\val\ValNotEmpty;
- use n2n\impl\web\dispatch\map\val\ValImageFile;
- use n2n\io\managed\File;
- use n2n\web\dispatch\map\MappingResult;
- use n2n\util\io\IoUtils;
- use internship\bo\NewsItem;
- use n2n\web\http\PageNotFoundException;
- use n2n\util\type\ArgUtils;
- class NewsForm implements Dispatchable {
- private ?NewsItem $news = null;
- protected string $title;
- protected string $content;
- protected ?File $imageFile = null;
- protected array $tagIds = array();
- /**
- * @param NewsCategory $category
- * @param NewsTag[] $tags
- * @param NewsItem|null $news
- */
- public function __construct(private NewsCategory $category, private array $tags, NewsItem $news = null) {
- ArgUtils::valArray($tags, NewsTag::class);
- if ($news === null) {
- return;
- }
- $this->title = $news->getTitle();
- $this->content = $news->getContent();
- $this->id = $news->getId();
- $this->tagIds = array_map(fn(NewsTag $t) => $t->getId(), $news->getTags()->getArrayCopy());
- $this->news = $news;
- }
- public function getTitle(): ?string {
- return $this->title ?? null;
- }
- /**
- * @param string $title
- */
- public function setTitle(string $title) {
- $this->title = $title;
- }
- public function getImageFile(): ?File {
- return $this->imageFile;
- }
- public function setImageFile(?File $imageFile) {
- $this->imageFile = $imageFile;
- }
- /**
- * @return array
- */
- public function getTagIds(): array {
- return $this->tagIds;
- }
- /**
- * @param array $tagIds
- */
- public function setTagIds(array $tagIds): void {
- $this->tagIds = $tagIds;
- }
- /**
- * @return string
- */
- public function getContent(): ?string {
- return $this->content ?? null;
- }
- /**
- * @param string $content
- */
- public function setContent(string $content) {
- $this->content = $content;
- }
- public function getTagOptions(): array {
- return $this->tags;
- }
- private function _mapping(MappingResult $mr) {
- $mr->setLabel('title', 'Titel');
- $mr->setLabel('content', 'Beschreibung');
- $mr->setLabel('imageFile', 'Bild');
- $mr->setLabel('tagIds', 'zugewiesene Tags');
- }
- private function _validation(BindingDefinition $bd) {
- $bd->val(array('content', 'title'), new ValNotEmpty());
- $bd->val('imageFile', new ValImageFile(true));
- }
- public function save(NewsDao $newsDao) {
- $selectedTagObjects = new \ArrayObject();
- foreach ($this->tags as $tagObject) {
- if (in_array($tagObject->getId(),$this->tagIds )) {
- $selectedTagObjects[] = $tagObject;
- }
- }
- $news = $this->news ?? new NewsItem();
- $news->setTitle($this->title);
- $news->setLead(substr_replace($this->content, "...", 50));
- $news->setContent($this->content);
- $news->setImageFile($this->imageFile);
- $news->setUrlPart(strtolower(IoUtils::stripSpecialChars($this->title)));
- $news->setCategory($this->category);
- $news->setTags($selectedTagObjects);
- $newsDao->saveNews($news);
- }
- public function delete(NewsDao $newsDao) {
- if (!$this->news) {
- throw new PageNotFoundException('no id set');
- }
- $news = $this->news;
- $newsDao->deleteNews($news);
- }
- }
|