<?php

/**
 * @version        1.9.10
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright	   Copyright (C) 2018-2023 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
// no direct access
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\Filesystem\Folder;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Toolbar\ToolbarHelper;

class EDocmanViewImportHtml extends OSViewHtml
{

	function display()
	{
		$db                     = $db = Factory::getContainer()->get('db');;
		$jinput                 = Factory::getApplication()->getInput();
		$config                 = EDocmanHelper::loadConfig();
		$path                   = $config->get('documents_path');
		$path                   = str_replace("\\", '/', $path);
		$folders                = Folder::folders($path, '.', true, true);
		$pathLength             = strlen($path);
		$options                = array();
		$options[]              = HTMLHelper::_('select.option', '', Text::_('Root'));
		if (count($folders))
		{
			foreach ($folders as $folder)
			{
				$folder         = str_replace("\\", '/', $folder);
				$folder         = substr($folder, $pathLength + 1);
				$options[]      = HTMLHelper::_('select.option', $folder, $folder);
			}
		}		
		$lists['folder'] = HTMLHelper::_('select.genericlist', $options, 'folder', ' class="input-large form-select" ', 'value', 'text', $jinput->getString('folder'));		
		$sql                    = 'SELECT id, title, parent_id FROM #__edocman_categories WHERE published = 1';
		$db->setQuery($sql);
		$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);
		$options                = array();
		$options[]              = HTMLHelper::_('select.option', 0, Text::_('EDOCMAN_SELECT_CATEGORY'));
		if (count($list))
		{
			foreach ($list as $row)
			{
				$options[]      = HTMLHelper::_('select.option', $row->id, $row->treename);
			}
		}				
		$lists['category_id']   = HTMLHelper::_('select.genericlist', $options, 'category_id',
			array(
				'option.text.toHtml' => false,
				'list.attr' => 'class="input-large form-select" ',
				'option.text' => 'text',
				'option.key' => 'value',
				'list.select' =>0));
		
		$lists['access']        = HTMLHelper::_('access.level', 'access', 1, ' class="input-large form-select" ', false);
        $lists['groups']        = HTMLHelper::_('access.usergroup','groups[]',array(),'multiple class="input-large chosen inputbox form-select"');
        $optionArr              = array();
        $optionArr[]            = HTMLHelper::_('select.option',0,Text::_('EDOCMAN_PRESETS'));
        $optionArr[]            = HTMLHelper::_('select.option',1,Text::_('EDOCMAN_GROUPS'));
        $lists['accesspicker']  = HTMLHelper::_('select.genericlist',$optionArr,'accesspicker','','value','text');
		$allowedFileTypes = EdocmanHelper::getConfigValue('allowed_file_types');
		$this->allowedFileTypes = $allowedFileTypes;
		$this->lists = $lists;
		$this->config = $config;
		// We don't need toolbar in the modal window.
		if (version_compare(JVERSION, '3.0', 'ge')) {
			if ($this->getLayout() !== 'modal')
			{
				//EdocmanHelper::addSideBarmenus('import');
				//$this->sidebar = JHtmlSidebar::render();
				EDocmanHelperHtml::renderSubmenu('import');
			}
		}
		parent::display();
	}
}
