You've already forked joomla_test
first commit
This commit is contained in:
1181
libraries/legacy/model/admin.php
Normal file
1181
libraries/legacy/model/admin.php
Normal file
File diff suppressed because it is too large
Load Diff
325
libraries/legacy/model/form.php
Normal file
325
libraries/legacy/model/form.php
Normal file
@ -0,0 +1,325 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* Prototype form model.
|
||||
*
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
* @see JForm
|
||||
* @see JFormField
|
||||
* @see JFormRule
|
||||
* @since 12.2
|
||||
*/
|
||||
abstract class JModelForm extends JModelLegacy
|
||||
{
|
||||
/**
|
||||
* Array of form objects.
|
||||
*
|
||||
* @var array
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $_forms = array();
|
||||
|
||||
/**
|
||||
* Method to checkin a row.
|
||||
*
|
||||
* @param integer $pk The numeric id of the primary key.
|
||||
*
|
||||
* @return boolean False on failure or error, true otherwise.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function checkin($pk = null)
|
||||
{
|
||||
// Only attempt to check the row in if it exists.
|
||||
if ($pk)
|
||||
{
|
||||
$user = JFactory::getUser();
|
||||
|
||||
// Get an instance of the row to checkin.
|
||||
$table = $this->getTable();
|
||||
|
||||
if (!$table->load($pk))
|
||||
{
|
||||
$this->setError($table->getError());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if this is the user having previously checked out the row.
|
||||
if ($table->checked_out > 0 && $table->checked_out != $user->get('id') && !$user->authorise('core.admin', 'com_checkin'))
|
||||
{
|
||||
$this->setError(JText::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Attempt to check the row in.
|
||||
if (!$table->checkin($pk))
|
||||
{
|
||||
$this->setError($table->getError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to check-out a row for editing.
|
||||
*
|
||||
* @param integer $pk The numeric id of the primary key.
|
||||
*
|
||||
* @return boolean False on failure or error, true otherwise.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function checkout($pk = null)
|
||||
{
|
||||
// Only attempt to check the row in if it exists.
|
||||
if ($pk)
|
||||
{
|
||||
// Get an instance of the row to checkout.
|
||||
$table = $this->getTable();
|
||||
|
||||
if (!$table->load($pk))
|
||||
{
|
||||
$this->setError($table->getError());
|
||||
return false;
|
||||
}
|
||||
|
||||
// If there is no checked_out or checked_out_time field, just return true.
|
||||
if (!property_exists($table, 'checked_out') || !property_exists($table, 'checked_out_time'))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
$user = JFactory::getUser();
|
||||
|
||||
// Check if this is the user having previously checked out the row.
|
||||
if ($table->checked_out > 0 && $table->checked_out != $user->get('id'))
|
||||
{
|
||||
$this->setError(JText::_('JLIB_APPLICATION_ERROR_CHECKOUT_USER_MISMATCH'));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Attempt to check the row out.
|
||||
if (!$table->checkout($user->get('id'), $pk))
|
||||
{
|
||||
$this->setError($table->getError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method for getting the form from the model.
|
||||
*
|
||||
* @param array $data Data for the form.
|
||||
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
|
||||
*
|
||||
* @return mixed A JForm object on success, false on failure
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
abstract public function getForm($data = array(), $loadData = true);
|
||||
|
||||
/**
|
||||
* Method to get a form object.
|
||||
*
|
||||
* @param string $name The name of the form.
|
||||
* @param string $source The form source. Can be XML string if file flag is set to false.
|
||||
* @param array $options Optional array of options for the form creation.
|
||||
* @param boolean $clear Optional argument to force load a new form.
|
||||
* @param string $xpath An optional xpath to search for the fields.
|
||||
*
|
||||
* @return mixed JForm object on success, False on error.
|
||||
*
|
||||
* @see JForm
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function loadForm($name, $source = null, $options = array(), $clear = false, $xpath = false)
|
||||
{
|
||||
// Handle the optional arguments.
|
||||
$options['control'] = JArrayHelper::getValue($options, 'control', false);
|
||||
|
||||
// Create a signature hash.
|
||||
$hash = md5($source . serialize($options));
|
||||
|
||||
// Check if we can use a previously loaded form.
|
||||
if (isset($this->_forms[$hash]) && !$clear)
|
||||
{
|
||||
return $this->_forms[$hash];
|
||||
}
|
||||
|
||||
// Get the form.
|
||||
JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
|
||||
JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
|
||||
JForm::addFormPath(JPATH_COMPONENT . '/model/form');
|
||||
JForm::addFieldPath(JPATH_COMPONENT . '/model/field');
|
||||
|
||||
try
|
||||
{
|
||||
$form = JForm::getInstance($name, $source, $options, false, $xpath);
|
||||
|
||||
if (isset($options['load_data']) && $options['load_data'])
|
||||
{
|
||||
// Get the data for the form.
|
||||
$data = $this->loadFormData();
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = array();
|
||||
}
|
||||
|
||||
// Allow for additional modification of the form, and events to be triggered.
|
||||
// We pass the data because plugins may require it.
|
||||
$this->preprocessForm($form, $data);
|
||||
|
||||
// Load the data into the form after the plugins have operated.
|
||||
$form->bind($data);
|
||||
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store the form for later.
|
||||
$this->_forms[$hash] = $form;
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the data that should be injected in the form.
|
||||
*
|
||||
* @return array The default data is an empty array.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function loadFormData()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to allow derived classes to preprocess the data.
|
||||
*
|
||||
* @param string $context The context identifier.
|
||||
* @param mixed &$data The data to be processed. It gets altered directly.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
protected function preprocessData($context, &$data)
|
||||
{
|
||||
// Get the dispatcher and load the users plugins.
|
||||
$dispatcher = JEventDispatcher::getInstance();
|
||||
JPluginHelper::importPlugin('content');
|
||||
|
||||
// Trigger the data preparation event.
|
||||
$results = $dispatcher->trigger('onContentPrepareData', array($context, $data));
|
||||
|
||||
// Check for errors encountered while preparing the data.
|
||||
if (count($results) > 0 && in_array(false, $results, true))
|
||||
{
|
||||
$this->setError($dispatcher->getError());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to allow derived classes to preprocess the form.
|
||||
*
|
||||
* @param JForm $form A JForm object.
|
||||
* @param mixed $data The data expected for the form.
|
||||
* @param string $group The name of the plugin group to import (defaults to "content").
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @see JFormField
|
||||
* @since 12.2
|
||||
* @throws Exception if there is an error in the form event.
|
||||
*/
|
||||
protected function preprocessForm(JForm $form, $data, $group = 'content')
|
||||
{
|
||||
// Import the appropriate plugin group.
|
||||
JPluginHelper::importPlugin($group);
|
||||
|
||||
// Get the dispatcher.
|
||||
$dispatcher = JEventDispatcher::getInstance();
|
||||
|
||||
// Trigger the form preparation event.
|
||||
$results = $dispatcher->trigger('onContentPrepareForm', array($form, $data));
|
||||
|
||||
// Check for errors encountered while preparing the form.
|
||||
if (count($results) && in_array(false, $results, true))
|
||||
{
|
||||
// Get the last error.
|
||||
$error = $dispatcher->getError();
|
||||
|
||||
if (!($error instanceof Exception))
|
||||
{
|
||||
throw new Exception($error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to validate the form data.
|
||||
*
|
||||
* @param JForm $form The form to validate against.
|
||||
* @param array $data The data to validate.
|
||||
* @param string $group The name of the field group to validate.
|
||||
*
|
||||
* @return mixed Array of filtered data if valid, false otherwise.
|
||||
*
|
||||
* @see JFormRule
|
||||
* @see JFilterInput
|
||||
* @since 12.2
|
||||
*/
|
||||
public function validate($form, $data, $group = null)
|
||||
{
|
||||
// Filter and validate the form data.
|
||||
$data = $form->filter($data);
|
||||
$return = $form->validate($data, $group);
|
||||
|
||||
// Check for an error.
|
||||
if ($return instanceof Exception)
|
||||
{
|
||||
$this->setError($return->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check the validation results.
|
||||
if ($return === false)
|
||||
{
|
||||
// Get the validation messages from the form.
|
||||
foreach ($form->getErrors() as $message)
|
||||
{
|
||||
$this->setError($message);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Tags B/C break at 3.1.2
|
||||
if (isset($data['metadata']['tags']) && !isset($data['tags']))
|
||||
{
|
||||
$data['tags'] = $data['metadata']['tags'];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
1
libraries/legacy/model/index.html
Normal file
1
libraries/legacy/model/index.html
Normal file
@ -0,0 +1 @@
|
||||
<!DOCTYPE html><title></title>
|
54
libraries/legacy/model/item.php
Normal file
54
libraries/legacy/model/item.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* Prototype item model.
|
||||
*
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
* @since 12.2
|
||||
*/
|
||||
abstract class JModelItem extends JModelLegacy
|
||||
{
|
||||
/**
|
||||
* An item.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_item = null;
|
||||
|
||||
/**
|
||||
* Model context string.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $_context = 'group.type';
|
||||
|
||||
/**
|
||||
* Method to get a store id based on model configuration state.
|
||||
*
|
||||
* This is necessary because the model is used by the component and
|
||||
* different modules that might need different sets of data or different
|
||||
* ordering requirements.
|
||||
*
|
||||
* @param string $id A prefix for the store id.
|
||||
*
|
||||
* @return string A store id.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function getStoreId($id = '')
|
||||
{
|
||||
// Compile the store id.
|
||||
return md5($id);
|
||||
}
|
||||
}
|
521
libraries/legacy/model/legacy.php
Normal file
521
libraries/legacy/model/legacy.php
Normal file
@ -0,0 +1,521 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* Base class for a Joomla Model
|
||||
*
|
||||
* Acts as a Factory class for application specific objects and
|
||||
* provides many supporting API functions.
|
||||
*
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
* @since 12.2
|
||||
*/
|
||||
abstract class JModelLegacy extends JObject
|
||||
{
|
||||
/**
|
||||
* Indicates if the internal state has been set
|
||||
*
|
||||
* @var boolean
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $__state_set = null;
|
||||
|
||||
/**
|
||||
* Database Connector
|
||||
*
|
||||
* @var object
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $_db;
|
||||
|
||||
/**
|
||||
* The model (base) name
|
||||
*
|
||||
* @var string
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* The URL option for the component.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $option = null;
|
||||
|
||||
/**
|
||||
* A state object
|
||||
*
|
||||
* @var string
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* The event to trigger when cleaning cache.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $event_clean_cache = null;
|
||||
|
||||
/**
|
||||
* Add a directory where JModelLegacy should search for models. You may
|
||||
* either pass a string or an array of directories.
|
||||
*
|
||||
* @param mixed $path A path or array[sting] of paths to search.
|
||||
* @param string $prefix A prefix for models.
|
||||
*
|
||||
* @return array An array with directory elements. If prefix is equal to '', all directories are returned.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public static function addIncludePath($path = '', $prefix = '')
|
||||
{
|
||||
static $paths;
|
||||
|
||||
if (!isset($paths))
|
||||
{
|
||||
$paths = array();
|
||||
}
|
||||
|
||||
if (!isset($paths[$prefix]))
|
||||
{
|
||||
$paths[$prefix] = array();
|
||||
}
|
||||
|
||||
if (!isset($paths['']))
|
||||
{
|
||||
$paths[''] = array();
|
||||
}
|
||||
|
||||
if (!empty($path))
|
||||
{
|
||||
jimport('joomla.filesystem.path');
|
||||
|
||||
if (!in_array($path, $paths[$prefix]))
|
||||
{
|
||||
array_unshift($paths[$prefix], JPath::clean($path));
|
||||
}
|
||||
|
||||
if (!in_array($path, $paths['']))
|
||||
{
|
||||
array_unshift($paths[''], JPath::clean($path));
|
||||
}
|
||||
}
|
||||
|
||||
return $paths[$prefix];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds to the stack of model table paths in LIFO order.
|
||||
*
|
||||
* @param mixed $path The directory as a string or directories as an array to add.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public static function addTablePath($path)
|
||||
{
|
||||
JTable::addIncludePath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the filename for a resource
|
||||
*
|
||||
* @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 12.2
|
||||
*/
|
||||
protected static function _createFileName($type, $parts = array())
|
||||
{
|
||||
$filename = '';
|
||||
|
||||
switch ($type)
|
||||
{
|
||||
case 'model':
|
||||
$filename = strtolower($parts['name']) . '.php';
|
||||
break;
|
||||
|
||||
}
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Model object, always creating it
|
||||
*
|
||||
* @param string $type The model type to instantiate
|
||||
* @param string $prefix Prefix for the model class name. Optional.
|
||||
* @param array $config Configuration array for model. Optional.
|
||||
*
|
||||
* @return mixed A model object or false on failure
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public static function getInstance($type, $prefix = '', $config = array())
|
||||
{
|
||||
$type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
|
||||
$modelClass = $prefix . ucfirst($type);
|
||||
|
||||
if (!class_exists($modelClass))
|
||||
{
|
||||
jimport('joomla.filesystem.path');
|
||||
$path = JPath::find(self::addIncludePath(null, $prefix), self::_createFileName('model', array('name' => $type)));
|
||||
if (!$path)
|
||||
{
|
||||
$path = JPath::find(self::addIncludePath(null, ''), self::_createFileName('model', array('name' => $type)));
|
||||
}
|
||||
if ($path)
|
||||
{
|
||||
require_once $path;
|
||||
|
||||
if (!class_exists($modelClass))
|
||||
{
|
||||
JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_MODELCLASS_NOT_FOUND', $modelClass), JLog::WARNING, 'jerror');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return new $modelClass($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request).
|
||||
*
|
||||
* @since 12.2
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($config = array())
|
||||
{
|
||||
// Guess the option from the class name (Option)Model(View).
|
||||
if (empty($this->option))
|
||||
{
|
||||
$r = null;
|
||||
|
||||
if (!preg_match('/(.*)Model/i', get_class($this), $r))
|
||||
{
|
||||
throw new Exception(JText::_('JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
|
||||
}
|
||||
|
||||
$this->option = 'com_' . strtolower($r[1]);
|
||||
}
|
||||
|
||||
// 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']);
|
||||
}
|
||||
elseif (defined('JPATH_COMPONENT_ADMINISTRATOR'))
|
||||
{
|
||||
$this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
|
||||
$this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR . '/table');
|
||||
}
|
||||
|
||||
// Set the internal state marker - used to ignore setting state from the request
|
||||
if (!empty($config['ignore_request']))
|
||||
{
|
||||
$this->__state_set = true;
|
||||
}
|
||||
|
||||
// Set the clean cache event
|
||||
if (isset($config['event_clean_cache']))
|
||||
{
|
||||
$this->event_clean_cache = $config['event_clean_cache'];
|
||||
}
|
||||
elseif (empty($this->event_clean_cache))
|
||||
{
|
||||
$this->event_clean_cache = 'onContentCleanCache';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array of objects from the results of database query.
|
||||
*
|
||||
* @param string $query The query.
|
||||
* @param integer $limitstart Offset.
|
||||
* @param integer $limit The number of records.
|
||||
*
|
||||
* @return array An array of results.
|
||||
*
|
||||
* @since 12.2
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected 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 JDatabaseQuery|string $query The query.
|
||||
*
|
||||
* @return integer Number of rows for query.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function _getListCount($query)
|
||||
{
|
||||
// Use fast COUNT(*) on JDatabaseQuery objects if there no GROUP BY or HAVING clause:
|
||||
if ($query instanceof JDatabaseQuery
|
||||
&& $query->type == 'select'
|
||||
&& $query->group === null
|
||||
&& $query->having === null)
|
||||
{
|
||||
$query = clone $query;
|
||||
$query->clear('select')->clear('order')->select('COUNT(*)');
|
||||
|
||||
$this->_db->setQuery($query);
|
||||
return (int) $this->_db->loadResult();
|
||||
}
|
||||
|
||||
// Otherwise fall back to inefficient way of counting all results.
|
||||
$this->_db->setQuery($query);
|
||||
$this->_db->execute();
|
||||
|
||||
return (int) $this->_db->getNumRows();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to load and return a model object.
|
||||
*
|
||||
* @param string $name The name of the view
|
||||
* @param string $prefix The class prefix. Optional.
|
||||
* @param array $config Configuration settings to pass to JTable::getInstance
|
||||
*
|
||||
* @return mixed Model object or boolean false if failed
|
||||
*
|
||||
* @since 12.2
|
||||
* @see JTable::getInstance
|
||||
*/
|
||||
protected function _createTable($name, $prefix = 'Table', $config = array())
|
||||
{
|
||||
// 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();
|
||||
}
|
||||
|
||||
return JTable::getInstance($name, $prefix, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the database driver object
|
||||
*
|
||||
* @return JDatabaseDriver
|
||||
*/
|
||||
public function getDbo()
|
||||
{
|
||||
return $this->_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
|
||||
*
|
||||
* @return string The name of the model
|
||||
*
|
||||
* @since 12.2
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
if (empty($this->name))
|
||||
{
|
||||
$r = null;
|
||||
if (!preg_match('/Model(.*)/i', get_class($this), $r))
|
||||
{
|
||||
throw new Exception(JText::_('JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
|
||||
}
|
||||
$this->name = strtolower($r[1]);
|
||||
}
|
||||
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get model state variables
|
||||
*
|
||||
* @param string $property Optional parameter name
|
||||
* @param mixed $default Optional default value
|
||||
*
|
||||
* @return object The property where specified, the state object where omitted
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function getState($property = null, $default = null)
|
||||
{
|
||||
if (!$this->__state_set)
|
||||
{
|
||||
// Protected method to auto-populate the model state.
|
||||
$this->populateState();
|
||||
|
||||
// Set the model state set flag to true.
|
||||
$this->__state_set = true;
|
||||
}
|
||||
|
||||
return $property === null ? $this->state : $this->state->get($property, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a table object, load it if necessary.
|
||||
*
|
||||
* @param string $name The table name. Optional.
|
||||
* @param string $prefix The class prefix. Optional.
|
||||
* @param array $options Configuration array for model. Optional.
|
||||
*
|
||||
* @return JTable A JTable object
|
||||
*
|
||||
* @since 12.2
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getTable($name = '', $prefix = 'Table', $options = array())
|
||||
{
|
||||
if (empty($name))
|
||||
{
|
||||
$name = $this->getName();
|
||||
}
|
||||
|
||||
if ($table = $this->_createTable($name, $prefix, $options))
|
||||
{
|
||||
return $table;
|
||||
}
|
||||
|
||||
throw new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_TABLE_NAME_NOT_SUPPORTED', $name), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
* This method should only be called once per instantiation and is designed
|
||||
* to be called on the first call to the getState() method unless the model
|
||||
* configuration flag to ignore the request is set.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @note Calling getState in this method will result in recursion.
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function populateState()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to set the database driver object
|
||||
*
|
||||
* @param JDatabaseDriver $db A JDatabaseDriver based object
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function setDbo($db)
|
||||
{
|
||||
$this->_db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to set model state variables
|
||||
*
|
||||
* @param string $property The name of the property.
|
||||
* @param mixed $value The value of the property to set or null.
|
||||
*
|
||||
* @return mixed The previous value of the property or null if not set.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function setState($property, $value = null)
|
||||
{
|
||||
return $this->state->set($property, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the cache
|
||||
*
|
||||
* @param string $group The cache group
|
||||
* @param integer $client_id The ID of the client
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function cleanCache($group = null, $client_id = 0)
|
||||
{
|
||||
$conf = JFactory::getConfig();
|
||||
$dispatcher = JEventDispatcher::getInstance();
|
||||
|
||||
$options = array(
|
||||
'defaultgroup' => ($group) ? $group : (isset($this->option) ? $this->option : JFactory::getApplication()->input->get('option')),
|
||||
'cachebase' => ($client_id) ? JPATH_ADMINISTRATOR . '/cache' : $conf->get('cache_path', JPATH_SITE . '/cache'));
|
||||
|
||||
$cache = JCache::getInstance('callback', $options);
|
||||
$cache->clean();
|
||||
|
||||
// Trigger the onContentCleanCache event.
|
||||
$dispatcher->trigger($this->event_clean_cache, $options);
|
||||
}
|
||||
}
|
374
libraries/legacy/model/list.php
Normal file
374
libraries/legacy/model/list.php
Normal file
@ -0,0 +1,374 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
defined('JPATH_PLATFORM') or die;
|
||||
|
||||
/**
|
||||
* Model class for handling lists of items.
|
||||
*
|
||||
* @package Joomla.Legacy
|
||||
* @subpackage Model
|
||||
* @since 12.2
|
||||
*/
|
||||
class JModelList extends JModelLegacy
|
||||
{
|
||||
/**
|
||||
* Internal memory based cache array of data.
|
||||
*
|
||||
* @var array
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $cache = array();
|
||||
|
||||
/**
|
||||
* Context string for the model type. This is used to handle uniqueness
|
||||
* when dealing with the getStoreId() method and caching data structures.
|
||||
*
|
||||
* @var string
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $context = null;
|
||||
|
||||
/**
|
||||
* Valid filter fields or ordering.
|
||||
*
|
||||
* @var array
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $filter_fields = array();
|
||||
|
||||
/**
|
||||
* An internal cache for the last query used.
|
||||
*
|
||||
* @var JDatabaseQuery
|
||||
* @since 12.2
|
||||
*/
|
||||
protected $query = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $config An optional associative array of configuration settings.
|
||||
*
|
||||
* @see JModelLegacy
|
||||
* @since 12.2
|
||||
*/
|
||||
public function __construct($config = array())
|
||||
{
|
||||
parent::__construct($config);
|
||||
|
||||
// Add the ordering filtering fields white list.
|
||||
if (isset($config['filter_fields']))
|
||||
{
|
||||
$this->filter_fields = $config['filter_fields'];
|
||||
}
|
||||
|
||||
// Guess the context as Option.ModelName.
|
||||
if (empty($this->context))
|
||||
{
|
||||
$this->context = strtolower($this->option . '.' . $this->getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to cache the last query constructed.
|
||||
*
|
||||
* This method ensures that the query is constructed only once for a given state of the model.
|
||||
*
|
||||
* @return JDatabaseQuery A JDatabaseQuery object
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function _getListQuery()
|
||||
{
|
||||
// Capture the last store id used.
|
||||
static $lastStoreId;
|
||||
|
||||
// Compute the current store id.
|
||||
$currentStoreId = $this->getStoreId();
|
||||
|
||||
// If the last store id is different from the current, refresh the query.
|
||||
if ($lastStoreId != $currentStoreId || empty($this->query))
|
||||
{
|
||||
$lastStoreId = $currentStoreId;
|
||||
$this->query = $this->getListQuery();
|
||||
}
|
||||
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an array of data items.
|
||||
*
|
||||
* @return mixed An array of data items on success, false on failure.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
// Get a storage key.
|
||||
$store = $this->getStoreId();
|
||||
|
||||
// Try to load the data from internal storage.
|
||||
if (isset($this->cache[$store]))
|
||||
{
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
// Load the list items.
|
||||
$query = $this->_getListQuery();
|
||||
|
||||
try
|
||||
{
|
||||
$items = $this->_getList($query, $this->getStart(), $this->getState('list.limit'));
|
||||
}
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add the items to the internal cache.
|
||||
$this->cache[$store] = $items;
|
||||
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a JDatabaseQuery object for retrieving the data set from a database.
|
||||
*
|
||||
* @return JDatabaseQuery A JDatabaseQuery object to retrieve the data set.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function getListQuery()
|
||||
{
|
||||
$db = $this->getDbo();
|
||||
$query = $db->getQuery(true);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a JPagination object for the data set.
|
||||
*
|
||||
* @return JPagination A JPagination object for the data set.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function getPagination()
|
||||
{
|
||||
// Get a storage key.
|
||||
$store = $this->getStoreId('getPagination');
|
||||
|
||||
// Try to load the data from internal storage.
|
||||
if (isset($this->cache[$store]))
|
||||
{
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
// Create the pagination object.
|
||||
$limit = (int) $this->getState('list.limit') - (int) $this->getState('list.links');
|
||||
$page = new JPagination($this->getTotal(), $this->getStart(), $limit);
|
||||
|
||||
// Add the object to the internal cache.
|
||||
$this->cache[$store] = $page;
|
||||
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a store id based on the model configuration state.
|
||||
*
|
||||
* This is necessary because the model is used by the component and
|
||||
* different modules that might need different sets of data or different
|
||||
* ordering requirements.
|
||||
*
|
||||
* @param string $id An identifier string to generate the store id.
|
||||
*
|
||||
* @return string A store id.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function getStoreId($id = '')
|
||||
{
|
||||
// Add the list state to the store id.
|
||||
$id .= ':' . $this->getState('list.start');
|
||||
$id .= ':' . $this->getState('list.limit');
|
||||
$id .= ':' . $this->getState('list.ordering');
|
||||
$id .= ':' . $this->getState('list.direction');
|
||||
|
||||
return md5($this->context . ':' . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the total number of items for the data set.
|
||||
*
|
||||
* @return integer The total number of items available in the data set.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function getTotal()
|
||||
{
|
||||
// Get a storage key.
|
||||
$store = $this->getStoreId('getTotal');
|
||||
|
||||
// Try to load the data from internal storage.
|
||||
if (isset($this->cache[$store]))
|
||||
{
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
// Load the total.
|
||||
$query = $this->_getListQuery();
|
||||
try
|
||||
{
|
||||
$total = (int) $this->_getListCount($query);
|
||||
}
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add the total to the internal cache.
|
||||
$this->cache[$store] = $total;
|
||||
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the starting number of items for the data set.
|
||||
*
|
||||
* @return integer The starting number of items available in the data set.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function getStart()
|
||||
{
|
||||
$store = $this->getStoreId('getstart');
|
||||
|
||||
// Try to load the data from internal storage.
|
||||
if (isset($this->cache[$store]))
|
||||
{
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
$start = $this->getState('list.start');
|
||||
$limit = $this->getState('list.limit');
|
||||
$total = $this->getTotal();
|
||||
if ($start > $total - $limit)
|
||||
{
|
||||
$start = max(0, (int) (ceil($total / $limit) - 1) * $limit);
|
||||
}
|
||||
|
||||
// Add the total to the internal cache.
|
||||
$this->cache[$store] = $start;
|
||||
|
||||
return $this->cache[$store];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to auto-populate the model state.
|
||||
*
|
||||
* This method should only be called once per instantiation and is designed
|
||||
* to be called on the first call to the getState() method unless the model
|
||||
* configuration flag to ignore the request is set.
|
||||
*
|
||||
* Note. Calling getState in this method will result in recursion.
|
||||
*
|
||||
* @param string $ordering An optional ordering field.
|
||||
* @param string $direction An optional direction (asc|desc).
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
protected function populateState($ordering = null, $direction = null)
|
||||
{
|
||||
// If the context is set, assume that stateful lists are used.
|
||||
if ($this->context)
|
||||
{
|
||||
$app = JFactory::getApplication();
|
||||
|
||||
$value = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'), 'uint');
|
||||
$limit = $value;
|
||||
$this->setState('list.limit', $limit);
|
||||
|
||||
$value = $app->getUserStateFromRequest($this->context . '.limitstart', 'limitstart', 0);
|
||||
$limitstart = ($limit != 0 ? (floor($value / $limit) * $limit) : 0);
|
||||
$this->setState('list.start', $limitstart);
|
||||
|
||||
// Check if the ordering field is in the white list, otherwise use the incoming value.
|
||||
$value = $app->getUserStateFromRequest($this->context . '.ordercol', 'filter_order', $ordering);
|
||||
if (!in_array($value, $this->filter_fields))
|
||||
{
|
||||
$value = $ordering;
|
||||
$app->setUserState($this->context . '.ordercol', $value);
|
||||
}
|
||||
$this->setState('list.ordering', $value);
|
||||
|
||||
// Check if the ordering direction is valid, otherwise use the incoming value.
|
||||
$value = $app->getUserStateFromRequest($this->context . '.orderdirn', 'filter_order_Dir', $direction);
|
||||
if (!in_array(strtoupper($value), array('ASC', 'DESC', '')))
|
||||
{
|
||||
$value = $direction;
|
||||
$app->setUserState($this->context . '.orderdirn', $value);
|
||||
}
|
||||
$this->setState('list.direction', $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->setState('list.start', 0);
|
||||
$this->state->set('list.limit', 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of a user state variable and sets it in the session
|
||||
*
|
||||
* This is the same as the method in JApplication except that this also can optionally
|
||||
* force you back to the first page when a filter has changed
|
||||
*
|
||||
* @param string $key The key of the user state variable.
|
||||
* @param string $request The name of the variable passed in a request.
|
||||
* @param string $default The default value for the variable if not found. Optional.
|
||||
* @param string $type Filter for the variable, for valid values see {@link JFilterInput::clean()}. Optional.
|
||||
* @param boolean $resetPage If true, the limitstart in request is set to zero
|
||||
*
|
||||
* @return The request user state.
|
||||
*
|
||||
* @since 12.2
|
||||
*/
|
||||
public function getUserStateFromRequest($key, $request, $default = null, $type = 'none', $resetPage = true)
|
||||
{
|
||||
$app = JFactory::getApplication();
|
||||
$input = $app->input;
|
||||
$old_state = $app->getUserState($key);
|
||||
$cur_state = (!is_null($old_state)) ? $old_state : $default;
|
||||
$new_state = $input->get($request, null, $type);
|
||||
|
||||
if (($cur_state != $new_state) && ($resetPage))
|
||||
{
|
||||
$input->set('limitstart', 0);
|
||||
}
|
||||
|
||||
// Save the new value only if it is set in this request.
|
||||
if ($new_state !== null)
|
||||
{
|
||||
$app->setUserState($key, $new_state);
|
||||
}
|
||||
else
|
||||
{
|
||||
$new_state = $cur_state;
|
||||
}
|
||||
|
||||
return $new_state;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user