b2evolution PHP Cross Reference Blogging Systems

Source: /inc/widgets/widgets/_coll_category_list.widget.php - 483 lines - 13750 bytes - Summary - Text - Print

Description: This file implements the Category list Widget 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 Category list Widget 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)2008 by Daniel HAHLER - {@link http://daniel.hahler.de/}.
  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   * @package evocore
  21   *
  22   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  23   * @author blueyed: Daniel HAHLER
  24   * @author fplanque: Francois PLANQUE.
  25   *
  26   * @version $Id: _coll_category_list.widget.php 6136 2014-03-08 07:59:48Z manuel $
  27   */
  28  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  29  
  30  load_class( 'widgets/model/_widget.class.php', 'ComponentWidget' );
  31  
  32  /**
  33   * ComponentWidget Class
  34   *
  35   * A ComponentWidget is a displayable entity that can be placed into a Container on a web page.
  36   *
  37   * @package evocore
  38   */
  39  class coll_category_list_Widget extends ComponentWidget
  40  {
  41      /**
  42       * Constructor
  43       */
  44  	function coll_category_list_Widget( $db_row = NULL )
  45      {
  46          // Call parent constructor:
  47          parent::ComponentWidget( $db_row, 'core', 'coll_category_list' );
  48      }
  49  
  50  
  51      /**
  52       * Get name of widget
  53       */
  54  	function get_name()
  55      {
  56          return T_('Category list');
  57      }
  58  
  59  
  60      /**
  61       * Get a very short desc. Used in the widget list.
  62       */
  63  	function get_short_desc()
  64      {
  65          return format_to_output($this->disp_params['title']);
  66      }
  67  
  68  
  69      /**
  70       * Get short description
  71       */
  72  	function get_desc()
  73      {
  74          return T_('List of all categories; click filters blog on selected category.');
  75      }
  76  
  77  
  78      /**
  79       * Get definitions for editable params
  80       *
  81       * @see Plugin::GetDefaultSettings()
  82       * @param array local params
  83       *  - 'title': block title (string, default "Categories")
  84       *  - 'option_all': "All categories" link title, empty to disable (string, default "All")
  85       *  - 'use_form': Add a form with checkboxes to allow selection of multiple categories (boolean)
  86       *  - 'disp_names_for_coll_list': Display blog names, if this is an aggregated blog? (boolean)
  87       *  - 'display_checkboxes': Add checkboxes (but not a complete form) to allow selection of multiple categories (boolean)
  88       */
  89  	function get_param_definitions( $params )
  90      {
  91          $r = array_merge( array(
  92              'title' => array(
  93                      'type' => 'text',
  94                      'label' => T_('Block title'),
  95                      'defaultvalue' => T_('Categories'),
  96                      'maxlength' => 100,
  97                  ),
  98              'option_all' => array(
  99                      'type' => 'text',
 100                      'label' => T_('Option "All"'),
 101                      'defaultvalue' => T_('All'),
 102                      'maxlength' => 100,
 103                      'note' => T_('The "All categories" link allows to reset the filter. Leave blank if you want no such option.'),
 104                  ),
 105              'use_form' => array(
 106                      'type' => 'checkbox',
 107                      'label' => T_('Use form'),
 108                      'defaultvalue' => 0,
 109                      'note' => T_('Add checkboxes to allow selection of multiple categories.'),
 110                  ),
 111              'default_match' => array(
 112                      'label' => T_('Default Match'),
 113                      'note' => '',
 114                      'type' => 'radio',
 115                      'options' => array(
 116                              array( 'or', T_('OR') ),
 117                              array( 'nor', T_('NOR') ),
 118                              array( 'and', T_('AND') ) ),
 119                      'defaultvalue' => 'or',
 120                  ),
 121              'disp_names_for_coll_list' => array(
 122                      'type' => 'checkbox',
 123                      'label' => T_('Display blog names'),
 124                      'defaultvalue' => 1, /* previous behaviour */
 125                      'note' => T_('Display blog names, if this is an aggregated blog.'),
 126                  ),
 127              'exclude_cats' => array(
 128                      'type' => 'text',
 129                      'label' => T_('Exclude categories'),
 130                      'note' => T_('A comma-separated list of category IDs that you want to exclude from the list.'),
 131                  ),
 132  
 133              // Hidden, used by the item list sidebar in the backoffice.
 134              'display_checkboxes' => array(
 135                      'label' => 'Internal: Display checkboxes', // This key is required
 136                      'defaultvalue' => 0,
 137                      'no_edit' => true,
 138                  ),
 139              ), parent::get_param_definitions( $params ) );
 140  
 141          return $r;
 142      }
 143  
 144  
 145      /**
 146       * Display the widget!
 147       *
 148       * @param array MUST contain at least the basic display params
 149       */
 150  	function display( $params )
 151      {
 152          global $cat_modifier;
 153          global $Blog;
 154  
 155          $this->init_display( $params );
 156  
 157          /**
 158           * @var ChapterCache
 159           */
 160          $ChapterCache = & get_ChapterCache();
 161  
 162          $callbacks = array(
 163              'line'         => array( $this, 'cat_line' ),
 164              'no_children'  => array( $this, 'cat_no_children' ),
 165              'before_level' => array( $this, 'cat_before_level' ),
 166              'after_level'  => array( $this, 'cat_after_level' )
 167          );
 168  
 169          if( !empty( $params['callback_posts'] ) )
 170          {
 171              $callbacks['posts'] = $params['callback_posts'];
 172          }
 173  
 174          // START DISPLAY:
 175          echo $this->disp_params['block_start'];
 176  
 177          // Display title if requested
 178          $this->disp_title();
 179  
 180          if ( $this->disp_params['use_form'] )
 181          {    // We want a complete form:
 182              echo '<form method="get" action="'.$Blog->gen_blogurl().'">';
 183          }
 184  
 185          $aggregate_coll_IDs = $Blog->get_setting('aggregate_coll_IDs');
 186          if( empty($aggregate_coll_IDs) )
 187          { // ____________________ We want to display cats for ONE blog ____________________
 188              $tmp_disp = '';
 189  
 190              if( $this->disp_params['option_all'] )
 191              {    // We want to display a link to all cats:
 192                  $tmp_disp .= $this->add_cat_class_attr( $this->disp_params['item_start'], 'all' );
 193                  $tmp_disp .= '<a href="';
 194                  if( $this->disp_params['link_type'] == 'context' )
 195                  {    // We want to preserve current browsing context:
 196                      $tmp_disp .= regenerate_url( 'cats,catsel' );
 197                  }
 198                  else
 199                  {
 200                      $tmp_disp .= $Blog->gen_blogurl();
 201                  }
 202                  $tmp_disp .= '">'.$this->disp_params['option_all'].'</a>';
 203                  $tmp_disp .= $this->disp_params['item_end'];
 204              }
 205  
 206              $r = $tmp_disp . $ChapterCache->recurse( $callbacks, $Blog->ID );
 207  
 208              if( ! empty($r) )
 209              {
 210                  echo $this->disp_params['list_start'];
 211                  echo $r;
 212                  echo $this->disp_params['list_end'];
 213              }
 214          }
 215          else
 216          { // ____________________ We want to display cats for SEVERAL blogs ____________________
 217  
 218              $BlogCache = & get_BlogCache();
 219  
 220              // Make sure everything is loaded at once (vs multiple queries)
 221              // fp> TODO: scaling
 222              $ChapterCache->load_all();
 223  
 224              echo $this->disp_params['collist_start'];
 225  
 226              if( $aggregate_coll_IDs == '*' )
 227              {
 228                  $BlogCache->load_all();
 229                  $coll_ID_array = $BlogCache->get_ID_array();
 230              }
 231              else
 232              {
 233                  $coll_ID_array = sanitize_id_list($aggregate_coll_IDs, true);
 234              }
 235              foreach( $coll_ID_array as $curr_blog_ID )
 236              {
 237                  // Get blog:
 238                  $loop_Blog = & $BlogCache->get_by_ID( $curr_blog_ID, false );
 239                  if( empty($loop_Blog) )
 240                  {    // That one doesn't exist (any more?)
 241                      continue;
 242                  }
 243  
 244                  // Display blog title, if requested:
 245                  if( $this->disp_params['disp_names_for_coll_list'] )
 246                  {
 247                      echo $this->disp_params['coll_start'];
 248                      echo '<a href="';
 249                      if( $this->disp_params['link_type'] == 'context' )
 250                      {    // We want to preserve current browsing context:
 251                          echo regenerate_url( 'blog,cats,catsel', 'blog='.$curr_blog_ID );
 252                      }
 253                      else
 254                      {
 255                          $loop_Blog->disp('url','raw');
 256                      }
 257                      echo '">';
 258                      $loop_Blog->disp('name');
 259                      echo '</a>';
 260                      echo $this->disp_params['coll_end'];
 261                  }
 262  
 263                  $r = $ChapterCache->recurse( $callbacks, $curr_blog_ID );
 264  
 265                  if( ! empty($r) )
 266                  {
 267                      echo $this->disp_params['list_start'];
 268                      echo $r;
 269                      echo $this->disp_params['list_end'];
 270                  }
 271              }
 272          }
 273  
 274  
 275          if( $this->disp_params['use_form'] || $this->disp_params['display_checkboxes'] )
 276          { // We want to add form fields:
 277              if( $cat_modifier == '-' || ( empty( $cat_modifier ) && $this->disp_params['default_match'] == 'nor' ) )
 278              { // Select NOR
 279                  $cat_modifier_selected = 'nor';
 280              }
 281              else if( $cat_modifier == '*' || ( empty( $cat_modifier ) && $this->disp_params['default_match'] == 'and' ) )
 282              { // Select AND
 283                  $cat_modifier_selected = 'and';
 284              }
 285              else
 286              { // Select OR
 287                  $cat_modifier_selected = 'or';
 288              }
 289          ?>
 290          <div class="multiple_cat_match_options">
 291              <p class="multiple_cat_match_title"><?php echo T_('Retain only results that match:'); ?></p>
 292              <div class="tile">
 293                  <input type="radio" name="cat" value="|" id="cat_or" class="radio"<?php if( $cat_modifier_selected == 'or' ) echo ' checked="checked"'; ?> />
 294                  <label for="cat_or"><?php echo T_( 'Any selected category (OR)' ); ?></label>
 295              </div>
 296              <div class="tile">
 297                  <input type="radio" name="cat" value="-" id="cat_nor" class="radio"<?php if( $cat_modifier_selected == 'nor' ) echo ' checked="checked"'; ?> />
 298                  <label for="cat_nor"><?php echo T_( 'None of the selected categories (NOR)' ); ?></label>
 299              </div>
 300              <div class="tile">
 301                  <input type="radio" name="cat" value="*" id="cat_and" class="radio"<?php if( $cat_modifier_selected == 'and' ) echo ' checked="checked"'; ?> />
 302                  <label for="cat_and"><?php echo T_( 'All of the selected categories (AND)' ); ?></label>
 303              </div>
 304              <?php
 305              if( $this->disp_params['use_form'] )
 306              { // We want a complete form:
 307              ?>
 308                  <div class="tile">
 309                      <input type="submit" value="<?php echo T_( 'Filter categories' ); ?>" />
 310                  </div>
 311                  </form>
 312              <?php
 313              }
 314              ?>
 315          </div>
 316          <?php
 317          }
 318  
 319          echo $this->disp_params['block_end'];
 320  
 321          return true;
 322      }
 323  
 324  
 325      /**
 326       * Callback: Generate category line when it has children
 327       *
 328       * @param Chapter generic category we want to display
 329       * @param int level of the category in the recursive tree
 330       * @return string HTML
 331       */
 332  	function cat_line( $Chapter, $level )
 333      {
 334          global $cat_array;
 335  
 336          if( !isset($cat_array) )
 337          {
 338              $cat_array = array();
 339          }
 340  
 341          $exclude_cats = sanitize_id_list($this->disp_params['exclude_cats'], true);
 342          if( in_array( $Chapter->ID, $exclude_cats ) )
 343          { // Cat ID is excluded, skip it
 344              return;
 345          }
 346  
 347          if( in_array( $Chapter->ID, $cat_array ) )
 348          { // This category is in the current selection
 349              $start_tag = $this->disp_params['item_selected_start'];
 350          }
 351          else if( empty( $Chapter->children ) )
 352          { // This category has no children
 353              $start_tag = $this->disp_params['item_last_start'];
 354          }
 355          else
 356          {
 357              $start_tag = $this->disp_params['item_start'];
 358          }
 359  
 360          if( $Chapter->meta )
 361          { // Add class name "meta" for meta categories
 362              $start_tag = $this->add_cat_class_attr( $start_tag, 'meta' );
 363          }
 364  
 365          $r = $start_tag;
 366  
 367          if( $this->disp_params['use_form'] || $this->disp_params['display_checkboxes'] )
 368          { // We want to add form fields:
 369              $cat_checkbox_params = '';
 370              if( $Chapter->meta )
 371              { // Disable the checkbox of meta category ( and hide it by css )
 372                  $cat_checkbox_params = ' disabled="disabled"';
 373              }
 374  
 375              $r .= '<label><input type="checkbox" name="catsel[]" value="'.$Chapter->ID.'" class="checkbox middle"';
 376              if( in_array( $Chapter->ID, $cat_array ) )
 377              { // This category is in the current selection
 378                  $r .= ' checked="checked"';
 379              }
 380              $r .= $cat_checkbox_params.' /> ';
 381          }
 382  
 383          $cat_name = $Chapter->dget('name');
 384          if( $Chapter->lock && isset( $this->disp_params['show_locked'] ) && $this->disp_params['show_locked'] )
 385          {
 386              $cat_name .= '<span style="padding:0 5px;" >'.get_icon( 'file_not_allowed', 'imgtag', array( 'title' => T_('Locked') ) ).'</span>';
 387          }
 388  
 389          // Make a link from category name
 390          $r .= '<a href="';
 391          if( $this->disp_params['link_type'] == 'context' )
 392          { // We want to preserve current browsing context:
 393              $r .= regenerate_url( 'cats,catsel', 'cat='.$Chapter->ID );
 394          }
 395          else
 396          {
 397              $r .= $Chapter->get_permanent_url();
 398          }
 399          $r .= '">'.$cat_name.'</a>';
 400  
 401          if( $this->disp_params['use_form'] || $this->disp_params['display_checkboxes'] )
 402          { // We want to add form fields:
 403              $r .= '</label>';
 404          }
 405  
 406          // Do not end line here because we need to include children first!
 407          // $r .= $this->disp_params['item_end'];
 408  
 409          return $r;
 410      }
 411  
 412  
 413      /**
 414       * Callback: Generate category line when it has no children
 415       *
 416       * @param Chapter generic category we want to display
 417       * @param int level of the category in the recursive tree
 418       * @return string HTML
 419       */
 420  	function cat_no_children( $Chapter, $level )
 421      {
 422          // End current line:
 423          return $this->disp_params['item_end'];
 424      }
 425  
 426  
 427      /**
 428       * Callback: Generate code when entering a new level
 429       *
 430       * @param int level of the category in the recursive tree
 431       * @return string HTML
 432       */
 433  	function cat_before_level( $level )
 434      {
 435          $r = '';
 436          if( $level > 0 )
 437          {    // If this is not the root:
 438              $r .= $this->disp_params['group_start'];
 439          }
 440          return $r;
 441      }
 442  
 443  
 444      /**
 445       * Callback: Generate code when exiting from a level
 446       *
 447       * @param int level of the category in the recursive tree
 448       * @return string HTML
 449       */
 450  	function cat_after_level( $level )
 451      {
 452          $r = '';
 453          if( $level > 0 )
 454          {    // If this is not the root:
 455              $r .= $this->disp_params['group_end'];
 456              // End current (parent) line:
 457              $r .= $this->disp_params['item_end'];
 458          }
 459          return $r;
 460      }
 461  
 462  
 463      /**
 464       * Add new class name for start tag
 465       *
 466       * @param string HTML start tag: e.g. <div class="div_class"> or <div>
 467       * @param string New class name
 468       * @return string HTML start tag with new added class name
 469       */
 470  	function add_cat_class_attr( $start_tag, $class_name )
 471      {
 472          if( preg_match( '/ class="[^"]*"/i', $start_tag ) )
 473          { // Append to already existing attribute
 474              return preg_replace( '/ class="([^"]*)"/i', ' class="$1 '.$class_name.'"', $start_tag );
 475          }
 476          else
 477          { // Add new attribute for meta class
 478              return preg_replace( '/^<([^\s>]+)/', '<$1 class="'.$class_name.'"', $start_tag );
 479          }
 480      }
 481  }
 482  
 483  ?>

title

Description

title

Description

title

Description

title

title

Body