b2evolution PHP Cross Reference Blogging Systems

Source: /inc/widgets/widgets/_menu_link.widget.php - 496 lines - 14812 bytes - Summary - Text - Print

Description: This file implements the menu_link_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 menu_link_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   *
  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   * @package evocore
  20   *
  21   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  22   * @author fplanque: Francois PLANQUE.
  23   *
  24   * @version $Id: _menu_link.widget.php 6136 2014-03-08 07:59:48Z manuel $
  25   */
  26  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  27  
  28  load_class( 'widgets/model/_widget.class.php', 'ComponentWidget' );
  29  
  30  global $menu_link_widget_link_types;
  31  $menu_link_widget_link_types = array(
  32          'home' => T_('Blog home'),
  33          'search' => T_('Search page'),
  34          'arcdir' => T_('Archive directory'),
  35          'catdir' => T_('Category directory'),
  36          'postidx' => T_('Post index'),
  37          'mediaidx' => T_('Photo index'),
  38          'sitemap' => T_('Site Map'),
  39          'latestcomments' => T_('Latest comments'),
  40  
  41          'ownercontact' => T_('Blog owner contact form'),
  42          'owneruserinfo' => T_('Blog owner profile'),
  43  
  44          'users' => T_('User directory'),
  45  
  46          'login' => T_('Log in form'),
  47          'register' => T_('Registration form'),
  48          'myprofile' => T_('My profile'),
  49          'profile' => T_('Edit profile'),
  50          'avatar' => T_('Edit profile picture'),
  51  
  52          'item' => T_('Any item (post, page, etc...)'),
  53          'url' => T_('Any URL'),
  54  
  55          'postnew' => T_('Write a new post'),
  56      );
  57  
  58  /**
  59   * ComponentWidget Class
  60   *
  61   * A ComponentWidget is a displayable entity that can be placed into a Container on a web page.
  62   *
  63   * @todo dh> this needs to implement BlockCaching cache_keys properly:
  64   *            - "login": depends on $currentUser being set or not
  65   *            ...
  66   *
  67   * @package evocore
  68   */
  69  class menu_link_Widget extends ComponentWidget
  70  {
  71      /**
  72       * Constructor
  73       */
  74  	function menu_link_Widget( $db_row = NULL )
  75      {
  76          // Call parent constructor:
  77          parent::ComponentWidget( $db_row, 'core', 'menu_link' );
  78      }
  79  
  80  
  81      /**
  82       * Get name of widget
  83       */
  84  	function get_name()
  85      {
  86          return T_('Menu link');
  87      }
  88  
  89  
  90      /**
  91       * Get a very short desc. Used in the widget list.
  92       */
  93  	function get_short_desc()
  94      {
  95          global $menu_link_widget_link_types;
  96  
  97          $this->load_param_array();
  98  
  99  
 100          if( !empty($this->param_array['link_text']) )
 101          {    // We have a custom link text:
 102              return $this->param_array['link_text'];
 103          }
 104  
 105          if( !empty($this->param_array['link_type']) )
 106          {    // TRANS: %s is the link type, e. g. "Blog home" or "Log in form"
 107              return sprintf( T_( '%s link' ), $menu_link_widget_link_types[$this->param_array['link_type']] );
 108          }
 109  
 110          return $this->get_name();
 111      }
 112  
 113  
 114      /**
 115       * Get short description
 116       */
 117  	function get_desc()
 118      {
 119          return T_('Display a configurable menu entry/link');
 120      }
 121  
 122  
 123      /**
 124       * Get definitions for editable params
 125       *
 126       * @see Plugin::GetDefaultSettings()
 127       * @param local params like 'for_editing' => true
 128       */
 129  	function get_param_definitions( $params )
 130      {
 131          global $menu_link_widget_link_types;
 132  
 133          $r = array_merge( array(
 134                  'link_type' => array(
 135                      'label' => T_( 'Link Type' ),
 136                      'note' => T_('What do you want to link to?'),
 137                      'type' => 'select',
 138                      'options' => $menu_link_widget_link_types,
 139                      'defaultvalue' => 'home',
 140                      'onchange' => '
 141                          var curr_link_type = this.value;
 142                          var allow_blockcache = jQuery("[id$=\'_set_allow_blockcache\']");
 143                          if( curr_link_type == "login" || curr_link_type == "register" )
 144                          {
 145                              allow_blockcache.removeAttr(\'checked\');
 146                              allow_blockcache.attr( \'disabled\', \'disabled\' );
 147                          }
 148                          else
 149                          {
 150                              allow_blockcache.removeAttr(\'disabled\');
 151                              allow_blockcache.attr( \'checked\', \'checked\' );
 152                          };'
 153                  ),
 154                  'link_text' => array(
 155                      'label' => T_('Link text'),
 156                      'note' => T_( 'Text to use for the link (leave empty for default).' ),
 157                      'type' => 'text',
 158                      'size' => 20,
 159                      'defaultvalue' => '',
 160                  ),
 161                  // fp> TODO: ideally we would have a link icon to go click on the destination...
 162                  'item_ID' => array(
 163                      'label' => T_('Item ID'),
 164                      'note' => T_( 'ID of post, page, etc. for "Item" type links.' ),
 165                      'type' => 'text',
 166                      'size' => 5,
 167                      'defaultvalue' => '',
 168                  ),
 169                  'link_href' => array(
 170                      'label' => T_('URL'),
 171                      'note' => T_( 'Destination URL for "URL" type links.' ),
 172                      'type' => 'text',
 173                      'size' => 30,
 174                      'defaultvalue' => '',
 175                  ),
 176              ), parent::get_param_definitions( $params )    );
 177  
 178          // Disable allow blockcache if the link type param is set to 'login' or 'register'
 179          // Do not modify anything during update because the editing form contains all of the required modifications
 180          if( !isset( $params['for_updating'] ) && ( ! empty( $this->params ) ) && ( ! isset( $params['infinite_loop'] ) ) )
 181          { // This is an already existing widget
 182              $link_type = $this->get_param( 'link_type', true );
 183              if( $link_type == 'login' || $link_type == 'register' )
 184              { // Disable allow blockcache
 185                  $r['allow_blockcache']['defaultvalue'] = false;
 186                  $r['allow_blockcache']['disabled'] = 'disabled';
 187                  $this->set( 'allow_blockcache', false );
 188              }
 189          }
 190  
 191          return $r;
 192      }
 193  
 194  
 195      /**
 196       * Display the widget!
 197       *
 198       * @param array MUST contain at least the basic display params
 199       */
 200  	function display( $params )
 201      {
 202          /**
 203          * @var Blog
 204          */
 205          global $Blog;
 206          global $disp;
 207  
 208          $this->init_display( $params );
 209  
 210          // Default link class
 211          $link_class = $this->disp_params['link_default_class'];
 212  
 213          switch(    $this->disp_params['link_type'] )
 214          {
 215              case 'search':
 216                  $url = $Blog->get('searchurl');
 217                  $text = T_('Search');
 218                  // Is this the current display?
 219                  if( $disp == 'search' )
 220                  {    // Let's display the link as selected
 221                      $link_class = $this->disp_params['link_selected_class'];
 222                  }
 223                  break;
 224  
 225              case 'arcdir':
 226                  $url = $Blog->get('arcdirurl');
 227                  $text = T_('Archives');
 228                  if( $disp == 'arcdir' )
 229                  {    // Let's display the link as selected
 230                      $link_class = $this->disp_params['link_selected_class'];
 231                  }
 232                  break;
 233  
 234              case 'catdir':
 235                  $url = $Blog->get('catdirurl');
 236                  $text = T_('Categories');
 237                  if( $disp == 'catdir' )
 238                  {    // Let's display the link as selected
 239                      $link_class = $this->disp_params['link_selected_class'];
 240                  }
 241                  break;
 242  
 243              case 'postidx':
 244                  $url = $Blog->get('postidxurl');
 245                  $text = T_('Post index');
 246                  if( $disp == 'postidx' )
 247                  {    // Let's display the link as selected
 248                      $link_class = $this->disp_params['link_selected_class'];
 249                  }
 250                  break;
 251  
 252              case 'mediaidx':
 253                  $url = $Blog->get('mediaidxurl');
 254                  $text = T_('Photo index');
 255                  if( $disp == 'mediaidx' )
 256                  {    // Let's display the link as selected
 257                      $link_class = $this->disp_params['link_selected_class'];
 258                  }
 259                  break;
 260  
 261              case 'sitemap':
 262                  $url = $Blog->get('sitemapurl');
 263                  $text = T_('Site map');
 264                  if( $disp == 'sitemap' )
 265                  {    // Let's display the link as selected
 266                      $link_class = $this->disp_params['link_selected_class'];
 267                  }
 268                  break;
 269  
 270              case 'latestcomments':
 271                  if( !$Blog->get_setting( 'comments_latest' ) )
 272                  { // This page is disabled
 273                      return false;
 274                  }
 275                  $url = $Blog->get('lastcommentsurl');
 276                  $text = T_('Latest comments');
 277                  if( $disp == 'comments' )
 278                  {    // Let's display the link as selected
 279                      $link_class = $this->disp_params['link_selected_class'];
 280                  }
 281                  break;
 282  
 283              case 'owneruserinfo':
 284                  $url = url_add_param( $Blog->get('userurl'), 'user_ID='.$Blog->owner_user_ID );
 285                  $text = T_('Owner details');
 286                  // Is this the current display?
 287                  global $User;
 288                  if( $disp == 'user' && !empty($User) && $User->ID == $Blog->owner_user_ID )
 289                  {    // Let's display the link as selected
 290                      $link_class = $this->disp_params['link_selected_class'];
 291                  }
 292                  break;
 293  
 294              case 'ownercontact':
 295                  if( ! $url = $Blog->get_contact_url( true ) )
 296                  { // user does not allow contact form:
 297                      return;
 298                  }
 299                  $text = T_('Contact');
 300                  // Is this the current display?
 301                  if( $disp == 'msgform' )
 302                  {    // Let's display the link as selected
 303                      // fp> I think it's interesting to select this link , even if the recipient ID is different from the owner
 304                      // odds are there is no other link to highlight in this case
 305                      $link_class = $this->disp_params['link_selected_class'];
 306                  }
 307                  break;
 308  
 309              case 'login':
 310                  if( is_logged_in() ) return false;
 311                  $url = get_login_url( 'menu link' );
 312                  if( isset($this->BlockCache) )
 313                  {    // Do NOT cache because some of these links are using a redirect_to param, which makes it page dependent.
 314                      // so this will be cached by the PageCache; there is no added benefit to cache it in the BlockCache
 315                      // (which could have been shared between several pages):
 316                      $this->BlockCache->abort_collect();
 317                  }
 318                  $text = T_('Log in');
 319                  // Is this the current display?
 320                  if( $disp == 'login' )
 321                  {    // Let's display the link as selected
 322                      $link_class = $this->disp_params['link_selected_class'];
 323                  }
 324                  break;
 325  
 326              case 'register':
 327                  if( ! $url = get_user_register_url( NULL, 'menu link' ) )
 328                  {
 329                      return false;
 330                  }
 331                  if( isset($this->BlockCache) )
 332                  {    // Do NOT cache because some of these links are using a redirect_to param, which makes it page dependent.
 333                      // Note: also beware of the source param.
 334                      // so this will be cached by the PageCache; there is no added benefit to cache it in the BlockCache
 335                      // (which could have been shared between several pages):
 336                      $this->BlockCache->abort_collect();
 337                  }
 338                  $text = T_('Register');
 339                  // Is this the current display?
 340                  if( $disp == 'register' )
 341                  {    // Let's display the link as selected
 342                      $link_class = $this->disp_params['link_selected_class'];
 343                  }
 344                  break;
 345  
 346              case 'profile':
 347                  if( ! is_logged_in() ) return false;
 348                  $url = get_user_profile_url();
 349                  $text = T_('Edit profile');
 350                  // Is this the current display?  (Edit my Profile)
 351                  if( in_array( $disp, array( 'profile', 'avatar', 'pwdchange', 'userprefs', 'subs' ) ) )
 352                  {    // Let's display the link as selected
 353                      $link_class = $this->disp_params['link_selected_class'];
 354                  }
 355                  break;
 356  
 357              case 'avatar':
 358                  if( ! is_logged_in() ) return false;
 359                  $url = get_user_avatar_url();
 360                  $text = T_('Profile picture');
 361                  // Note: we never highlight this, it will always highlight 'profile' instead
 362                  break;
 363  
 364              case 'users':
 365                  global $Settings;
 366                  if( ! is_logged_in() && ! $Settings->get( 'allow_anonymous_user_list' ) )
 367                  {    // Don't allow anonymous users to see users list
 368                      return false;
 369                  }
 370                  $url = $Blog->get( 'usersurl' );
 371                  $text = T_('User directory');
 372                  // Is this the current display?
 373                  // Note: If $user_ID is not set, it means we are viewing "My Profile" instead
 374                  global $user_ID;
 375                  if( $disp == 'users' || ($disp == 'user' && !empty($user_ID)) )
 376                  {    // Let's display the link as selected
 377                      // Note: we also highlight this for any user profile that is displayed
 378                      $link_class = $this->disp_params['link_selected_class'];
 379                  }
 380                  break;
 381  
 382              case 'item':
 383                  $ItemCache = & get_ItemCache();
 384                  /**
 385                  * @var Item
 386                  */
 387                  $item_ID = (integer)($this->disp_params['item_ID']);
 388                  $disp_Item = & $ItemCache->get_by_ID( $item_ID, false, false );
 389                  if( empty($disp_Item) )
 390                  {    // Item not found
 391                      return false;
 392                  }
 393                  $url = $disp_Item->get_permanent_url();
 394                  $text = $disp_Item->title;
 395                  // Is this the current item?
 396                  global $Item;
 397                  if( !empty($Item) && $disp_Item->ID == $Item->ID )
 398                  {    // The current page is currently displaying the Item this link is pointing to
 399                      // Let's display it as selected
 400                      $link_class = $this->disp_params['link_selected_class'];
 401                  }
 402                  break;
 403  
 404              case 'url':
 405                  $url = $this->disp_params['link_href'];
 406                  $text = '[URL]';    // should normally be overriden below...
 407                  // Note: we never highlight this link
 408                  break;
 409  
 410              case 'postnew':
 411                  if( ! check_item_perm_create() )
 412                  {    // Don't allow users to create a new post
 413                      return false;
 414                  }
 415                  $url = url_add_param( $Blog->get( 'url' ), 'disp=edit' );
 416                  $text = T_('Write a new post');
 417                  // Is this the current display?
 418                  if( $disp == 'edit' )
 419                  {    // Let's display the link as selected
 420                      $link_class = $this->disp_params['link_selected_class'];
 421                  }
 422                  break;
 423  
 424              case 'myprofile':
 425                  if( ! is_logged_in() )
 426                  {    // Don't show this link for not logged in users
 427                      return false;
 428                  }
 429                  $url = url_add_param( $Blog->get( 'url' ), 'disp=user' );
 430                  $text = T_('My profile');
 431                  // Is this the current display?  (Edit my Profile)
 432                  global $user_ID, $current_User;
 433                  // If $user_ID is not set, it means we will fall back to the current user, so it's ok
 434                  // If $user_ID is set, it means we are browsing the directory instead
 435                  if( $disp == 'user' && empty($user_ID) )
 436                  {    // Let's display the link as selected
 437                      $link_class = $this->disp_params['link_selected_class'];
 438                  }
 439                  break;
 440  
 441              case 'home':
 442              default:
 443                  $url = $Blog->get('url');
 444                  $text = T_('Home');
 445          }
 446  
 447          // Override default link text?
 448          if( !empty($this->param_array['link_text']) )
 449          {    // We have a custom link text:
 450              $text = $this->param_array['link_text'];
 451          }
 452  
 453          echo $this->disp_params['block_start'];
 454          echo $this->disp_params['list_start'];
 455  
 456          echo $this->disp_params['item_start'];
 457          echo '<a href="'.$url.'" class="'.$link_class.'">'.$text.'</a>';
 458          echo $this->disp_params['item_end'];
 459  
 460          echo $this->disp_params['list_end'];
 461          echo $this->disp_params['block_end'];
 462  
 463          return true;
 464      }
 465  
 466  
 467      /**
 468       * Maybe be overriden by some widgets, depending on what THEY depend on..
 469       *
 470       * @return array of keys this widget depends on
 471       */
 472  	function get_cache_keys()
 473      {
 474          global $Blog, $current_User;
 475  
 476          $keys = array(
 477                  'wi_ID'   => $this->ID,                    // Have the widget settings changed ?
 478                  'set_coll_ID' => $Blog->ID            // Have the settings of the blog changed ? (ex: new owner, new skin)
 479              );
 480  
 481          switch( $this->disp_params['link_type'] )
 482          {
 483              case 'login':          /* This one will probably abort caching by itself anyways */
 484              case 'register':    /* This one will probably abort caching by itself anyways */
 485              case 'profile':        // This can be cached
 486              case 'avatar':
 487                  // This link also depends on whether or not someone is logged in:
 488                  $keys['loggedin'] = (is_logged_in() ? 1 : 0);
 489  
 490          }
 491  
 492          return $keys;
 493      }
 494  }
 495  
 496  ?>

title

Description

title

Description

title

Description

title

title

Body