Passed
Push — master ( 68443e...939347 )
by Mihail
04:31
created

Feedback::actionCreate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.439
cc 5
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace Apps\Controller\Front;
4
5
use Apps\ActiveRecord\FeedbackPost;
6
use Apps\Model\Front\Feedback\FormAnswerAdd;
7
use Apps\Model\Front\Feedback\FormFeedbackAdd;
8
use Extend\Core\Arch\FrontAppController as Controller;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Exception\ForbiddenException;
11
use Ffcms\Core\Exception\NotFoundException;
12
use Ffcms\Core\Helper\HTML\SimplePagination;
13
use Ffcms\Core\Helper\Type\Obj;
14
use Ffcms\Core\Helper\Type\Str;
15
16
/**
17
 * Class Feedback. Create, read, update and delete app for user feedback
18
 * @package Apps\Controller\Front
19
 */
20
class Feedback extends Controller
21
{
22
    const ITEM_PER_PAGE = 10;
23
24
    /**
25
     * This action is not allowed there
26
     * @throws NotFoundException
27
     */
28
    public function actionIndex()
29
    {
30
        throw new NotFoundException('Nothing there...');
31
    }
32
33
    /**
34
     * Add new feedback message action
35
     * @return string
36
     * @throws ForbiddenException
37
     * @throws \Ffcms\Core\Exception\SyntaxException
38
     */
39
    public function actionCreate()
40
    {
41
        // get configs
42
        $configs = $this->getConfigs();
43
        if (!App::$User->isAuth() && (int)$configs['guestAdd'] !== 1) {
44
            throw new ForbiddenException(__('Feedback available only for authorized users'));
45
        }
46
47
        // initialize model
48
        $model = new FormFeedbackAdd((int)$configs['useCaptcha'] === 1);
49
        if ($model->send()) {
50
            if ($model->validate()) {
51
                // if validation is passed save data to db and get row
52
                $record = $model->make();
53
                App::$Session->getFlashBag()->add('success', __('Your message was added successful'));
54
                // todo: add email notification
55
                App::$Response->redirect('feedback/read/' . $record->id . '/' . $record->hash);
56
            } else {
57
                App::$Session->getFlashBag()->add('error', __('Message is not sended! Please, fix issues in form below'));
58
            }
59
        }
60
61
        // render output view
62
        return App::$View->render('create', [
63
            'model' => $model->export(),
64
            'useCaptcha' => (int)$configs['useCaptcha'] === 1
65
        ]);
66
    }
67
68
69
    /**
70
     * Read feedback message and answers and work with add answer model
71
     * @param int $id
72
     * @param string $hash
73
     * @return string
74
     * @throws ForbiddenException
75
     * @throws \Ffcms\Core\Exception\SyntaxException
76
     */
77
    public function actionRead($id, $hash)
78
    {
79
        if (!Obj::isLikeInt($id) || Str::length($hash) < 16 || Str::length($hash) > 64) {
80
            throw new ForbiddenException(__('The feedback request is not founded'));
81
        }
82
83
        // get feedback post record from database
84
        $recordPost = FeedbackPost::where('id', '=', $id)
85
            ->where('hash', '=', $hash)
86
            ->first();
87
88
        if ($recordPost === null) {
89
            throw new ForbiddenException(__('The feedback request is not founded'));
90
        }
91
92
        $userId = App::$User->isAuth() ? App::$User->identity()->getId() : 0;
93
        $model = null;
94
        // check if feedback post is not closed for answers
95
        if ((int)$recordPost->closed === 0) {
96
            // init new answer add model
97
            $model = new FormAnswerAdd($recordPost, $userId);
98
            // if answer is sender lets try to make it model
99
            if ($model->send() && $model->validate()) {
100
                $model->make();
101
                App::$Session->getFlashBag()->add('success', __('Your answer was added'));
102
                $model->clearProperties();
0 ignored issues
show
Bug introduced by
The method clearProperties() does not seem to exist on object<Apps\Model\Front\Feedback\FormAnswerAdd>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            }
104
        }
105
106
        // render output view
107
        return App::$View->render('read', [
108
            'model' => $model,
109
            'post' => $recordPost,
110
            'answers' => $recordPost->getAnswers()->get() // get feedback answers
111
        ]);
112
    }
113
114
    public function actionClose($id, $hash)
115
    {
116
        // get feedback post record from database
117
        $record = FeedbackPost::where('id', '=', $id)
118
            ->where('hash', '=', $hash)
119
            ->where('closed', '=', 0)
120
            ->first();
121
122
        // check does we found it
123
        if ($record === null) {
124
            throw new ForbiddenException(__('The feedback request is not founded'));
125
        }
126
127
        // check if action is submited
128
        if (App::$Request->request->get('closeRequest', false)) {
129
            // if created by authorized user
130
            if ((int)$record->user_id !== 0) {
131
                $user = App::$User->identity();
132
                // button is pressed not by request creator
133
                if ($user === null || $user->getId() !== (int)$record->user_id) {
134
                    throw new ForbiddenException(__('This feedback request was created by another user'));
135
                }
136
            }
137
138
            // switch closed to 1 and make sql query
139
            $record->closed = 1;
140
            $record->save();
141
142
            // add notification and redirect
143
            App::$Session->getFlashBag()->add('warning', __('Feedback request now is closed!'));
144
            App::$Response->redirect('feedback/read/' . $id . '/' . $hash);
145
        }
146
147
        return App::$View->render('close');
148
    }
149
150
    /**
151
     * List feedback requests messages from authorized user
152
     * @return string
153
     * @throws ForbiddenException
154
     * @throws \Ffcms\Core\Exception\SyntaxException
155
     */
156
    public function actionList()
157
    {
158
        // set current page and offset
159
        $page = (int)App::$Request->query->get('page');
160
        $offset = $page * self::ITEM_PER_PAGE;
161
162
        // check if user is authorized or throw exception
163
        if (!App::$User->isAuth()) {
164
            throw new ForbiddenException(__('Feedback listing available only for authorized users'));
165
        }
166
167
        // get current user object
168
        $user = App::$User->identity();
169
170
        // initialize query with major condition
171
        $query = FeedbackPost::where('user_id', '=', $user->getId());
172
173
        // build pagination
174
        $pagination = new SimplePagination([
175
            'url' => ['feedback/list'],
176
            'page' => $page,
177
            'step' => self::ITEM_PER_PAGE,
178
            'total' => $query->count()
179
        ]);
180
181
        // build records object from prepared query using page offset
182
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
183
184
        // render viewer with parameters
185
        return App::$View->render('list', [
186
            'records' => $records,
187
            'pagination' => $pagination,
188
        ]);
189
    }
190
191
}