/includes/functions/func_news.php

https://github.com/lewellyn/TrellisDesk · PHP · 302 lines · 186 code · 73 blank · 43 comment · 11 complexity · b76fcb2ae8d4e7b498c95efdfe323947 MD5 · raw file

  1. <?php
  2. /**
  3. * Trellis Desk
  4. *
  5. * @copyright Copyright (C) 2009-2012 ACCORD5. All rights reserved.
  6. * @license GNU General Public License version 3 or later; see LICENSE.txt
  7. */
  8. class td_func_news {
  9. public $error = '';
  10. #=======================================
  11. # @ Get News
  12. #=======================================
  13. public function get($input)
  14. {
  15. $return = array();
  16. if ( ! $input['from'] ) $input['from'] = 'news';
  17. $this->trellis->db->construct( array(
  18. 'select' => $input['select'],
  19. 'from' => $input['from'],
  20. 'where' => $input['where'],
  21. 'order' => $input['order'],
  22. 'limit' => $input['limit'],
  23. ) );
  24. $this->trellis->db->execute();
  25. if ( ! $this->trellis->db->get_num_rows() ) return false;
  26. while ( $n = $this->trellis->db->fetch_row() )
  27. {
  28. $return[ $n['id'] ] = $n;
  29. }
  30. return $return;
  31. }
  32. #=======================================
  33. # @ Get Single News Item
  34. #=======================================
  35. public function get_single($select, $where='')
  36. {
  37. $this->trellis->db->construct( array(
  38. 'select' => $select,
  39. 'from' => 'news',
  40. 'where' => $where,
  41. 'limit' => array( 0, 1 ),
  42. ) );
  43. $this->trellis->db->execute();
  44. if ( ! $this->trellis->db->get_num_rows() ) return false;
  45. return $this->trellis->db->fetch_row();
  46. }
  47. #=======================================
  48. # @ Get Single News Item By ID
  49. #=======================================
  50. public function get_single_by_id($select, $id)
  51. {
  52. return $this->get_single( $select, array( 'id', '=', intval( $id ) ) );
  53. }
  54. #=======================================
  55. # @ Get Comments
  56. #=======================================
  57. public function get_comments($input)
  58. {
  59. $input['from'] = 'news_comments';
  60. return $this->get($input);
  61. }
  62. #=======================================
  63. # @ Get Single Comment
  64. #=======================================
  65. public function get_single_comment($select, $where='')
  66. {
  67. $this->trellis->db->construct( array(
  68. 'select' => $select,
  69. 'from' => 'news_comments',
  70. 'where' => $where,
  71. 'limit' => array( 0, 1 ),
  72. ) );
  73. $this->trellis->db->execute();
  74. if ( ! $this->trellis->db->get_num_rows() ) return false;
  75. return $this->trellis->db->fetch_row();
  76. }
  77. #=======================================
  78. # @ Get Single Comment By ID
  79. #=======================================
  80. public function get_single_comment_by_id($select, $id)
  81. {
  82. return $this->get_single_comment( $select, array( 'id', '=', intval( $id ) ) );
  83. }
  84. #=======================================
  85. # @ Add News
  86. #=======================================
  87. public function add($data)
  88. {
  89. $fields = array(
  90. 'uid' => 'int',
  91. 'title' => 'string',
  92. 'excerpt' => 'string',
  93. 'content' => 'string',
  94. 'allow_comments' => 'int',
  95. 'html' => 'int',
  96. 'date' => 'int',
  97. 'ipadd' => 'string',
  98. );
  99. $this->trellis->db->construct( array(
  100. 'insert' => 'news',
  101. 'set' => $this->trellis->process_data( $fields, $data ),
  102. ) );
  103. $this->trellis->db->execute();
  104. return $this->trellis->db->get_insert_id();
  105. }
  106. #=======================================
  107. # @ Add Comment
  108. #=======================================
  109. public function add_comment($data, $nid)
  110. {
  111. # $nid can be pulled from $data!
  112. $fields = array(
  113. 'nid' => 'int',
  114. 'uid' => 'int',
  115. 'message' => 'string',
  116. 'staff' => 'int',
  117. 'html' => 'int',
  118. 'date' => 'int',
  119. 'ipadd' => 'string',
  120. );
  121. $this->trellis->db->construct( array(
  122. 'insert' => 'news_comments',
  123. 'set' => $this->trellis->process_data( $fields, $data ),
  124. ) );
  125. $this->trellis->db->execute();
  126. $id = $this->trellis->db->get_insert_id();
  127. $this->trellis->db->construct( array(
  128. 'update' => 'news',
  129. 'set' => array( 'comments' => array( '+', 1 ) ),
  130. 'where' => array( 'id', '=', $nid ),
  131. 'limit' => array( 1 ),
  132. ) );
  133. $this->trellis->db->execute();
  134. return $id;
  135. }
  136. #=======================================
  137. # @ Edit News
  138. #=======================================
  139. public function edit($data, $id)
  140. {
  141. if ( ! $id = intval( $id ) ) return false;
  142. $fields = array(
  143. 'uid' => 'int',
  144. 'title' => 'string',
  145. 'excerpt' => 'string',
  146. 'content' => 'string',
  147. 'allow_comments' => 'int',
  148. 'html' => 'int',
  149. 'date' => 'int',
  150. 'ipadd' => 'string',
  151. );
  152. $this->trellis->db->construct( array(
  153. 'update' => 'news',
  154. 'set' => $this->trellis->process_data( $fields, $data ),
  155. 'where' => array( 'id', '=', $id ),
  156. 'limit' => array( 1 ),
  157. ) );
  158. $this->trellis->db->execute();
  159. return $this->trellis->db->get_affected_rows();
  160. }
  161. #=======================================
  162. # @ Edit Comment
  163. #=======================================
  164. public function edit_comment($data, $id)
  165. {
  166. if ( ! $id = intval( $id ) ) return false;
  167. $fields = array(
  168. 'message' => 'string',
  169. 'staff' => 'int',
  170. 'html' => 'int',
  171. );
  172. $this->trellis->db->construct( array(
  173. 'update' => 'news_comments',
  174. 'set' => $this->trellis->process_data( $fields, $data ),
  175. 'where' => array( 'id', '=', $id ),
  176. 'limit' => array( 1 ),
  177. ) );
  178. $this->trellis->db->execute();
  179. return $this->trellis->db->get_affected_rows();
  180. }
  181. #=======================================
  182. # @ Delete News
  183. #=======================================
  184. public function delete($id)
  185. {
  186. if ( ! $id = intval( $id ) ) return false;
  187. $this->trellis->db->construct( array(
  188. 'delete' => 'news_comments',
  189. 'where' => array( 'nid', '=', $id ),
  190. ) );
  191. $this->trellis->db->execute();
  192. $this->trellis->db->construct( array(
  193. 'delete' => 'news',
  194. 'where' => array( 'id', '=', $id ),
  195. 'limit' => array( 1 ),
  196. ) );
  197. $this->trellis->db->execute();
  198. return $this->trellis->db->get_affected_rows();
  199. }
  200. #=======================================
  201. # @ Delete Comment
  202. #=======================================
  203. public function delete_comment($id, $params=array())
  204. {
  205. if ( ! $id = intval( $id ) ) return false;
  206. if ( ! isset( $params['nid'] ) )
  207. {
  208. if ( ! $c = $this->get_single_comment_by_id( array( 'nid' ), $id ) ) return false;
  209. $params['nid'] = $c['nid'];
  210. }
  211. $this->trellis->db->construct( array(
  212. 'delete' => 'news_comments',
  213. 'where' => array( 'id', '=', $id ),
  214. 'limit' => array( 1 ),
  215. ) );
  216. $this->trellis->db->execute();
  217. $dc = $this->trellis->db->get_affected_rows();
  218. $this->trellis->db->construct( array(
  219. 'update' => 'news',
  220. 'set' => array( 'comments' => array( '-', 1 ) ),
  221. 'where' => array( 'id', '=', $params['nid'] ),
  222. 'limit' => array( 1 ),
  223. ) );
  224. $this->trellis->db->next_shutdown();
  225. $this->trellis->db->execute();
  226. return $dc;
  227. }
  228. }
  229. ?>