10 defined(
'JPATH_PLATFORM') or die;
30 protected $__state_set = null;
54 protected $option = null;
70 protected $event_clean_cache = null;
83 public static function addIncludePath($path =
'', $prefix =
'')
92 if (!isset($paths[$prefix]))
94 $paths[$prefix] = array();
97 if (!isset($paths[
'']))
104 jimport(
'joomla.filesystem.path');
106 if (!in_array($path, $paths[$prefix]))
111 if (!in_array($path, $paths[
'']))
117 return $paths[$prefix];
129 public static function addTablePath($path)
144 protected static function _createFileName($type, $parts = array())
151 $filename = strtolower($parts[
'name']) .
'.php';
169 public static function getInstance($type, $prefix =
'', $config = array())
171 $type = preg_replace(
'/[^A-Z0-9_\.-]/i',
'', $type);
172 $modelClass = $prefix . ucfirst($type);
174 if (!class_exists($modelClass))
176 jimport(
'joomla.filesystem.path');
177 $path =
JPath::find(self::addIncludePath(null, $prefix), self::_createFileName(
'model', array(
'name' => $type)));
180 $path =
JPath::find(self::addIncludePath(null,
''), self::_createFileName(
'model', array(
'name' => $type)));
186 if (!class_exists($modelClass))
198 return new $modelClass($config);
209 public function __construct($config = array())
212 if (empty($this->option))
216 if (!preg_match(
'/(.*)Model/i', get_class($this), $r))
218 throw new Exception(
JText::_(
'JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
221 $this->option =
'com_' . strtolower($r[1]);
225 if (empty($this->name))
227 if (array_key_exists(
'name', $config))
229 $this->name = $config[
'name'];
233 $this->name = $this->getName();
238 if (array_key_exists(
'state', $config))
240 $this->state = $config[
'state'];
244 $this->state =
new JObject;
248 if (array_key_exists(
'dbo', $config))
250 $this->_db = $config[
'dbo'];
258 if (array_key_exists(
'table_path', $config))
260 $this->addTablePath($config[
'table_path']);
262 elseif (defined(
'JPATH_COMPONENT_ADMINISTRATOR'))
264 $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR .
'/tables');
265 $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR .
'/table');
269 if (!empty($config[
'ignore_request']))
271 $this->__state_set =
true;
275 if (isset($config[
'event_clean_cache']))
277 $this->event_clean_cache = $config[
'event_clean_cache'];
279 elseif (empty($this->event_clean_cache))
281 $this->event_clean_cache =
'onContentCleanCache';
298 protected function _getList($query, $limitstart = 0, $limit = 0)
300 $this->_db->setQuery($query, $limitstart, $limit);
301 $result = $this->_db->loadObjectList();
315 protected function _getListCount($query)
319 && $query->type ==
'select'
320 && $query->
group === null
321 && $query->having === null)
323 $query = clone $query;
324 $query->clear(
'select')->clear(
'order')->select(
'COUNT(*)');
326 $this->_db->setQuery($query);
327 return (
int) $this->_db->loadResult();
331 $this->_db->setQuery($query);
332 $this->_db->execute();
334 return (
int) $this->_db->getNumRows();
349 protected function _createTable($name, $prefix =
'Table', $config = array())
352 $name = preg_replace(
'/[^A-Z0-9_]/i',
'', $name);
353 $prefix = preg_replace(
'/[^A-Z0-9_]/i',
'', $prefix);
356 if (!array_key_exists(
'dbo', $config))
358 $config[
'dbo'] = $this->getDbo();
369 public function getDbo()
385 public function getName()
387 if (empty($this->name))
390 if (!preg_match(
'/Model(.*)/i', get_class($this), $r))
392 throw new Exception(
JText::_(
'JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
394 $this->name = strtolower($r[1]);
410 public function getState($property = null, $default = null)
412 if (!$this->__state_set)
415 $this->populateState();
418 $this->__state_set =
true;
421 return $property === null ? $this->state : $this->state->get($property, $default);
436 public function getTable($name =
'', $prefix =
'Table', $options = array())
440 $name = $this->getName();
443 if ($table = $this->_createTable($name, $prefix, $options))
448 throw new Exception(
JText::sprintf(
'JLIB_APPLICATION_ERROR_TABLE_NAME_NOT_SUPPORTED', $name), 0);
461 public function loadHistory($version_id,
JTable &$table)
471 if (!$historyTable->load($version_id))
473 $this->setError($historyTable->getError());
482 if ($historyTable->ucm_type_id != $typeId)
484 $this->setError(
JText::_(
'JLIB_APPLICATION_ERROR_HISTORY_ID_MISMATCH'));
487 if (isset($rowArray[$key]))
489 $table->
checkIn($rowArray[$key]);
496 $this->setState(
'save_date', $historyTable->save_date);
497 $this->setState(
'version_note', $historyTable->version_note);
499 return $table->
bind($rowArray);
514 protected function populateState()
527 public function setDbo($db)
542 public function setState($property, $value = null)
544 return $this->state->set($property, $value);
557 protected function cleanCache($group = null, $client_id = 0)
563 'defaultgroup' => ($group) ? $group : (isset($this->option) ? $this->option :
JFactory::getApplication()->input->get(
'option')),
564 'cachebase' => ($client_id) ? JPATH_ADMINISTRATOR .
'/cache' : $conf->get(
'cache_path', JPATH_SITE .
'/cache'));
570 $dispatcher->trigger($this->event_clean_cache, $options);