1 | <?php |
||
2 | |||
3 | namespace Apps\Controller\Front\Content; |
||
4 | |||
5 | use Apps\ActiveRecord\Content as ContentRecord; |
||
6 | use Apps\Model\Front\Content\FormNarrowContentUpdate; |
||
7 | use Ffcms\Core\App; |
||
8 | use Ffcms\Core\Arch\View; |
||
9 | use Ffcms\Core\Exception\ForbiddenException; |
||
10 | use Ffcms\Core\Exception\NotFoundException; |
||
11 | use Ffcms\Core\Network\Request; |
||
12 | use Ffcms\Core\Network\Response; |
||
13 | |||
14 | /** |
||
15 | * Trait ActionUpdate |
||
16 | * @package Apps\Controller\Front\Content |
||
17 | * @property View $view |
||
18 | * @property Request $request |
||
19 | * @property Response $response |
||
20 | * @method array getConfigs |
||
21 | */ |
||
22 | trait ActionUpdate |
||
23 | { |
||
24 | /** |
||
25 | * Update personal content items or add new content item |
||
26 | * @param string|null $id |
||
27 | * @return null|string |
||
28 | * @throws ForbiddenException |
||
29 | * @throws NotFoundException |
||
30 | * @throws \Ffcms\Core\Exception\SyntaxException |
||
31 | */ |
||
32 | public function update(?string $id = null): ?string |
||
33 | { |
||
34 | // check if user is auth |
||
35 | if (!App::$User->isAuth()) { |
||
36 | throw new ForbiddenException(__('Only authorized users can add content')); |
||
37 | } |
||
38 | |||
39 | // check if user add enabled |
||
40 | $configs = $this->getConfigs(); |
||
41 | if (!(bool)$configs['userAdd']) { |
||
42 | throw new NotFoundException(__('User add is disabled')); |
||
43 | } |
||
44 | |||
45 | // find record in db |
||
46 | $record = ContentRecord::findOrNew($id); |
||
47 | $new = $record->id === null; |
||
48 | |||
49 | // reject edit published items and items from other authors |
||
50 | if (($new === false && (int)$record->author_id !== App::$User->identity()->getId()) || (int)$record->display === 1) { |
||
51 | throw new ForbiddenException(__('You have no permissions to edit this content')); |
||
52 | } |
||
53 | |||
54 | // initialize model |
||
55 | $model = new FormNarrowContentUpdate($record, $configs); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
56 | if ($model->send() && $model->validate()) { |
||
57 | $model->make(); |
||
58 | // if is new - make redirect to listing & add notify |
||
59 | if ($new === true) { |
||
60 | App::$Session->getFlashBag()->add('success', __('Content successfully added')); |
||
61 | $this->response->redirect('content/my'); |
||
62 | } else { |
||
63 | App::$Session->getFlashBag()->add('success', __('Content successfully updated')); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // render view output |
||
68 | return $this->view->render('update', [ |
||
69 | 'model' => $model, |
||
70 | 'configs' => $configs |
||
71 | ]); |
||
72 | } |
||
73 | } |
||
74 |