<?php
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
/**
 * @version        1.9.10
 * @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();

/**
 * View to edit
 */
class EdocmanViewUploadHtml extends OSViewHtml
{
	public function display()
	{
		$layout                 = $this->getLayout();
		if ($layout == 'edit')
		{
			$this->_displayEditDocumentsForm();
			return;
		}
		$db                     = $db = Factory::getContainer()->get('db');;
		$query                  = $db->getQuery(true);
		$query->select('id, title, parent_id');
		$query->from('#__edocman_categories');
		$query->where('published=1');
		$db->setQuery($query);
		$rows                   = $db->loadObjectList();
		$children               = [];
		// 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                = [];
		$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="form-select ilarge"', 
				'option.text' => 'text', 
				'option.key' => 'value', 
				'list.select' => 0));
		$lists['published']     = EdocmanHelper::showCheckboxfield2('published',1);
		$optionArr              = [];
		$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');
		//Access
		$lists['access'] 		= HTMLHelper::_('access.level', 'access', 1, true);
		$lists['access'] 		= str_replace('<select', '<select class="form-select"', $lists['access']);
		$lists['groups']        = HTMLHelper::_('access.usergroup','groups[]',array(),'multiple class="input-large form-select"');
		$config                 = EDocmanHelper::loadConfig();
		$maxFilesize            = $config->get('max_file_size',2);
		$maxFilesizeType        = $config->get('max_filesize_type',3);
		if ($maxFilesizeType == 1)
		{
			$maxFilesize        = ceil($maxFilesize / (1024 * 1024));
		}
		elseif ($maxFilesizeType == 2)
		{
			$maxFilesize        = ceil($maxFilesize / 1024);
		}
		if (!$maxFilesize)
		{
			$maxFilesize        = 2;
		}
		$allowedFiletypes       = $config->get('allowed_file_types');
		if (!$allowedFiletypes)
		{
			$allowedFiletypes   = 'doc, docx, ppt, pptx, pdf, zip, rar, png, zipx';
		}
		$allowedFiletypes       = explode(',', $allowedFiletypes);
		$allowedFiletypes       = array_map('trim', $allowedFiletypes);
		$this->lists            = $lists;
		$this->maxFilesize      = $maxFilesize;
		$this->allowedFiletypes = implode(',', $allowedFiletypes);
		$this->config           = $config;
		EDocmanHelperJquery::upload();
		
		//Clear session data
		$session = Factory::getApplication()->getSession();
		$session->clear('files');
		$session->clear('originalFiles');
        $session->clear('filesize');
		EDocmanHelperHtml::renderSubmenu('upload');
		parent::display();
	}		
	/**
	 * Display form which allows editing documents before storing into database
	 */
	protected function _displayEditDocumentsForm()
	{
		$session                = Factory::getSession();
		$this->state            = $this->model->getState();
		$this->files            = (array) $session->get('files', array());
		$this->originalFiles    = (array) $session->get('originalFiles', array());
        $this->filesizes        = (array) $session->get('filesize', array());
        $this->fileid           = (array) $session->get('fileid', array());
		EDocmanHelperHtml::renderSubmenu('upload');
		parent::display();
	}
}
