[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> news_api.php (source)

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


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7