<?php

/**
 * @version        1.10.0
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011-2025 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;
use Joomla\CMS\HTML\HTMLHelper;
class EDocmanViewCategoriesHtml extends OSViewHtml
{

	function display()
	{
		$model          = $this->getModel();
		$state          = $model->getState();
		$config         = EDocmanHelper::getConfig();
		$items          = $model->getData();
        $menus          = Factory::getApplication()->getMenu();
        $menu           = $menus->getActive();
		$content_plugin = $this->input->getInt('content_plugin');
        $document       = Factory::getApplication()->getDocument();
		if ($state->id && !EDocmanHelper::canAccessCategory($state->id))
		{
			$user = Factory::getApplication()->getIdentity();
			if (!$user->id)
			{
				// Give not logged in users to login
				$return = base64_encode(Uri::getInstance()->toString());
				Factory::getApplication()->redirect('index.php?option=com_users&view=login&return=' . $return);
			}
			else
			{
				Factory::getApplication()->enqueueMessage(Text::_('EDOCMAN_INVALID_CATEGORY'));
				Factory::getApplication()->redirect('index.php');

				return;
			}
		}

        $setmetakeyword = 0;
        $setmetadescription = 0;
        $setrobots      = 0;
        $showPageTitle  = 0;

        if (is_object($menu) && $content_plugin == 0)
        {
            $params = new Registry() ;
            $params->loadString($menu->getParams()) ;
            if ($params->get('menu-meta_description'))
            {
                $document->setDescription($params->get('menu-meta_description'));
                $setmetadescription = 1;
            }

            if ($params->get('menu-meta_keywords'))
            {
                $document->setMetadata('keywords', $params->get('menu-meta_keywords'));
                $setmetakeyword     = 1;
            }

            if ($params->get('robots'))
            {
                $document->setMetadata('robots', $params->get('robots'));
                $setrobots          = 1;
            }

            $page_title = $params->get('page_title', '');
            if($page_title != "")
            {
                $document->setTitle($page_title);
                $showPageTitle      = 1;
            }
        }

		if ($state->id && $content_plugin == 0)
		{
			$document = Factory::getApplication()->getDocument();
			$category = EDocmanHelper::getCategory($state->id);
			if ($category->metakey && $setmetakeyword == 0)
			{
				$document->setMetaData('keywords', $category->metakey);
			}
			if ($category->metadesc && $setmetadescription == 0)
			{
				$document->setMetaData('description', $category->metadesc);
			}
			if ($config->process_plugin)
			{
				$category->description = HTMLHelper::_('content.prepare', $category->description);
			}
			$this->category = $category;
			if($showPageTitle == 0)
            {
                $document->setTitle($category->title . ' - ' . Factory::getConfig()->get('sitename'));
            }
		}

		// Trigger content plugin in category description
		if ($config->process_plugin && count($items))
		{
			foreach ($items as $item)
			{
				$item->description = HTMLHelper::_('content.prepare', $item->description);
			}
		}

		$this->Itemid	  = EDocmanHelperRoute::getDefaultMenuItem();
		$this->bootstrapHelper = new EDocmanHelperBootstrap($config->twitter_bootstrap_version);
		$this->config     = $config;
		$this->items      = $items;
		$this->pagination = $model->getPagination();
		$this->categoryId = $state->id;
		$menus			  = Factory::getApplication()->getMenu();
		$this->menu		  = $menus->getActive();
		$this->params = Factory::getApplication()->getParams();

		parent::display();
	}
}
