Files
depot/admin/src/Model/DepotsModel.php
2025-08-31 22:22:52 +02:00

189 lines
5.6 KiB
PHP

<?php
namespace KW4NZ\Component\Jron\Administrator\Model;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Table\Table;
use Joomla\Database\ParameterType;
//use Joomla\CMS\Factory;
//use Joomla\Database\DatabaseInterface;
//use Joomla\CMS\Language\Associations;
\defined('_JEXEC') or die;
class JronitemsModel extends ListModel {
public function __construct($config = array())
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = [
'id', 'a.id',
'component_name',
'alias',
'description',
'quantity',
'quantity_exp',
'asset_id',
'state',
'ordering',
'author',
'created',
'created_by',
'checked_out',
'checked_out_time',
'modifier',
'language',
'lft',
'access',
'association',
'category_id',
'published',
'level', 'c.level',
];
}
parent::__construct($config);
}
protected function populateState($ordering = null, $direction = null) {
$app = Factory::getApplication();
// Adjust the context to support modal layouts.
if ($layout = $app->input->get('layout')) {
$this->context .= '.' . $layout;
}
// Adjust the context to support forced languages.
$forcedLanguage = $app->input->get('forcedLanguage', '', 'CMD');
if ($forcedLanguage) {
$this->context .= '.' . $forcedLanguage;
}
parent::populateState($ordering, $direction);
// If there's a forced language then define that filter for the query where clause
if (!empty($forcedLanguage)) {
$this->setState('filter.language', $forcedLanguage);
}
}
/**
* Build an SQL query to load the list data.
*
* @return \Joomla\Database\DatabaseQuery
*
* @since 1.6
*/
protected function getListQuery() {
// Initialize variables.
$db = $this->getDatabase();
$query = $db->getQuery(true);
$user = Factory::getApplication()->getIdentity();
// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
[
$db->quoteName('a.id'),
$db->quoteName('a.component_name'),
$db->quoteName('a.alias'),
$db->quoteName('a.description'),
$db->quoteName('a.quantity'),
$db->quoteName('a.quantity_exp'),
$db->quoteName('a.asset_id'),
$db->quoteName('a.checked_out'),
$db->quoteName('a.checked_out_time'),
$db->quoteName('a.category_id'),
$db->quoteName('a.state'),
$db->quoteName('a.ordering'),
]
)
)
/*
$query->select('a.id as id, a.name as name, a.quantity as quantity,
a.quantity_exp as quantity_exp, a.manufacturer_id as manufacturer_id,
a.datasheet_id as datasheet_id, a.datasheet_alt as datasheet_alt,
a.stock_id as stock_id,
a.greeting as greeting,
a.state as state, a.created as created, a.access as access,
a.modified as modified,
a.checked_out as checked_out, a.checked_out_time as checked_out_time, a.catid as catid,
a.lft as lft, a.rgt as rgt, a.parent_id as parent_id, a.level as level, a.path as path,
a.image as imageInfo, a.latitude as latitude, a.longitude as longitude, a.alias as alias, a.language as language')
->from($db->quoteName('#__jron', 'a'));
*/
->select(
[
$db->quoteName('u.name', 'editor'),
$db->quoteName('c.title', 'category_title'),
// @TODO add manufacturers and stocks too
]
)
->from($db->quoteName('#__depot', 'a'))
->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('a.checked_out'))
->join('LEFT', $db->quoteName('#__categories', 'c'), $db->quoteName('c.id') . ' = ' . $db->quoteName('a.category_id'))
// @TODO join also with manufacturers and stocks
;
//
$published = (string) $this->getState('filter.published');
if (is_numeric($published)) {
$published = (int) $published;
$query->where($db->quoteName('a.state') . ' = :published')
->bind(':published', $published, ParameterType::INTEGER);
} elseif ($published === '') {
$query->where($db->quoteName('a.state') . ' IN (0, 1)');
}
// Filter by category
$category_id = $this->getState('filter.category_id');
if (is_numeric($category_id)) {
$category_id = (int) $category_id;
$query->where($db->quoteName('a.category_id') . ' = :category_id')
->bind(':category_id', $category_id, ParameterType::INTEGER);
}
// @TODO Filter by manufacturer and stock
// Filter by search in title
if ($search = $this->getState('filter.search')) {
if (stripos($search, 'id:') === 0) {
$search = (int) substr($search, 3);
$query->where($db->quoteName('a.id') . ' = :search')
->bind(':search', $search, ParameterType::INTEGER);
} else {
$search = '%' . str_replace(' ', '%', trim($search)) . '%';
$query->where('(' . $db->quoteName('a.component_name') . ' LIKE :search1 OR ' . $db->quoteName('a.alias') . ' LIKE :search2)')
->bind([':search1', ':search2'], $search);
}
}
// Filter on the level.
if ($level = (int) $this->getState('filter.level')) {
$query->where($db->quoteName('c.level') . ' <= :level')
->bind(':level', $level, ParameterType::INTEGER);
}
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering', 'a.component_name');
$orderDirn = $this->state->get('list.direction', 'ASC');
if ($orderCol === 'a.ordering' || $orderCol === 'category_title') {
$ordering = [
$db->quoteName('c.title') . ' ' . $db->escape($orderDirn),
$db->quoteName('a.ordering') . ' ' . $db->escape($orderDirn),
];
} else {
if ($orderCol === 'client_name') {
$orderCol = 'cl.name';
}
$ordering = $db->escape($orderCol) . ' ' . $db->escape($orderDirn);
}
$query->order($ordering);
return $query;
}
}