1 <?php
defined('IN_CMS') or die('No direct access allowed.');
5 public static function list_all($params = array()) {
6 $sql = "select * from comments where 1 = 1";
9 if(isset($params['post'])) {
10 $sql .= " and post = ?";
11 $args[] = $params['post'];
14 if(isset($params['sortby'])) {
15 $sql .= " order by " . $params['sortby'];
17 if(isset($params['sortmode'])) {
18 $sql .= " " . $params['sortmode'];
22 if(isset($params['limit'])) {
23 $sql .= " limit " . $params['limit'];
25 if(isset($params['offset'])) {
26 $sql .= " offset " . $params['offset'];
30 $result = Db
::results($sql, $args);
32 return new Items($result);
35 public static function add($post_id) {
36 $post = Input
::post(array('name', 'email', 'text'));
39 if(empty($post['name'])) {
40 $errors[] = 'Please enter your name';
43 if(filter_var($post['email'], FILTER_VALIDATE_EMAIL
) === false) {
44 $errors[] = 'Please enter a valid email address';
47 if(empty($post['text'])) {
48 $errors[] = 'Please enter your comments';
52 Notifications
::set('error', $errors);
56 $post['date'] = time();
57 $post['status'] = Config
::get('metadata.auto_published_comments', 0) ?
'published' : 'pending';
58 $post['post'] = $post_id;
64 foreach($post as $key => $value) {
65 $keys[] = '`' . $key . '`';
66 $values[] = '\'' . $value . '\'';
69 $sql = "insert into comments (" . implode(', ', $keys) . ") values (" . implode(', ', $values) . ")";
71 Db
::query($sql, $args);
73 Notifications
::set('success', 'Your comment has been sent');
78 public static function update() {
79 $post = Input
::post(array('id', 'text', 'status'));
82 if(empty($post['text'])) {
83 $errors[] = 'Please enter comment text';
87 $output = json_encode(array('result' => false, 'errors' => $errors));
88 Response
::content($output);
98 foreach($post as $key => $value) {
99 $updates[] = '`' . $key . '` = ?';
103 $sql = "update comments set " . implode(', ', $updates) . " where id = ?";
106 Db
::query($sql, $args);
108 $output = json_encode(array('result' => true));
109 Response
::content($output);
112 public static function update_status() {
113 $post = Input
::post(array('id', 'status'));
116 if(in_array($post['status'], array('published', 'pending', 'spam')) === false) {
117 $errors[] = 'Invalid comment status';
121 $output = json_encode(array('result' => false, 'errors' => $errors));
122 Response
::content($output);
132 foreach($post as $key => $value) {
133 $updates[] = '`' . $key . '` = ?';
137 $sql = "update comments set " . implode(', ', $updates) . " where id = ?";
140 Db
::query($sql, $args);
142 $output = json_encode(array('result' => true));
143 Response
::content($output);
146 public static function remove() {
147 $id = Input
::post('id');
149 $sql = "delete from comments where id = ?";
152 Db
::query($sql, $args);
154 $output = json_encode(array('result' => true));
155 Response
::content($output);