Source for file list.php
Documentation is available at list.php
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Model class for handling lists of items.
* Internal memory based cache array of data.
* Context string for the model type. This is used to handle uniqueness
* when dealing with the getStoreId() method and caching data structures.
* Valid filter fields or ordering.
* An internal cache for the last query used.
* Name of the filter form to load
* @param array $config An optional associative array of configuration settings.
// Add the ordering filtering fields white list.
if (isset
($config['filter_fields']))
// Guess the context as Option.ModelName.
* 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
// Capture the last store id used.
// Compute the current store id.
// If the last store id is different from the current, refresh the query.
if ($lastStoreId !=
$currentStoreId ||
empty($this->query))
$lastStoreId =
$currentStoreId;
* Function to get the active filters
* @return array Associative array in the format: array('filter_published' => 0)
$activeFilters =
array();
$filterName =
'filter.' .
$filter;
$activeFilters[$filter] =
$this->state->get($filterName);
* Method to get an array of data items.
* @return mixed An array of data items on success, false on failure.
// Try to load the data from internal storage.
if (isset
($this->cache[$store]))
return $this->cache[$store];
catch
(RuntimeException $e)
// 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.
$query =
$db->getQuery(true);
* Method to get a JPagination object for the data set.
* @return JPagination A JPagination object for the data set.
// 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');
// 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
* @param string $id An identifier string to generate the store id.
* @return string A store 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');
* Method to get the total number of items for the data set.
* @return integer The total number of items available in the data set.
// Try to load the data from internal storage.
if (isset
($this->cache[$store]))
return $this->cache[$store];
catch
(RuntimeException $e)
// 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.
// Try to load the data from internal storage.
if (isset
($this->cache[$store]))
return $this->cache[$store];
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];
* @param array $data data
* @param boolean $loadData load current data
* @return JForm/false the JForm object or false
// Try to locate the filter form automatically. Example: ContentModelArticles => "filter_articles"
if (count($classNameParts) ==
2)
* 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.
protected function loadForm($name, $source =
null, $options =
array(), $clear =
false, $xpath =
false)
// Handle the optional arguments.
// Create a signature hash.
// Check if we can use a previously loaded form.
if (isset
($this->_forms[$hash]) &&
!$clear)
return $this->_forms[$hash];
if (isset
($options['load_data']) &&
$options['load_data'])
// Get the data for the form.
// Allow for additional modification of the form, and events to be triggered.
// We pass the data because plugins may require it.
// Load the data into the form after the plugins have operated.
// Store the form for later.
$this->_forms[$hash] =
$form;
* Method to get the data that should be injected in the form.
* @return mixed The data for the form.
// Check the session for previously entered form data.
// Pre-fill the list options
'direction' =>
$this->state->{'list.direction'},
'limit' =>
$this->state->{'list.limit'},
'ordering' =>
$this->state->{'list.ordering'},
'start' =>
$this->state->{'list.start'}
* 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).
protected function populateState($ordering =
null, $direction =
null)
// If the context is set, assume that stateful lists are used.
foreach ($filters as $name =>
$value)
$this->setState('filter.' .
$name, $value);
// Receive & set list options
foreach ($list as $name =>
$value)
$orderingParts =
explode(' ', $value);
if (count($orderingParts) >=
2)
// Latest part will be considered the direction
$fullDirection =
end($orderingParts);
$this->setState('list.direction', $fullDirection);
unset
($orderingParts[count($orderingParts) -
1]);
// The rest will be the ordering
$fullOrdering =
implode(' ', $orderingParts);
$this->setState('list.ordering', $fullOrdering);
$this->setState('list.ordering', $ordering);
$this->setState('list.direction', $direction);
// Just to keep the default case
$this->setState('list.' .
$name, $value);
// Keep B/C for components previous to jform forms for filters
// Check if the ordering field is in the white list, otherwise use the incoming value.
$this->setState('list.ordering', $value);
// Check if the ordering direction is valid, otherwise use the incoming value.
$this->setState('list.direction', $value);
// Support old ordering field
$oldOrdering =
$app->input->get('filter_order');
$this->setState('list.ordering', $oldOrdering);
// Support old direction field
$oldDirection =
$app->input->get('filter_order_Dir');
if (!empty($oldDirection) &&
in_array(strtoupper($oldDirection), array('ASC', 'DESC', '')))
$this->setState('list.direction', $oldDirection);
$limitstart =
($limit !=
0 ?
(floor($value /
$limit) *
$limit) :
0);
$this->setState('list.start', $limitstart);
* 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").
* @throws Exception if there is an error in the form event.
protected function preprocessForm(JForm $form, $data, $group =
'content')
// Import the appropriate plugin group.
// Trigger the form preparation event.
$results =
$dispatcher->trigger('onContentPrepareForm', array($form, $data));
// Check for errors encountered while preparing the form.
if (!($error instanceof
Exception))
throw
new Exception($error);
* 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.
$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.
Documentation generated on Tue, 19 Nov 2013 15:07:17 +0100 by phpDocumentor 1.4.3