Passed
Push — master ( 1dbfa0...aaaee4 )
by Mihail
07:38
created

Comments::actionRead()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
6
use Apps\ActiveRecord\CommentAnswer;
7
use Apps\ActiveRecord\CommentPost;
8
use Apps\Model\Admin\Comments\FormSettings;
9
use Extend\Core\Arch\AdminController;
10
use Ffcms\Core\App;
11
use Ffcms\Core\Exception\NotFoundException;
12
use Ffcms\Core\Helper\HTML\SimplePagination;
13
use Ffcms\Core\Helper\Type\Arr;
14
15
/**
16
 * Class Comments. Admin controller for management user comments.
17
 * This class provide general admin implementation of control for user comments and its settings.
18
 * @package Apps\Controller\Admin
19
 */
20
class Comments extends AdminController
21
{
22
    const VERSION = 0.1;
23
    const ITEM_PER_PAGE = 10;
24
25
    const TYPE_COMMENT = 'comment';
26
    const TYPE_ANSWER = 'answer';
27
28
    public $type = 'widget';
29
30
    /**
31
     * List user comments with pagination
32
     * @return string
33
     * @throws \Ffcms\Core\Exception\SyntaxException
34
     */
35 View Code Duplication
    public function actionIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        // set current page and offset
38
        $page = (int)App::$Request->query->get('page');
39
        $offset = $page * self::ITEM_PER_PAGE;
40
41
        // initialize active record model
42
        $query = new CommentPost();
43
44
        // make pagination
45
        $pagination = new SimplePagination([
46
            'url' => ['comments/index'],
47
            'page' => $page,
48
            'step' => self::ITEM_PER_PAGE,
49
            'total' => $query->count()
50
        ]);
51
52
        // get result as active records object with offset
53
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
54
55
        // render output view
56
        return App::$View->render('index', [
57
            'records' => $records,
58
            'pagination' => $pagination
59
        ]);
60
    }
61
62
    /**
63
     * List comment - read comment and list answers
64
     * @param int $id
65
     * @return string
66
     * @throws NotFoundException
67
     * @throws \Ffcms\Core\Exception\SyntaxException
68
     */
69
    public function actionRead($id)
70
    {
71
        // find object in active record model
72
        $record = CommentPost::find($id);
73
        if ($record === null || $record === false) {
74
            throw new NotFoundException(__('Comment is not founded'));
75
        }
76
77
        // render response
78
        return App::$View->render('comment_read', [
79
            'record' => $record
80
        ]);
81
    }
82
83
    public function actionEdit($type, $id)
84
    {
85
        // get active record by type and id from active records
86
        $record = null;
87
        switch ($type) {
88
            case static::TYPE_COMMENT:
89
                $record = CommentPost::find($id);
90
                break;
91
            case static::TYPE_ANSWER:
92
                $record = CommentAnswer::find($id);
93
                break;
94
        }
95
96
        // check if response is not empty
97
        if ($record === null || $record->count() != 1) {
98
            throw new NotFoundException(__('Comment is not founded'));
99
        }
100
101
102
103
    }
104
105
    public function actionAnswerlist()
106
    {
107
108
    }
109
110
    /**
111
     * Comment widget global settings
112
     * @return string
113
     * @throws \Ffcms\Core\Exception\SyntaxException
114
     */
115 View Code Duplication
    public function actionSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        // initialize settings model
118
        $model = new FormSettings($this->getConfigs());
119
120
        // check if form is send
121
        if ($model->send()) {
122
            if ($model->validate()) {
123
                $this->setConfigs($model->getAllProperties());
124
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
125
                App::$Response->redirect('comments/index');
126
            } else {
127
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
128
            }
129
        }
130
131
        // render view
132
        return App::$View->render('settings', [
133
            'model' => $model
134
        ]);
135
    }
136
137
138
139
140
}