Your IP : 216.73.216.240


Current Path : /home/juvelize/martine/administrator/components/com_easybookreloaded/models/
Upload File :
Current File : /home/juvelize/martine/administrator/components/com_easybookreloaded/models/easybookreloaded.php

<?php

/**
 * @copyright
 * @package    Easybook Reloaded - EBR for Joomla! 3.x
 * @author     Viktor Vogel <admin@kubik-rubik.de>
 * @version    3.4.1.1-FREE - 2021-08-29
 * @link       https://kubik-rubik.de/ebr-easybook-reloaded
 *
 * @license    GNU/GPL
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') || die('Restricted access');

use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\{Factory, Pagination\Pagination};

/**
 * Class EasybookReloadedModelEasybookReloaded
 *
 * @since   3.4.0-FREE
 * @version 3.4.1.0-FREE
 */
class EasybookReloadedModelEasybookReloaded extends BaseDatabaseModel
{
    /**
     * @var object $data
     * @since 3.4.0-FREE
     */
    protected $data;

    /**
     * @var int $total
     * @since 3.4.0-FREE
     */
    protected $total;

    /**
     * @var object $pagination
     * @since 3.4.0-FREE
     */
    protected $pagination;

    /**
     * EasybookReloadedModelEasybookReloaded constructor.
     *
     * @throws Exception
     * @since 3.4.0-FREE
     */
    public function __construct()
    {
        parent::__construct();

        $app = Factory::getApplication();
        $limit = (int)$app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'), 'int');
        $limitStart = $app->getUserStateFromRequest('easybookreloaded.limitstart', 'limitstart', 0, 'int');
        $limitStart = ($limit !== 0 ? (floor($limitStart / $limit) * $limit) : 0);
        $search = $app->getUserStateFromRequest('easybookreloaded.filter.search', 'filter_search');
        $gbId = $app->getUserStateFromRequest('easybookreloaded.filter.gb_id', 'filter_gb_id');

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitStart);
        $this->setState('filter.search', $search);
        $this->setState('filter.gb_id', $gbId);
    }

    /**
     * Gets the database query
     *
     * @return object|object[]
     * @since 3.4.0-FREE
     */
    public function getData()
    {
        if (empty($this->data)) {
            $query = $this->buildQuery();
            $this->data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->data;
    }

    /**
     * Builds the database query
     *
     * @return JDatabaseQuery|string
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    private function buildQuery()
    {
        $query = $this->_db->getQuery(true);
        $query->select('a.*');
        $query->select('b.title as gbid_title');
        $query->from('#__easybook AS a');
        $query->from('#__easybook_gb AS b');
        $query->where('(a.gbid = b.id)');

        // Is a search term provided?
        $search = $this->getState('filter.search');

        if (!empty($search)) {
            $search = $this->_db->quote('%' . $this->_db->escape($search, true) . '%');
            $query->where('((a.gbname LIKE ' . $search . ') OR (a.gbmail LIKE ' . $search . ') OR (a.gbtext LIKE ' . $search . ') OR (a.gbtitle LIKE ' . $search . ') OR (a.gbcomment LIKE ' . $search . '))');
        }

        // Is a category selected?
        $gbId = $this->getState('filter.gb_id');

        if (!empty($gbId)) {
            $query->where('(a.gbid = ' . (int)$gbId . ')');
        }

        $query->order($this->_db->escape('id DESC'));

        return $query;
    }

    /**
     * Gets the pagination object
     *
     * @return Pagination|object
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function getPagination()
    {
        if (empty($this->pagination)) {
            $this->pagination = new Pagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->pagination;
    }

    /**
     * Gets the total number of entries
     *
     * @return int
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function getTotal(): int
    {
        if (empty($this->total)) {
            $query = $this->buildQuery();
            $this->total = $this->_getListCount($query);
        }

        return $this->total;
    }
}