Your IP : 216.73.216.240
<?php
/*
* @package TF Components
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace TechFry\Library\Model\Front;
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\ItemModel;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use TechFry\Library\Model\TfModelTrait;
class TfModelItem extends ItemModel
{
use TfModelTrait;
public $json_fields = [];
public $select_fields;
public $left_joins = [];
public $join_tables = array(); // Depreciated
public $where = array();
public function __construct($config = [], ?MVCFactoryInterface $factory = null)
{
parent::__construct($config, $factory);
$this->app = Factory::getApplication();
$this->input = $this->app->input;
$this->today = Factory::getDate();
$this->date_sql = $this->today->toSql();
$this->date_unix = $this->today->toUnix();
$this->date_lc3 = $this->today->format(Text::_('DATE_FORMAT_LC3'));
$this->juser = $this->app->getIdentity();
$this->user_levels = $this->juser->getAuthorisedViewLevels();
$this->cparams = ComponentHelper::getParams($this->option);
$this->url = Uri::getInstance()->toString();
$this->post = $this->input->post->getArray();
$this->get = $this->input->get->getArray();
}
// 01. Get single item
public function getItem($pk = null)
{
if ($pk == null)
{
$input = Factory::getApplication()->input;
$pk = $input->get('id', 0, 'int');
}
if (empty($this->select_fields))
{
$this->select_fields = 'a.*';
}
$char = 'a';
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select($this->select_fields)
->from($db->quoteName('#__' . $this->table_name, $char))
->where($db->quoteName('a.id') . ' = '. $db->quote($pk));
// Where conditions
foreach ($this->where as $condition)
{
$query->andWhere($condition);
}
// Join over other tables (Depreciated)
foreach ($this->join_tables as $table)
{
$char++;
$query->select($char . '.' . $table[1])
->join('LEFT', $db->quoteName('#__' . $table[0], $char) . ' ON a.' . $table[2] . ' = ' . $char . '.id');
}
// Join over other tables
if (!empty($this->left_joins))
{
foreach ($this->left_joins as $join)
{
$char++;
$join_as = isset($join['as']) && $join['as'] ? $join['as'] : $char;
$query->join('LEFT', $db->quoteName('#__' . $join['table'], $join_as), $db->quoteName($join['on1']) . ' = ' . $db->quoteName($join['on2']));
}
}
//echo $query;
$db->setQuery($query);
$row = $db->loadObject();
// Raise 404 Error for Unpublished
if (!$row || $row->published < 1)
{
throw new \Exception(Text::_('JERROR_PAGE_NOT_FOUND'), 404);
}
// Convert JSON strings to Objects
foreach ($this->json_fields as $field)
{
$row->$field = json_decode($row->$field);
}
return $row;
}
// 02. Increment hits
public function record_hits($pk = null)
{
if (is_null($pk))
{
$input = Factory::getApplication()->input;
$pk = $input->get('id', 0, 'int');
}
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->update('#__' . $this->table_name)
->set($db->quoteName('hits') . ' = (' . $db->quoteName('hits') . ' + 1)')
->where($db->quoteName('id') . ' = '. $db->quote($pk));
$db->setQuery($query);
$db->execute();
}
}