PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/application/component/model.php

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