<?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\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;


/**
 * View class for a list of EDocman.
 */
class EDocmanViewDocumentsHtml extends OSViewList
{
	/**
	 * Preare data for the view before displaying
	 */
	protected function prepareView()
	{
		parent::prepareView();
		$db = Factory::getContainer()->get('db');
		$query = $db->getQuery(true);
		$user = Factory::getApplication()->getIdentity();
		$userId = $user->id;

		$usergroup          = $user->groups;
        $usergroupArr       = array();
        $usergroupSql       = "";
        if(count($usergroup) > 0){
            foreach ($usergroup as $group){
                $usergroupArr[] = " (`groups`='$group' OR `groups` LIKE '$group,%' OR `groups` LIKE '%,$group,%' OR `groups` LIKE '%,$group') AND `data_type` = '0'";
            }
            $usergroupSql = implode(" OR ",$usergroupArr);
            $usergroupSql = " id in (Select item_id from #__edocman_levels where $usergroupSql) ";
            $usergroupSql = " OR ((user_ids = '' or user_ids = '0') AND accesspicker = '1' AND $usergroupSql ) ";
        }

		$query->select('id, title, parent_id');
		$query->from('#__edocman_categories');
		$query->where("published=1 and (((user_ids = '' OR user_ids = 0) AND access IN (" . implode(',', $user->getAuthorisedViewLevels()) . ")) $usergroupSql OR user_ids='$userId' OR user_ids LIKE '$userId,%' OR user_ids LIKE '%,$userId,%' OR user_ids LIKE '%,$userId' OR (created_user_id='".$userId."' AND created_user_id > 0))");
		$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);
		$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);
			}
		}

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


		// Upload New Document Form
		$layout = $this->getLayout();
		$user = Factory::getApplication()->getIdentity();
		if ($user->authorise('core.create', 'com_edocman') || count(EDocmanHelper::getAuthorisedCategories('core.create')))
		{
			$this->canUpload = true;
		}
		else
		{
			$this->canUpload = false;
		}
		if ($layout == 'modal' && $this->canUpload)
		{
			if(count($options))
			{
				foreach ($options as $i => $option)
				{
					if ($i == 0)
					{
						continue;
					}
					// To take save or create in a category you need to have create rights for that category
					// unless the item is already in that category.
					// Unset the option if the user isn't authorised for it. In this field assets are always categories.
					if ($option->value > 0 && $user->authorise('core.create', 'com_edocman.category.' . $option->value) != true)
					{
						unset($options[$i]);
					}
				}
			}
			// Upload new document form options
			$this->lists['category_id'] = HTMLHelper::_('select.genericlist', $options, 'category_id',
				array(
					'option.text.toHtml' => false,
					'list.attr'          => 'class="inputbox form-select" required="required"',
					'option.text'        => 'text',
					'option.key'         => 'value',
					'list.select'        => $this->input->getInt('category_id', 0)));

			$documentLinkPlugin                    = PluginHelper::getPlugin('editors-xtd', 'edocman');
			$params                                = new Registry($documentLinkPlugin->params);
			$options                               = array();
			$options[]                             = HTMLHelper::_('select.option', 0, Text::_('EDOCMAN_CHOOSE_EXISTING_DOCUMENT'));
			$options[]                             = HTMLHelper::_('select.option', 1, Text::_('EDOCMAN_UPLOAD_NEW_DOCUMENT'));
			$this->lists['choose_document_option'] = HTMLHelper::_('select.radiolist', $options, 'choose_document_option', ' class="inputbox" onclick="changeOption(this.value)" ', 'value', 'text', $this->input->getInt('choose_document_option', $params->get('default_insert_option', 0)));

			$this->lists['published'] = HTMLHelper::_('select.booleanlist', 'published', ' class="inputbox form-select" ', $this->input->getInt('published', 1));

			//Access
			$this->lists['access'] = HTMLHelper::_('access.level', 'access', $this->input->getInt('access', 1), 'class="form-select form-control"', true);
		}
		$config = EdocmanHelper::getConfig();
		$this->bootstrapHelper	= new EDocmanHelperBootstrap($config->twitter_bootstrap_version);
		$this->config = $config;
	}
}
