Your IP : 216.73.216.240


Current Path : /home/juvelize/martine/components/com_easybookreloaded/models/
Upload File :
Current File : /home/juvelize/martine/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 EasybookReloaded\Helper;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\{Factory, Pagination\Pagination};

/**
 * Class EasybookReloadedModelEasybookReloaded
 *
 * @since 3.4.0-FREE
 */
class EasybookReloadedModelEasybookReloaded extends BaseDatabaseModel
{
    /**
     * @var int $gbId
     * @since 3.4.0-FREE
     */
    protected $gbId;

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

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

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

    /**
     * @var JInput $input
     * @since 3.4.0-FREE
     */
    protected $input;

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

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

        $this->input = Factory::getApplication()->input;

        // Get guestbook ID
        $this->gbId = $this->input->getInt('gbid', 0);
        Factory::getSession()->set('gbid', $this->gbId, 'easybookreloaded');

        // Single entry requested
        $this->entryId = $this->input->getInt('entryid');
    }

    /**
     * Gets all entries
     *
     * @return object[]
     * @since 3.4.0-FREE
     */
    public function getData(): array
    {
        if (empty($this->data)) {
            $query = $this->buildQuery();
            $this->data = $this->_getList($query);
        }

        return $this->data;
    }

    /**
     * Builds correct query to retrieve all needed entries
     *
     * @return string
     * @since 3.4.0-FREE
     */
    private function buildQuery(): string
    {
        $query = $this->_db->getQuery(true);
        $query->select('*');
        $query->from($this->_db->quoteName('#__easybook'));

        if (!empty($this->gbId) && is_int($this->gbId)) {
            $query->where($this->_db->quoteName('gbid') . " = " . $this->gbId);
        }

        if (!empty($this->entryId) && is_int($this->entryId)) {
            $query->where($this->_db->quoteName('id') . " = " . $this->entryId);
        }

        if (!EASYBOOK_CANEDIT) {
            $query->where($this->_db->quoteName('published') . " = 1");
        }

        $order = Helper::getParams('entriesOrder', 'DESC');

        // If type is feed, then the order has to be DESC to get the latest entries in the feed reader
        if (Factory::getDocument()->getType() === 'feed') {
            $order = 'DESC';
        }

        // Check whether limit is already set - e.g. from feed function
        $limit = $this->input->getInt('limit', 0);

        if (empty($limit)) {
            $limit = (int)Helper::getParams('entriesPerPage', 5);
        }

        $query->order($this->_db->quoteName('gbdate') . " " . $order . " LIMIT " . $this->input->getInt('limitstart', 0) . ", " . $limit);

        return $query;
    }

    /**
     * Loads the guestbook data for a specific ID
     *
     * @return array
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function getGbData(): array
    {
        $gbData = [];

        $query = $this->_db->getQuery(true);
        $query->select('*');
        $query->from($this->_db->quoteName('#__easybook_gb'));

        if (!empty($this->gbId) && is_int($this->gbId)) {
            $query->where($this->_db->quoteName('id') . " = " . $this->gbId);
        }

        $gbItems = $this->_getList($query);

        if (!empty($gbItems)) {
            foreach ($gbItems as $gbItem) {
                $gbData[$gbItem->id] = $gbItem;
            }
        }

        return $gbData;
    }

    /**
     * Creates pagination object
     *
     * @return object
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function getPagination(): object
    {
        if (empty($this->pagination)) {
            // Check whether limit is already set - e.g. from feed function
            $limit = $this->input->getInt('limit', 0);

            if (empty($limit)) {
                $limit = (int)Helper::getParams('entriesPerPage', 5);
            }

            $total = $this->getTotal();
            $this->pagination = new Pagination($total, $this->input->getInt('limitstart', 0), $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->buildCountQuery();
            $this->total = $this->_getListCount($query);
        }

        return $this->total;
    }

    /**
     * Builds the count query
     *
     * @return object
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    private function buildCountQuery(): object
    {
        $query = $this->_db->getQuery(true);
        $query->select('*');
        $query->from($this->_db->quoteName('#__easybook'));

        if (!empty($this->gbId) && is_int($this->gbId)) {
            $query->where($this->_db->quoteName('gbid') . " = " . $this->gbId);
        }

        if (!empty($this->entryId) && is_int($this->entryId)) {
            $query->where($this->_db->quoteName('id') . " = " . $this->entryId);
        }

        if (!EASYBOOK_CANEDIT) {
            $query->where($this->_db->quoteName('published') . " = 1");
        }

        return $query;
    }

    /**
     * Gets the guestbook ID
     *
     * @return int
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public function getGbId(): int
    {
        if (is_int($this->gbId) && !is_null($this->gbId)) {
            return $this->gbId;
        }

        return 0;
    }
}