[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/application/component/ -> model.php (source)

   1  <?php
   2  /**
   3  * @version        $Id: model.php 14401 2010-01-26 14:10:00Z louis $
   4  * @package        Joomla.Framework
   5  * @subpackage    Application
   6  * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * Base class for a Joomla Model
  20   *
  21   * Acts as a Factory class for application specific objects and
  22   * provides many supporting API functions.
  23   *
  24   * @abstract
  25   * @package        Joomla.Framework
  26   * @subpackage    Application
  27   * @since        1.5
  28   */
  29  class JModel extends JObject
  30  {
  31      /**
  32       * The model (base) name
  33       *
  34       * @var string
  35       * @access    protected
  36       */
  37      var $_name;
  38  
  39      /**
  40       * Database Connector
  41       *
  42       * @var object
  43       * @access    protected
  44       */
  45      var $_db;
  46  
  47      /**
  48       * An state object
  49       *
  50       * @var string
  51       * @access    protected
  52       */
  53      var $_state;
  54  
  55      /**
  56       * Constructor
  57       *
  58       * @since    1.5
  59       */
  60  	function __construct($config = array())
  61      {
  62          //set the view name
  63          if (empty( $this->_name ))
  64          {
  65              if (array_key_exists('name', $config))  {
  66                  $this->_name = $config['name'];
  67              } else {
  68                  $this->_name = $this->getName();
  69              }
  70          }
  71  
  72          //set the model state
  73          if (array_key_exists('state', $config))  {
  74              $this->_state = $config['state'];
  75          } else {
  76              $this->_state = new JObject();
  77          }
  78  
  79          //set the model dbo
  80          if (array_key_exists('dbo', $config))  {
  81              $this->_db = $config['dbo'];
  82          } else {
  83              $this->_db = &JFactory::getDBO();
  84          }
  85  
  86          // set the default view search path
  87          if (array_key_exists('table_path', $config)) {
  88              $this->addTablePath($config['table_path']);
  89          } else if (defined( 'JPATH_COMPONENT_ADMINISTRATOR' )){
  90              $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
  91          }
  92      }
  93  
  94      /**
  95       * Returns a reference to the a Model object, always creating it
  96       *
  97       * @param    string    The model type to instantiate
  98       * @param    string    Prefix for the model class name. Optional.
  99       * @param    array    Configuration array for model. Optional.
 100       * @return    mixed    A model object, or false on failure
 101       * @since    1.5
 102      */
 103      function &getInstance( $type, $prefix = '', $config = array() )
 104      {
 105          $type        = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
 106          $modelClass    = $prefix.ucfirst($type);
 107          $result        = false;
 108  
 109          if (!class_exists( $modelClass ))
 110          {
 111              jimport('joomla.filesystem.path');
 112              $path = JPath::find(
 113                  JModel::addIncludePath(),
 114                  JModel::_createFileName( 'model', array( 'name' => $type))
 115              );
 116              if ($path)
 117              {
 118                  require_once $path;
 119  
 120                  if (!class_exists( $modelClass ))
 121                  {
 122                      JError::raiseWarning( 0, 'Model class ' . $modelClass . ' not found in file.' );
 123                      return $result;
 124                  }
 125              }
 126              else return $result;
 127          }
 128  
 129          $result = new $modelClass($config);
 130          return $result;
 131      }
 132  
 133      /**
 134       * Method to set model state variables
 135       *
 136       * @access    public
 137       * @param    string    The name of the property
 138       * @param    mixed    The value of the property to set
 139       * @return    mixed    The previous value of the property
 140       * @since    1.5
 141       */
 142  	function setState( $property, $value=null )
 143      {
 144          return $this->_state->set($property, $value);
 145      }
 146  
 147      /**
 148       * Method to get model state variables
 149       *
 150       * @access    public
 151       * @param    string    Optional parameter name
 152       * @return    object    The property where specified, the state object where omitted
 153       * @since    1.5
 154       */
 155  	function getState($property = null)
 156      {
 157          return $property === null ? $this->_state : $this->_state->get($property);
 158      }
 159  
 160      /**
 161       * Method to get the database connector object
 162       *
 163       * @access    public
 164       * @return    object JDatabase connector object
 165       * @since    1.5
 166       */
 167      function &getDBO()
 168      {
 169          return $this->_db;
 170      }
 171  
 172      /**
 173       * Method to set the database connector object
 174       *
 175       * @param    object    $db    A JDatabase based object
 176       * @return    void
 177       * @since    1.5
 178       */
 179  	function setDBO(&$db)
 180      {
 181          $this->_db =& $db;
 182      }
 183  
 184      /**
 185       * Method to get the model name
 186       *
 187       * The model name by default parsed using the classname, or it can be set
 188       * by passing a $config['name�] in the class constructor
 189       *
 190       * @access    public
 191       * @return    string The name of the model
 192       * @since    1.5
 193       */
 194  	function getName()
 195      {
 196          $name = $this->_name;
 197  
 198          if (empty( $name ))
 199          {
 200              $r = null;
 201              if (!preg_match('/Model(.*)/i', get_class($this), $r)) {
 202                  JError::raiseError (500, "JModel::getName() : Can't get or parse class name.");
 203              }
 204              $name = strtolower( $r[1] );
 205          }
 206  
 207          return $name;
 208      }
 209  
 210      /**
 211       * Method to get a table object, load it if necessary.
 212       *
 213       * @access    public
 214       * @param    string The table name. Optional.
 215       * @param    string The class prefix. Optional.
 216       * @param    array    Configuration array for model. Optional.
 217       * @return    object    The table
 218       * @since    1.5
 219       */
 220      function &getTable($name='', $prefix='Table', $options = array())
 221      {
 222          if (empty($name)) {
 223              $name = $this->getName();
 224          }
 225  
 226          if($table = &$this->_createTable( $name, $prefix, $options ))  {
 227              return $table;
 228          }
 229  
 230          JError::raiseError( 0, 'Table ' . $name . ' not supported. File not found.' );
 231          $null = null;
 232          return $null;
 233      }
 234  
 235      /**
 236       * Add a directory where JModel should search for models. You may
 237       * either pass a string or an array of directories.
 238       *
 239       * @access    public
 240       * @param    string    A path to search.
 241       * @return    array    An array with directory elements
 242       * @since    1.5
 243       */
 244  	function addIncludePath( $path='' )
 245      {
 246          static $paths;
 247  
 248          if (!isset($paths)) {
 249              $paths = array();
 250          }
 251          if (!empty( $path ) && !in_array( $path, $paths )) {
 252              jimport('joomla.filesystem.path');
 253              array_unshift($paths, JPath::clean( $path ));
 254          }
 255          return $paths;
 256      }
 257  
 258      /**
 259       * Adds to the stack of model table paths in LIFO order.
 260       *
 261       * @static
 262       * @param    string|array The directory (-ies) to add.
 263       * @return    void
 264       */
 265  	function addTablePath($path)
 266      {
 267          jimport('joomla.database.table');
 268          JTable::addIncludePath($path);
 269      }
 270  
 271      /**
 272       * Returns an object list
 273       *
 274       * @param    string The query
 275       * @param    int Offset
 276       * @param    int The number of records
 277       * @return    array
 278       * @access    protected
 279       * @since    1.5
 280       */
 281      function &_getList( $query, $limitstart=0, $limit=0 )
 282      {
 283          $this->_db->setQuery( $query, $limitstart, $limit );
 284          $result = $this->_db->loadObjectList();
 285  
 286          return $result;
 287      }
 288  
 289      /**
 290       * Returns a record count for the query
 291       *
 292       * @param    string The query
 293       * @return    int
 294       * @access    protected
 295       * @since    1.5
 296       */
 297  	function _getListCount( $query )
 298      {
 299          $this->_db->setQuery( $query );
 300          $this->_db->query();
 301  
 302          return $this->_db->getNumRows();
 303      }
 304  
 305      /**
 306       * Method to load and return a model object.
 307       *
 308       * @access    private
 309       * @param    string    The name of the view
 310       * @param   string  The class prefix. Optional.
 311       * @return    mixed    Model object or boolean false if failed
 312       * @since    1.5
 313       */
 314      function &_createTable( $name, $prefix = 'Table', $config = array())
 315      {
 316          $result = null;
 317  
 318          // Clean the model name
 319          $name    = preg_replace( '/[^A-Z0-9_]/i', '', $name );
 320          $prefix = preg_replace( '/[^A-Z0-9_]/i', '', $prefix );
 321  
 322          //Make sure we are returning a DBO object
 323          if (!array_key_exists('dbo', $config))  {
 324              $config['dbo'] =& $this->getDBO();;
 325          }
 326  
 327          $instance =& JTable::getInstance($name, $prefix, $config );
 328          return $instance;
 329      }
 330  
 331      /**
 332       * Create the filename for a resource
 333       *
 334       * @access    private
 335       * @param    string     $type  The resource type to create the filename for
 336       * @param    array     $parts An associative array of filename information
 337       * @return    string The filename
 338       * @since    1.5
 339       */
 340  	function _createFileName($type, $parts = array())
 341      {
 342          $filename = '';
 343  
 344          switch($type)
 345          {
 346              case 'model':
 347                  $filename = strtolower($parts['name']).'.php';
 348                  break;
 349  
 350          }
 351          return $filename;
 352      }
 353  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1