<?php

/**
 * @version        1.9.5
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
// no direct access
defined('_JEXEC') or die();
use Joomla\CMS\Component\Router\RouterBase;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;


JLoader::registerPrefix('EDocman', JPATH_ROOT . '/components/com_edocman');
//require_once JPATH_ROOT . '/components/com_edocman/helper/route.php';
JLoader::register('EDocmanHelperRoute', JPATH_ROOT . '/components/com_edocman/helper/route.php');

class EdocmanRouter extends RouterBase
{
    public function build(&$query)
    {
        $objectName = '';
        $segments = [];
        $db = Factory::getDbo();
        $q = $db->getQuery(true);

        $queryArr = $query;

        $app = Factory::getApplication();
        $menu = $app->getMenu();
        $parentId = 0;
		$catId			= isset($query['catid']) ? (int)$query['catid'] : 0;
        // We need a menu item. Either the one specified in the query, or the current active one if none specified
		/*
        if (empty($query['Itemid'])) 
		{
            $menuItem = $menu->getActive();
        } 
		else 
		{
            $menuItem = $menu->getItem($query['Itemid']);
        }
		*/
		if (empty($query['Itemid']))
		{
			$menuItem = $this->menu->getActive();
		}
		else
		{
			$menuItem = $this->menu->getItem($query['Itemid']);

			// If the given menu item doesn't belong to our component, unset the Itemid from query array
			if ($menuItem && $menuItem->component != 'com_edocman')
			{
				unset($query['Itemid']);
			}
		}
        if ($menuItem) 
		{
            if (isset($menuItem->query['id']) && isset($menuItem->query['view']) && ($menuItem->query['view'] == 'categories' || $menuItem->query['view'] == 'category')) 
			{
                $parentId = (int)$menuItem->query['id'];
            }
        }
		$mView = empty($menuItem->query['view']) ? null : $menuItem->query['view'];
		$mId   = empty($menuItem->query['id']) ? null : (int) $menuItem->query['id'];

        if ($menuItem && empty($menuItem->query['view'])) 
		{
            $menuItem->query['view'] = '';
        }
        // Are we dealing with the current view which is attached to a menu item?
        if (($menuItem instanceof stdClass) && isset($query['view']) && isset($query['id']) && $menuItem->query['view'] == $query['view'] &&
            isset($query['id']) && $menuItem->query['id'] == intval($query['id'])
        ) {
            unset($query['view']);
            if (isset($query['catid'])) {
                unset($query['catid']);
            }
            unset($query['id']);
        }

        // Dealing with the catid parameter in the link to document.
        if (($menuItem instanceof stdClass) && ($menuItem->query['view'] == 'category') && isset($query['catid']) &&
            $menuItem->query['id'] == intval($query['catid'])
        ) {
            if (isset($query['catid'])) {
                unset($query['catid']);
            }
        }

        // Dealing with upload link
        if (($menuItem instanceof stdClass) && ($menuItem->query['view'] == 'document') && isset($menuItem->query['layout']) && ($menuItem->query['layout'] == 'edit')) {
            if (isset($query['view'])) {
                unset($query['view']);
            }

            if (isset($query['layout'])) {
                unset($query['layout']);
            }
        }

        $view			= isset($query['view']) ? $query['view'] : '';		
        $id				= isset($query['id']) ? (int)$query['id'] : 0;
		if ($menuItem && $mView && $mId && $mView == $view && $mId == $id)
		{
			unset($query['view']);
			if (isset($query['catid']))
			{
				unset($query['catid']);
			}
			unset($query['id']);

			$view = '';
			$id   = '';
		}
        $task			= isset($query['task']) ? $query['task'] : '';
        switch ($view) {
            case 'categories':
            case 'category':
                if ($id) {
                    $segments = array_merge($segments, EDocmanHelperRoute::getCategoriesPath($id, 'alias', true, $parentId));
                }
                if (!empty($query['format']) && $query['format'] != 'html') {
                    $segments[] = $query['format'];
                    if (!empty($query['type'])) {
                        $segments[] = $query['type'];
                    }
                }
                unset($query['id']);
				if (isset($query['layout'])) 
				{
					$segments[] = $query['layout'];
				}
				unset($query['layout']);
                $objectName = 'category';
                break;
            case 'document':
                if ($id) {
                    $segments[] = EDocmanHelperRoute::getDocumentTitle($id);
                }
                unset($query['id']);
                $config = EDocmanHelper::getConfig();
                if ($catId && $config->insert_category != 2) 
				{
                    $segments = array_merge(EDocmanHelperRoute::getCategoriesPath($catId, 'alias', true, $parentId), $segments);
                }
                if (isset($query['layout']) && $query['layout'] == 'edit') 
				{
                    if ($id) 
					{
                        $segments[] = 'Edit';
                    } 
					else 
					{
                        $segments[] = 'Upload Document';
                        $queryArr['id'] = 0;
                    }
					if($catId)
					{
						$segments[] = "catid ".$catId;
					}
                    unset($query['layout']);
                    break;
                }
                $objectName = 'document';
                break;
            case 'license':
                if ($id) {
                    $q->clear();
                    $q->select('title')
                        ->from('#__edocman_licenses')
                        ->where('id=' . $id);
                    $db->setQuery($q);
                    $segments[] = $db->loadResult();
                }
                $segments[] = 'View License';
                unset($query['id']);
                $objectName = 'license';
                break;
            case 'search':
                $segments[] = 'Search result';
                break;
            case 'documents':
                $segments[] = 'Documents List';
                break;
            case 'users':
                $segments[] = 'Users List';
                break;
			case 'editcategory':
				$config = EDocmanHelper::getConfig();
				if ($id) 
				{
					$segments[] = 'Edit category';
					$q->clear();
                    $q->select('title')
                        ->from('#__edocman_categories')
                        ->where('id=' . $id);
                    $db->setQuery($q);
					if($config->insert_category_id)
					{
						$segments[] = $id." ".$db->loadResult();
					}
					else
					{
						$segments[] = $db->loadResult();
					}
				} 
				else 
				{
					if((int)$query['addsubcat'] == 1)
					{
						$segments[] = 'Add sub category';
						unset($query['addsubcat']);
					}
					else
					{
						$segments[] = 'Add category';
					}
					if(isset($query['catid']))
                    {

                        $catId  = $query['catid'];
                        $q->clear();
                        $q->select('title')
                            ->from('#__edocman_categories')
                            ->where('id=' . $catId);
                        $db->setQuery($q);
						if($config->insert_category_id)
						{
							$segments[] = $query['catid']." ".Text::_('EDOCMAN_PARENT').' '.$db->loadResult();
						}
						else
						{
							$segments[] = Text::_('EDOCMAN_PARENT').' '.$db->loadResult();
						}
                        unset($query['catid']);
                    }
				}
				break;
            case 'managecategories':
                $segments[] = 'Manage Categories';
                break;
			case 'summary':
                $segments[] = 'Mass Download Summary';
                break;
        }

        switch ($task) {
            case 'document.download':
                if ($id) {
                    $segments[] = EDocmanHelperRoute::getDocumentTitle($id);
                }
                unset($query['id']);
                $segments[] = 'Download';
                unset($query['task']);
                $objectName = 'document';
                break;
            case 'document.viewdoc':
                if ($id) 
				{
					$segments[] = EDocmanHelperRoute::getDocumentTitle($id);
					$segments[] = 'Viewdocument';
					$segments[] = $query['id'];
                }
				unset($query['id']);
                unset($query['task']);
                $objectName = 'document';
                break;
			case 'document.forcedownload':
				if ($id) {
                    $segments[] = EDocmanHelperRoute::getDocumentTitle($id);
                }
                unset($query['id']);
                $segments[] = 'fdocument';
                unset($query['task']);
                $objectName = 'document';
				break;
            case 'document.edit':
                if ($id) {
                    $segments[] = EDocmanHelperRoute::getDocumentTitle($id);
                }
                unset($query['id']);
                $segments[] = 'Edit Document';
                unset($query['task']);
                $objectName = 'document';
                break;
			case 'search':
				$segments[] = 'Search Documents';
                unset($query['task']);
				break;
        }
        if (isset($query['view'])) {
            unset($query['view']);
        }

        if (isset($query['catid'])) {
            unset($query['catid']);
        }
        if (count($segments)) {
            // Store the query string to use in the parseRouter method

            $unProcessedVariables = array(
                'option',
                'Itemid',
                'filter_category_id',
                'filter_search',
                'filter_tag',
                'start',
                'limitstart',
                'limit',
                'download_code'
            );


            foreach ($unProcessedVariables as $variable) {
                if (isset($queryArr[$variable])) {
                    unset($queryArr[$variable]);
                }
            }

            $queryString = http_build_query($queryArr);
            $segments    = array_map('Joomla\CMS\Application\ApplicationHelper::stringURLSafe', $segments);
            $key = md5(implode('/', $segments));

            //$q = $db->getQuery(true);

			$q->clear();
			$q->select('id')
			->from('#__edocman_urls')
			->where('md5_key="' . $key . '"');
			$db->setQuery($q);
			$urlId = (int) $db->loadResult();

			if (!$urlId)
			{
				$q->clear();
				$q->insert('#__edocman_urls')
					->columns('md5_key, `query`, `object_name`, `object_id`')
					->values("'$key', '$queryString', '$objectName', '$id'");
				$db->setQuery($q);
				$db->execute();
			}
        }

        return $segments;
    }

    /**
     *
     *
     * Parse the segments of a URL.
     *
     * @param
     *            array    The segments of the URL to parse.
     *
     * @return array URL attributes to be used by the application.
     */
    public function parse(&$segments)
    {
        $vars = [];
        if (count($segments)) {
            $db = Factory::getDbo();
            $key = md5(str_replace(':', '-', implode('/', $segments)));

            $query = $db->getQuery(true);
            $query->select('`query`')
                ->from('#__edocman_urls')
                ->where('md5_key="' . $key . '"');
            $db->setQuery($query);
            $queryString = $db->loadResult();
            if ($queryString) 
			{
                parse_str(html_entity_decode($queryString), $vars);
            }
			else
			{
				$method = strtoupper(Factory::getApplication()->getInput()->getMethod());

				if ($method == 'GET')
				{
					throw new Exception('Page not found', 404);
				}
			}

            if (version_compare(JVERSION, '4.0.0-dev', 'ge')) {
                $segments = [];
            }
        }

        $item = Factory::getApplication()->getMenu()->getActive();
        if ($item) {
            if (!empty($vars['view']) && !empty($item->query['view']) && $vars['view'] == $item->query['view']) {
                foreach ($item->query as $key => $value) {
                    if ($key != 'option' && $key != 'Itemid' && !isset($vars[$key])) {
                        $vars[$key] = $value;
                    }
                }
            }
        }

        if (isset($vars['view']) && $vars['view'] == 'document') {
            if (isset($vars['layout']) && $vars['layout'] != 'edit') {
                $vars['layout'] = 'default';
            }
        }

        return $vars;
    }
}

function EdocmanBuildRoute(&$query)
{
    $router = new EdocmanRouter();
    return $router->build($query);
}

function EdocmanParseRoute($segments)
{
    $router = new EdocmanRouter();
    return $router->parse($segments);
}
