Current File : /home/pacjaorg/public_html/km/administrator/components/com_djclassifieds/models/categories.php |
<?php
/**
* @package DJ-Classifieds
* @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
* @license http://www.gnu.org/licenses GNU/GPL
* @author url: http://dj-extensions.com
* @author email: contact@dj-extensions.com
*/
defined ('_JEXEC') or die('Restricted access');
class DjClassifiedsModelCategories extends JModelList
{
public function __construct($config = array())
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = array(
'name', 'c.name',
'id', 'c.id',
'price', 'c.price',
'published', 'c.published',
'ordering', 'c.ordering',
'category'
);
}
parent::__construct($config);
}
protected function populateState($ordering = null, $direction = null)
{
$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
$this->setState('filter.search', $search);
$published = $this->getUserStateFromRequest($this->context.'.filter.published', 'filter_published', '');
$this->setState('filter.published', $published);
$category = $this->getUserStateFromRequest($this->context.'.filter.category', 'filter_category', '');
$this->setState('filter.category', $category);
// List state information.
parent::populateState('name', 'asc');
$order = $this->getUserStateFromRequest($this->context.'.filter.order', 'filter_order');
if($order){
$this->setState('list.ordering', $order);
}
$order_dir = $this->getUserStateFromRequest($this->context.'.filter.order_Dir', 'filter_order_Dir');
if($order_dir){
$this->setState('list.direction', $order_dir);
}
}
protected function getStoreId($id = '')
{
// Compile the store id.
$id .= ':'.$this->getState('filter.search');
$id .= ':'.$this->getState('filter.category');
$id .= ':'.$this->getState('filter.published');
return parent::getStoreId($id);
}
public function _buildWhere()
{
$where = '';
$category = $this->getState('filter.category');
if ($category != '') {
$where = ' AND c.parent_id = ' . (int) $category;
}
$search = $this->getState('filter.search');
if (!empty($search)) {
$db = JFactory::getDBO();
$search_id = $db->Quote($db->escape($search, true));
$search = $db->Quote('%'.$db->escape($search, true).'%');
$where .= " AND (CONCAT_WS(' ', c.name, c.alias, c.description) LIKE ".$search." OR c.id=".$search_id.") ";
}
$published = $this->getState('filter.published');
if (is_numeric($published)) {
$where .= ' AND c.published = ' . (int) $published;
}
return $where;
}
protected function getListQuery()
{
$orderCol = $this->getState('list.ordering');
$orderDirn = $this->getState('list.direction');
$query = "SELECT c.*, cc.name as parent_name FROM #__djcf_categories c "
."LEFT JOIN #__djcf_categories cc ON c.parent_id=cc.id "
."WHERE 1 ".$this->_buildWhere()." ORDER BY c.".$orderCol.' '.$orderDirn;
return $query;
}
function getItems()
{
if($this->getState('filter.category') != '' || $this->getState('filter.search') != '' || $this->getState('filter.published') != ''){
return parent::getItems();
}else{
$orderCol = $this->getState('list.ordering');
$orderDirn = $this->getState('list.direction');
$limitstart = $this->getState('list.start', 0);
$limit = $this->getState('list.limit', 0);
$categories = DJClassifiedsCategory::getCatAll(0, $orderCol, $orderDirn);
if($limit){
$categories = array_slice($categories, $limitstart, $limit);
}else{
$categories = array_slice($categories, 0);
}
return $categories;
}
}
function getBatchForm()
{
return JForm::getInstance('batch_categories', JPATH_ROOT.'/administrator/components/com_djclassifieds/models/forms/batch_categories.xml');
}
}