b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_core/model/dataobjects/_dataobjectlist2.class.php - 492 lines - 12269 bytes - Summary - Text - Print

Description: This file implements the abstract DataObjectList2 base class. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the abstract DataObjectList2 base class.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * }}
  21   *
  22   * @package evocore
  23   *
  24   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  25   * @author fplanque: Francois PLANQUE
  26   *
  27   * @version $Id: _dataobjectlist2.class.php 6136 2014-03-08 07:59:48Z manuel $
  28   */
  29  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  30  
  31  load_class('_core/ui/results/_results.class.php', 'Results' );
  32  
  33  
  34  /**
  35   * @package evocore
  36   */
  37  class FilteredResults extends Results
  38  {
  39      /**
  40       * Default filter set (used if no specific params are passed)
  41       */
  42      var $default_filters = array();
  43  
  44      /**
  45       * Current filter set (depending on user input)
  46       */
  47      var $filters = array();
  48  
  49      /**
  50      * Constructor
  51      * 
  52      * @param string Filterset name
  53      */
  54  	function FilteredResults( $filterset_name )
  55      {
  56          $this->filterset_name = $filterset_name;
  57      }
  58  
  59      /**
  60       * Check if the Result set is filtered or not
  61       */
  62  	function is_filtered()
  63      {
  64          if( empty( $this->filters ) )
  65          {
  66              return false;
  67          }
  68  
  69          return ( $this->filters != $this->default_filters );
  70      }
  71  
  72  
  73      /**
  74       * Get a specific active filter
  75       */
  76  	function get_active_filter( $key )
  77      {
  78          if( isset($this->filters[$key]) )
  79          {
  80              return $this->filters[$key];
  81          }
  82  
  83          return NULL;
  84      }
  85  
  86  
  87      /**
  88       * Get every active filter that is not the same as the defaults
  89       */
  90  	function get_active_filters()
  91      {
  92          $r = array();
  93  
  94          foreach( $this->default_filters as $key => $value )
  95          {
  96              if( !isset( $this->filters[$key] ) )
  97              {    // Some value has not been copied over from defaults to active or specifically set:
  98                  if( !is_null($value)) // Note: NULL value are not copied over. that's normal.
  99                  {    // A NON NULL value is missing
 100                      $r[] = $key;
 101                  }
 102              }
 103              elseif( $value != $this->filters[$key] )
 104              {
 105                  $r[] = $key;
 106              }
 107          }
 108          return $r;
 109      }
 110  
 111  
 112      /**
 113       * Show every active filter that is not the same as the defaults
 114       */
 115  	function dump_active_filters()
 116      {
 117          foreach( $this->default_filters as $key => $value )
 118          {
 119              if( !isset( $this->filters[$key] ) )
 120              {    // SOme value has not been copied over from defaults to active or specifically set:
 121                  if( !is_null($value)) // Note: NULL value ar enot copied over. that's normal.
 122                  {    // A NON NULL value is missing
 123                      pre_dump( 'no active value for default '.$key );
 124                  }
 125              }
 126              elseif( $value != $this->filters[$key] )
 127              {
 128                  pre_dump( 'default '.$key, $value );
 129                  pre_dump( 'active '.$key, $this->filters[$key] );
 130              }
 131          }
 132      }
 133  
 134  
 135      /**
 136       * Set default filter values we always want to use if not individually specified otherwise:
 137       *
 138       * @param array default filters to be merged with the class defaults
 139       * @param array default filters for each preset, to be merged with general default filters if the preset is used
 140       */
 141  	function set_default_filters( $default_filters, $preset_filters = array() )
 142      {
 143          $this->default_filters = array_merge( $this->default_filters, $default_filters );
 144          $this->preset_filters = $preset_filters;
 145      }
 146  
 147  
 148      /**
 149       * Activate preset default filters if necessary
 150       *
 151       */
 152  	function activate_preset_filters()
 153      {
 154          if( empty( $this->filters['filter_preset'] ) )
 155          { // No filter preset, there are no additional defaults to use:
 156              return;
 157          }
 158  
 159          // Override general defaults with the specific defaults for the preset:
 160          $this->default_filters = array_merge( $this->default_filters, $this->preset_filters[$this->filters['filter_preset']] );
 161  
 162          // Save the name of the preset in order for is_filtered() to work properly:
 163          $this->default_filters['filter_preset'] = $this->filters['filter_preset'];
 164      }
 165  
 166  
 167      /**
 168       * Save current filterset to session.
 169       */
 170  	function save_filterset()
 171      {
 172          /**
 173           * @var Session
 174           */
 175          global $Session, $Debuglog, $localtimenow;
 176  
 177          $Debuglog->add( 'Saving filterset <strong>'.$this->filterset_name.'</strong>', 'filters' );
 178  
 179          $Session->set( $this->filterset_name, $this->filters );
 180          $Session->set( $this->filterset_name.'_refresh_time', floor( $localtimenow / 3600 ) * 3600 );
 181      }
 182  
 183  
 184      /**
 185       * Load previously saved filterset from session.
 186       *
 187       * @return boolean true if we could restore something
 188       */
 189  	function restore_filterset()
 190      {
 191          /**
 192           * @var Session
 193           */
 194              global $Session;
 195          /**
 196           * @var Request
 197           */
 198  
 199          global $Debuglog;
 200  
 201          $filters = $Session->get( $this->filterset_name );
 202  
 203          /*
 204          fp> 2007-09-26> even if there are no filters, we need to "set" them in order to set global variables like $show_statuses
 205          if( empty($filters) )
 206          { // We have no saved filters:
 207              return false;
 208          }
 209          */
 210  
 211          if( empty($filters) )
 212          { // set_filters() expects array
 213              $filters = array();
 214          }
 215  
 216          $Debuglog->add( 'Restoring filterset <strong>'.$this->filterset_name.'</strong>', 'filters' );
 217  
 218          // Restore filters:
 219          $this->set_filters( $filters );
 220  
 221          return true;
 222      }
 223  
 224  
 225      /**
 226       * Set/Activate filterset.
 227       *
 228       * @param array
 229       */
 230  	function set_filters( $filters )
 231      {
 232          if( !empty( $filters ) )
 233          { // Activate the filterset (fallback to default filter when a value is not set):
 234              $this->filters = array_merge( $this->default_filters, $filters );
 235          }
 236  
 237          // Activate preset filters if necessary:
 238          $this->activate_preset_filters();
 239      }
 240  }
 241  
 242  
 243  
 244  /**
 245   * Data Object List Base Class 2
 246   *
 247   * This is typically an abstract class, useful only when derived.
 248   * Holds DataObjects in an array and allows walking through...
 249   *
 250   * This SECOND implementation will deprecate the first one when finished.
 251   *
 252   * @package evocore
 253   * @version beta
 254   * @abstract
 255   */
 256  class DataObjectList2 extends FilteredResults
 257  {
 258  
 259  
 260      /**
 261       * Constructor
 262       *
 263       * If provided, executes SQL query via parent Results object
 264       *
 265       * @param DataObjectCache
 266       * @param integer number of lines displayed on one screen (null for default [20])
 267       * @param string prefix to differentiate page/order params when multiple Results appear on same page
 268       * @param string default ordering of columns (special syntax)
 269       */
 270  	function DataObjectList2( & $Cache, $limit = null, $param_prefix = '', $default_order = NULL )
 271      {
 272          // WARNING: we are not passing any SQL query to the Results object
 273          // This will make the Results object behave a little bit differently than usual:
 274          parent::Results( NULL, $param_prefix, $default_order, $limit, NULL, false );
 275  
 276          // The list objects will also be cached in this cache.
 277          // The Cache object may also be useful to get table information for the Items.
 278          $this->Cache = & $Cache;
 279  
 280          // Colum used for IDs
 281          $this->ID_col = $Cache->dbIDname;
 282      }
 283  
 284  
 285      function & get_row_by_idx( $idx )
 286      {
 287          return $this->rows[ $idx ];
 288      }
 289  
 290  
 291      /**
 292       * Instantiate an object for requested row and cache it:
 293       */
 294      function & get_by_idx( $idx )
 295      {
 296          return $this->Cache->instantiate( $this->rows[$idx] ); // pass by reference: creates $rows[$idx]!
 297      }
 298  
 299  
 300      /**
 301       * Instantiate an object for requested row by field and cache it:
 302       *
 303       * @param string DB field name
 304       * @param string Value
 305       * @return object
 306       */
 307      function & get_by_field( $field_name, $field_value )
 308      {
 309          $obj_ID = 0;
 310          $null_Obj = NULL;
 311  
 312          foreach( $this->rows as $row )
 313          {    // Find object ID by field value
 314              if( $row->$field_name == $field_value )
 315              {
 316                  $obj_ID = $row->{$this->ID_col};
 317                  break;
 318              }
 319          }
 320  
 321          if( $obj_ID == 0 )
 322          {    // No object ID found, Exit here
 323              return $null_Obj;
 324          }
 325  
 326          $this->restart();
 327          while( $Obj = & $this->get_next() )
 328          {    // Find Object by ID
 329              if( $Obj->ID == $obj_ID )
 330              {
 331                  return $Obj;
 332              }
 333          }
 334  
 335          return $null_Obj;
 336      }
 337  
 338  
 339      /**
 340       * Get next object in list
 341       */
 342      function & get_next()
 343      {
 344          // echo '<br />Get next, current idx was: '.$this->current_idx.'/'.$this->result_num_rows;
 345  
 346          if( $this->current_idx >= $this->result_num_rows )
 347          {    // No more object in list
 348              $this->current_Obj = NULL;
 349              $r = false; // TODO: try with NULL
 350              return $r;
 351          }
 352  
 353          // We also keep a local ref in case we want to use it for display:
 354          $this->current_Obj = & $this->get_by_idx( $this->current_idx );
 355          $this->next_idx();
 356  
 357          return $this->current_Obj;
 358      }
 359  
 360  
 361      /**
 362       * Display a global title matching filter params
 363       *
 364       * @todo implement $order
 365       *
 366       * @param string prefix to display if a title is generated
 367       * @param string suffix to display if a title is generated
 368       * @param string glue to use if multiple title elements are generated
 369       * @param string comma separated list of titles inthe order we would like to display them
 370       * @param string format to output, default 'htmlbody'
 371       */
 372  	function get_filter_title( $prefix = ' ', $suffix = '', $glue = ' - ', $order = NULL, $format = 'htmlbody' )
 373      {
 374          $title_array = $this->get_filter_titles();
 375  
 376          if( empty( $title_array ) )
 377          {
 378              return '';
 379          }
 380  
 381          // We have something to display:
 382          $r = implode( $glue, $title_array );
 383          $r = $prefix.format_to_output( $r, $format ).$suffix;
 384          return $r;
 385      }
 386  
 387  
 388      /**
 389       * Move up the element order in database
 390       *
 391       * @param integer id element
 392       * @return unknown
 393       */
 394  	function move_up( $id )
 395      {
 396          global $DB, $Messages, $result_fadeout;
 397  
 398          $DB->begin();
 399  
 400          if( ($obj = & $this->Cache->get_by_ID( $id )) === false )
 401          {
 402              $Messages->add( sprintf( T_('Requested &laquo;%s&raquo; object does not exist any longer.'), T_('Entry') ), 'error' );
 403              $DB->commit();
 404              return false;
 405          }
 406          $order = $obj->order;
 407  
 408          // Get the ID of the inferior element which his order is the nearest
 409          $rows = $DB->get_results( 'SELECT '.$this->Cache->dbIDname
 410                   .' FROM '.$this->Cache->dbtablename
 411                  .' WHERE '.$this->Cache->dbprefix.'order < '.$order
 412                  .' ORDER BY '.$this->Cache->dbprefix.'order DESC'
 413                  .' LIMIT 0,1' );
 414  
 415          if( count( $rows ) )
 416          {
 417              // instantiate the inferior element
 418              $obj_inf = & $this->Cache->get_by_ID( $rows[0]->{$this->Cache->dbIDname} );
 419  
 420              //  Update element order
 421              $obj->set( 'order', $obj_inf->order );
 422              $obj->dbupdate();
 423  
 424              // Update inferior element order
 425              $obj_inf->set( 'order', $order );
 426              $obj_inf->dbupdate();
 427  
 428              // EXPERIMENTAL FOR FADEOUT RESULT
 429              $result_fadeout[$this->Cache->dbIDname][] = $id;
 430              $result_fadeout[$this->Cache->dbIDname][] = $obj_inf->ID;
 431          }
 432          else
 433          {
 434              $Messages->add( T_('This element is already at the top.'), 'error' );
 435          }
 436          $DB->commit();
 437      }
 438  
 439  
 440      /**
 441       * Move down the element order in database
 442       *
 443       * @param integer id element
 444       * @return unknown
 445       */
 446  	function move_down( $id )
 447      {
 448          global $DB, $Messages, $result_fadeout;
 449  
 450          $DB->begin();
 451  
 452          if( ($obj = & $this->Cache->get_by_ID( $id )) === false )
 453          {
 454              $Messages->add( sprintf( T_('Requested &laquo;%s&raquo; object does not exist any longer.'), T_('Entry') ), 'error' );
 455              $DB->commit();
 456              return false;
 457          }
 458          $order = $obj->order;
 459  
 460          // Get the ID of the inferior element which his order is the nearest
 461          $rows = $DB->get_results( 'SELECT '.$this->Cache->dbIDname
 462                                                               .' FROM '.$this->Cache->dbtablename
 463                                                           .' WHERE '.$this->Cache->dbprefix.'order > '.$order
 464                                                      .' ORDER BY '.$this->Cache->dbprefix.'order ASC
 465                                                                   LIMIT 0,1' );
 466  
 467          if( count( $rows ) )
 468          {
 469              // instantiate the inferior element
 470              $obj_sup = & $this->Cache->get_by_ID( $rows[0]->{$this->Cache->dbIDname} );
 471  
 472              //  Update element order
 473              $obj->set( 'order', $obj_sup->order );
 474              $obj->dbupdate();
 475  
 476              // Update inferior element order
 477              $obj_sup->set( 'order', $order );
 478              $obj_sup->dbupdate();
 479  
 480              // EXPERIMENTAL FOR FADEOUT RESULT
 481              $result_fadeout[$this->Cache->dbIDname][] = $id;
 482              $result_fadeout[$this->Cache->dbIDname][] = $obj_sup->ID;
 483          }
 484          else
 485          {
 486              $Messages->add( T_('This element is already at the bottom.'), 'error' );
 487          }
 488          $DB->commit();
 489      }
 490  }
 491  
 492  ?>

title

Description

title

Description

title

Description

title

title

Body