1 | <?php |
||
2 | |||
3 | namespace Apps\Model\Front\Feedback; |
||
4 | |||
5 | use Apps\ActiveRecord\FeedbackAnswer; |
||
6 | use Apps\Model\Front\Profile\EntityAddNotification; |
||
7 | use Ffcms\Core\App; |
||
8 | use Ffcms\Core\Arch\Model; |
||
9 | use Ffcms\Core\Helper\Text; |
||
10 | |||
11 | /** |
||
12 | * Class FormAnswerAdd. Model to work with add answer to feedback post thread |
||
13 | * @package Apps\Model\Front\Feedback |
||
14 | */ |
||
15 | class FormAnswerAdd extends Model |
||
16 | { |
||
17 | public $name; |
||
18 | public $email; |
||
19 | public $message; |
||
20 | |||
21 | /** @var \Apps\ActiveRecord\FeedbackPost */ |
||
22 | protected $_post; |
||
23 | protected $_userId; |
||
24 | protected $_ip; |
||
25 | |||
26 | /** |
||
27 | * FormAnswerAdd constructor. Pass active record of comment post and user id |
||
28 | * @param $recordPost |
||
29 | */ |
||
30 | public function __construct($recordPost) |
||
31 | { |
||
32 | $this->_post = $recordPost; |
||
33 | if (App::$User->isAuth()) { |
||
34 | $this->_userId = App::$User->identity()->getId(); |
||
35 | } |
||
36 | parent::__construct(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Define local properties if user is authorized |
||
41 | */ |
||
42 | public function before() |
||
43 | { |
||
44 | if ($this->_userId > 0) { |
||
45 | $user = App::$User->identity($this->_userId); |
||
46 | $this->name = $user->profile->nick; |
||
47 | $this->email = $user->getParam('email'); |
||
48 | } |
||
49 | $this->_ip = App::$Request->getClientIp(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Labels for display form |
||
54 | * @return array |
||
55 | */ |
||
56 | public function labels(): array |
||
57 | { |
||
58 | return [ |
||
59 | 'name' => __('Name'), |
||
60 | 'email' => __('Email'), |
||
61 | 'message' => __('Message') |
||
62 | ]; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Form validation rules |
||
67 | * @return array |
||
68 | */ |
||
69 | public function rules(): array |
||
70 | { |
||
71 | return [ |
||
72 | [['name', 'email', 'message'], 'required'], |
||
73 | ['name', 'length_min', 2], |
||
74 | ['email', 'email'], |
||
75 | ['message', 'length_min', 10] |
||
76 | ]; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Add new row to database and set post is unreaded |
||
81 | */ |
||
82 | public function make() |
||
83 | { |
||
84 | // update readed marker |
||
85 | $this->_post->readed = 0; |
||
0 ignored issues
–
show
|
|||
86 | $this->_post->save(); |
||
87 | |||
88 | // add new answer row in database |
||
89 | $record = new FeedbackAnswer(); |
||
90 | $record->feedback_id = $this->_post->id; |
||
91 | $record->name = $this->name; |
||
92 | $record->email = $this->email; |
||
93 | $record->message = $this->message; |
||
94 | if ($this->_userId > 0) { |
||
95 | $record->user_id = $this->_userId; |
||
96 | } |
||
97 | $record->ip = $this->_ip; |
||
98 | $record->save(); |
||
99 | |||
100 | // add notification msg |
||
101 | $targetId = $this->_post->user_id; |
||
102 | if ($targetId !== null && (int)$targetId > 0 && $targetId !== $this->_userId) { |
||
103 | $notify = new EntityAddNotification($targetId); |
||
104 | $uri = '/feedback/read/' . $this->_post->id . '/' . $this->_post->hash . '#feedback-answer-' . $record->id; |
||
105 | $notify->add($uri, EntityAddNotification::MSG_ADD_FEEDBACKANSWER, [ |
||
106 | 'snippet' => Text::snippet($this->message, 50), |
||
107 | 'post' => Text::snippet($this->_post->message, 50) |
||
108 | ]); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.