Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/application/component/model.php

Documentation is available at model.php

  1. <?php
  2. /**
  3. @version        $Id: model.php 6523 2007-02-08 02:58:05Z louis $
  4. @package        Joomla.Framework
  5. @subpackage    Application
  6. @copyright    Copyright (C) 2005 - 2007 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.  * @author        Johan Janssens <[email protected]>
  28.  * @author        Louis Landry <[email protected]>
  29.  * @author         Andrew Eddie
  30.  * @since        1.5
  31.  */
  32. class JModel extends JObject
  33. {
  34.     /**
  35.      * The model (base) name
  36.      *
  37.      * @var string 
  38.      * @access protected
  39.      */
  40.     var $_name;
  41.  
  42.     /**
  43.      * Database Connector
  44.      *
  45.      * @var object 
  46.      * @access protected
  47.      */
  48.     var $_db;
  49.  
  50.     /**
  51.      * An error message
  52.      *
  53.      * @var string 
  54.      * @access protected
  55.      */
  56.     var $_error;
  57.  
  58.     /**
  59.      * An state object
  60.      *
  61.      * @var string 
  62.      * @access protected
  63.      */
  64.     var $_state;
  65.  
  66.     /**
  67.      * Constructor
  68.      *
  69.      * @since 1.5
  70.      */
  71.     function __construct($config array())
  72.     {
  73.         $this->_db    = &JFactory::getDBO();
  74.         $this->_state = new JObject();
  75.  
  76.         //set the view name
  77.         if (empty$this->_name ))
  78.         {
  79.             if (isset($config['name']))  {
  80.                 $this->_name = $config['name'];
  81.             }
  82.             else
  83.             {
  84.                 $r null;
  85.                 if (!preg_match('/Model(.*)/i'get_class($this)$r)) {
  86.                     JError::raiseError (500"JModel::__construct() : Can't get or parse class name.");
  87.                 }
  88.                 $this->_name = strtolower$r[1);
  89.             }
  90.         }
  91.  
  92.         // set the default view search path
  93.         if (isset($config['table_path'])) {
  94.             // user-defined dirs
  95.             $this->addTablePath($config['table_path']);
  96.         else if (defined'JPATH_COMPONENT_ADMINISTRATOR' )){
  97.             $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Returns a reference to the a Model object, always creating it
  103.      *
  104.      * @param    string    The model type to instantiate
  105.      * @param    string    Prefix for the model class name
  106.      * @return    mixed    A model object, or false on failure
  107.      * @since 1.5
  108.     */
  109.     function &getInstance$type$prefix='' )
  110.     {
  111.         $modelClass    $prefix.ucfirst($type);
  112.         $result        false;
  113.  
  114.         if (!class_exists$modelClass ))
  115.         {
  116.             jimport('joomla.filesystem.path');
  117.             if ($path JPath::find(JModel::addIncludePath()strtolower($type).'.php'))
  118.             {
  119.                 require_once $path;
  120.  
  121.                 if (!class_exists$modelClass ))
  122.                 {
  123.                     JError::raiseWarning0'Model class ' $modelClass ' not found in file.' );
  124.                     return $result;
  125.                 }
  126.             }
  127.             else
  128.             {
  129.                 JError::raiseWarning0'Model ' $type ' not supported. File not found.' );
  130.                 return $result;
  131.             }
  132.         }
  133.  
  134.         $result new $modelClass();
  135.         return $result;
  136.     }
  137.  
  138.     /**
  139.      * Method to set model state variables
  140.      *
  141.      * @access    public
  142.      * @param    string    The name of the property
  143.      * @param    mixed    The value of the property to set
  144.      * @return    mixed    The previous value of the property
  145.      * @since    1.5
  146.      */
  147.     function setState$property$value=null )
  148.     {
  149.         return $this->_state->set($property$value);
  150.     }
  151.  
  152.     /**
  153.      * Method to get model state variables
  154.      *
  155.      * @access    public
  156.      * @param    string    Optional parameter name
  157.      * @return    object    The property where specified, the state object where omitted
  158.      * @since    1.5
  159.      */
  160.     function getState($property null)
  161.     {
  162.         return $property === null $this->_state : $this->_state->get($property);
  163.     }
  164.  
  165.     /**
  166.      * Method to get the database connector object
  167.      *
  168.      * @access    public
  169.      * @return    object JDatabase connector object
  170.      * @since 1.5
  171.      */
  172.     function &getDBO()
  173.     {
  174.         return $this->_db;
  175.     }
  176.  
  177.     /**
  178.      * Get the error message
  179.      *
  180.      * @return string The error message
  181.      * @since 1.5
  182.      */
  183.     function getError($escaped false)
  184.     {
  185.         if ($escaped{
  186.             return addslashes($this->_error);
  187.         else {
  188.             return $this->_error;
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Sets the error message
  194.      *
  195.      * @param string The error message
  196.      * @return string The new error message
  197.      * @since 1.5
  198.      */
  199.     function setError$value )
  200.     {
  201.         $this->_error = $value;
  202.         return $this->_error;
  203.     }
  204.  
  205.     /**
  206.      * Method to get a table object, load it if necessary.
  207.      *
  208.      * @access    public
  209.      * @param    string The table name
  210.      * @param    string The class prefix
  211.      * @return    object    The table
  212.      * @since    1.5
  213.      */
  214.     function &getTable($name=''$prefix='Table')
  215.     {
  216.         if (empty($name)) {
  217.             $name $this->_name;
  218.         }
  219.  
  220.         if($table &$this->_createTable$name$prefix )) {
  221.             return $table;
  222.         else {
  223.             JError::raiseError0'Table ' $name ' not supported. File not found.' );
  224.             return null;
  225.         }
  226.     }
  227.  
  228.     /**
  229.      * Add a directory where JModel should search for models. You may
  230.      * either pass a string or an array of directories.
  231.      *
  232.      * @access    public
  233.      * @param    string    A path to search.
  234.      * @return    array    An array with directory elements
  235.      * @since 1.5
  236.      */
  237.     function addIncludePath$path='' )
  238.     {
  239.         static $paths;
  240.  
  241.         if (!isset($paths)) {
  242.             $paths array();
  243.         }
  244.         if (!empty$path && !in_array$path$paths )) {
  245.             array_unshift($pathsJPath::clean$path ));
  246.         }
  247.         return $paths;
  248.     }
  249.  
  250.     /**
  251.      * Adds to the stack of model table paths in LIFO order.
  252.      *
  253.      * @static
  254.      * @param string|arrayThe directory (-ies) to add.
  255.      * @return void 
  256.      */
  257.     function addTablePath($path)
  258.     {
  259.         JTable::addIncludePath($path);
  260.     }
  261.  
  262.     /**
  263.      * Returns an object list
  264.      *
  265.      * @param string The query
  266.      * @param int Offset
  267.      * @param int The number of records
  268.      * @return array 
  269.      * @access protected
  270.      * @since 1.5
  271.      */
  272.     function &_getList$query$limitstart=0$limit=)
  273.     {
  274.         $db =JFactory::getDBO();
  275.         $db->setQuery$query$limitstart$limit );
  276.         $result $db->loadObjectList();
  277.  
  278.         return $result;
  279.     }
  280.  
  281.     /**
  282.      * Returns a record count for the query
  283.      *
  284.      * @param string The query
  285.      * @return int 
  286.      * @access protected
  287.      * @since 1.5
  288.      */
  289.     function _getListCount$query )
  290.     {
  291.         $db =JFactory::getDBO();
  292.         $db->setQuery$query );
  293.         $db->query();
  294.  
  295.         return $db->getNumRows();
  296.     }
  297.  
  298.     /**
  299.      * Method to load and return a model object.
  300.      *
  301.      * @access    private
  302.      * @param    string    $modelName    The name of the view
  303.      * @return    mixed    Model object or boolean false if failed
  304.      * @since    1.5
  305.      */
  306.     function &_createTable$name$prefix 'Table')
  307.     {
  308.         $result null;
  309.  
  310.         // Clean the model name
  311.         $tableName        preg_replace'#\W#'''$name );
  312.         $classPrefix    preg_replace'#\W#'''$prefix );
  313.  
  314.         // Build the model class name
  315.         $tableClass $classPrefix.$tableName;
  316.  
  317.         if (!class_exists$tableClass ))
  318.         {
  319.             jimport('joomla.filesystem.path');
  320.             if($path JPath::find(JTable::addIncludePath()$this->_createFileName('table'array('name' => $tableName))))
  321.             {
  322.                 require_once $path;
  323.  
  324.                 if (!class_exists$tableClass ))
  325.                 {
  326.                     $result JError::raiseError500'Table class ' $tableClass ' not found in file.' );
  327.                     return $result;
  328.                 }
  329.             }
  330.             else {
  331.                 return $result;
  332.             }
  333.         }
  334.  
  335.         $db =$this->getDBO();
  336.         $result new $tableClass($db);
  337.  
  338.         return $result;
  339.     }
  340.  
  341.     /**
  342.      * Create the filename for a resource
  343.      *
  344.      * @access private
  345.      * @param string     $type  The resource type to create the filename for
  346.      * @param array     $parts An associative array of filename information
  347.      * @return string The filename
  348.      * @since 1.5
  349.      */
  350.     function _createFileName($type$parts array())
  351.     {
  352.         $filename '';
  353.  
  354.         switch($type)
  355.         {
  356.             case 'table' :
  357.                  $filename strtolower($parts['name']).'.php';
  358.                 break;
  359.  
  360.         }
  361.         return $filename;
  362.     }
  363. }
  364. ?>

Documentation generated on Mon, 05 Mar 2007 21:10:54 +0000 by phpDocumentor 1.3.1