/issues/core/news_api.php

https://github.com/osarrat/sigmah-website · PHP · 326 lines · 198 code · 67 blank · 61 comment · 28 complexity · be523d58d72888a2c45a43c82afc9c04 MD5 · raw file

  1. <?php
  2. # MantisBT - a php based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * News API
  17. * @package CoreAPI
  18. * @subpackage NewsAPI
  19. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  20. * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  21. * @link http://www.mantisbt.org
  22. */
  23. /**
  24. * requires current_user_api
  25. */
  26. require_once( 'current_user_api.php' );
  27. /**
  28. * requires twitter_api
  29. */
  30. require_once( 'twitter_api.php' );
  31. # --------------------
  32. # Add a news item
  33. function news_create( $p_project_id, $p_poster_id, $p_view_state, $p_announcement, $p_headline, $p_body ) {
  34. $c_project_id = db_prepare_int( $p_project_id );
  35. $c_poster_id = db_prepare_int( $p_poster_id );
  36. $c_view_state = db_prepare_int( $p_view_state );
  37. $c_announcement = db_prepare_bool( $p_announcement );
  38. if( is_blank( $p_headline ) ) {
  39. error_parameters( lang_get( 'headline' ) );
  40. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  41. }
  42. if( is_blank( $p_body ) ) {
  43. error_parameters( lang_get( 'body' ) );
  44. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  45. }
  46. $t_news_table = db_get_table( 'mantis_news_table' );
  47. # Add item
  48. $query = "INSERT
  49. INTO $t_news_table
  50. ( project_id, poster_id, date_posted, last_modified,
  51. view_state, announcement, headline, body )
  52. VALUES
  53. ( " . db_param() . ",
  54. " . db_param() . ",
  55. " . db_param() . ",
  56. " . db_param() . ",
  57. " . db_param() . ",
  58. " . db_param() . ",
  59. " . db_param() . ",
  60. " . db_param() . "
  61. )";
  62. db_query_bound( $query, Array( $c_project_id, $c_poster_id, db_now(), db_now(), $c_view_state, $c_announcement, $p_headline, $p_body ) );
  63. $t_news_id = db_insert_id( $t_news_table );
  64. twitter_news( $t_news_id );
  65. return $t_news_id;
  66. }
  67. # --------------------
  68. # Delete the news entry
  69. function news_delete( $p_news_id ) {
  70. $c_news_id = db_prepare_int( $p_news_id );
  71. $t_news_table = db_get_table( 'mantis_news_table' );
  72. $query = "DELETE FROM $t_news_table
  73. WHERE id=" . db_param();
  74. db_query_bound( $query, Array( $c_news_id ) );
  75. # db_query errors on failure so:
  76. return true;
  77. }
  78. # --------------------
  79. # Delete the news entry
  80. function news_delete_all( $p_project_id ) {
  81. $c_project_id = db_prepare_int( $p_project_id );
  82. $t_news_table = db_get_table( 'mantis_news_table' );
  83. $query = "DELETE FROM $t_news_table
  84. WHERE project_id=" . db_param();
  85. db_query_bound( $query, Array( $c_project_id ) );
  86. # db_query errors on failure so:
  87. return true;
  88. }
  89. # --------------------
  90. # Update news item
  91. function news_update( $p_news_id, $p_project_id, $p_view_state, $p_announcement, $p_headline, $p_body ) {
  92. $c_news_id = db_prepare_int( $p_news_id );
  93. $c_project_id = db_prepare_int( $p_project_id );
  94. $c_view_state = db_prepare_int( $p_view_state );
  95. $c_announcement = db_prepare_bool( $p_announcement );
  96. if( is_blank( $p_headline ) ) {
  97. error_parameters( lang_get( 'headline' ) );
  98. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  99. }
  100. if( is_blank( $p_body ) ) {
  101. error_parameters( lang_get( 'body' ) );
  102. trigger_error( ERROR_EMPTY_FIELD, ERROR );
  103. }
  104. $t_news_table = db_get_table( 'mantis_news_table' );
  105. # Update entry
  106. $query = "UPDATE $t_news_table
  107. SET view_state=" . db_param() . ",
  108. announcement=" . db_param() . ",
  109. headline=" . db_param() . ",
  110. body=" . db_param() . ",
  111. project_id=" . db_param() . ",
  112. last_modified= " . db_param() . "
  113. WHERE id=" . db_param();
  114. db_query_bound( $query, Array( $c_view_state, $c_announcement, $p_headline, $p_body, $c_project_id, db_now(), $c_news_id ) );
  115. # db_query errors on failure so:
  116. return true;
  117. }
  118. # --------------------
  119. # Selects the news item associated with the specified id
  120. function news_get_row( $p_news_id ) {
  121. $c_news_id = db_prepare_int( $p_news_id );
  122. $t_news_table = db_get_table( 'mantis_news_table' );
  123. $query = "SELECT *
  124. FROM $t_news_table
  125. WHERE id=" . db_param();
  126. $result = db_query_bound( $query, Array( $c_news_id ) );
  127. if( 0 == db_num_rows( $result ) ) {
  128. trigger_error( ERROR_NEWS_NOT_FOUND, ERROR );
  129. } else {
  130. $row = db_fetch_array( $result );
  131. return $row;
  132. }
  133. }
  134. # --------------------
  135. # get news count (selected project plus sitewide posts)
  136. function news_get_count( $p_project_id, $p_sitewide = true ) {
  137. $c_project_id = db_prepare_int( $p_project_id );
  138. $t_news_table = db_get_table( 'mantis_news_table' );
  139. $t_project_where = helper_project_specific_where( $p_project_id );
  140. $query = "SELECT COUNT(*)
  141. FROM $t_news_table
  142. WHERE $t_project_where";
  143. if( $p_sitewide ) {
  144. $query .= ' OR project_id=' . ALL_PROJECTS;
  145. }
  146. $result = db_query_bound( $query );
  147. return db_result( $result, 0, 0 );
  148. }
  149. # --------------------
  150. # get news items (selected project plus sitewide posts)
  151. function news_get_rows( $p_project_id, $p_sitewide = true ) {
  152. $t_news_table = db_get_table( 'mantis_news_table' );
  153. $t_projects = current_user_get_all_accessible_subprojects( $p_project_id );
  154. $t_projects[] = (int)$p_project_id;
  155. if( $p_sitewide && ALL_PROJECTS != $p_project_id ) {
  156. $t_projects[] = ALL_PROJECTS;
  157. }
  158. $query = "SELECT *
  159. FROM $t_news_table";
  160. if( 1 == count( $t_projects ) ) {
  161. $c_project_id = $t_projects[0];
  162. $query .= " WHERE project_id='$c_project_id'";
  163. } else {
  164. $query .= ' WHERE project_id IN (' . join( $t_projects, ',' ) . ')';
  165. }
  166. $query .= " ORDER BY date_posted DESC";
  167. $result = db_query( $query );
  168. $t_rows = array();
  169. $t_row_count = db_num_rows( $result );
  170. for( $i = 0;$i < $t_row_count;$i++ ) {
  171. $row = db_fetch_array( $result );
  172. array_push( $t_rows, $row );
  173. }
  174. return $t_rows;
  175. }
  176. # --------------------
  177. # Check if the specified news item is private
  178. function news_get_field( $p_news_id, $p_field_name ) {
  179. $row = news_get_row( $p_news_id );
  180. return( $row[$p_field_name] );
  181. }
  182. # --------------------
  183. # Check if the specified news item is private
  184. function news_is_private( $p_news_id ) {
  185. return( news_get_field( $p_news_id, 'view_state' ) == VS_PRIVATE );
  186. }
  187. # --------------------
  188. # Gets a limited set of news rows to be viewed on one page based on the criteria
  189. # defined in the configuration file.
  190. function news_get_limited_rows( $p_offset, $p_project_id = null ) {
  191. if( $p_project_id === null ) {
  192. $p_project_id = helper_get_current_project();
  193. }
  194. $c_offset = db_prepare_int( $p_offset );
  195. $t_projects = current_user_get_all_accessible_subprojects( $p_project_id );
  196. $t_projects[] = (int)$p_project_id;
  197. if( ALL_PROJECTS != $p_project_id ) {
  198. $t_projects[] = ALL_PROJECTS;
  199. }
  200. $t_news_table = db_get_table( 'mantis_news_table' );
  201. $t_news_view_limit = config_get( 'news_view_limit' );
  202. $t_news_view_limit_days = config_get( 'news_view_limit_days' ) * SECONDS_PER_DAY;
  203. switch( config_get( 'news_limit_method' ) ) {
  204. case 0:
  205. # BY_LIMIT - Select the news posts
  206. $query = "SELECT *
  207. FROM $t_news_table";
  208. if( 1 == count( $t_projects ) ) {
  209. $c_project_id = $t_projects[0];
  210. $query .= " WHERE project_id='$c_project_id'";
  211. } else {
  212. $query .= ' WHERE project_id IN (' . join( $t_projects, ',' ) . ')';
  213. }
  214. $query .= ' ORDER BY announcement DESC, id DESC';
  215. $result = db_query( $query, $t_news_view_limit, $c_offset );
  216. break;
  217. case 1:
  218. # BY_DATE - Select the news posts
  219. $query = "SELECT *
  220. FROM $t_news_table WHERE
  221. ( " . db_helper_compare_days( 0, 'date_posted', "< $t_news_view_limit_days" ) . "
  222. OR announcement = " . db_param() . " ) ";
  223. $t_params = Array(
  224. db_now(),
  225. 1,
  226. );
  227. if( 1 == count( $t_projects ) ) {
  228. $c_project_id = $t_projects[0];
  229. $query .= " AND project_id=" . db_param();
  230. $t_params[] = $c_project_id;
  231. } else {
  232. $query .= ' AND project_id IN (' . join( $t_projects, ',' ) . ')';
  233. }
  234. $query .= " ORDER BY announcement DESC, id DESC";
  235. $result = db_query_bound( $query, $t_params, $t_news_view_limit, $c_offset );
  236. break;
  237. }
  238. # end switch
  239. $t_row_count = db_num_rows( $result );
  240. $t_rows = array();
  241. for( $i = 0;$i < $t_row_count;$i++ ) {
  242. $row = db_fetch_array( $result );
  243. array_push( $t_rows, $row );
  244. }
  245. return $t_rows;
  246. }
  247. # --------------------
  248. # Checks if the news feature is enabled or not.
  249. # true: enabled, otherwise false.
  250. function news_is_enabled() {
  251. return config_get( 'news_enabled' ) == ON;
  252. }
  253. # --------------------
  254. # Ensures that the news feature is enabled, otherwise generates an access denied error.
  255. function news_ensure_enabled() {
  256. if ( !news_is_enabled() ) {
  257. access_denied();
  258. }
  259. }