<?php

/**
 * @version        1.9.8
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Dang Thuc Dam
 * @copyright	   Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;

class EDocmanViewSearchHtml extends OSViewHtml
{

	function display()
	{
		$db			= Factory::getContainer()->get('db');
		$user		= Factory::getApplication()->getIdentity();
		$config		= EDocmanHelper::getConfig();
		$userId		= $user->get('id');
		$model		= $this->getModel();
		$state		= $model->getState();
		$menus		= Factory::getApplication()->getMenu();
        $menu		= $menus->getActive(); 
		$document   = Factory::getApplication()->getDocument();
		$items		= [];
		if($state->filter_category_id || $state->filter_search || $state->filter_tag)
		{
			$items = $model->getData();
			$pagination = $model->getPagination();

			$layout = $this->input->getString('layout','');
		}
		$Itemid	= $this->input->getInt('Itemid',0);

		if(count($items))
		{
			foreach ($items as $item)
			{
				if($state->show_category == 1)
				{
					$query = $db->getQuery(true);
					$query->select('a.*')->from('#__edocman_categories as a')->join('inner','#__edocman_document_category as b on a.id = b.category_id')->where('b.document_id = "'.$item->id.'" and b.is_main_category = 1');
					$db->setQuery($query);
					$item->category = $db->loadObject();
				}
			}
		}

		if ($config->process_plugin && count($items))
		{
			foreach ($items as $item)
			{
				$item->short_description = HTMLHelper::_('content.prepare', $item->short_description);
			}
		}
		if ($config->show_view_button && count($items))
		{
			$extsForView = explode(',', $config->exts_for_view);
			for ($i = 0, $n = count($extsForView); $i < $n; $i++)
			{
				$extsForView[$i] = strtolower(trim($extsForView[$i]));
			}
			foreach ($items as $item)
			{
				$fileName = $item->filename;
				$fileExt = strtolower(EdocmanHelper::getExt($fileName));
				if (in_array($fileExt, $extsForView))
				{
					$item->canView = 1;
				}
				else
				{
					$item->canView = 0;
				}
			}
		}

		$this->page_heading = Text::_('EDOCMAN_SEARCH_DOCUMENTS');

		$searchValue		= [];
		if (is_object($menu) && $Itemid > 0)
        {
			$menuquery		= $menu->query;
			$option			= $menuquery['option'];
			
			$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_heading = $params->get('page_heading','');
			if($page_heading != "")
			{
				$this->page_heading = $page_heading;
			}

			if($layout == '')
			{
				$layout		= $params->get('search_layout', 'default');
			}
			$this->setLayout($layout);
        }
	
		// Set new indicator
		if ($config->day_for_new > 0 && count($items))
		{
			EDocmanHelper::setNewIndicator($items, (int) $config->day_for_new);
		}

		if ($config->day_for_update > 0 && count($items))
		{
			EDocmanHelper::setUpdateIndicator($items, (int) $config->day_for_update);
		}
		
		if ($state->filter_category_id)
		{
			$pagination->setAdditionalUrlParam('filter_category_id', $state->filter_category_id);
			$searchedCategory = EdocmanHelper::getCategory($state->filter_category_id);
			$searchValue[]	  = Text::_('EDOCMAN_CATEGORY').' "'.$searchedCategory->title.'"';
		}
		if ($state->filter_search)
		{
			$pagination->setAdditionalUrlParam('filter_search', $state->filter_search);
			$searchValue[]	  = Text::_('EDOCMAN_KEYWORD').' "'.$state->filter_search.'"';
		}
		if ($state->filter_tag)
		{
			$pagination->setAdditionalUrlParam('filter_tag', $state->filter_tag);
			$searchValue[]	  = Text::_('EDOCMAN_TAG').' "'.$state->filter_tag.'"';
		}
		if($pagination)
		{
			$pagination->setAdditionalUrlParam('view', 'search');
			$pagination->setAdditionalUrlParam('show_category', $state->show_category);
		}
		

		$this->show_search_form		= true;
		if(!$state->filter_tag && ! $state->filter_search && !$state->filter_category_id)
		{
			$show_search_form		= 1;
		}
		if($show_search_form == 0)
		{
			$show_search_form		= Factory::getApplication()->getInput()->getInt('show_searchform', 0);
		}
		
		if ($show_search_form == 0)
		{
			$this->show_search_form = false;
		}
		if($pagination)
		{
			$pagination->setAdditionalUrlParam('show_searchform', $show_search_form);
			$searchphrase	= Factory::getApplication()->getInput()->getString('searchphrase','all');
			if($searchphrase)
			{
				$pagination->setAdditionalUrlParam('searchphrase', $searchphrase);
			}
		}

		//load modal && jQuery color box
		EDocmanHelperHtml::initModal();
		$this->userId				= $userId;
		$this->items				= $items;
		$this->pagination			= $pagination;
		$this->config				= $config;
		$this->category				= EDocmanHelper::getCategory($state->filter_category_id);
		$this->bootstrapHelper		= new EDocmanHelperBootstrap($config->twitter_bootstrap_version);
		$this->show_category		= $state->show_category;
		$this->categoryId			= $state->filter_category_id;
		$this->state				= $state;
		$this->categoryList			= EdocmanHelper::categoryList($state->filter_category_id, 1);
		$this->Itemid				= $Itemid;
		if(count($searchValue))
		{
			$this->searchValue		= implode(", ",$searchValue);
		}
		parent::display();
	}
}
