| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace internship\model;
- use n2n\web\dispatch\Dispatchable;
- use internship\bo\NewsCategory;
- 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 n2n\web\http\PageNotFoundException;
- class CategoryForm implements Dispatchable {
- private NewsCategory $category;
- protected string $title;
- protected string $content;
- protected $imageFile;
- public function __construct(NewsCategory $category = null) {
- $this->title = '';
- $this->content = '';
- if ($category) {
- $this->title = $category->getTitle();
- $this->id = $category->getId();
- $this->content = $category->getContent();
- $this->category = $category;
- }
- }
- public function getTitle() {
- return $this->title;
- }
- public function setTitle(string $title) {
- $this->title = $title;
- }
- public function getImageFile() {
- return $this->imageFile;
- }
- public function setImageFile(File $imageFile = null) {
- $this->imageFile = $imageFile;
- }
- public function getContent() {
- return $this->content;
- }
- public function setContent(string $content) {
- $this->content = $content;
- }
- private function _mapping(MappingResult $mr) {
- $mr->setLabel('title', 'Titel');
- $mr->setLabel('content', 'Beschreibung');
- $mr->setLabel('imageFile', 'Bild');
- }
- private function _validation(BindingDefinition $bd) {
- $bd->val(array('content', 'title'), new ValNotEmpty());
- $bd->val('imageFile', new ValImageFile(true));
- }
- public function save(NewsDao $newsDao) {
- $category = $this->category ?? new NewsCategory();
- $category->setTitle($this->title);
- $category->setLead(substr_replace($this->content, "...", 50));
- $category->setContent($this->content);
- $category->setImageFile($this->imageFile);
- $category->setUrlPart(strtolower(IoUtils::stripSpecialChars($this->title)));
- $newsDao->saveCategory($category);
- }
- public function delete(NewsDao $newsDao) {
- if (!$this->category) {
- throw new PageNotFoundException('no id set');
- }
- $category = $this->category;
- $newsDao->deleteCategory($category);
- }
- }
|