Your IP : 216.73.216.240


Current Path : /home/j/u/v/juvelize/martine/components/com_easybookreloaded/helpers/
Upload File :
Current File : /home/j/u/v/juvelize/martine/components/com_easybookreloaded/helpers/Route.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/>.
 */

namespace EasybookReloaded;

defined('_JEXEC') || die('Restricted access');

use Exception;
use Joomla\CMS\{Factory, Component\ComponentHelper, Menu\AbstractMenu};

/**
 * Class Route
 *
 * @package EasybookReloaded
 *
 * @since   3.4.0-FREE
 * @version 3.4.1.0-FREE
 */
class Route
{
    /**
     * Creates correct URL to the entry
     *
     * @param int $id
     * @param int $gbId
     *
     * @return string
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public static function getRoute(int $id, int $gbId): string
    {
        $itemId = self::getItemId($gbId);
        $limit = self::getLimitstart($id, $gbId);

        $link = 'index.php?option=com_easybookreloaded&view=easybookreloaded';
        $link .= '&gbid=' . $gbId;
        $link .= '&Itemid=' . $itemId;

        if ($limit !== 0) {
            $link .= '&limitstart=' . $limit;
        }

        $link .= '#gbentry_' . $id;

        return $link;
    }

    /**
     * Gets the Item ID of the component - the Item ID is the ID from the menu entry
     *
     * @param int $gbId
     *
     * @return bool|int
     * @throws Exception
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public static function getItemId(int $gbId = 1)
    {
        static $itemId = [];

        if (empty($itemId[$gbId])) {
            $db = Factory::getDbo();
            $query = "SELECT " . $db->quoteName('id') . " FROM " . $db->quoteName('#__menu') . " WHERE " . $db->quoteName('link') . " = 'index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=" . $gbId . "' AND " . $db->quoteName('published') . " = 1";
            $db->setQuery($query);
            $itemId[$gbId] = (int)$db->loadResult();
        }

        if ($itemId[$gbId] !== 0) {
            return $itemId[$gbId];
        }

        $itemIdRequest = Factory::getApplication()->input->getInt('Itemid');

        if ($itemIdRequest !== 0 || ($itemIdRequest === $itemId[$gbId])) {
            return $itemIdRequest;
        }

        return false;
    }

    /**
     * Gets limitstart value to set the correct page with the entry
     *
     * @param int $id
     * @param int $gbId
     *
     * @return int
     * @since   3.4.0-FREE
     * @version 3.4.1.0-FREE
     */
    public static function getLimitstart(int $id, int $gbId): int
    {
        $entriesPerPage = (int)Helper::getParams('entriesPerPage', 5);
        $order = Helper::getParams('entriesOrder', 'DESC');

        static $result = [];

        if (empty($result[$gbId])) {
            $db = Factory::getDbo();
            $query = "SELECT * FROM " . $db->quoteName('#__easybook') . " WHERE " . $db->quoteName('published') . " AND " . $db->quoteName('gbid') . " = " . $gbId . " ORDER BY " . $db->quoteName('id') . " " . $order;
            $db->setQuery($query);
            $result[$gbId] = $db->loadRowList();
        }

        $entryKey = 0;

        foreach ($result[$gbId] as $entryKey => $entryValue) {
            if ((int)$entryValue[0] === $id) {
                break;
            }
        }

        $limit = $entriesPerPage * (int)($entryKey / $entriesPerPage);

        return (int)$limit;
    }

    /**
     * Creates correct URL with the task for the hash link in the notification mail
     *
     * @param string $task
     * @param int    $gbId
     *
     * @return string
     * @throws Exception
     * @since 3.4.0-FREE
     */
    public static function getRouteHash(string $task, int $gbId): string
    {
        $link = 'index.php?option=com_easybookreloaded&task=';

        // Add the task to the URL
        $link .= $task;

        // Add the GB ID and Item ID to the URL
        $link .= '&gbid=' . $gbId;
        $link .= '&Itemid=' . self::getItemId($gbId);
        $link .= '&hash=';

        return $link;
    }

    /**
     * Gets the guestbook ID from the menu using the Item ID
     *
     * @return int
     * @throws Exception
     * @since 3.4.1.0-FREE
     */
    public static function getGbId(): int
    {
        return (int)Factory::getSession()->get('gbid', 0, 'easybookreloaded');
    }
}