Passed
Push — master ( a29a7e...12a432 )
by Mihail
08:06
created

ActionUpdate::update()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace Apps\Controller\Admin\Content;
4
5
use Apps\Model\Admin\Content\FormContentUpdate;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\View;
8
use Ffcms\Core\Network\Request;
9
use Ffcms\Core\Network\Response;
10
use Apps\ActiveRecord\Content as ContentEntity;
11
12
/**
13
 * Trait ActionUpdate
14
 * @package Apps\Controller\Admin\Content
15
 * @property Request $request
16
 * @property Response $response
17
 * @property View $view
18
 */
19
trait ActionUpdate
20
{
21
    /**
22
     * Edit and add content items
23
     * @param string|null $id
24
     * @return string
25
     * @throws \Ffcms\Core\Exception\SyntaxException
26
     */
27
    public function update(?string $id = null): ?string
28
    {
29
        // get item with trashed objects
30
        $record = ContentEntity::withTrashed()->findOrNew($id);
31
        $isNew = $record->id === null;
32
33
        // init model
34
        $model = new FormContentUpdate($record);
35
36
        // check if model is submit
37
        if ($model->send() && $model->validate()) {
38
            $model->save();
39
            if ($isNew === true) {
40
                $this->response->redirect('content/index');
41
            }
42
            App::$Session->getFlashBag()->add('success', __('Content is successful updated'));
43
        }
44
45
        // draw response
46
        return $this->view->render('content_update', [
47
            'model' => $model
48
        ]);
49
    }
50
}
51