10 defined(
'JPATH_PLATFORM') or die;
27 protected $cache = array();
36 protected $context = null;
44 protected $filter_fields = array();
52 protected $query = array();
60 protected $filterFormName = null;
67 protected $htmlFormName =
'adminForm';
77 public function __construct($config = array())
79 parent::__construct($config);
82 if (isset($config[
'filter_fields']))
84 $this->filter_fields = $config[
'filter_fields'];
88 if (empty($this->context))
90 $this->context = strtolower($this->option .
'.' . $this->getName());
103 protected function _getListQuery()
109 $currentStoreId = $this->getStoreId();
112 if ($lastStoreId != $currentStoreId || empty($this->query))
114 $lastStoreId = $currentStoreId;
115 $this->query = $this->getListQuery();
128 public function getActiveFilters()
130 $activeFilters = array();
132 if (!empty($this->filter_fields))
134 foreach ($this->filter_fields as $filter)
136 $filterName =
'filter.' . $filter;
138 if (property_exists($this->state, $filterName) && (!empty($this->state->{$filterName}) || is_numeric($this->state->{$filterName})))
140 $activeFilters[$filter] = $this->state->get($filterName);
145 return $activeFilters;
155 public function getItems()
158 $store = $this->getStoreId();
161 if (isset($this->cache[$store]))
163 return $this->cache[$store];
167 $query = $this->_getListQuery();
171 $items = $this->_getList($query, $this->getStart(), $this->getState(
'list.limit'));
173 catch (RuntimeException $e)
175 $this->setError($e->getMessage());
181 $this->cache[$store] = $items;
183 return $this->cache[$store];
193 protected function getListQuery()
195 $db = $this->getDbo();
196 $query = $db->getQuery(
true);
208 public function getPagination()
211 $store = $this->getStoreId(
'getPagination');
214 if (isset($this->cache[$store]))
216 return $this->cache[$store];
220 $limit = (int) $this->getState(
'list.limit') - (int) $this->getState(
'list.links');
221 $page =
new JPagination($this->getTotal(), $this->getStart(), $limit);
224 $this->cache[$store] = $page;
226 return $this->cache[$store];
242 protected function getStoreId($id =
'')
245 $id .=
':' . $this->getState(
'list.start');
246 $id .=
':' . $this->getState(
'list.limit');
247 $id .=
':' . $this->getState(
'list.ordering');
248 $id .=
':' . $this->getState(
'list.direction');
250 return md5($this->context .
':' . $id);
260 public function getTotal()
263 $store = $this->getStoreId(
'getTotal');
266 if (isset($this->cache[$store]))
268 return $this->cache[$store];
272 $query = $this->_getListQuery();
276 $total = (int) $this->_getListCount($query);
278 catch (RuntimeException $e)
280 $this->setError($e->getMessage());
286 $this->cache[$store] = $total;
288 return $this->cache[$store];
298 public function getStart()
300 $store = $this->getStoreId(
'getstart');
303 if (isset($this->cache[$store]))
305 return $this->cache[$store];
308 $start = $this->getState(
'list.start');
309 $limit = $this->getState(
'list.limit');
310 $total = $this->getTotal();
312 if ($start > $total - $limit)
314 $start = max(0, (
int) (ceil($total / $limit) - 1) * $limit);
318 $this->cache[$store] = $start;
320 return $this->cache[$store];
333 public function getFilterForm($data = array(), $loadData =
true)
338 if (empty($this->filterFormName))
340 $classNameParts = explode(
'Model', get_called_class());
342 if (count($classNameParts) == 2)
344 $this->filterFormName =
'filter_' . strtolower($classNameParts[1]);
348 if (!empty($this->filterFormName))
351 $form = $this->loadForm($this->context .
'.filter', $this->filterFormName, array(
'control' =>
'',
'load_data' => $loadData));
371 protected function loadForm($name, $source = null, $options = array(), $clear =
false, $xpath =
false)
377 $hash = md5($source . serialize($options));
380 if (isset($this->_forms[$hash]) && !$clear)
382 return $this->_forms[$hash];
393 if (isset($options[
'load_data']) && $options[
'load_data'])
396 $data = $this->loadFormData();
405 $this->preprocessForm($form, $data);
412 $this->setError($e->getMessage());
418 $this->_forms[$hash] = $form;
430 protected function loadFormData()
436 if (!property_exists($data,
'list'))
439 'direction' => $this->state->{
'list.direction'},
440 'limit' => $this->state->{
'list.limit'},
441 'ordering' => $this->state->{
'list.ordering'},
442 'start' => $this->state->{
'list.start'}
465 protected function populateState($ordering = null, $direction = null)
473 if ($filters = $app->getUserStateFromRequest($this->context .
'.filter',
'filter', array(),
'array'))
475 foreach ($filters as $name => $value)
477 $this->setState(
'filter.' . $name, $value);
484 if ($list = $app->getUserStateFromRequest($this->context .
'.list',
'list', array(),
'array'))
486 foreach ($list as $name => $value)
492 $orderingParts = explode(
' ', $value);
494 if (count($orderingParts) >= 2)
497 $fullDirection = end($orderingParts);
499 if (in_array(strtoupper($fullDirection), array(
'ASC',
'DESC',
'')))
501 $this->setState(
'list.direction', $fullDirection);
504 unset($orderingParts[count($orderingParts) - 1]);
507 $fullOrdering = implode(
' ', $orderingParts);
509 if (in_array($fullOrdering, $this->filter_fields))
511 $this->setState(
'list.ordering', $fullOrdering);
516 $this->setState(
'list.ordering', $ordering);
517 $this->setState(
'list.direction', $direction);
522 if (!in_array($value, $this->filter_fields))
529 if (!in_array(strtoupper($value), array(
'ASC',
'DESC',
'')))
545 $this->setState(
'list.' . $name, $value);
552 $limit = $app->getUserStateFromRequest(
'global.list.limit',
'limit', $app->getCfg(
'list_limit'),
'uint');
553 $this->setState(
'list.limit', $limit);
556 $value = $app->getUserStateFromRequest($this->context .
'.ordercol',
'filter_order', $ordering);
558 if (!in_array($value, $this->filter_fields))
561 $app->setUserState($this->context .
'.ordercol', $value);
564 $this->setState(
'list.ordering', $value);
567 $value = $app->getUserStateFromRequest($this->context .
'.orderdirn',
'filter_order_Dir', $direction);
569 if (!in_array(strtoupper($value), array(
'ASC',
'DESC',
'')))
572 $app->setUserState($this->context .
'.orderdirn', $value);
575 $this->setState(
'list.direction', $value);
578 $value = $app->getUserStateFromRequest($this->context .
'.limitstart',
'limitstart', 0);
579 $limitstart = ($limit != 0 ? (floor($value / $limit) * $limit) : 0);
580 $this->setState(
'list.start', $limitstart);
584 $this->setState(
'list.start', 0);
585 $this->setState(
'list.limit', 0);
601 protected function preprocessForm(
JForm $form, $data, $group =
'content')
604 JPluginHelper::importPlugin($group);
610 $results = $dispatcher->trigger(
'onContentPrepareForm', array($form, $data));
613 if (count($results) && in_array(
false, $results,
true))
616 $error = $dispatcher->getError();
618 if (!($error instanceof Exception))
620 throw new Exception($error);
641 public function getUserStateFromRequest($key, $request, $default = null, $type =
'none', $resetPage =
true)
644 $input = $app->input;
645 $old_state = $app->getUserState($key);
646 $cur_state = (!is_null($old_state)) ? $old_state : $default;
647 $new_state = $input->get($request, null, $type);
649 if (($cur_state != $new_state) && ($resetPage))
651 $input->set(
'limitstart', 0);
655 if ($new_state !== null)
657 $app->setUserState($key, $new_state);
661 $new_state = $cur_state;