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