Issues (281)

Branch: master

src/Backend/Modules/Blog/Actions/Add.php (1 issue)

1
<?php
2
3
namespace Backend\Modules\Blog\Actions;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Backend\Core\Engine\Base\ActionAdd as BackendBaseActionAdd;
7
use Backend\Core\Engine\Authentication as BackendAuthentication;
8
use Backend\Core\Engine\Form as BackendForm;
9
use Backend\Core\Language\Language as BL;
10
use Backend\Core\Engine\Model as BackendModel;
11
use Backend\Core\Engine\Meta as BackendMeta;
12
use Backend\Modules\Blog\Engine\Model as BackendBlogModel;
13
use Backend\Modules\Search\Engine\Model as BackendSearchModel;
14
use Backend\Modules\Tags\Engine\Model as BackendTagsModel;
15
use Backend\Modules\Users\Engine\Model as BackendUsersModel;
16
17
/**
18
 * This is the add-action, it will display a form to create a new item
19
 */
20
class Add extends BackendBaseActionAdd
21
{
22
    /**
23
     * Is the image field allowed?
24
     *
25
     * @var bool
26
     */
27
    protected $imageIsAllowed = true;
28
29
    public function execute(): void
30
    {
31
        parent::execute();
32
        $this->loadForm();
33
        $this->validateForm();
34
        $this->parse();
35
        $this->display();
36
    }
37
38
    private function loadForm(): void
39
    {
40
        $this->imageIsAllowed = $this->get('fork.settings')->get($this->url->getModule(), 'show_image_form', true);
41
42
        $this->form = new BackendForm('add');
43
44
        // set hidden values
45
        $rbtHiddenValues = [
46
            ['label' => BL::lbl('Hidden', $this->url->getModule()), 'value' => 1],
47
            ['label' => BL::lbl('Published'), 'value' => 0],
48
        ];
49
50
        // get categories
51
        $categories = BackendBlogModel::getCategories();
52
        $categories['new_category'] = \SpoonFilter::ucfirst(BL::getLabel('AddCategory'));
53
54
        // create elements
55
        $this->form->addText('title', null, null, 'form-control title', 'form-control danger title')->makeRequired();
56
        $this->form->addEditor('text')->makeRequired();
57
        $this->form->addEditor('introduction');
58
        $this->form->addRadiobutton('hidden', $rbtHiddenValues, 0);
59
        $this->form->addCheckbox('allow_comments', $this->get('fork.settings')->get($this->getModule(), 'allow_comments', false));
60
        $this->form->addDropdown('category_id', $categories, $this->getRequest()->query->getInt('category'));
61
        if (count($categories) !== 2) {
62
            $this->form->getField('category_id')->setDefaultElement('');
63
        }
64
        $this->form->addDropdown('user_id', BackendUsersModel::getUsers(), BackendAuthentication::getUser()->getUserId());
65
        $this->form->addText(
66
            'tags',
67
            null,
68
            null,
69
            'form-control js-tags-input',
70
            'form-control danger js-tags-input'
71
        )->setAttribute('aria-describedby', 'tags-info');
72
        $this->form->addDate('publish_on_date');
73
        $this->form->addTime('publish_on_time');
74
        if ($this->imageIsAllowed) {
75
            $this->form->addImage('image');
76
        }
77
78
        // meta
79
        $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

79
        $this->meta = /** @scrutinizer ignore-deprecated */ new BackendMeta($this->form, null, 'title', true);
Loading history...
80
    }
81
82
    protected function parse(): void
83
    {
84
        parent::parse();
85
        $this->template->assign('imageIsAllowed', $this->imageIsAllowed);
86
87
        // parse base url for preview
88
        $url = BackendModel::getUrlForBlock($this->url->getModule(), 'Detail');
89
        $url404 = BackendModel::getUrl(BackendModel::ERROR_PAGE_ID);
90
        if ($url404 !== $url) {
91
            $this->template->assign('detailURL', SITE_URL . $url);
92
        }
93
    }
94
95
    private function validateForm(): void
96
    {
97
        // is the form submitted?
98
        if ($this->form->isSubmitted()) {
99
            // get the status
100
            $status = $this->getRequest()->request->has('saveAsDraft') ? 'draft' : 'active';
101
102
            // cleanup the submitted fields, ignore fields that were added by hackers
103
            $this->form->cleanupFields();
104
105
            // validate fields
106
            $this->form->getField('title')->isFilled(BL::err('TitleIsRequired'));
107
            $this->form->getField('text')->isFilled(BL::err('FieldIsRequired'));
108
            $this->form->getField('publish_on_date')->isValid(BL::err('DateIsInvalid'));
109
            $this->form->getField('publish_on_time')->isValid(BL::err('TimeIsInvalid'));
110
            $this->form->getField('category_id')->isFilled(BL::err('FieldIsRequired'));
111
            if ($this->form->getField('category_id')->getValue() == 'new_category') {
112
                $this->form->getField('category_id')->addError(BL::err('FieldIsRequired'));
113
            }
114
115
            // validate meta
116
            $this->meta->validate();
117
118
            if ($this->form->isCorrect()) {
119
                // build item
120
                $item = [
121
                    'id' => (int) BackendBlogModel::getMaximumId() + 1,
122
                    'meta_id' => $this->meta->save(),
123
                    'category_id' => (int) $this->form->getField('category_id')->getValue(),
124
                    'user_id' => $this->form->getField('user_id')->getValue(),
125
                    'language' => BL::getWorkingLanguage(),
126
                    'title' => $this->form->getField('title')->getValue(),
127
                    'introduction' => $this->form->getField('introduction')->getValue(),
128
                    'text' => $this->form->getField('text')->getValue(),
129
                    'publish_on' => BackendModel::getUTCDate(
130
                        null,
131
                        BackendModel::getUTCTimestamp(
132
                            $this->form->getField('publish_on_date'),
133
                            $this->form->getField('publish_on_time')
134
                        )
135
                    ),
136
                    'created_on' => BackendModel::getUTCDate(),
137
                    'hidden' => $this->form->getField('hidden')->getValue(),
138
                    'allow_comments' => $this->form->getField('allow_comments')->getChecked(),
139
                    'num_comments' => 0,
140
                    'status' => $status,
141
                ];
142
                $item['edited_on'] = $item['created_on'];
143
144
                // insert the item
145
                $item['revision_id'] = BackendBlogModel::insert($item);
146
147
                if ($this->imageIsAllowed) {
148
                    // the image path
149
                    $imagePath = FRONTEND_FILES_PATH . '/Blog/images';
150
151
                    // create folders if needed
152
                    $filesystem = new Filesystem();
153
                    $filesystem->mkdir([$imagePath . '/source', $imagePath . '/128x128']);
154
155
                    // image provided?
156
                    if ($this->form->getField('image')->isFilled()) {
157
                        // build the image name
158
                        $item['image'] = $this->meta->getUrl()
159
                            . '-' . BL::getWorkingLanguage()
160
                            . '-' . $item['revision_id']
161
                            . '.' . $this->form->getField('image')->getExtension();
162
163
                        // upload the image & generate thumbnails
164
                        $this->form->getField('image')->generateThumbnails($imagePath, $item['image']);
165
166
                        // add the image to the database without changing the revision id
167
                        BackendBlogModel::updateRevision($item['revision_id'], ['image' => $item['image']]);
168
                    }
169
                }
170
171
                // save the tags
172
                BackendTagsModel::saveTags($item['id'], $this->form->getField('tags')->getValue(), $this->url->getModule());
173
174
                // active
175
                if ($item['status'] == 'active') {
176
                    // add search index
177
                    BackendSearchModel::saveIndex($this->getModule(), $item['id'], ['title' => $item['title'], 'text' => $item['text']]);
178
179
                    // everything is saved, so redirect to the overview
180
                    $this->redirect(BackendModel::createUrlForAction('Index') . '&report=added&var=' . rawurlencode($item['title']) . '&highlight=row-' . $item['revision_id']);
181
                } elseif ($item['status'] == 'draft') {
182
                    // draft: everything is saved, so redirect to the edit action
183
                    $this->redirect(BackendModel::createUrlForAction('Edit') . '&report=saved-as-draft&var=' . rawurlencode($item['title']) . '&id=' . $item['id'] . '&draft=' . $item['revision_id'] . '&highlight=row-' . $item['revision_id']);
184
                }
185
            }
186
        }
187
    }
188
}
189