[ 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.resourceset.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   * resourceset extends recordset to store dynamically a set of
  28   * resources. These resources can be news, articles, etc...
  29   * The goal is to provide a convinient way to iterate through
  30   * different type of resources.
  31   * An instance of recordset is created from a query against the
  32   * `resources` table joined to the `categories` to be able to
  33   * have the current category information. 
  34   * @see /manager/extinc/class.mysql.php for more details.
  35   * It relies on the resources to have some predefined methods.
  36   */
  37  class ResourceSet extends recordset
  38  {
  39      /**
  40       * Store the current resource object in the stack.
  41       */
  42      var $cur = null;
  43  
  44      /**
  45       * Store the fields to save.
  46       */
  47      var $auto_save = array();
  48  
  49      /**
  50       * Init the resourceset from an array.
  51       * If not empty, load the current resource object.
  52       */
  53      function ResourceSet($data='')
  54      {
  55          parent::recordset($data);
  56          if (!$this->isEmpty()) {
  57              $this->loadCurrent();
  58          }
  59      }
  60  
  61      /**
  62       * Auto save for load some fields.
  63       *
  64       * When loading a resource from the data in the current list
  65       * the list special data is not used in the object. For example
  66       * if the list of resources is coming from a search, the score
  67       * is lost. The auto save feature is used to recover such fields.
  68       *
  69       * @param array Fields to save
  70       * @return bool True
  71       */
  72      function autoSave($fields)
  73      {
  74          $this->auto_save = $fields;
  75          $this->recoverFields();
  76          return true;
  77      }
  78  
  79      /**
  80       * Recover the fields set by autoSave().
  81       *
  82       * @return bool True
  83       */
  84      function recoverFields()
  85      {
  86          if (!is_null($this->cur)) {
  87              foreach ($this->auto_save as $field) {
  88                  $this->cur->setField($field, parent::f($field));
  89              }
  90          }
  91          return true;
  92      }
  93  
  94      /**
  95       * Access to data in the current resource.
  96       *
  97       * @param string Field to retrieve
  98       * @return mixed Field data
  99       */
 100      function f($field)
 101      {
 102          return $this->cur->f($field);
 103      }
 104  
 105      /**
 106       * Access to extended data in the current resource.
 107       *
 108       * @param string Extended resource
 109       * @param string Field to retrieve
 110       * @return mixed Field data
 111       */
 112      function extf($ext, $field)
 113      {
 114          if (!empty($this->cur->$ext)) {
 115              return $this->cur->$ext->f($field);
 116          } else {
 117              //try to access not available extended data 
 118              //this is a problem, trigger a warning as most likely the code
 119              //has a problem
 120              trigger_error('Extended data "'.$ext
 121                            .'" not available for the current resource', 
 122                            E_USER_WARNING);
 123              return '';
 124          }
 125      }
 126  
 127      /**
 128       * Get the path of the resource.
 129       *
 130       * @string string Force the type of path ('')
 131       * @return string Path
 132       */
 133      function getPath($type='')
 134      {
 135          return $this->cur->getPath($type);
 136      }
 137  
 138      /**
 139       * From the data at the current index, load the
 140       * corresponding resource object into $this->cur
 141       */
 142      function loadCurrent()
 143      {
 144          $data = array();
 145          if (false === ($data[0] = $this->getRow())) {
 146              // this should never happen
 147              trigger_error('No data available to load a resource object '
 148                            .parent::f('resource_id'), 
 149                            E_USER_WARNING); 
 150              return false;
 151          }
 152          $type = parent::f('type_id');
 153          if (!class_exists($type)) {
 154              // we suppose here that the class file has been loaded before
 155              $type = 'Resource';
 156          }
 157          $this->cur = new $type($data);
 158          if (false !== ($res = MemStorage::get($this->cur->f('resource_id')))) {
 159              $this->cur = $res;
 160          } else {
 161              $this->cur->load(); 
 162              $this->recoverFields();
 163              MemStorage::save($this->cur);
 164          }
 165      }
 166  
 167      /**
 168       * Move to the next resource.
 169       * 
 170       * @return bool false if no next resource
 171       */
 172      function moveNext()
 173      {
 174          if (parent::moveNext() && !$this->EOF()) {
 175              $this->loadCurrent();
 176              return true;
 177          } else {
 178              return false;
 179          }
 180      }
 181  
 182  
 183      /**
 184       * Get content of a field as text.
 185       * & is encoded as &amp;
 186       *
 187       * @param string Field to get
 188       * @return string Content
 189       */
 190      function getTextContent($field)
 191      {
 192          return $this->cur->getTextContent($field);
 193      }
 194  
 195  
 196      /**
 197       * Move to a given index.
 198       * 
 199       * @param int Index to move to
 200       * @return bool false if no index
 201       */
 202      function move($index)
 203      {
 204          if ($this->int_index == $index) {
 205              return true;
 206          }
 207          if (parent::move($index)) {
 208              $this->loadCurrent();
 209              return true;
 210          } else {
 211              return false;
 212          }
 213      }
 214  
 215  
 216      /**
 217       * Iterate through extended data.
 218       *
 219       * @param string Extended data
 220       * @return bool The move was a success or not
 221       */
 222      function extMoveNext($ext)
 223      {
 224          if (!empty($this->cur->$ext)) {
 225              return $this->cur->$ext->moveNext();
 226          } else {
 227              trigger_error('Extended data "'.$ext.'" not available for the current resource.', E_USER_WARNING);
 228              return false;
 229          }
 230      }
 231  
 232      /**
 233       * Check the end of extended data.
 234       *
 235       * @param string Extended data
 236       * @return bool At the end of the data
 237       */
 238      function extEOF($ext)
 239      {
 240          if (!empty($this->cur->$ext)) {
 241              return $this->cur->$ext->EOF();
 242          } else {
 243              trigger_error('Extended data "'.$ext.'" not available for the current resource.', E_USER_WARNING);
 244              return true; //Force to be at the end of the data
 245          }
 246      }
 247  
 248  }
 249  
 250  
 251  /**
 252   * Global storage of resources. Used by the ResourceSet to avoid reloading
 253   * many times a resource within a page.
 254   */
 255  class MemStorage
 256  {
 257      /**
 258       * Returns a resource or false if not available
 259       *
 260       * @param int Resource id
 261       * @return mixed Resource object or false
 262       */
 263      function get($res_id)
 264      {
 265          if (isset($GLOBALS['_PX_mem_storage'][$res_id])) {
 266              return $GLOBALS['_PX_mem_storage'][$res_id];
 267          } else {
 268              return false;
 269          }
 270      }
 271  
 272      /**
 273       * Save the resource.
 274       *
 275       * @param Object Resource object
 276       * @return true
 277       */
 278      function save($res)
 279      {
 280          if (!isset($GLOBALS['_PX_mem_storage'])
 281              or count($GLOBALS['_PX_mem_storage']) > 20) {
 282              $GLOBALS['_PX_mem_storage'] = array();
 283          }
 284          $GLOBALS['_PX_mem_storage'][$res->f('resource_id')] = $res;
 285          return true;
 286      }
 287  }
 288  ?>


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