1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Admin\Comments; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Arch\Model; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FormCommentUpdate. Model for display & update comments and answers |
9
|
|
|
* @package Apps\Model\Admin\Comments |
10
|
|
|
*/ |
11
|
|
|
class FormCommentUpdate extends Model |
12
|
|
|
{ |
13
|
|
|
public $message; |
14
|
|
|
public $guestName; |
15
|
|
|
|
16
|
|
|
/** @var \Apps\ActiveRecord\CommentPost|\Apps\ActiveRecord\CommentAnswer $_record */ |
17
|
|
|
private $_record; |
18
|
|
|
private $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* FormCommentUpdate constructor. Pass record inside the model. |
22
|
|
|
* @param \Apps\ActiveRecord\CommentPost|\Apps\ActiveRecord\CommentAnswer $record |
23
|
|
|
* @param string $type |
24
|
|
|
*/ |
25
|
|
|
public function __construct($record, $type = 'comment') |
26
|
|
|
{ |
27
|
|
|
$this->_record = $record; |
28
|
|
|
$this->type = $type; |
29
|
|
|
parent::__construct(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set default values from active record data |
34
|
|
|
*/ |
35
|
|
|
public function before() |
36
|
|
|
{ |
37
|
|
|
$this->message = $this->_record->message; |
38
|
|
|
$this->guestName = $this->_record->guest_name; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Labels to display in view |
43
|
|
|
*/ |
44
|
|
|
public function labels(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
'message' => __('Message'), |
48
|
|
|
'guestName' => __('Guest name') |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Validation rules for comment body |
54
|
|
|
*/ |
55
|
|
|
public function rules(): array |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
['message', 'required'], |
59
|
|
|
['guestName', 'used'], |
60
|
|
|
['guestName', 'length_max', 100] |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set attribute validation types |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function types(): array |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
'message' => 'html' |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Save updated data to database |
77
|
|
|
*/ |
78
|
|
|
public function make() |
79
|
|
|
{ |
80
|
|
|
$this->_record->message = $this->message; |
81
|
|
|
$this->_record->guest_name = $this->guestName; |
82
|
|
|
$this->_record->save(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get comment_id for comment or answer |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function getCommentId(): int |
90
|
|
|
{ |
91
|
|
|
$id = $this->_record->id; |
92
|
|
|
if ($this->type === 'answer') { |
93
|
|
|
$id = $this->_record->post->id; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $id; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.