b2evolution PHP Cross Reference Blogging Systems

Source: /inc/chapters/model/_chapter.class.php - 469 lines - 11569 bytes - Summary - Text - Print

Description: This file implements the Chapter 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 Chapter 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   * @package evocore
  21   *
  22   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  23   * @author fplanque: Francois PLANQUE.
  24   *
  25   * @version $Id: _chapter.class.php 6136 2014-03-08 07:59:48Z manuel $
  26   */
  27  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  28  
  29  load_class( 'generic/model/_genericcategory.class.php', 'GenericCategory' );
  30  
  31  
  32  /**
  33   * Chapter Class
  34   *
  35   * @package evocore
  36   */
  37  class Chapter extends GenericCategory
  38  {
  39      /**
  40       * @var integer
  41       */
  42      var $blog_ID;
  43      /**
  44       * The Blog of the Item (lazy filled, use {@link get_Blog()} to access it.
  45       * @access protected
  46       * @var Blog
  47       */
  48      var $Blog;
  49  
  50      var $urlname;
  51      var $description;
  52      var $order;
  53      var $meta;
  54      var $lock;
  55  
  56      /**
  57       * Lazy filled
  58       * @var Chapter
  59       */
  60      var $parent_Chapter;
  61  
  62      /**
  63       * Constructor
  64       *
  65       * @param table Database row
  66        * @param integer|NULL subset to use for new object
  67       */
  68  	function Chapter( $db_row = NULL, $subset_ID = NULL )
  69      {
  70          // Call parent constructor:
  71          parent::GenericCategory( 'T_categories', 'cat_', 'cat_ID', $db_row );
  72  
  73          /**
  74           * Delete restrictions
  75           */
  76          $this->delete_restrictions = array(
  77                  array( 'table'=>'T_categories', 'fk'=>'cat_parent_ID', 'msg'=>T_('%d sub categories') ),
  78                  array( 'table'=>'T_items__item', 'fk'=>'post_main_cat_ID', 'msg'=>T_('%d posts within category through main cat') ),
  79                  array( 'table'=>'T_postcats', 'fk'=>'postcat_cat_ID', 'msg'=>T_('%d posts within category through extra cat') ),
  80              );
  81  
  82          if( is_null($db_row) )
  83          {    // We are creating an object here:
  84              $this->set( 'blog_ID', $subset_ID );
  85          }
  86          else
  87          {    // Wa are loading an object:
  88              $this->blog_ID = $db_row->cat_blog_ID;
  89              $this->urlname = $db_row->cat_urlname;
  90              $this->description = $db_row->cat_description;
  91              $this->order = $db_row->cat_order;
  92              $this->meta = $db_row->cat_meta;
  93              $this->lock = $db_row->cat_lock;
  94          }
  95      }
  96  
  97  
  98      /**
  99       * Load data from Request form fields.
 100       *
 101       * @return boolean true if loaded data seems valid.
 102       */
 103  	function load_from_request()
 104      {
 105          global $DB, $Settings;
 106  
 107          parent::load_from_Request();
 108  
 109          // Check url name
 110          param( 'cat_urlname', 'string' );
 111          $this->set_from_Request( 'urlname' );
 112  
 113          // Check description
 114          param( 'cat_description', 'string' );
 115          $this->set_from_Request( 'description' );
 116  
 117          if( $Settings->get('chapter_ordering') == 'manual' )
 118          {    // Manual ordering
 119              param( 'cat_order', 'integer' );
 120              $this->set_from_Request( 'order' );
 121          }
 122  
 123          // Meta category
 124          $cat_meta = param( 'cat_meta', 'integer', 0 );
 125          if( $this->has_posts() && $cat_meta )
 126          {    // Display error message if we want make the meta category from category with posts
 127              global $Messages;
 128              $Messages->add( sprintf( T_('The category &laquo;%s&raquo; cannot be set as meta category. You must remove the posts it contains first.'), $this->dget('name') ) );
 129          }
 130          else
 131          {    // Save the category as 'Meta' only if it has no posts
 132              $this->set_from_Request( 'meta' );
 133          }
 134  
 135          // Locked category
 136          param( 'cat_lock', 'integer', 0 );
 137          $this->set_from_Request( 'lock' );
 138  
 139          return ! param_errors_detected();
 140      }
 141  
 142  
 143      /**
 144       *
 145       */
 146      function & get_parent_Chapter()
 147      {
 148          if( ! isset( $this->parent_Chapter ) )
 149          {    // Not resoleved yet!
 150              if( empty( $this->parent_ID ) )
 151              {
 152                  $this->parent_Chapter = NULL;
 153              }
 154              else
 155              {
 156                  $ChapterCache = & get_ChapterCache();
 157                  $this->parent_Chapter = & $ChapterCache->get_by_ID( $this->parent_ID, false );
 158              }
 159          }
 160  
 161          return $this->parent_Chapter;
 162      }
 163  
 164  
 165      /**
 166       * Get URL path (made of URL names) back to the root
 167       */
 168  	function get_url_path()
 169      {
 170          $r = $this->urlname.'/';
 171  
 172          $parent_Chapter = & $this->get_parent_Chapter();
 173          if( !is_null( $parent_Chapter ) )
 174          {    // Recurse:
 175              $r = $parent_Chapter->get_url_path().$r;
 176          }
 177  
 178          return $r;
 179      }
 180  
 181  
 182      /**
 183       * Generate the URL to access the category.
 184       *
 185       * @param string|NULL 'param_num', 'subchap', 'chapters'
 186       * @param string|NULL url to use
 187       * @param integer category page to link to, default:1
 188       * @param integer|NULL number of posts per page (used for param_num only)
 189       * @param string glue between url params
 190       */
 191  	function get_permanent_url( $link_type = NULL, $blogurl = NULL, $paged = 1, $chapter_posts_per_page = NULL, $glue = '&amp;' )
 192      {
 193          global $DB, $cacheweekly, $Settings;
 194  
 195          if( empty( $link_type ) )
 196          {    // Use default from settings:
 197              $this->get_Blog();
 198              $link_type = $this->Blog->get_setting( 'chapter_links' );
 199          }
 200  
 201          if( empty( $blogurl ) )
 202          {
 203              $this->get_Blog();
 204              $blogurl = $this->Blog->gen_blogurl();
 205          }
 206  
 207          switch( $link_type )
 208          {
 209              case 'param_num':
 210                  $r = url_add_param( $blogurl, 'cat='.$this->ID, $glue );
 211                  if( empty($chapter_posts_per_page) )
 212                  {    // Use default from Blog
 213                      $this->get_Blog();
 214                      $chapter_posts_per_page = $this->Blog->get_setting( 'chapter_posts_per_page' );
 215                  }
 216                  if( !empty($chapter_posts_per_page) && $chapter_posts_per_page != $this->Blog->get_setting( 'posts_per_page' ) )
 217                  {    // We want a specific post per page count:
 218                      $r = url_add_param( $r, 'posts='.$chapter_posts_per_page, $glue );
 219                  }
 220                  break;
 221  
 222              case 'subchap':
 223                  $this->get_Blog();
 224                  $category_prefix = $this->Blog->get_setting('category_prefix');
 225                  if( !empty( $category_prefix ) )
 226                  {
 227                      $r = url_add_tail( $blogurl, '/'.$category_prefix.'/'.$this->urlname.'/' );
 228                  }
 229                  else
 230                  {
 231                      $r = url_add_tail( $blogurl, '/'.$this->urlname.'/' );
 232                  }
 233                  break;
 234  
 235              case 'chapters':
 236              default:
 237                  $this->get_Blog();
 238                  $category_prefix = $this->Blog->get_setting('category_prefix');
 239                  if( !empty( $category_prefix ) )
 240                  {
 241                      $r = url_add_tail( $blogurl, '/'.$category_prefix.'/'.$this->get_url_path() );
 242                  }
 243                  else
 244                  {
 245                      $r = url_add_tail( $blogurl, '/'.$this->get_url_path() );
 246                  }
 247                  break;
 248          }
 249  
 250          if( $paged > 1 )
 251          {
 252              $r = url_add_param( $r, 'paged='.$paged, $glue );
 253          }
 254  
 255          return $r;
 256      }
 257  
 258      /**
 259       * Get the Blog object for the Chapter.
 260       *
 261       * @return Blog
 262       */
 263      function & get_Blog()
 264      {
 265          if( is_null($this->Blog) )
 266          {
 267              $this->load_Blog();
 268          }
 269  
 270          return $this->Blog;
 271      }
 272  
 273  
 274      /**
 275       * Load the Blog object for the Chapter, without returning it.
 276       */
 277  	function load_Blog()
 278      {
 279          if( is_null($this->Blog) )
 280          {
 281              $BlogCache = & get_BlogCache();
 282              $this->Blog = & $BlogCache->get_by_ID( $this->blog_ID );
 283          }
 284      }
 285  
 286  
 287      /**
 288       * Insert object into DB based on previously recorded changes.
 289       *
 290       * @return boolean true on success
 291       */
 292  	function dbinsert()
 293      {
 294          global $DB;
 295  
 296          load_funcs( 'items/model/_item.funcs.php' );
 297  
 298          if( $this->ID != 0 ) die( 'Existing object cannot be inserted!' );
 299  
 300          // Start transaction because of urltitle validation
 301          $DB->begin( 'SERIALIZABLE' );
 302  
 303          // validate url title / slug
 304          $this->set( 'urlname', urltitle_validate( $this->urlname, $this->name, $this->ID, false, $this->dbprefix.'urlname', $this->dbIDname, $this->dbtablename) );
 305  
 306          if( parent::dbinsert() )
 307          { // The chapter was inserted successful
 308              $DB->commit();
 309              return true;
 310          }
 311  
 312          // Could not insert the chapter object
 313          $DB->rollback();
 314          return false;
 315      }
 316  
 317      /**
 318       * Update the DB based on previously recorded changes
 319       *
 320       * @return boolean true on success
 321       */
 322  	function dbupdate()
 323      {
 324          global $DB;
 325  
 326          // Start transaction because of urltitle validation
 327          $DB->begin( 'SERIALIZABLE' );
 328  
 329          // validate url title / slug
 330          if( empty($this->urlname) || isset($this->dbchanges['cat_urlname']) )
 331          { // Url title has changed or is empty
 332              $this->set( 'urlname', urltitle_validate( $this->urlname, $this->name, $this->ID, false, $this->dbprefix.'urlname', $this->dbIDname, $this->dbtablename) );
 333          }
 334  
 335          if( parent::dbupdate() === false )
 336          { // The update was unsuccessful
 337              $DB->rollback();
 338              return false;
 339          }
 340  
 341          // The chapter was updated successful
 342          $DB->commit();
 343          return true;
 344      }
 345  
 346  
 347      /**
 348       * Check if this category has at least one post
 349       *
 350       * @return boolean 
 351       */
 352  	function has_posts()
 353      {
 354          global $DB;
 355  
 356          if( $this->ID == 0 )
 357          {    // New category has no posts
 358              return false;
 359          }
 360  
 361          if( !isset( $this->count_posts ) )
 362          {
 363              $SQL = new SQL();
 364              $SQL->SELECT( 'COUNT( postcat_post_ID )' );
 365              $SQL->FROM( 'T_postcats' );
 366              $SQL->WHERE( 'postcat_cat_ID = '.$DB->quote( $this->ID ) );
 367              $count_posts = $DB->get_var( $SQL->get() );
 368              $this->count_posts = $count_posts;
 369          }
 370  
 371          return ( $this->count_posts > 0 );
 372      }
 373  
 374  
 375      /**
 376       * Get URL to edit a chapter if user has edit rights.
 377       *
 378       * @param array Params:
 379       *  - 'redirect_page': redirect to page: 'front', 'manual', 'list'
 380       *  - 'glue' : Glue string between url params
 381       * @return string|FALSE URL
 382       */
 383  	function get_edit_url( $params = array() )
 384      {
 385          $params = array_merge( array(
 386                  'redirect_page' => '',
 387                  'glue'          => '&amp;',
 388              ), $params );
 389  
 390          if( ! is_logged_in( false ) )
 391          { // User is not logged in
 392              return false;
 393          }
 394  
 395          if( ! $this->ID )
 396          { // New chapter
 397              return false;
 398          }
 399  
 400          global $current_User;
 401  
 402          if( ! $current_User->check_perm( 'admin', 'restricted' ) ||
 403              ! $current_User->check_perm( 'blog_cats', '', false, $this->blog_ID ) )
 404          { // User has no right to edit this chapter
 405              return false;
 406          }
 407  
 408          global $admin_url;
 409          $url = $admin_url.'?ctrl=chapters'.$params['glue']
 410              .'action=edit'.$params['glue']
 411              .'cat_ID='.$this->ID.$params['glue']
 412              .'blog='.$this->blog_ID;
 413          if( !empty( $params['redirect_page'] ) )
 414          {
 415              $url .= $params['glue'].'redirect_page='.$params['redirect_page'];
 416          }
 417  
 418          return $url;
 419      }
 420  
 421  
 422      /**
 423       * Provide link to edit a chapter if user has edit rights
 424       *
 425       * @param array Params:
 426       *  - 'before': to display before link
 427       *  - 'after':    to display after link
 428       *  - 'text': link text
 429       *  - 'title': link title
 430       *  - 'class': CSS class name
 431       *  - 'redirect_page': redirect to page: 'front', 'manual', 'list'
 432       *  - 'glue' : Glue string between url params
 433       * @return string|FALSE Link tag
 434       */
 435  	function get_edit_link( $params = array() )
 436      {
 437          $edit_url = $this->get_edit_url( $params );
 438          if( ! $edit_url )
 439          {
 440              return false;
 441          }
 442  
 443          // Make sure we are not missing any param:
 444          $params = array_merge( array(
 445                  'before'        => '',
 446                  'after'         => '',
 447                  'text'          => '#',
 448                  'title'         => '#',
 449                  'class'         => '',
 450                  'redirect_page' => '',
 451                  'glue'          => '&amp;',
 452              ), $params );
 453  
 454  
 455          if( $params['text'] == '#' ) $params['text'] = get_icon( 'edit' ).' '.T_('Edit...');
 456          if( $params['title'] == '#' ) $params['title'] = T_('Edit this chapter...');
 457  
 458          $r = $params['before'];
 459          $r .= '<a href="'.$edit_url;
 460          $r .= '" title="'.$params['title'].'"';
 461          if( !empty( $params['class'] ) ) $r .= ' class="'.$params['class'].'"';
 462          $r .=  '>'.$params['text'].'</a>';
 463          $r .= $params['after'];
 464  
 465          return $r;
 466      }
 467  }
 468  
 469  ?>

title

Description

title

Description

title

Description

title

title

Body