<?php

/**
 * @version        1.9.7
 * @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\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;

class EDocmanViewCategoriesHtml extends OSViewList
{
	/**
	 * Prepare data for the view before displaying
	 */
	protected function prepareView()
	{
		parent::prepareView();

		/**
		 * Level filter
		 */
		$options   = array();
		$options[] = HTMLHelper::_('select.option', '0', Text::_('EDOCMAN_SELECT_LEVEL'));
		$options[] = HTMLHelper::_('select.option', '1', Text::_('J1'));
		$options[] = HTMLHelper::_('select.option', '2', Text::_('J2'));
		$options[] = HTMLHelper::_('select.option', '3', Text::_('J3'));
		$options[] = HTMLHelper::_('select.option', '4', Text::_('J4'));
		$options[] = HTMLHelper::_('select.option', '5', Text::_('J5'));
		$options[] = HTMLHelper::_('select.option', '6', Text::_('J6'));
		$options[] = HTMLHelper::_('select.option', '7', Text::_('J7'));
		$options[] = HTMLHelper::_('select.option', '8', Text::_('J8'));
		$options[] = HTMLHelper::_('select.option', '9', Text::_('J9'));
		$options[] = HTMLHelper::_('select.option', '10', Text::_('J10'));

		$db = Factory::getContainer()->get('db');
		$query = $db->getQuery(true);
		$query->select('MAX(level)')
			->from('#__edocman_categories');
		$db->setQuery($query);
		$maxLevel = (int) $db->loadResult();
		$maxLevel = min($maxLevel, 10);
		$options  = array_slice($options, 0, $maxLevel + 1);

		$this->lists['filter_level'] = HTMLHelper::_('select.genericlist', $options, 'filter_level', ' class="input-medium form-select" onchange="submit();" ', 'value', 'text', $this->state->filter_level);

		$query->clear();
		$query->select('id, title, parent_id');
		$query->from('#__edocman_categories');
		$query->where('published=1 order by parent_id');
		$db->setQuery($query);
		$rows     = $db->loadObjectList();
		$children = array();
		// first pass - collect children
		if (count($rows))
		{
			foreach ($rows as $v)
			{
				$pt   = $v->parent_id;
				$list = @$children[$pt] ? $children[$pt] : array();
				array_push($list, $v);
				$children[$pt] = $list;
			}
		}
		$list      = HTMLHelper::_('menu.treerecurse', 0, '', array(), $children, 9999, 0);
		$options   = array();
		$options[] = HTMLHelper::_('select.option', 0, Text::_('EDOCMAN_SELECT_PARENT_CATEGORY'));
		if (count($list))
		{
			foreach ($list as $row)
			{
				$row->treename = str_replace("&#160;", "", $row->treename);
				$options[] = HTMLHelper::_('select.option', $row->id, $row->treename);
			}
		}

		$this->lists['filter_category_id'] = HTMLHelper::_('select.genericlist', $options, 'filter_category_id',
			array(
				'option.text.toHtml' => false,
				'list.attr'          => 'class="input-large form-control ilarge form-select" onchange="submit();"  ',
				'option.text'        => 'text',
				'option.key'         => 'value',
				'list.select'        => (int) $this->state->filter_category_id));

		// We don't need toolbar in the modal window.
		if (version_compare(JVERSION, '3.0', 'ge'))
		{
			if ($this->getLayout() !== 'modal')
			{
				//$this->sidebar = JHtmlSidebar::render();
			}
		}
		$this->bootstrapHelper	= new EDocmanHelperBootstrap(EDocmanHelperHtml::getAdminBootstrapHelper());
		$this->db = $db;
	}
}
