b2evolution PHP Cross Reference Blogging Systems

Source: /inc/slugs/model/_slug.class.php - 289 lines - 7611 bytes - Summary - Text - Print

Description: This file implements the Slug class. This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}. See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the Slug class.
   4   *
   5   * This file is part of the b2evolution/evocms project - {@link http://b2evolution.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   * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)
  11   *
  12   * {@internal Open Source relicensing agreement:
  13   * EVO FACTORY grants Francois PLANQUE the right to license
  14   * EVO FACTORY contributions to this file and the b2evolution project
  15   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  16   * }}
  17   *
  18   * @package evocore
  19   *
  20   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  21   * @author asimo: Evo Factory / Attila Simo
  22   *
  23   * @version $Id: _slug.class.php 6136 2014-03-08 07:59:48Z manuel $
  24   */
  25  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  26  
  27  load_class( '_core/model/dataobjects/_dataobject.class.php', 'DataObject' );
  28  
  29  
  30  /**
  31   * Slug Class
  32   *
  33   * @package evocore
  34   */
  35  class Slug extends DataObject
  36  {
  37      var $title;
  38  
  39      var $type;
  40  
  41      var $itm_ID;
  42  
  43      /**
  44       * Constructor
  45       *
  46       * @param object table Database row
  47       */
  48  	function Slug( $db_row = NULL )
  49      {
  50          // Call parent constructor:
  51          parent::DataObject( 'T_slug', 'slug_', 'slug_ID' );
  52  
  53          $this->delete_restrictions = array(
  54                  array( 'table'=>'T_items__item', 'fk'=>'post_canonical_slug_ID', 'fk_short'=>'canonical_slug_ID', 'msg'=>T_('%d related post') ),
  55                  array( 'table'=>'T_items__item', 'fk'=>'post_tiny_slug_ID', 'fk_short'=>'tiny_slug_ID', 'msg'=>T_('%d related post') ),
  56              );
  57  
  58          if( $db_row != NULL )
  59          {
  60              $this->ID = $db_row->slug_ID;
  61              $this->title = $db_row->slug_title;
  62              $this->type = $db_row->slug_type;
  63              $this->itm_ID = $db_row->slug_itm_ID;
  64          }
  65      }
  66  
  67  
  68      /**
  69       * Get a member param by its name
  70       *
  71       * @param mixed Name of parameter
  72       * @return mixed Value of parameter
  73       */
  74  	function get( $parname )
  75      {
  76          return parent::get( $parname );
  77      }
  78  
  79  
  80      /**
  81       * Set param value
  82       *
  83       * @param string parameter name
  84       * @param mixed parameter value
  85       * @param boolean true to set to NULL if empty value
  86       * @return boolean true, if a value has been set; false if it has not changed
  87       */
  88  	function set( $parname, $parvalue, $make_null = false )
  89      {
  90          return $this->set_param( $parname, 'string', $parvalue, $make_null );
  91      }
  92  
  93  
  94      /**
  95       * Load data from Request form fields.
  96       *
  97       * @return boolean true if loaded data seems valid.
  98       */
  99  	function load_from_Request()
 100      {
 101          global $Messages;
 102          // title
 103          $slug_title = param( 'slug_title', 'string', true );
 104          $slug_title = urltitle_validate( $slug_title, '', 0, true, 'slug_title', 'slug_ID', 'T_slug' );
 105          if( $this->dbexists( 'slug_title', $slug_title ) )
 106          {
 107              $Messages->add( sprintf( T_('The slug &laquo;%s&raquo; already exists.'), $slug_title ), 'error' );
 108          }
 109          $this->set( 'title', $slug_title );
 110  
 111          // type
 112          $this->set_string_from_param( 'type', true );
 113  
 114          // object ID:
 115          $object_id = param( 'slug_object_ID', 'string' );
 116          // All DataObject ID must be a number
 117          if( ! is_number( $object_id ) && $this->type != 'help' )
 118          { // not a number
 119              $Messages->add( T_('Object ID must be a number!'), 'error' );
 120              return false;
 121          }
 122  
 123          switch( $this->type )
 124          {
 125              case 'item':
 126                  $ItemCache = & get_ItemCache();
 127                  if( $ItemCache->get_by_ID( $object_id, false, false ) )
 128                  {
 129                      $this->set_from_Request( 'itm_ID', 'slug_object_ID', true );
 130                  }
 131                  else
 132                  {
 133                      $Messages->add( T_('Object ID must be a valid Post ID!'), 'error' );
 134                  }
 135                  break;
 136          }
 137  
 138          return ! param_errors_detected();
 139      }
 140  
 141  
 142      /**
 143       * Create a link to the related oject.
 144       *
 145       * @param string Display text - if NULL, will get the object title
 146       * @param string type values:
 147       *         - 'admin_view': link to this object admin interface view
 148       *         - 'public_view': link to this object public interface view (on blog)
 149       *         - 'edit': link to this object edit screen
 150       * @return string link to related object, or empty if no related object, or url does not exist.
 151       */
 152  	function get_link_to_object( $link_text = NULL, $type = 'admin_view' )
 153      {
 154          if( $object = $this->get_object() )
 155          {
 156              if( ! isset($link_text ) )
 157              { // link_text is not set -> get object title for link text
 158                  $link_text = $object->get( 'title' );
 159              }
 160              // get respective url
 161              $link_url = $this->get_url_to_object( $type );
 162              if( $link_url != '' )
 163              { // URL exists
 164                  // add link title
 165                  if( $type == 'public_view' || $type == 'admin_view' )
 166                  {
 167                      $link_title = ' title="'.sprintf( T_('View this %s...'), $this->get( 'type') ).'"';
 168                  }
 169                  elseif( $type == 'edit' )
 170                  {
 171                      $link_title = ' title="'.sprintf( T_('Edit this %s...'), $this->get( 'type') ).'"';
 172                  }
 173                  else
 174                  {
 175                      $link_title = '';
 176                  }
 177                  // return created link
 178                  return '<a href="'.$link_url.'"'.$link_title.'>'.$link_text.'</a>';
 179              }
 180          }
 181          return '';
 182      }
 183  
 184  
 185      /**
 186       * Create a link to the related oject (in the admin!).
 187       *
 188       * @param string type values:
 189       *         - 'admin_view': url to this item admin interface view
 190       *         - 'public_view': url to this item public interface view (on blog)
 191       *         - 'edit': url to this item edit screen
 192       * @return string URL to related object, or empty if no related object or URL does not exist.
 193       */
 194  	function get_url_to_object( $type = 'admin_view' )
 195      {
 196          if( $object = $this->get_object() )
 197          { // related object exists
 198              // asimo> Every slug target class need to have get_url() function
 199              return $object->get_url( $type );
 200          }
 201          return '';
 202      }
 203  
 204  
 205      /**
 206       * Get link to restricted object
 207       *
 208       * Used when try to delete a slug, which is another object slug
 209       *
 210       * @param array restriction
 211       * @return string message with links to objects
 212       */
 213  	function get_restriction_link( $restriction )
 214      {
 215          if( $object = $this->get_object() )
 216          { // object exists
 217              // check if this is a restriction for this slug or not!
 218              if( $object->get( $restriction['fk_short'] ) == $this->ID )
 219              {
 220                  $restriction_link = $this->get_link_to_object();
 221              }
 222          }
 223          if( isset( $restriction_link ) )
 224          { // there are restrictions
 225              return sprintf( $restriction['msg'].'<br/>'.str_replace('%', '%%', $restriction_link), 1 );
 226          }
 227          // no restriction
 228          return '';
 229      }
 230  
 231  
 232      /**
 233       * Get linked object.
 234       * @return object
 235       */
 236      function & get_object()
 237      {
 238          global $DB, $admin_url;
 239  
 240          switch( $this->type )
 241          { // can be different type of object
 242              case 'item':
 243                  // TODO: dh> should use ItemCache altogether
 244                  // was: $object_query = 'SELECT post_ID, post_title FROM T_items__item WHERE '.$fk.' = '.$this->ID;
 245                  $ItemCache = & get_ItemCache();
 246                  return $ItemCache->get_by_ID( $this->itm_ID, false, false );
 247  
 248              case 'help':
 249                  return false;
 250  
 251              default:
 252                  // not defined restriction
 253                  debug_die('Slug::get_object: Unhandled object type: '.htmlspecialchars($this->type));
 254          }
 255      }
 256  
 257  
 258      /**
 259       * Update the DB based on previously recorded changes.
 260       *
 261       * @todo dh> this is very Item specific, and should get fixed probably.
 262       *
 263       * @return boolean true on success
 264       */
 265  	function dbupdate()
 266      {
 267          global $DB, $Messages;
 268          $ItemCache = & get_ItemCache();
 269          $Item = & $ItemCache->get_by_id( $this->itm_ID );
 270  
 271          $DB->begin();
 272          if( $Item->get( 'canonical_slug_ID' ) == $this->ID )
 273          {
 274              $Item->set( 'urltitle', $this->title );
 275              if( ! $Item->dbupdate( true, false, false ) )
 276              {
 277                  $DB->rollback();
 278                  return false;
 279              }
 280              $Messages->add( sprintf(T_('Warning: this change also changed the canonical slug of the post! (%s)'), $this->get_link_to_object()), 'warning' );
 281          }
 282  
 283          parent::dbupdate();
 284          $DB->commit();
 285          return true;
 286      }
 287  }
 288  
 289  ?>

title

Description

title

Description

title

Description

title

title

Body