1 | <?php |
||
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) { |
||
150 | /** |
||
151 | * @param \cs\Request $Request |
||
152 | * |
||
153 | * @throws ExitException |
||
154 | */ |
||
155 | static function index_delete ($Request) { |
||
186 | /** |
||
187 | * @return bool |
||
188 | */ |
||
189 | static function index_is_admin () { |
||
192 | } |
||
193 |