1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Comments |
4
|
|
|
* @category modules |
5
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
7
|
|
|
* @license MIT License, see license.txt |
8
|
|
|
*/ |
9
|
|
|
namespace cs\modules\Comments\api; |
10
|
|
|
use |
11
|
|
|
cs\Event, |
12
|
|
|
cs\ExitException, |
13
|
|
|
cs\Language, |
14
|
|
|
cs\User, |
15
|
|
|
cs\modules\Comments\Comments; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides next events: |
19
|
|
|
* api/Comments/add |
20
|
|
|
* [ |
21
|
|
|
* 'item' => item //Item id |
22
|
|
|
* 'module' => module //Module |
23
|
|
|
* 'allow' => &$allow //Whether allow or not |
24
|
|
|
* ] |
25
|
|
|
* |
26
|
|
|
* api/Comments/edit |
27
|
|
|
* [ |
28
|
|
|
* 'id' => id //Comment id |
29
|
|
|
* 'user' => user //User id |
30
|
|
|
* 'item' => item_id //Item id |
31
|
|
|
* 'module' => module //Module |
32
|
|
|
* 'allow' => &$allow //Whether allow or not |
33
|
|
|
* ] |
34
|
|
|
* |
35
|
|
|
* api/Comments/delete |
36
|
|
|
* [ |
37
|
|
|
* 'id' => id //Comment id |
38
|
|
|
* 'user' => user //User id |
39
|
|
|
* 'item' => item_id //Item id |
40
|
|
|
* 'module' => module //Module |
41
|
|
|
* 'allow' => &$allow //Whether allow or not |
42
|
|
|
* ] |
43
|
|
|
*/ |
44
|
|
|
class Controller { |
45
|
|
|
/** |
46
|
|
|
* @param \cs\Request $Request |
47
|
|
|
* |
48
|
|
|
* @return array|array[]|int |
49
|
|
|
* |
50
|
|
|
* @throws ExitException |
51
|
|
|
*/ |
52
|
|
|
static function index_get ($Request) { |
53
|
|
|
$query = $Request->query('module', 'item'); |
54
|
|
|
$id = $Request->route_ids(0); |
55
|
|
|
$Comments = Comments::instance(); |
56
|
|
|
if ($query) { |
57
|
|
|
if ($Request->route_path(0) == 'count') { |
58
|
|
|
return $Comments->get_for_module_item_count($query['module'], $query['item']); |
59
|
|
|
} |
60
|
|
|
return $Comments->get_extended( |
61
|
|
|
$Comments->get_for_module_item($query['module'], $query['item']) |
62
|
|
|
); |
63
|
|
|
} elseif ($id) { |
64
|
|
|
$comment = $Comments->get_extended($id); |
65
|
|
|
if (!$comment) { |
66
|
|
|
throw new ExitException(404); |
67
|
|
|
} |
68
|
|
|
return $comment; |
69
|
|
|
} |
70
|
|
|
throw new ExitException(400); |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* @param \cs\Request $Request |
74
|
|
|
* @param \cs\Response $Response |
75
|
|
|
* |
76
|
|
|
* @throws ExitException |
77
|
|
|
*/ |
78
|
|
|
static function index_post ($Request, $Response) { |
79
|
|
|
if (!User::instance()->user()) { |
80
|
|
|
throw new ExitException(403); |
81
|
|
|
} |
82
|
|
|
$data = $Request->data('item', 'module', 'text', 'parent'); |
83
|
|
|
if (!$data) { |
84
|
|
|
throw new ExitException(400); |
85
|
|
|
} |
86
|
|
|
$L = Language::prefix('comments_'); |
87
|
|
|
if (!strip_tags($data['text'])) { |
88
|
|
|
throw new ExitException($L->comment_cant_be_empty, 400); |
89
|
|
|
} |
90
|
|
|
$allow = false; |
91
|
|
|
Event::instance()->fire( |
92
|
|
|
'api/Comments/add', |
93
|
|
|
[ |
94
|
|
|
'item' => $data['item'], |
95
|
|
|
'module' => $data['module'], |
96
|
|
|
'allow' => &$allow |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
if (!$allow) { |
100
|
|
|
throw new ExitException($L->comment_sending_server_error, 500); |
101
|
|
|
} |
102
|
|
|
$Comments = Comments::instance(); |
103
|
|
|
$id = $Comments->add($data['module'], $data['item'], $data['text'], $data['parent']); |
104
|
|
|
if (!$id) { |
105
|
|
|
throw new ExitException($L->comment_sending_server_error, 500); |
106
|
|
|
} |
107
|
|
|
$Response->code = 201; |
108
|
|
|
} |
109
|
|
|
/** |
110
|
|
|
* @param \cs\Request $Request |
111
|
|
|
* |
112
|
|
|
* @throws ExitException |
113
|
|
|
*/ |
114
|
|
|
static function index_put ($Request) { |
115
|
|
|
if (!User::instance()->user()) { |
116
|
|
|
throw new ExitException(403); |
117
|
|
|
} |
118
|
|
|
$id = $Request->route(0); |
119
|
|
|
if (!$id) { |
120
|
|
|
throw new ExitException(400); |
121
|
|
|
} |
122
|
|
|
$L = Language::prefix('comments_'); |
123
|
|
|
$text = $Request->data('text'); |
124
|
|
|
if (!strip_tags($text)) { |
125
|
|
|
throw new ExitException($L->comment_cant_be_empty, 400); |
126
|
|
|
} |
127
|
|
|
$Comments = Comments::instance(); |
128
|
|
|
$comment = $Comments->get($id); |
129
|
|
|
if (!$comment) { |
130
|
|
|
throw new ExitException(404); |
131
|
|
|
} |
132
|
|
|
$allow = false; |
133
|
|
|
Event::instance()->fire( |
134
|
|
|
'api/Comments/edit', |
135
|
|
|
[ |
136
|
|
|
'id' => $comment['id'], |
137
|
|
|
'user' => $comment['user'], |
138
|
|
|
'item' => $comment['item'], |
139
|
|
|
'module' => $comment['module'], |
140
|
|
|
'allow' => &$allow |
141
|
|
|
] |
142
|
|
|
); |
143
|
|
|
if ( |
144
|
|
|
!$allow || |
145
|
|
|
!$Comments->set($comment['id'], $text) |
146
|
|
|
) { |
147
|
|
|
throw new ExitException($L->comment_editing_server_error, 500); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* @param \cs\Request $Request |
152
|
|
|
* |
153
|
|
|
* @throws ExitException |
154
|
|
|
*/ |
155
|
|
|
static function index_delete ($Request) { |
156
|
|
|
if (!User::instance()->user()) { |
157
|
|
|
throw new ExitException(403); |
158
|
|
|
} |
159
|
|
|
$id = $Request->route(0); |
160
|
|
|
if (!$id) { |
161
|
|
|
throw new ExitException(400); |
162
|
|
|
} |
163
|
|
|
$Comments = Comments::instance(); |
164
|
|
|
$comment = $Comments->get($id); |
165
|
|
|
if (!$comment) { |
166
|
|
|
throw new ExitException(404); |
167
|
|
|
} |
168
|
|
|
$allow = false; |
169
|
|
|
Event::instance()->fire( |
170
|
|
|
'api/Comments/delete', |
171
|
|
|
[ |
172
|
|
|
'id' => $comment['id'], |
173
|
|
|
'user' => $comment['user'], |
174
|
|
|
'item' => $comment['item'], |
175
|
|
|
'module' => $comment['module'], |
176
|
|
|
'allow' => &$allow |
177
|
|
|
] |
178
|
|
|
); |
179
|
|
|
if ( |
180
|
|
|
!$allow || |
181
|
|
|
!$Comments->del($comment['id']) |
182
|
|
|
) { |
183
|
|
|
throw new ExitException(Language::prefix('comments_')->comment_deleting_server_error, 500); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
/** |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
static function index_is_admin () { |
190
|
|
|
return (bool)User::instance()->admin(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|