[ Index ]
 

Code source de Plume CMS 1.2.2

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/manager/inc/ -> class.category.php (source)

   1  <?php
   2  /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
   3  /*
   4  # ***** BEGIN LICENSE BLOCK *****
   5  # This file is part of Plume CMS, a website management application.
   6  # Copyright (C) 2001-2005 Loic d'Anterroches and contributors.
   7  #
   8  # Plume CMS is free software; you can redistribute it and/or modify
   9  # it under the terms of the GNU General Public License as published by
  10  # the Free Software Foundation; either version 2 of the License, or
  11  # (at your option) any later version.
  12  #
  13  # Plume CMS is distributed in the hope that it will be useful,
  14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  # GNU General Public License for more details.
  17  #
  18  # You should have received a copy of the GNU General Public License
  19  # along with this program; if not, write to the Free Software
  20  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21  #
  22  # ***** END LICENSE BLOCK ***** */
  23  
  24  require_once dirname(__FILE__).'/../extinc/class.recordset.php';
  25  
  26  /**
  27   * A Category is a basic class to store the info of a category or
  28   * a list of categories.
  29   * It is similar to the Resource class.
  30   */
  31  class Category extends recordset 
  32  {
  33      var $con = null; /**< Connection object. */
  34      var $res = null; /**< Current resources in the category. */
  35      var $isModified = False; /**< is update of the DB needed. */    
  36  
  37      /**
  38       * Constructor.
  39       */
  40      function Category($data='')
  41      {
  42          parent::recordset($data);
  43      }
  44  
  45      /**
  46       * Load a category.
  47       *
  48       * If no id is given, try to load from
  49       * the $this->f('category_id') value.
  50       *
  51       * @param int Id of the category ('')
  52       * @return bool Success
  53       */
  54      function load($id='')
  55      {
  56          if (empty($id)) {
  57              $id = $this->f('category_id');
  58          }
  59          if (!empty($id)) {
  60              $this->getConnection();
  61              if (($rs = $this->con->select(SQL::getCategoryById($id))) !== false) {
  62                  parent::recordset($rs->getData());
  63              } else {
  64                  $this->setError('MySQL: '.$this->con->error(), 500);
  65                  return false;
  66              }
  67          }
  68          $this->isModified = False;
  69          return true;
  70      }
  71  
  72      /**
  73       * Return the content of the category as a string ready for indexation.
  74       *   
  75       * @param string Format of the string ('html')
  76       * @return string The content of the category as a string
  77       */
  78      function getAsString($format='html')
  79      {
  80          return '';
  81      }
  82  
  83      /**
  84       * Check if the path is in use by another category.
  85       *
  86       * @return bool In use or not
  87       */
  88      function isPathInUse()
  89      {
  90          
  91          $this->getConnection();
  92          $r = SQL::getCategoryByPath($this->f('category_path'),
  93                                      $this->f('website_id'));
  94          if (($rs = $this->con->select($r)) === false) {
  95              $this->setError('MySQL: '.$this->con->error(), 500);
  96              return true; // safe approach
  97          }
  98          if ($rs->isEmpty()) {
  99              return false;
 100          } else if ($rs->nbRow() == 1
 101                     && $rs->f('category_id') == $this->f('category_id')) {
 102              return false;
 103          } else {
 104              return true;
 105          }
 106      }
 107  
 108      /**
 109       * Get the path to the category.
 110       * The function is context aware. It means that depending of
 111       * the context it will return a full path or not, with nice
 112       * urls or the simple format.
 113       *
 114       * @param string Force type of path ('')
 115       * @param bool Get the feed path (false)
 116       * @return string The path
 117       */
 118      function getPath($type='', $feed=false)
 119      {
 120          // Need to get the context:
 121          // - 'website': Must return relative path
 122          // - 'manager': Must return full path
 123          // - 'external': Must return full path (this is the case for external
 124          // use of the data, like in an RSS link.)
 125          $context = config::f('context');
 126          if ($type == 'fullurl' 
 127              || $context == 'manager' || $context == 'external') {
 128              $base = $this->f('website_url');
 129          } else {
 130              $base = $this->f('website_reurl');
 131          }
 132          
 133          //format
 134          if (config::f('url_format') == 'simple') {
 135              $base .= '/?';
 136          }
 137          if ($feed) {
 138              $base .= '/feed';
 139          }
 140          return $base.$this->f('category_path');
 141      }
 142  
 143  
 144      /**
 145       * Set the default values for the category.
 146       *
 147       * @param object User object to have the preferences
 148       * @return bool True
 149       */
 150      function setDefaults($user)
 151      {
 152          $this->setField('website_id', $user->website);
 153          $this->setField('category_publicationdate', date::stamp());
 154          $this->setField('category_creationdate', date::stamp());
 155          $this->setField('category_enddate', date::EOT());
 156          $this->setField('category_cachetime', 3600);
 157          $this->setField('category_description', 
 158                          '='.$user->getPref('content_format')."\n");
 159  
 160          $this->isModified = true;
 161          return true;
 162      }
 163  
 164      /**
 165       * Set the data of a category.
 166       *
 167       * @param int Id of the parent category
 168       * @param string Name
 169       * @param string Description
 170       * @param string Format of the description
 171       * @param string Keywords
 172       * @param string Path
 173       * @param string Template
 174       * @param string Cache time
 175       * @return bool True
 176       */
 177      function set($parentid, $name, $description, $format, $subject, $path,
 178                   $template, $cachetime)
 179      {
 180          $this->setField('category_parentid', $parentid);
 181          $this->setField('category_name', $name);
 182          $this->setField('category_description', '='.$format."\n".$description);
 183          $this->setField('category_keywords', $subject);
 184          if ($this->f('category_path') != '/') {
 185              $this->setField('category_path', $path);
 186          }
 187          $this->setField('category_template', $template);
 188          $this->setField('category_cachetime', $cachetime);
 189  
 190          $this->isModified = true;
 191          return true;
 192      }
 193  
 194      /**
 195       * Check the integrity of the category.
 196       *
 197       * The error is set if error found.
 198       *
 199       * @return bool Success
 200       */
 201      function check()
 202      {
 203          $path = $this->f('category_path');
 204          if ((strlen($path) == 0) 
 205              || (strlen($path) == 1 && $path != '/') 
 206              || (strlen($path) > 1 && !preg_match('/^\/[A-Za-z0-9\_\-\/]+\/$/',
 207                                                   $path))
 208              ) { 
 209              $this->setError(__('Error: Invalid path.'), 400);   
 210          }
 211          if ($this->isPathInUse()) {
 212              $this->setError(__('This path is already used by another category, please change it.'), 400);   
 213          }
 214          if (false !== strpos($this->f('category_template'), '..')) {
 215              $this->setError(__('The category template seems invalid, please select it in the form.'), 400);   
 216          }
 217          if (strlen($this->f('category_name')) == 0) {
 218              $this->setError(__('You need to provide a name for the category.'),
 219                              400);   
 220          }
 221          if (false !== $this->error()) {
 222              return false;
 223          }
 224          return true;
 225      }
 226  
 227      /**
 228       * Load resources in this category.
 229       *
 230       * @return bool Success
 231       */
 232      function loadResources()
 233      {
 234          include_once config::f('manager_path').'/inc/class.resourceset.php';
 235          $this->getConnection();
 236          $r = SQL::getResourcesInCat($this->f('category_id'));
 237          if (false === ($this->res = $this->con->select($r, 'ResourceSet'))) {
 238              $this->setError('MySQL: '.$this->con->error(), 500);
 239              return false;
 240          }
 241          return true;
 242      }
 243  
 244  
 245      /**
 246       * Get content of a field as text.
 247       * No modification of the content is performed.
 248       *
 249       * @param string Field to get
 250       * @return string Content
 251       */
 252      function getTextContent($field)
 253      {
 254          return $this->f($field);
 255      }
 256  
 257      /**
 258       * Get unformatted content of a field.
 259       * It removes the content type and returns the content without
 260       * parsing.
 261       *
 262       * @param string Field to get
 263       * @return string Content
 264       */
 265      function getUnformattedContent($field)
 266      {
 267          return text::getRawContent($this->f($field));
 268      }
 269  
 270      /**
 271       * Get parsed content.
 272       * If content is wiki, transform it as HTML, etc.
 273       *
 274       * @param string Field to get
 275       * @param string Output format ('html')
 276       * @return string Formatted content
 277       */
 278      function getFormattedContent($field, $format='html')
 279      {
 280          return text::parseContent($this->f($field), $format);
 281      }
 282  
 283      /**
 284       * Get the format of a content.
 285       *
 286       * @param string Field of the content
 287       * @return string Content format
 288       */
 289      function getContentFormat($field)
 290      {
 291          return text::getType($this->f($field));
 292      }
 293  
 294  
 295      /**
 296       * Get a Connection object for the resource.
 297       * It reuses the main connexion object. After calling this method
 298       * a Connection object is available as $this->con 
 299       * It is safe to call it many times.
 300       */
 301      function getConnection()
 302      {
 303          if ($this->con === null) $this->con =& pxDBConnect();
 304      }
 305  
 306      /* ===================================================================== *
 307       *                                                                       *
 308       *                Methods for rendering the pages.                       *
 309       *                                                                       *
 310       * Note: All standalone methods.                                         *
 311       * ===================================================================== */
 312  
 313      /**
 314       * Action to display a category.
 315       *
 316       * @param string Server query string
 317       * @return int Success code
 318       */
 319      function action($query)
 320      {
 321          Hook::register('onInitTemplate', 'Category', 'hookOnInitTemplate');
 322  
 323          // Easy access
 324          $GLOBALS['_PX_render']['last'] = '';
 325          $last =& $GLOBALS['_PX_render']['last']; 
 326          $GLOBALS['_PX_render']['website'] = '';
 327          $website =& $GLOBALS['_PX_render']['website']; 
 328          $GLOBALS['_PX_render']['cat'] = '';
 329          $cat =& $GLOBALS['_PX_render']['cat']; 
 330          $GLOBALS['_PX_render']['res'] = '';
 331          $res =& $GLOBALS['_PX_render']['res']; 
 332  
 333          // Parse query string to find the matching category
 334          list($path, $page) = Category::parseQueryString($query);
 335          config::setVar('category_page', $page);
 336  
 337          // Load the matching category
 338          // If category does not exists, returns error code
 339          // Will be catched up by the 404 at the end
 340          $sql = SQL::getCategoryByPath($path, config::f('website_id'));
 341          $con =& pxDBConnect();
 342          if (($cat = $con->select($sql, 'Category')) !== false) {
 343              if (!$cat->isEmpty()) {
 344                  $cat->load();
 345              } else {
 346                  return 404;
 347              }
 348          } else {
 349              $GLOBALS['_PX_render']['error']->setError('MySQL: '
 350                                                        .$con->error(), 500);
 351              return 404;
 352          }
 353  
 354          include_once dirname(__FILE__).'/class.cache.php';
 355          $cache = new Cache(urlencode('cat%%'.$path.'%%'.$page));
 356          $cache->setCacheDirectory(config::getCacheDir());
 357          $cache->debug = config::f('debug');
 358  
 359          config::setVar('category_current_id', $cat->f('category_id'));
 360          header(FrontEnd::getHeader($cat->f('category_template')));
 361          // Load the template
 362          include config::f('manager_path').'/templates/'
 363              .config::f('theme_id').'/'.$cat->f('category_template');
 364          return 200;
 365      }
 366  
 367      /**
 368       * Hook on the initialization of the templates.
 369       *
 370       * @param string Name of the calling hook
 371       * @param array Default parameters (not used)
 372       * @return bool Success
 373       */
 374      function hookOnInitTemplate($hook, $param)
 375      {
 376          if (config::f('action') == 'Category') {
 377              $GLOBALS['_PX_render']['website'] = FrontEnd::getWebsite();
 378              $GLOBALS['_PX_render']['pcat'] = FrontEnd::getCategory($GLOBALS['_PX_render']['cat']->f('category_parentid'));
 379              $category = $GLOBALS['_PX_render']['cat']->f('category_id');
 380              $limit = config::fint('res_per_page');
 381              $type = ''; 
 382              if (config::f('order_res_manual')) {
 383                  $order = 'ORDER BY %sresources.title ASC';
 384              } else {
 385                  $order = 'ORDER BY %sresources.publicationdate DESC';
 386              }
 387              $GLOBALS['_PX_render']['res'] = FrontEnd::getResources($category,
 388                                                                     $limit, 
 389                                                                     $type, 
 390                                                                     config::f('category_page'),
 391                                                                     $order);
 392          }
 393          return true;
 394      }
 395  
 396      /**
 397       * Parse query string.
 398       *
 399       * @param string Query string
 400       * @return array (Category path, page number)
 401       */
 402      function parseQueryString($query)
 403      {
 404          $category = '';
 405          $page = '';
 406  
 407          if (preg_match('#^(.*/)(index)([0-9]*)$#i', $query, $match)) {
 408              $category = $match[1];
 409              $page     = ($match[3]) ? (int) $match[3] : 1;
 410          } elseif (preg_match('#^(.*/)$#i', $query, $match)) {
 411              $category = $match[1];
 412              $page     = 1;
 413          } else {
 414              $category = '/';
 415              $page     = 1;
 416          }
 417  
 418          return array($category, $page);
 419      }
 420  
 421      /* ===================================================================== *
 422       *                                                                       *
 423       *                Methods modifying data in the database.                *
 424       *                                                                       *
 425       * ===================================================================== */
 426  
 427      /**
 428       * Save the data into the database.
 429       *
 430       * @return bool Success
 431       */
 432      function commit()
 433      {
 434          if (false == $this->check()) {
 435              return false;
 436          }
 437          if (false === $this->isModified) {
 438              return true;
 439          }
 440  
 441          $this->getConnection();
 442          $update = (0 < (int) $this->f('category_id')) ? true : false;
 443  
 444          if ($update) {
 445              $req = 'UPDATE '.$this->con->pfx.'categories SET
 446                  category_parentid = \''.$this->con->esc($this->f('category_parentid')).'\',
 447                  category_name = \''.$this->con->esc($this->f('category_name')).'\',
 448                  category_description = \''.$this->con->esc($this->f('category_description')).'\',  
 449                  category_keywords = \''.$this->con->esc($this->f('category_keywords')).'\',
 450                  category_path = \''.$this->con->esc($this->f('category_path')).'\',
 451                  category_template = \''.$this->con->esc($this->f('category_template')).'\',
 452                  category_cachetime= \''.$this->con->esc($this->f('category_cachetime')).'\'
 453                  WHERE category_id = \''.$this->con->esc($this->f('category_id')).'\'';
 454          } else {
 455              $req = 'INSERT INTO '.$this->con->pfx.'categories SET
 456                  category_parentid = \''.$this->con->esc($this->f('category_parentid')).'\',
 457                  category_name = \''.$this->con->esc($this->f('category_name')).'\',
 458                  category_description = \''.$this->con->esc($this->f('category_description')).'\',  
 459                  category_keywords = \''.$this->con->esc($this->f('category_keywords')).'\',
 460                  category_path = \''.$this->con->esc($this->f('category_path')).'\',
 461                  category_template = \''.$this->con->esc($this->f('category_template')).'\',
 462                  category_cachetime = \''.$this->con->esc($this->f('category_cachetime')).'\',
 463                  website_id = \''.$this->con->esc($this->f('website_id')).'\',
 464                  category_publicationdate = \''.$this->con->esc($this->f('category_publicationdate')).'\',           
 465                  category_creationdate = \''.$this->con->esc($this->f('category_creationdate')).'\',  
 466                  category_enddate = \''.$this->con->esc($this->f('category_enddate')).'\',
 467                  category_type = \'default\',
 468                  image_id = \'0\',
 469                  icon_id = \'0\',
 470                  forum_id = \'0\'';
 471          }
 472          if (!$this->con->execute($req)) {
 473              $this->setError('MySQL: '.$this->con->error(), 500);
 474              return false;
 475          }
 476          if (!$update) {
 477              $this->setField('category_id', $this->con->getLastID());
 478          }
 479          $this->load();
 480          return true;
 481      }
 482  
 483      /**
 484       * Remove the category from the database.
 485       *
 486       * @return bool Success
 487       */
 488      function remove()
 489      {
 490          if ($this->f('category_path') == '/') {
 491              $this->setError(__('Error: A root category cannot be deleted.'),
 492                              400);
 493              return false;
 494          }
 495          if (false === $this->loadResources()) {
 496              return false;
 497          }
 498          if (!$this->res->isEmpty()) {
 499              $this->setError(__('Error: A category must be empty to be deleted.'), 400);
 500              return false;
 501          }
 502          
 503          $delReq = 'DELETE FROM '.$this->con->pfx.'categories 
 504              WHERE category_id = \''.$this->con->esc($this->f('category_id'))
 505              .'\'';
 506          if (!$this->con->execute($delReq)) {
 507              $this->setError('MySQL: '.$this->con->error(), 500);
 508              return false;
 509          }        
 510  
 511          // not necessary but can remove some orphan entries
 512          $delReq = 'DELETE FROM '.$this->con->pfx.'categoryasso
 513              WHERE category_id = \''.$this->con->esc($this->f('category_id'))
 514              .'\'';
 515          if (!$this->con->execute($delReq)) {
 516              $this->setError('MySQL: '.$this->con->error(), 500);
 517              return false;
 518          } 
 519          return true;
 520      }
 521  
 522  
 523  }
 524  
 525  ?>


Généré le : Mon Nov 26 11:57:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics