| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | #====================================================== |
|---|
| 5 | | <#TAG_NAME_TAG#> |
|---|
| 6 | | ===================================== |
|---|
| 7 | | <#TAG_DEVELOPERS_TAG#> |
|---|
| 8 | | <#TAG_COPYRIGHT_TAG#> |
|---|
| 9 | | <#TAG_URL_TAG#> |
|---|
| 10 | | ===================================== |
|---|
| 11 | | Email: <#TAG_EMAIL_TAG#> |
|---|
| 12 | #====================================================== |
|---|
| 13 | | @ Version: <#TAG_VER_HUMAN_TAG#> |
|---|
| 14 | | @ Version Int: <#TAG_VER_INT_TAG#> |
|---|
| 15 | | @ Version Num: <#TAG_VER_NUM_TAG#> |
|---|
| 16 | | @ Build: <#TAG_VER_BUILD_TAG#> |
|---|
| 17 | #====================================================== |
|---|
| 18 | | | Functions: Articles |
|---|
| 19 | #====================================================== |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | class td_func_articles extends td_func_template { |
|---|
| 23 | |
|---|
| 24 | #======================================= |
|---|
| 25 | # @ Get Articles |
|---|
| 26 | #======================================= |
|---|
| 27 | |
|---|
| 28 | public function get($input) |
|---|
| 29 | { |
|---|
| 30 | $return = array(); |
|---|
| 31 | |
|---|
| 32 | if ( ! $input['from'] ) $input['from'] = 'articles'; |
|---|
| 33 | |
|---|
| 34 | $this->trellis->db->construct( array( |
|---|
| 35 | 'select' => $input['select'], |
|---|
| 36 | 'from' => $input['from'], |
|---|
| 37 | 'where' => $input['where'], |
|---|
| 38 | 'order' => $input['order'], |
|---|
| 39 | 'limit' => $input['limit'], |
|---|
| 40 | ) ); |
|---|
| 41 | |
|---|
| 42 | $this->trellis->db->execute(); |
|---|
| 43 | |
|---|
| 44 | if ( ! $this->trellis->db->get_num_rows() ) return false; |
|---|
| 45 | |
|---|
| 46 | while ( $a = $this->trellis->db->fetch_row() ) |
|---|
| 47 | { |
|---|
| 48 | $return[ $a['id'] ] = $a; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | return $return; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | #======================================= |
|---|
| 55 | # @ Get Single Article |
|---|
| 56 | #======================================= |
|---|
| 57 | |
|---|
| 58 | public function get_single($select, $where='') |
|---|
| 59 | { |
|---|
| 60 | $this->trellis->db->construct( array( |
|---|
| 61 | 'select' => $select, |
|---|
| 62 | 'from' => 'articles', |
|---|
| 63 | 'where' => $where, |
|---|
| 64 | 'limit' => array( 0, 1 ), |
|---|
| 65 | ) ); |
|---|
| 66 | |
|---|
| 67 | $this->trellis->db->execute(); |
|---|
| 68 | |
|---|
| 69 | if ( ! $this->trellis->db->get_num_rows() ) return false; |
|---|
| 70 | |
|---|
| 71 | return $this->trellis->db->fetch_row(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | #======================================= |
|---|
| 75 | # @ Get Single Article By ID |
|---|
| 76 | #======================================= |
|---|
| 77 | |
|---|
| 78 | public function get_single_by_id($select, $id) |
|---|
| 79 | { |
|---|
| 80 | return $this->get_single( $select, array( 'id', '=', intval( $id ) ) ); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | #======================================= |
|---|
| 84 | # @ Get Comments |
|---|
| 85 | #======================================= |
|---|
| 86 | |
|---|
| 87 | public function get_comments($input) |
|---|
| 88 | { |
|---|
| 89 | $input['from'] = 'article_comments'; |
|---|
| 90 | |
|---|
| 91 | return $this->get($input); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | #======================================= |
|---|
| 95 | # @ Get Single Comment |
|---|
| 96 | #======================================= |
|---|
| 97 | |
|---|
| 98 | public function get_single_comment($select, $where='') |
|---|
| 99 | { |
|---|
| 100 | $this->trellis->db->construct( array( |
|---|
| 101 | 'select' => $select, |
|---|
| 102 | 'from' => 'article_comments', |
|---|
| 103 | 'where' => $where, |
|---|
| 104 | 'limit' => array( 0, 1 ), |
|---|
| 105 | ) ); |
|---|
| 106 | |
|---|
| 107 | $this->trellis->db->execute(); |
|---|
| 108 | |
|---|
| 109 | if ( ! $this->trellis->db->get_num_rows() ) return false; |
|---|
| 110 | |
|---|
| 111 | return $this->trellis->db->fetch_row(); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | #======================================= |
|---|
| 115 | # @ Get Single Comment By ID |
|---|
| 116 | #======================================= |
|---|
| 117 | |
|---|
| 118 | public function get_single_comment_by_id($select, $id) |
|---|
| 119 | { |
|---|
| 120 | return $this->get_single_comment( $select, array( 'id', '=', intval( $id ) ) ); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | #======================================= |
|---|
| 124 | # @ Get Ratings |
|---|
| 125 | #======================================= |
|---|
| 126 | |
|---|
| 127 | public function get_ratings($input) |
|---|
| 128 | { |
|---|
| 129 | $input['from'] = 'article_rate'; |
|---|
| 130 | |
|---|
| 131 | return $this->get($input); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | #======================================= |
|---|
| 135 | # @ Get Single Rating |
|---|
| 136 | #======================================= |
|---|
| 137 | |
|---|
| 138 | public function get_single_rating($select, $where='') |
|---|
| 139 | { |
|---|
| 140 | $this->trellis->db->construct( array( |
|---|
| 141 | 'select' => $select, |
|---|
| 142 | 'from' => 'article_rate', |
|---|
| 143 | 'where' => $where, |
|---|
| 144 | 'limit' => array( 0, 1 ), |
|---|
| 145 | ) ); |
|---|
| 146 | |
|---|
| 147 | $this->trellis->db->execute(); |
|---|
| 148 | |
|---|
| 149 | if ( ! $this->trellis->db->get_num_rows() ) return false; |
|---|
| 150 | |
|---|
| 151 | return $this->trellis->db->fetch_row(); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | #======================================= |
|---|
| 155 | # @ Get Single Rating By ID |
|---|
| 156 | #======================================= |
|---|
| 157 | |
|---|
| 158 | public function get_single_rating_by_id($select, $id) |
|---|
| 159 | { |
|---|
| 160 | return $this->get_single_rating( $select, array( 'id', '=', intval( $id ) ) ); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | #======================================= |
|---|
| 164 | # @ Add Article |
|---|
| 165 | #======================================= |
|---|
| 166 | |
|---|
| 167 | public function add($data) |
|---|
| 168 | { |
|---|
| 169 | $fields = array( |
|---|
| 170 | 'id' => 'int', |
|---|
| 171 | 'cid' => 'int', |
|---|
| 172 | 'title' => 'string', |
|---|
| 173 | 'description' => 'string', |
|---|
| 174 | 'content' => 'string', |
|---|
| 175 | 'html' => 'int', |
|---|
| 176 | 'allow_comments' => 'int', |
|---|
| 177 | 'allow_rating' => 'int', |
|---|
| 178 | 'date' => 'int', |
|---|
| 179 | ); |
|---|
| 180 | |
|---|
| 181 | $this->trellis->db->construct( array( |
|---|
| 182 | 'insert' => 'articles', |
|---|
| 183 | 'set' => $this->trellis->process_data( $fields, $data ), |
|---|
| 184 | ) ); |
|---|
| 185 | |
|---|
| 186 | $this->trellis->db->execute(); |
|---|
| 187 | |
|---|
| 188 | return $this->trellis->db->get_insert_id(); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | #======================================= |
|---|
| 192 | # @ Add Comment |
|---|
| 193 | #======================================= |
|---|
| 194 | |
|---|
| 195 | public function add_comment($data, $aid) |
|---|
| 196 | { |
|---|
| 197 | $fields = array( |
|---|
| 198 | 'aid' => 'int', |
|---|
| 199 | 'uid' => 'int', |
|---|
| 200 | 'message' => 'string', |
|---|
| 201 | 'staff' => 'int', |
|---|
| 202 | 'html' => 'int', |
|---|
| 203 | 'date' => 'int', |
|---|
| 204 | 'ipadd' => 'string', |
|---|
| 205 | ); |
|---|
| 206 | |
|---|
| 207 | $this->trellis->db->construct( array( |
|---|
| 208 | 'insert' => 'article_comments', |
|---|
| 209 | 'set' => $this->trellis->process_data( $fields, $data ), |
|---|
| 210 | ) ); |
|---|
| 211 | |
|---|
| 212 | $this->trellis->db->execute(); |
|---|
| 213 | |
|---|
| 214 | $id = $this->trellis->db->get_insert_id(); |
|---|
| 215 | |
|---|
| 216 | $this->increase_count( $aid, 1, 'comments' ); |
|---|
| 217 | |
|---|
| 218 | return $id; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | #======================================= |
|---|
| 222 | # @ Add Rating |
|---|
| 223 | #======================================= |
|---|
| 224 | |
|---|
| 225 | public function add_rating($amount, $aid, $params=array()) |
|---|
| 226 | { |
|---|
| 227 | if ( ! $amount = intval( $amount ) ) return false; |
|---|
| 228 | if ( ! $aid = intval( $aid ) ) return false; |
|---|
| 229 | |
|---|
| 230 | $sql_select = array(); |
|---|
| 231 | |
|---|
| 232 | if ( ! isset( $params['rating_total'] ) ) $sql_select[] = 'rating_total'; |
|---|
| 233 | if ( ! isset( $params['votes'] ) ) $sql_select[] = 'votes'; |
|---|
| 234 | |
|---|
| 235 | if ( ! empty( $sql_select ) ) |
|---|
| 236 | { |
|---|
| 237 | if ( ! $a = $this->get_single_by_id( array( 'select' => $sql_select ), $id ) ) return false; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if ( ! isset( $params['rating_total'] ) ) $params['rating_total'] = $a['rating_total']; |
|---|
| 241 | if ( ! isset( $params['votes'] ) ) $params['votes'] = $a['votes']; |
|---|
| 242 | |
|---|
| 243 | $db_array = array( |
|---|
| 244 | 'aid' => $aid, |
|---|
| 245 | 'uid' => $this->trellis->user['id'], |
|---|
| 246 | 'rating' => $amount, |
|---|
| 247 | 'date' => time(), |
|---|
| 248 | 'ipadd' => $this->trellis->input['ip_address'], |
|---|
| 249 | ); |
|---|
| 250 | |
|---|
| 251 | $this->trellis->db->construct( array( |
|---|
| 252 | 'insert' => 'article_rate', |
|---|
| 253 | 'set' => $db_array, |
|---|
| 254 | ) ); |
|---|
| 255 | |
|---|
| 256 | $this->trellis->db->execute(); |
|---|
| 257 | |
|---|
| 258 | $id = $this->trellis->db->get_insert_id(); |
|---|
| 259 | |
|---|
| 260 | $new_total = $params['rating_total'] + $amount; |
|---|
| 261 | $new_average = round( ( $new_total ) / ( $params['votes'] + 1 ), 2 ); |
|---|
| 262 | |
|---|
| 263 | $this->trellis->db->construct( array( |
|---|
| 264 | 'update' => 'articles', |
|---|
| 265 | 'set' => array( 'rating_average' => $new_average, 'rating_total' => $new_total, 'votes' => ( $params['votes'] + 1 ) ), |
|---|
| 266 | 'where' => array( 'id', '=', $aid ), |
|---|
| 267 | 'limit' => array( 1 ), |
|---|
| 268 | ) ); |
|---|
| 269 | |
|---|
| 270 | $this->trellis->db->execute(); |
|---|
| 271 | |
|---|
| 272 | return $id; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | #======================================= |
|---|
| 276 | # @ Edit Article |
|---|
| 277 | #======================================= |
|---|
| 278 | |
|---|
| 279 | public function edit($data, $id) |
|---|
| 280 | { |
|---|
| 281 | if ( ! $id = intval( $id ) ) return false; |
|---|
| 282 | |
|---|
| 283 | $fields = array( |
|---|
| 284 | 'id' => 'int', |
|---|
| 285 | 'cid' => 'int', |
|---|
| 286 | 'title' => 'string', |
|---|
| 287 | 'description' => 'string', |
|---|
| 288 | 'content' => 'string', |
|---|
| 289 | 'html' => 'int', |
|---|
| 290 | 'allow_comments' => 'int', |
|---|
| 291 | 'allow_rating' => 'int', |
|---|
| 292 | 'modified' => 'int', |
|---|
| 293 | ); |
|---|
| 294 | |
|---|
| 295 | $this->trellis->db->construct( array( |
|---|
| 296 | 'update' => 'articles', |
|---|
| 297 | 'set' => $this->trellis->process_data( $fields, $data ), |
|---|
| 298 | 'where' => array( 'id', '=', $id ), |
|---|
| 299 | 'limit' => array( 1 ), |
|---|
| 300 | ) ); |
|---|
| 301 | |
|---|
| 302 | $this->trellis->db->execute(); |
|---|
| 303 | |
|---|
| 304 | return $this->trellis->db->get_affected_rows(); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | #======================================= |
|---|
| 308 | # @ Edit Comment |
|---|
| 309 | #======================================= |
|---|
| 310 | |
|---|
| 311 | public function edit_comment($data, $id) |
|---|
| 312 | { |
|---|
| 313 | if ( ! $id = intval( $id ) ) return false; |
|---|
| 314 | |
|---|
| 315 | $fields = array( |
|---|
| 316 | 'message' => 'string', |
|---|
| 317 | 'staff' => 'int', |
|---|
| 318 | 'html' => 'int', |
|---|
| 319 | ); |
|---|
| 320 | |
|---|
| 321 | $this->trellis->db->construct( array( |
|---|
| 322 | 'update' => 'article_comments', |
|---|
| 323 | 'set' => $this->trellis->process_data( $fields, $data ), |
|---|
| 324 | 'where' => array( 'id', '=', $id ), |
|---|
| 325 | 'limit' => array( 1 ), |
|---|
| 326 | ) ); |
|---|
| 327 | |
|---|
| 328 | $this->trellis->db->execute(); |
|---|
| 329 | |
|---|
| 330 | return $this->trellis->db->get_affected_rows(); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | #======================================= |
|---|
| 334 | # @ Delete Article |
|---|
| 335 | #======================================= |
|---|
| 336 | |
|---|
| 337 | public function delete($id) |
|---|
| 338 | { |
|---|
| 339 | if ( ! $id = intval( $id ) ) return false; |
|---|
| 340 | |
|---|
| 341 | $this->trellis->db->construct( array( |
|---|
| 342 | 'delete' => 'article_comments', |
|---|
| 343 | 'where' => array( 'aid', '=', $id ), |
|---|
| 344 | 'limit' => array( 1 ), |
|---|
| 345 | ) ); |
|---|
| 346 | |
|---|
| 347 | $this->trellis->db->execute(); |
|---|
| 348 | |
|---|
| 349 | $this->trellis->db->construct( array( |
|---|
| 350 | 'delete' => 'article_rate', |
|---|
| 351 | 'where' => array( 'aid', '=', $id ), |
|---|
| 352 | 'limit' => array( 1 ), |
|---|
| 353 | ) ); |
|---|
| 354 | |
|---|
| 355 | $this->trellis->db->execute(); |
|---|
| 356 | |
|---|
| 357 | $this->trellis->db->construct( array( |
|---|
| 358 | 'delete' => 'articles', |
|---|
| 359 | 'where' => array( 'id', '=', $id ), |
|---|
| 360 | 'limit' => array( 1 ), |
|---|
| 361 | ) ); |
|---|
| 362 | |
|---|
| 363 | $this->trellis->db->execute(); |
|---|
| 364 | |
|---|
| 365 | return $this->trellis->db->get_affected_rows(); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | #======================================= |
|---|
| 369 | # @ Delete Comment |
|---|
| 370 | #======================================= |
|---|
| 371 | |
|---|
| 372 | public function delete_comment($id, $params=array()) |
|---|
| 373 | { |
|---|
| 374 | if ( ! $id = intval( $id ) ) return false; |
|---|
| 375 | |
|---|
| 376 | if ( ! isset( $params['aid'] ) ) |
|---|
| 377 | { |
|---|
| 378 | if ( ! $c = $this->get_single_comment_by_id( array( 'aid' ), $id ) ) return false; |
|---|
| 379 | |
|---|
| 380 | $params['aid'] = $c['aid']; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | $this->trellis->db->construct( array( |
|---|
| 384 | 'delete' => 'article_comments', |
|---|
| 385 | 'where' => array( 'id', '=', $id ), |
|---|
| 386 | 'limit' => array( 1 ), |
|---|
| 387 | ) ); |
|---|
| 388 | |
|---|
| 389 | $this->trellis->db->execute(); |
|---|
| 390 | |
|---|
| 391 | $dc = $this->trellis->db->get_affected_rows(); |
|---|
| 392 | |
|---|
| 393 | $this->decrease_count( $params['aid'], 1, 'comments' ); |
|---|
| 394 | |
|---|
| 395 | return $dc; |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | #======================================= |
|---|
| 399 | # @ Increase Count |
|---|
| 400 | #======================================= |
|---|
| 401 | |
|---|
| 402 | public function increase_count($id, $amount, $field, $now=0) |
|---|
| 403 | { |
|---|
| 404 | $this->update_stats( $id, $amount, 'articles', $field, 'increase', $now ); |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | #======================================= |
|---|
| 408 | # @ Decrease Count |
|---|
| 409 | #======================================= |
|---|
| 410 | |
|---|
| 411 | public function decrease_count($id, $amount, $field, $now=0) |
|---|
| 412 | { |
|---|
| 413 | $this->update_stats( $id, $amount, 'articles', $field, 'decrease', $now ); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | ?> |
|---|