Issues (281)

Branch: master

src/Backend/Modules/Faq/Actions/AddCategory.php (1 issue)

1
<?php
2
3
namespace Backend\Modules\Faq\Actions;
4
5
use Backend\Core\Engine\Base\ActionAdd as BackendBaseActionAdd;
6
use Backend\Core\Engine\Form as BackendForm;
7
use Backend\Core\Language\Language as BL;
8
use Backend\Core\Engine\Meta as BackendMeta;
9
use Backend\Core\Engine\Model as BackendModel;
10
use Backend\Modules\Faq\Engine\Model as BackendFaqModel;
11
12
/**
13
 * This is the add-action, it will display a form to create a new category
14
 */
15
class AddCategory extends BackendBaseActionAdd
16
{
17
    public function execute(): void
18
    {
19
        // only one category allowed, so we redirect
20
        if (!$this->get('fork.settings')->get('Faq', 'allow_multiple_categories', true)) {
21
            $this->redirect(BackendModel::createUrlForAction('Categories') . '&error=only-one-category-allowed');
22
        }
23
24
        parent::execute();
25
        $this->loadForm();
26
        $this->validateForm();
27
        $this->parse();
28
        $this->display();
29
    }
30
31
    public function parse(): void
32
    {
33
        parent::parse();
34
35
        $url = BackendModel::getUrlForBlock($this->url->getModule(), 'Category');
36
        $url404 = BackendModel::getUrl(BackendModel::ERROR_PAGE_ID);
37
        if ($url404 != $url) {
38
            $this->template->assign('detailURL', SITE_URL . $url);
39
        }
40
    }
41
42
    private function loadForm(): void
43
    {
44
        $this->form = new BackendForm('addCategory');
45
        $this->form->addText('title')->makeRequired();
46
47
        $this->meta = new BackendMeta($this->form, null, 'title', true);
0 ignored issues
show
Deprecated Code introduced by
The class Backend\Core\Engine\Meta has been deprecated: This class will be removed when all modules run on doctrine and will be replaced with the meta entity ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

47
        $this->meta = /** @scrutinizer ignore-deprecated */ new BackendMeta($this->form, null, 'title', true);
Loading history...
48
    }
49
50
    private function validateForm(): void
51
    {
52
        if ($this->form->isSubmitted()) {
53
            $this->meta->setUrlCallback('Backend\Modules\Faq\Engine\Model', 'getUrlForCategory');
54
55
            $this->form->cleanupFields();
56
57
            // validate fields
58
            $this->form->getField('title')->isFilled(BL::err('TitleIsRequired'));
59
            $this->meta->validate();
60
61
            if ($this->form->isCorrect()) {
62
                // build item
63
                $item = [];
64
                $item['title'] = $this->form->getField('title')->getValue();
65
                $item['language'] = BL::getWorkingLanguage();
66
                $item['meta_id'] = $this->meta->save();
67
                $item['sequence'] = BackendFaqModel::getMaximumCategorySequence() + 1;
68
69
                // save the data
70
                $item['id'] = BackendFaqModel::insertCategory($item);
71
72
                // everything is saved, so redirect to the overview
73
                $this->redirect(
74
                    BackendModel::createUrlForAction('Categories') . '&report=added-category&var=' .
75
                    rawurlencode($item['title']) . '&highlight=row-' . $item['id']
76
                );
77
            }
78
        }
79
    }
80
}
81