ActionUpdate::update()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 1
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Admin\Content;
4
5
use Apps\ActiveRecord\Content as ContentEntity;
6
use Apps\Model\Admin\Content\FormContentUpdate;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Network\Request;
10
use Ffcms\Core\Network\Response;
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::with(['commentPosts', 'commentPosts.user', 'commentPosts.user.profile'])
31
            ->withTrashed()
32
            ->findOrNew($id);
33
        $isNew = $record->id === null;
34
        $cloneId = (int)$this->request->query->get('from', 0);
35
36
        // init model
37
        $model = new FormContentUpdate($record, $cloneId);
38
39
        // check if model is submit
40
        if ($model->send() && $model->validate()) {
41
            $model->save();
42
            if ($isNew) {
43
                $this->response->redirect('content/index');
44
            }
45
            App::$Session->getFlashBag()->add('success', __('Content is successful updated'));
46
        }
47
48
        // draw response
49
        return $this->view->render('content/content_update', [
50
            'model' => $model
51
        ]);
52
    }
53
}
54