1 | <?php |
||||
2 | /** |
||||
3 | * @package Comments |
||||
4 | * @category modules |
||||
5 | * @author Nazar Mokrynskyi <[email protected]> |
||||
6 | * @license 0BSD |
||||
7 | */ |
||||
8 | namespace cs\modules\Comments; |
||||
9 | use |
||||
10 | cs\Cache, |
||||
11 | cs\Config, |
||||
12 | cs\Language, |
||||
13 | cs\Request, |
||||
14 | cs\User, |
||||
15 | cs\CRUD_helpers, |
||||
16 | cs\Singleton; |
||||
17 | |||||
18 | /** |
||||
19 | * @method static $this instance($check = false) |
||||
20 | */ |
||||
21 | class Comments { |
||||
22 | use |
||||
23 | CRUD_helpers, |
||||
24 | Singleton; |
||||
25 | |||||
26 | /** |
||||
27 | * @var Cache\Prefix |
||||
28 | */ |
||||
29 | protected $cache; |
||||
30 | |||||
31 | protected $data_model = [ |
||||
32 | 'id' => 'int:1', |
||||
33 | 'parent' => 'int:0', |
||||
34 | 'module' => 'text', |
||||
35 | 'item' => 'int:1', |
||||
36 | 'user' => 'int:1', |
||||
37 | 'date' => 'int:1', |
||||
38 | 'text' => 'html', |
||||
39 | 'lang' => 'text' |
||||
40 | ]; |
||||
41 | |||||
42 | protected $table = '[prefix]comments'; |
||||
43 | |||||
44 | protected function construct () { |
||||
45 | $this->cache = Cache::prefix('Comments'); |
||||
46 | } |
||||
47 | /** |
||||
48 | * Returns database index |
||||
49 | * |
||||
50 | * @return int |
||||
51 | */ |
||||
52 | protected function cdb () { |
||||
53 | return Config::instance()->module('Comments')->db('comments'); |
||||
54 | } |
||||
55 | /** |
||||
56 | * Get comment data |
||||
57 | * |
||||
58 | * @param int|int[] $id |
||||
59 | * |
||||
60 | * @return array|false |
||||
61 | */ |
||||
62 | public function get ($id) { |
||||
63 | return $this->read($id); |
||||
64 | } |
||||
65 | /** |
||||
66 | * Get comment data with user details and formatted date |
||||
67 | * |
||||
68 | * @param int|int[] $id |
||||
69 | * |
||||
70 | * @return array|false |
||||
71 | */ |
||||
72 | public function get_extended ($id) { |
||||
73 | if (is_array($id)) { |
||||
74 | return array_map([$this, 'get_extended'], $id); |
||||
75 | } |
||||
76 | $comment = $this->get($id); |
||||
77 | $User = User::instance(); |
||||
78 | $comment['username'] = $User->username($comment['user']); |
||||
79 | $comment['avatar'] = $User->avatar(null, $comment['user']); |
||||
80 | $comment['date_formatted'] = date(Language::instance()->_datetime, $comment['date']); |
||||
81 | $comment['time_formatted'] = date(Language::instance()->_datetime, $comment['date']); |
||||
82 | return $comment; |
||||
83 | } |
||||
84 | /** |
||||
85 | * @param string $module |
||||
86 | * @param int $item |
||||
87 | * |
||||
88 | * @return int[] |
||||
89 | */ |
||||
90 | public function get_for_module_item ($module, $item) { |
||||
91 | $search_parameters = [ |
||||
92 | 'module' => $module, |
||||
93 | 'item' => $item |
||||
94 | ]; |
||||
95 | return $this->search($search_parameters, 1, PHP_INT_MAX, 'id', true) ?: []; |
||||
96 | } |
||||
97 | /** |
||||
98 | * @param string $module |
||||
99 | * @param int $item |
||||
100 | * |
||||
101 | * @return int |
||||
102 | */ |
||||
103 | public function get_for_module_item_count ($module, $item) { |
||||
104 | $search_parameters = [ |
||||
105 | 'module' => $module, |
||||
106 | 'item' => $item, |
||||
107 | 'total_count' => true |
||||
108 | ]; |
||||
109 | return $this->search($search_parameters); |
||||
110 | } |
||||
111 | /** |
||||
112 | * Add new comment |
||||
113 | * |
||||
114 | * @param string $module Module name |
||||
115 | * @param int $item Item id |
||||
116 | * @param string $text Comment text |
||||
117 | * @param int $parent Parent comment id |
||||
118 | * |
||||
119 | * @return false|int |
||||
120 | */ |
||||
121 | public function add ($module, $item, $text, $parent = 0) { |
||||
122 | $L = Language::instance(); |
||||
123 | $User = User::instance(); |
||||
124 | $text = xap($text, true); |
||||
125 | if (!$text) { |
||||
126 | return false; |
||||
127 | } |
||||
128 | if ($parent) { |
||||
129 | $parent_comment = $this->read($parent); |
||||
130 | if ($parent_comment['item'] != $item || $parent_comment['module'] != $module) { |
||||
131 | return false; |
||||
132 | } |
||||
133 | } |
||||
134 | $id = $this->create($parent, $module, $item, $User->id, time(), $text, $L->clang); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() $parent of type integer is incompatible with the type array expected by parameter $arguments of cs\modules\Comments\Comments::create() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
135 | if ($id) { |
||||
136 | $this->cache->del("$module/$item"); |
||||
137 | } |
||||
138 | return $id; |
||||
139 | } |
||||
140 | /** |
||||
141 | * Set comment text |
||||
142 | * |
||||
143 | * @param int $id |
||||
144 | * @param string $text |
||||
145 | * |
||||
146 | * @return bool |
||||
147 | */ |
||||
148 | public function set ($id, $text) { |
||||
149 | $text = xap($text, true); |
||||
150 | if (!$text) { |
||||
151 | return false; |
||||
152 | } |
||||
153 | $comment = $this->get($id); |
||||
154 | if (!$comment) { |
||||
155 | return false; |
||||
156 | } |
||||
157 | $comment['text'] = $text; |
||||
158 | $result = $this->update($comment); |
||||
159 | if ($result) { |
||||
160 | $this->cache->del("$comment[module]/$comment[item]"); |
||||
161 | } |
||||
162 | return $result; |
||||
163 | } |
||||
164 | /** |
||||
165 | * Delete comment |
||||
166 | * |
||||
167 | * @param int $id |
||||
168 | * |
||||
169 | * @return bool |
||||
170 | */ |
||||
171 | public function del ($id) { |
||||
172 | $comment = $this->read($id); |
||||
173 | if ( |
||||
174 | !$comment || |
||||
175 | $this->search( |
||||
176 | [ |
||||
177 | 'parent' => $id, |
||||
178 | 'total_count' => true |
||||
179 | ] |
||||
180 | ) |
||||
181 | ) { |
||||
182 | return false; |
||||
183 | } |
||||
184 | $result = $this->delete($id); |
||||
185 | if ($result) { |
||||
186 | $this->cache->del("$comment[module]/$comment[item]"); |
||||
187 | } |
||||
188 | return $result; |
||||
189 | } |
||||
190 | /** |
||||
191 | * Delete all comments of specified item |
||||
192 | * |
||||
193 | * @param string $module Module name |
||||
194 | * @param int $item Item id |
||||
195 | * |
||||
196 | * @return bool |
||||
197 | */ |
||||
198 | public function del_all ($module, $item) { |
||||
199 | $item = (int)$item; |
||||
200 | $result = $this->db_prime()->q( |
||||
201 | "DELETE FROM `[prefix]comments` |
||||
202 | WHERE |
||||
203 | `module` = '%s' AND |
||||
204 | `item` = '%d'", |
||||
205 | $module, |
||||
0 ignored issues
–
show
$module of type string is incompatible with the type array expected by parameter $parameters of cs\DB\_Abstract::q() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
206 | $item |
||||
0 ignored issues
–
show
$item of type integer is incompatible with the type array expected by parameter $parameters of cs\DB\_Abstract::q() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
207 | ); |
||||
208 | if ($result) { |
||||
0 ignored issues
–
show
|
|||||
209 | $this->cache->del("$module/$item"); |
||||
210 | } |
||||
211 | return (bool)$result; |
||||
212 | } |
||||
213 | } |
||||
214 |