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 $_post */ |
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
|
|
|
* @param int $userId |
30
|
|
|
*/ |
31
|
|
|
public function __construct($recordPost, $userId = 0) |
32
|
|
|
{ |
33
|
|
|
$this->_post = $recordPost; |
34
|
|
|
$this->_userId = (int)$userId; |
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Define local properties if user is authorized |
40
|
|
|
*/ |
41
|
|
|
public function before() |
42
|
|
|
{ |
43
|
|
|
if ($this->_userId > 0) { |
44
|
|
|
$user = App::$User->identity($this->_userId); |
45
|
|
|
$this->name = $user->getProfile()->nick; |
46
|
|
|
$this->email = $user->getParam('email'); |
47
|
|
|
} |
48
|
|
|
$this->_ip = App::$Request->getClientIp(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Labels for display form |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function labels() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'name' => __('Name'), |
58
|
|
|
'email' => __('Email'), |
59
|
|
|
'message' => __('Message') |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Form validation rules |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function rules() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
[['name', 'email', 'message'], 'required'], |
70
|
|
|
['name', 'length_min', 2], |
71
|
|
|
['email', 'email'], |
72
|
|
|
['message', 'length_min', 10] |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add new row to database and set post is unreaded |
78
|
|
|
*/ |
79
|
|
|
public function make() |
80
|
|
|
{ |
81
|
|
|
// update readed marker |
82
|
|
|
$this->_post->readed = 0; |
83
|
|
|
$this->_post->save(); |
84
|
|
|
|
85
|
|
|
// add new answer row in database |
86
|
|
|
$record = new FeedbackAnswer(); |
87
|
|
|
$record->feedback_id = $this->_post->id; |
88
|
|
|
$record->name = App::$Security->strip_tags($this->name); |
89
|
|
|
$record->email = App::$Security->strip_tags($this->email); |
90
|
|
|
$record->message = App::$Security->strip_tags($this->message); |
91
|
|
|
if ($this->_userId > 0) { |
92
|
|
|
$record->user_id = $this->_userId; |
93
|
|
|
} |
94
|
|
|
$record->ip = $this->_ip; |
95
|
|
|
$record->save(); |
96
|
|
|
|
97
|
|
|
// add notification msg |
98
|
|
|
$targetId = $this->_post->user_id; |
99
|
|
|
if ($targetId !== null && (int)$targetId > 0 && $targetId !== $this->_userId) { |
100
|
|
|
$notify = new EntityAddNotification($targetId); |
|
|
|
|
101
|
|
|
$uri = '/feedback/read/' . $this->_post->id . '/' . $this->_post->hash . '#feedback-answer-' . $record->id; |
102
|
|
|
$notify->add($uri, EntityAddNotification::MSG_ADD_FEEDBACKANSWER, [ |
103
|
|
|
'snippet' => Text::snippet(App::$Security->strip_tags($this->message), 50), |
|
|
|
|
104
|
|
|
'post' => Text::snippet(App::$Security->strip_tags($this->_post->message), 50) |
|
|
|
|
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
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.