b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_core/ui/results/_resultsel.class.php - 595 lines - 18618 bytes - Summary - Text - Print

Description: This file implements the ResultSel 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 ResultSel 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   * Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.
  10   *
  11   * {@internal License choice
  12   * - If you have received this file as part of a package, please find the license.txt file in
  13   *   the same folder or the closest folder above for complete license terms.
  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15   *   then you must choose one of the following licenses before using the file:
  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18   * }}
  19   *
  20   * {@internal Open Source relicensing agreement:
  21   * PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license
  22   * PROGIDISTRI S.A.S.'s contributions to this file and the b2evolution project
  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  24   * }}
  25   *
  26   *
  27   * @package evocore
  28   *
  29   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  30   * @author fplanque: Francois PLANQUE
  31   * @author fsaya: Fabrice SAYA-GASNIER / PROGIDISTRI
  32   *
  33   * @version $Id: _resultsel.class.php 6136 2014-03-08 07:59:48Z manuel $
  34   */
  35  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  36  
  37  load_class('_core/ui/results/_results.class.php', 'Results' );
  38  
  39  
  40  /**
  41   * ResultSel class: displays Results and provides Selection capabilities
  42   *
  43   */
  44  class ResultSel extends Results
  45  {
  46      /**
  47       * var Form
  48       */
  49      var $Form;
  50  
  51      var $current_selection_ID;
  52      var $table_selections;
  53      var $field_selected;
  54      var $field_selection;
  55  
  56  
  57      /**
  58       * Constructor
  59       *
  60       * @param string fieldname of item ID to select on
  61       * @param string
  62       * @param string
  63       * @param string
  64       * @param string
  65       * @param string
  66       * @param string
  67       * @param integer current selection ID
  68       * @param string SQL query
  69       * @param NULL|string SQL query used to count the total # of rows (if NULL, we'll try to COUNT(*) by ourselves)
  70       * @param string prefix to differentiate page/order params when multiple Results appear one same page
  71       * @param string default ordering of columns (special syntax) if not URL specified
  72       * @param integer number of lines displayed on one screen
  73       */
  74  	function ResultSel( $field_ID, $table_selections, $field_sel_ID, $field_sel_name,
  75                                              $table_objsel, $field_selected, $field_selection, $current_selection_ID,
  76                                              $sql, $count_sql = NULL, $param_prefix = '', $default_order = '', $limit = 20 )
  77      {
  78          global $current_User;
  79  
  80          // Call parent:
  81          parent::Results( $sql, $param_prefix, $default_order, $limit, $count_sql );
  82  
  83          if( ! $current_User->check_perm( 'selections', 'view' ) )
  84          {    // User is NOT allowed to view selections
  85              // Don't do any more then base class:
  86              return;
  87          }
  88  
  89          $this->current_selection_ID = $current_selection_ID;
  90          $this->table_selections     = $table_selections;
  91          $this->field_sel_ID         = $field_sel_ID;
  92          $this->field_sel_name       = $field_sel_name;
  93          $this->table_objsel         = $table_objsel;
  94          $this->field_selected       = $field_selected;
  95          $this->field_selection      = $field_selection;
  96  
  97          // Presets a selection checkbox:
  98          $this->cols[] = array(
  99                          'th' => /* TRANS: abbr. for "Selection" */ T_('Sel'),
 100                          'td_class' => 'shrinkwrap',
 101                          'td' => '%selection_checkbox( #'.$field_ID.'#, \''.$param_prefix.'\' )%',
 102                      );
 103      }
 104  
 105  
 106      /**
 107       * Display list/table start preceeded by <form> opening.
 108       */
 109  	function display_list_start()
 110      {
 111          global $item_ID_array, $current_User;
 112  
 113          if( ! $current_User->check_perm( 'selections', 'view' ) )
 114          {    // User is NOT allowed to view selections
 115              // Don't do any more then base class:
 116              parent::display_list_start();
 117              return;
 118          }
 119  
 120          $this->Form = new Form( regenerate_url('', '', '', '&'), $this->param_prefix.'selections_checkchanges', 'post', 'none' ); // COPY!!
 121  
 122          $this->Form->begin_form( '' );
 123  
 124          if( $this->total_pages > 0 )
 125          {    // We have rows to display, we want the selection stuff:
 126  
 127              // Need it to check in the next page if the selection has to be updated
 128              $this->Form->hidden( $this->param_prefix.'previous_sel_ID', $this->current_selection_ID );
 129  
 130              // Sets the cols_check global variable to verify if checkboxes
 131              // have to be checked in the result set :
 132              cols_check( $this->current_selection_ID, $this->table_objsel, $this->field_selected, $this->field_selection );
 133  
 134              // item_ID_array must be emptied to avoid conflicts with previous result sets :
 135              // TODO: put this into object
 136              $item_ID_array = array();
 137          }
 138  
 139          // list/table start:
 140          parent::display_list_start();
 141      }
 142  
 143  
 144      /**
 145       * Display list/table end followed by </form> closing.
 146       *
 147       * Typically outputs </ul> or </table>
 148       */
 149  	function display_list_end()
 150      {
 151          global $current_User;
 152  
 153          if( ! $current_User->check_perm( 'selections', 'view' ) )
 154          {    // User is NOT allowed to view selections
 155              // Don't do any more then base class:
 156              parent::display_list_end();
 157              return;
 158          }
 159  
 160          // list/table end:
 161          parent::display_list_end();
 162  
 163          $this->Form->end_form();
 164      }
 165  
 166  
 167      /**
 168       * Display functions
 169       */
 170  	function display_functions()
 171      {
 172          $this->functions_area = array( 'callback' => array( 'this', 'selection_menu' ) );
 173  
 174          parent::display_functions();
 175      }
 176  
 177  
 178      /**
 179       * Callback for selection menu
 180       */
 181  	function selection_menu()
 182      {
 183          global $item_ID_array, $current_User;
 184  
 185          $can_edit = $current_User->check_perm( 'selections', 'edit' );
 186  
 187          if( $can_edit )
 188          { // links to check all and uncheck all
 189              echo $this->Form->check_all();
 190          }
 191  
 192          if( $current_User->check_perm( 'selections', 'view' ) )
 193          {
 194              // construction of the select menu :
 195              $selection_name = selection_select_tag( $this->param_prefix, $this->table_selections, $this->field_sel_name, $this->field_sel_ID, $this->current_selection_ID );
 196          }
 197  
 198          if( $can_edit )
 199          {
 200              $this->Form->text( 'selection_'.$this->param_prefix.'name', $selection_name, 25, T_('Selection name'), '', 60 );
 201  
 202              // List of IDs displayed on this page (needed for deletes):
 203              $this->Form->hidden( 'item_ID_list', implode( $item_ID_array, ',' ) );
 204  
 205              // actionArray[update_selection] is experimental
 206              $this->Form->submit( array( 'actionArray[update_'.$this->param_prefix.'selection]', T_('Update selection'), 'SaveButton' ) );
 207          }
 208      }
 209  
 210  }
 211  
 212  
 213  /**
 214   * Sets the cols_check global variable to verify if checkboxes have to be checked in the result set
 215   *
 216   * @param the selection ID
 217   * @param the name of the attachment table (which links the items and the selections
 218   * @param the item id field
 219   * @param the selection id field
 220   */
 221  function cols_check( $selection_ID, $sel_table, $sel_table_item, $sel_table_selection )
 222  {
 223      global $DB, $cols_check;
 224  
 225      if( $selection_ID !== 0 )
 226      {
 227          $sql_check = 'SELECT '.$sel_table_item.' FROM '.$sel_table.' WHERE '.$sel_table_selection.'='.$selection_ID;
 228          $cols_check = $DB->get_col( $sql_check );
 229      }
 230      else
 231      {
 232          $cols_check = array();
 233      }
 234  }
 235  
 236  
 237  /**
 238   * Display a checkbox allowing to add the item to a selection
 239   *
 240   * Only one checkbox will be displayed for each ID.
 241   * IDs which are already in the selection will be pre-checked.
 242   *
 243   * @deprecated should go into ResultSel class
 244   *
 245   * @param integer item ID
 246   * @param string item name / prefix for form values
 247   * @return string the correct input tag
 248   */
 249  function selection_checkbox( $item_ID, $param_prefix )
 250  {
 251      global $current_User;
 252      // List of checkboxes to pre-check:
 253      global $cols_check;
 254      // List of already displayed checkboxes (can be used outside to get a list of checkboxes which have been displayed)
 255      global $item_ID_array;
 256  
 257      if( in_array( $item_ID, $item_ID_array ) )
 258      {    // We have already displayed a checkbox for this ID
 259          return '&nbsp;';    // nbsp is for IE...
 260      }
 261  
 262      $item_ID_array[] = $item_ID; //construction of the ID list
 263  
 264      $r = '';
 265  
 266      if( $current_User->check_perm( 'selections', 'edit' ) )
 267      {    // User is allowed to edit
 268          $r .= '<span name="surround_check" class="checkbox_surround_init"><input type="checkbox" class="checkbox" name="'.$param_prefix.'items[]" value="'.$item_ID.'"';
 269          if( in_array( $item_ID, $cols_check ) )
 270          {    // already in selection:
 271              $r .= ' checked="checked" ';
 272          }
 273          $r .= ' /></span>';
 274      }
 275      else
 276      {    // User CANNOT edit:
 277          if( in_array( $item_ID, $cols_check ) )
 278          {    // already in selection:
 279              $r .= '*';
 280          }
 281          else
 282          {
 283              $r .= '&nbsp;';
 284          }
 285      }
 286  
 287      return $r;
 288  }
 289  
 290  
 291  /**
 292   * Creates the select tag in the search menu and fills it with the appropriate option tags
 293   *
 294   * @param string the selection category prefix(the one used during the construction of the result object)
 295   * @param string the name of the table containing the selections
 296   * @param string the name field  in the selection table
 297   * @param string the id field in the selection table
 298   * @param integer the current selection id
 299   */
 300  function selection_select_tag(
 301                                                              $category_prefix,
 302                                                              $selections_table,
 303                                                              $selections_table_name,
 304                                                              $selections_table_ID,
 305                                                              $selection_ID
 306                                                              )
 307  {
 308      global $DB, $selection_name, $current_User;
 309  
 310      $r = T_('Selection');
 311      $r .= ' <select name="selection_'.$category_prefix.'ID"
 312                      onchange="selform = get_form( this );
 313                          selform.elements[\''.$category_prefix.'previous_sel_ID\'].value=-1;
 314                          selform.submit()" >'."\n";
 315      // in the onchange attribute, option_db is set to -1 to avoid updating the database
 316  
 317      if( $current_User->check_perm( 'selections', 'edit' ) )
 318      {    // User is allowed to edit
 319          $r .= '<option value="0">'.T_('New selection')."</option>\n";
 320      }
 321      else
 322      {    // User CANNOT edit:
 323          $r .= '<option value="0">'.T_('None')."</option>\n";
 324      }
 325  
 326      $sql = 'SELECT * FROM '.$selections_table.' ORDER BY '.$selections_table_name;
 327      $rows = $DB->get_results( $sql );
 328      if( !empty( $rows ) )
 329      {
 330          $selection_name = '';
 331          foreach( $rows as $row )
 332          { // construction of the option tags
 333              if( $row->$selections_table_ID == $selection_ID )
 334              { // option selected by default
 335                  $selected = ' selected="selected" ';
 336                  $selection_name = $row->$selections_table_name;
 337              }
 338              else
 339              {
 340                  $selected = '';
 341              }
 342              $r .= '<option value="'.$row->$selections_table_ID.'" '.$selected.' >'.$row->$selections_table_name."</option>\n";
 343          }
 344      }
 345      $r .= "</select>\n\n";
 346  
 347      echo $r;
 348  
 349      return $selection_name;
 350  }
 351  
 352  
 353  /**
 354   * Handle selection action
 355   *
 356   * Determine if we need to perform an action to the current selection and do it in this case.
 357   *
 358   * @param integer the current selection id
 359   * @param string prefix (cont_, etab_, firm_, ..)
 360   * @param string selection prefix (cocs_, etes_, fifs_, ..)
 361   */
 362  function handle_selection_actions( $selection_ID, $prefix, $prefix_sel )
 363  {
 364      // previous selection ID, need it to check for updating sel (no javascript possibility)
 365      $previous_sel_ID = param( $prefix.'previous_sel_ID', 'integer', 0, true );
 366  
 367      if( $selection_ID == $previous_sel_ID ) // the databse has to be updated
 368      {    // The selected ID is the same than the edited one in the previous page
 369          if( $selection_ID == 0 )
 370          { // A new selection must be created
 371              $action = 'create';
 372          }
 373          elseif( $selection_ID >0 )
 374          { // An existing selection is being updated
 375              $action = 'update';
 376          }
 377          // Get the selection name
 378          $selection_name = param( 'selection_'.$prefix.'name', 'string', '', true );
 379  
 380          // Do the slection action
 381          selection_action( $action, $selection_ID, $selection_name, $prefix, $prefix_sel );
 382      }
 383  }
 384  
 385  
 386  /**
 387   * Manages the various database changes to make on selections
 388   *
 389   * @param string the action currently effectuated
 390   * @param integer the current selection id
 391   * @param string the current selection name
 392   * @param string prefix (cont_, etab_, firm_, ..)
 393   * @param string selection prefix (cocs_, etes_, fifs_, ..)
 394   */
 395  function selection_action( $action, $selection_ID, $selection_name, $prefix, $prefix_sel )
 396  { // the form has been submitted to act on the database and not only to change the display
 397  
 398      global $DB, $Messages, $confirm, $item_ID_list, $current_User;
 399  
 400      $items = param( $prefix.'items', 'array/string', array(), false );    // do NOT memorize // ?????????????
 401      param( 'item_ID_list', 'string', '', false );
 402  
 403      $current_User->check_perm( 'selections', 'edit', true );
 404  
 405  
 406      // Set global vars, selection_.prefix.ID, selection_.prefix_name
 407      $selection_prefix_ID = 'selection_'.$prefix.'ID';
 408      $selection_prefix_name = 'selection_'.$prefix.'name';
 409      global $$selection_prefix_ID, $$selection_prefix_name;
 410  
 411      // Moche ms bon...
 412      $selections_table = 'T_'.substr($prefix,0,strlen($prefix)-1).'selections';
 413      $sel_table = 'T_'.$prefix.substr($prefix,0,1).'sel';
 414      $selections_table_name = substr($prefix,0,1).'sel_name';
 415      $selections_table_id = substr($prefix,0,1).'sel_ID';
 416      $sel_table_selection = $prefix_sel.$selections_table_id;
 417      $sel_table_item = $prefix_sel.$prefix.'ID';
 418  
 419  
 420      switch( $action )
 421      {
 422  
 423          // creation of a new selection
 424          case 'create':
 425  
 426              if( empty($selection_name) )
 427              {    // No name provided:
 428                  $Messages->add( T_('Cannot create a selection with an empty name'), 'error' );
 429                  // Abord creation!
 430                  break;
 431              }
 432  
 433              $sql_selections = "INSERT INTO $selections_table ( $selections_table_name )
 434                                                              VALUES( ".$DB->quote($selection_name)." ) ";         // construction of the query
 435              $DB->query( $sql_selections ); // insertion of a new selection in the database
 436  
 437              $selection_ID = mysql_insert_id(); // id generated by the last sql query
 438  
 439              if( !empty( $items ) )
 440              { // nothing must be inserted if no items are selected
 441                  $sql_sel = 'INSERT INTO '.$sel_table.'( '.$sel_table_item.', '.$sel_table_selection .')  VALUES ';
 442                  $sel_array = array();
 443  
 444                  $i = 0;
 445                  foreach( $items as $item )
 446                  { // construction of the sql query depending on selected values in the result table
 447                      $sel_array[$i++] = ' ('.$item.','.$selection_ID.' ) ';
 448                  }
 449                  $sql_sel .= implode( $sel_array, ',' );
 450                  $DB->query( $sql_sel ); // insertion of the relation between selections and items in the database
 451              }
 452  
 453              // Set $selection_.$prefix.ID var
 454              $sel_prefix_ID = 'selection_'.$prefix.'ID';
 455              $$sel_prefix_ID = $selection_ID;
 456              // Set $selection_.$prefix.name var
 457              $sel_prefix_name = 'selection_'.$prefix.'name';
 458              $$sel_prefix_name = $selection_name;
 459  
 460              $Messages->add( T_('Selection created.'), 'success' );
 461  
 462              break;
 463  
 464  
 465          case 'edit':
 466          case 'update':
 467              // update of an existing selection
 468              $DB->begin();
 469  
 470               if( empty($selection_name) )
 471              {    // No name provided:
 472                  $Messages->add( T_('Please provide a selection name.'), 'error' );
 473              }
 474              else
 475              {    // Update name:
 476                  $sql_selections = "UPDATE $selections_table
 477                                                              SET $selections_table_name = ".$DB->quote($selection_name)."
 478                                                          WHERE $selections_table_id = $selection_ID"; // construction of the update query
 479                  $DB->query( $sql_selections );
 480              }
 481  
 482              if( preg_match( '#[0-9,]+#', $item_ID_list ) )
 483              { // check the format of the item list to avoid sql injection
 484                  $sql_delete = 'DELETE FROM '.$sel_table.' WHERE '.$sel_table_selection.' = '.$selection_ID
 485                              .' AND '.$sel_table_item.' IN ('.$item_ID_list.')'; // deletion of the former db entries
 486                  $DB->query( $sql_delete );
 487  
 488                  $Messages->add( T_('Obsolete selection entries deleted.'), 'success' );
 489              }
 490  
 491              if( !empty( $items ) )
 492              { // there have been some items selected in the result table: they must be inserted into the database
 493                  $sql_sel = 'INSERT INTO '.$sel_table.'( '.$sel_table_item.', '.$sel_table_selection .')  VALUES ';
 494                  $sel_array = array();
 495  
 496                  foreach( $items as $item )
 497                  { // construction of the sql query depending on selected values in the result table
 498                      $sel_array[] = ' ( '.$item.', '.$selection_ID.' ) ';
 499                  }
 500                  $sql_sel .= implode( $sel_array, ',' );
 501                  $DB->query( $sql_sel ); // insertion of the relation between selections and items in the database
 502  
 503                  $Messages->add( T_('New selections entries inserted.'), 'success' );
 504              }
 505  
 506              $DB->commit();
 507              break;
 508  
 509  
 510          case 'copy':
 511              // creation of a new selection with the same name
 512              $sql_selections = 'INSERT INTO '.$selections_table.'('.$selections_table_name.
 513                                                  ') VALUES( "'.$selection_name.'" )';
 514              $DB->query( $sql_selections );
 515              $Messages->add( T_('Selection copied.'), 'success' );
 516  
 517              $new_selection_ID = mysql_insert_id();// gets the new selection id
 518  
 519              // creation of the links between the new selection and the selected items
 520              $sql_sel = 'INSERT INTO '.$sel_table.'( '.$sel_table_item.', '.$sel_table_selection.' ) '
 521                                   .'SELECT '.$sel_table_item.', '.$new_selection_ID.' FROM '.$sel_table.' WHERE '
 522                                                  .$sel_table_selection.'='.$selection_ID;
 523              $DB->query( $sql_sel );
 524              $Messages->add( T_('Selection links copied.'), 'success' );
 525  
 526              $selection_ID = $new_selection_ID;
 527  
 528              break;
 529  
 530  
 531          case 'delete':
 532              // deletion of the selection
 533              if( !$confirm )
 534              { // ask for confirmation before deleting
 535                  ?>
 536                  <div class="panelinfo">
 537                      <h3><?php printf( T_('Delete selection &laquo;%s&raquo;?'), $selection_name )?></h3>
 538  
 539                      <p><?php echo T_('Warning').': '.T_('Cascading deletes!') ?></p>
 540  
 541                      <p><?php echo T_('THIS CANNOT BE UNDONE!') ?></p>
 542  
 543                      <p>
 544  
 545                  <?php
 546                      $Form = new Form( regenerate_url('', '', '', '&'), 'form_confirm', 'post', '' );
 547  
 548                      $action = '';
 549  
 550                      $Form->begin_form( 'inline' );
 551                      $Form->hidden( 'action', 'delete' );
 552                      $Form->hidden( 'selection_ID', $selection_ID );
 553                      $Form->hidden( 'selection_name', $selection_name );
 554                      $Form->hidden( 'confirm', 1 );
 555                      $Form->button( array( 'submit', '', T_('I am sure!'), 'DeleteButton' ) );
 556                      $Form->end_form();
 557  
 558                      unset( $Form );
 559  
 560                      $Form = new Form( regenerate_url('', '', '', '&'), 'form_cancel', 'post', '' );
 561  
 562                      $Form->begin_form( 'inline' );
 563                      $Form->button( array( 'submit', '', T_('CANCEL'), 'CancelButton' ) );
 564                      $Form->end_form();
 565                  ?>
 566  
 567                  </p>
 568  
 569                  </div>
 570                  <?php
 571              }
 572              else
 573              { // the deletion has been confirmed
 574                  $sql_sel = 'DELETE FROM '.$sel_table.' WHERE '.$sel_table_selection.'='.$selection_ID;
 575                  $DB->query( $sql_sel );// deletion of the links between the selection and the selected items
 576                  $Messages->add( T_('Selection attachments deleted.'), 'success' );
 577  
 578                  $sql_selections = 'DELETE FROM '.$selections_table.' WHERE '.$selections_table_id.'='.$selection_ID;
 579                  $DB->query( $sql_selections );// deletion of the selection
 580                  $Messages->add( T_('Selection deleted.'), 'success' );
 581              }
 582  
 583              $selection_ID = -1;
 584  
 585              break;
 586  
 587          default:
 588              break;
 589      }
 590  
 591      return $selection_ID;
 592  
 593  }
 594  
 595  ?>

title

Description

title

Description

title

Description

title

title

Body