Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Model/Front/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Model/Front/TfModelList.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\Model\Front;

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\ListModel;
use TechFry\Library\Model\TfModelTrait;

class TfModelList extends ListModel
{
    use TfModelTrait, TfSearchTrait;

    public $select_fields = [];

    public $left_joins = [];
    public $inner_joins = [];

    public $where = [];
    public $or_where = [];
    public $and_where = [];

    public $orderby;

    public $orderCol = 'a.id';
    public $orderDirn = 'DESC';

    public $sorting_fields = [];

    public function __construct($config = [], ?MVCFactoryInterface $factory = null)
	{
		// $this->context : Option.ModelName

        if ($this->sorting_fields)
        {
            foreach ($this->sorting_fields as $sorting_field)
            {
                $config['filter_fields'][] = 'a.' . $sorting_field;
            }
        }
        
        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. Query for list of items
    protected function getListQuery()
    {
        if (empty($this->select_fields))
        {
            $this->select_fields = '*';
        }

        // Get table columns
        $db = Factory::getDbo();
        $cols = $db->getTableColumns('#__' . $this->table_name);

        $char = 'a';

        $now_date = Factory::getDate()->toSql();

        // Initialize variables
        $db = Factory::getDbo();
        $query = $db->getQuery(true);

        $query->select($this->select_fields)
            ->from($db->quoteName('#__' . $this->table_name, $char));

        if (array_key_exists('published', $cols))
        {
            $query->where($db->quoteName('a.published') . ' = ' . $db->quote(1));
        }

        // Filter by start and end dates
        if (array_key_exists('publish_up', $cols))
        {
            $query->where(
            [
                '(' . $db->quoteName('a.publish_up') . ' IS NULL OR ' . $db->quoteName('a.publish_up') . ' <= :publishUp)',
                '(' . $db->quoteName('a.publish_down') . ' IS NULL OR ' . $db->quoteName('a.publish_down') . ' >= :publishDown)',
            ])
            ->bind(':publishUp', $now_date)
            ->bind(':publishDown', $now_date);
        }

        // Search Conditions
        $search_conditions = $this->handle_search();
        foreach ($search_conditions as $condition)
        {
            $query->where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2]));
        }

        // 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']));
            }
        }

        if (!empty($this->inner_joins))
        {
            foreach ($this->inner_joins as $join)
            {
                $char++;
                if (isset($join['as']))
                {
                    $join_as = $join['as'];
                }
                else
                {
                    $join_as = $char;
                }

                $query->join('INNER', $db->quoteName('#__' . $join['table'], $char), $db->quoteName($join['on1']) . ' = ' . $db->quoteName($join['on2']));
            }
        }

        // Where Conditions
        foreach ($this->where as $condition)
        {
            if (\is_array($condition))
            {
                $query->Where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2]));
            }
            else
            {
                $query->Where($condition);
            }
        }

        // Where Conditions (OR)
        foreach ($this->or_where as $condition)
        {
            $query->orWhere($condition);
        }

        // Where Conditions (AND)
        foreach ($this->and_where as $condition)
        {
            $query->andWhere($condition);
        }

        // Ordering (Remove After Shifting to State Variables)
        if (!empty($this->orderby))
        {
            $query->order($this->orderby);
        }
        else
        {
            $orderCol = $this->state->get('list.ordering', $this->orderCol);
            $orderDirn = $this->state->get('list.direction', $this->orderDirn);

            if (strtoupper($orderDirn) == 'RAND()')
            {
                $query->order($db->escape($orderDirn));
            }
            else
            {
                $query->order($db->escape($orderCol) . ' ' . $db->escape($orderDirn));
            }
        }

        //echo $query;

        return $query;
    }

    // 02. 
    protected function populateState($ordering = null, $direction = null)
    {
        $app = Factory::getApplication();
        $input = $app->getInput();

        // List state information
        $value = $input->get('limit', $app->get('list_limit', 0), 'uint');
        $this->setState('list.limit', $value);

        $value = $input->get('limitstart', 0, 'uint');
        $this->setState('list.start', $value);
        
        parent::populateState($this->orderCol, $this->orderDirn);
    }

    // 03. Count total items in table where column = value
    public function count_items($table, $column, $value)
    {
        $db = Factory::getDbo();
        $query = $db->getQuery(true);

        $query->select('COUNT(*)')
            ->from($db->quoteName('#__' . $table))
            ->where($db->quoteName($column) . ' = ' . $db->quote($value))
            ->andWhere($db->quoteName('published') . ' = ' . $db->quote('1'));

        $db->setQuery($query);

        $total = $db->loadResult();

        return $total;
    }
}