<?php
/**
 * @version        1.9.7
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2025 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Layout\LayoutHelper;

HTMLHelper::_('bootstrap.tooltip');

if(!EdocmanHelper::isJoomla4())
{
	HTMLHelper::_('formbehavior.chosen', 'select');
}
$config			= EDocmanHelper::loadConfig();
$user			= Factory::getApplication()->getIdentity();
$userId			= $user->get('id');
$listOrder		= $this->state->filter_order;
$listDirn		= $this->state->filter_order_Dir;
$saveOrder		= $listOrder == 'tbl.ordering';
$ordering		= ($this->state->filter_order == 'tbl.ordering');

if ($saveOrder)
{
	$saveOrderingUrl = 'index.php?option=com_edocman&task=category.save_order_ajax';
	HTMLHelper::_('sortablelist.sortable', 'categoryList', 'adminForm', strtolower($listDirn), $saveOrderingUrl, false, true);
}

$customOptions = array(
	'filtersHidden'       => true,
	'defaultLimit'        => Factory::getApplication()->get('list_limit', 20),
	'searchFieldSelector' => '#filter_search',
	'orderFieldSelector'  => '#filter_full_ordering'
);
HTMLHelper::_('searchtools.form', '#adminForm', $customOptions);
$this->items = (array)$this->items;
if (count($this->items))
{
	foreach ($this->items as $item)
	{
		$this->ordering[$item->parent_id][] = $item->id;
	}
}
$bootstrapHelper		= $this->bootstrapHelper;
$rowFluidClass			= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class			= $bootstrapHelper->getClassMapping('span12');
if(EdocmanHelper::isJoomla4())
{
	$searchBtnClass		= "btn-primary";
	$cancelSearchBtnClass = "btn-secondary";
	HTMLHelper::_('draggablelist.draggable');
}
else
{
	$searchBtnClass		= "";
	$cancelSearchBtnClass = "";
	HTMLHelper::_('behavior.multiselect');
}
?>
<form action="index.php?option=com_edocman&view=categories" method="post" name="adminForm" id="adminForm">
	<div class="<?php echo $rowFluidClass;?>">
		<div class="<?php echo $span12Class;?>">
			<div id="filter-bar" class="btn-toolbar js-stools<?php if (EdocmanHelper::isJoomla4()) echo ' js-stools-container-filters-visible'; ?>"">
				<div class="filter-search btn-group pull-left">
					<div class="input-group input-append">
						<input type="text" name="filter_search" id="filter_search" placeholder="<?php echo Text::_('JSEARCH_FILTER'); ?>" value="<?php echo $this->escape($this->state->filter_search); ?>" class="hasTooltip form-control input-medium" title="<?php echo HTMLHelper::tooltipText('EDOCMAN_SEARCH_CATEGORIES_DESC'); ?>" />
						<button type="submit" class="btn <?php echo $searchBtnClass;?> hasTooltip" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_SUBMIT'); ?>"><span class="icon-search"></span></button>
						<button type="button" class="btn <?php echo $cancelSearchBtnClass; ?> hasTooltip js-stools-btn-clear" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_CLEAR'); ?>" onclick="document.getElementById('filter_search').value='';this.form.submit();"><span class="icon-remove"></span></button>
					</div>
				</div>
				<div class="btn-group pull-right hidden-phone">
					<?php
					if ($config->get('activate_multilingual_feature'))
					{
						echo $this->lists['filter_language'];
					}
					
					echo EdocmanHelper::getChoicesJsSelect($this->lists['filter_category_id']);
					
					echo $this->lists['filter_level'] ;
					echo $this->lists['filter_state'] ;
					echo $this->lists['filter_access'] ;
					if (version_compare(JVERSION, '3.0', 'ge'))
					{
						echo $this->pagination->getLimitBox();
					}
					?>
				</div>
			</div>
		</div>
	</div>
	<div class="<?php echo $rowFluidClass;?>">
		<div class="<?php echo $span12Class;?>">
			<table class="adminlist table table-striped" id="categoryList">
				<thead>
					<tr>
						<th width="1%" class="nowrap center hidden-phone">
							<?php echo HTMLHelper::_('searchtools.sort', '', 'tbl.ordering', $listDirn, $listOrder, null, 'asc', 'JGRID_HEADING_ORDERING', 'icon-menu-2'); ?>
						</th>
						<th width="20" class="center">
							<input type="checkbox" name="toggle" value="" onclick="Joomla.checkAll(this);" />
						</th>
						<th width="5">
						</th>
						<th class="title" style="text-align: left;">
							<?php echo HTMLHelper::_('searchtools.sort',  Text::_('EDOCMAN_TITLE'), 'tbl.title', $this->state->filter_order_Dir, $this->state->filter_order ); ?>
						</th>
						<th class="title center">
							<?php echo Text::_('# Documents'); ?>
						</th>
						<?php
						if ($config->activate_multilingual_feature)
						{
							?>
							<th class="center">
								<?php echo HTMLHelper::_('searchtools.sort',  Text::_('JFIELD_LANGUAGE_LABEL'), 'tbl.language', $this->state->filter_order_Dir, $this->state->filter_order ); ?>
							</th>
							<?php
						}
						?>
						<th class="center">
							<?php echo HTMLHelper::_('searchtools.sort',  Text::_('EDOCMAN_PUBLISHED'), 'tbl.published', $this->state->filter_order_Dir, $this->state->filter_order ); ?>
						</th>
						<th width="10%" class="center">
                            <?php echo HTMLHelper::_('searchtools.sort',  'JGRID_HEADING_ACCESS', 'access_level', $this->state->filter_order_Dir, $this->state->filter_order ); ?>
						</th>
						<th class="center">
							<?php echo HTMLHelper::_('searchtools.sort',  Text::_('EDOCMAN_ID'), 'tbl.id', $this->state->filter_order_Dir, $this->state->filter_order); ?>
						</th>													
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="8">
							<?php echo $this->pagination->getListFooter(); ?>
						</td>
					</tr>
				</tfoot>
				<tbody <?php if ($saveOrder) :?> class="js-draggable" data-url="<?php echo $saveOrderingUrl; ?>" data-direction="<?php echo strtolower($listDirn); ?>" data-nested="false"<?php endif; ?>>
				<?php
				$k = 0;
				$db = $this->db;
				$query = $db->getQuery(true);
				for ($i=0, $n=count( $this->items ); $i < $n; $i++)
				{
					$item = $this->items[$i];
					$orderkey   = array_search($item->id, $this->ordering[$item->parent_id]);
					$canEdit	= $user->authorise('core.edit',			'com_edocman.category.'.$item->id);
					$canCheckin	= $user->authorise('core.admin', 'com_checkin') || $item->checked_out == $userId || $item->checked_out == 0;
					$canEditOwn	= $user->authorise('core.edit.own',		'com_edocman.category.'.$item->id) && $item->created_user_id == $userId;
					$canChange	= $user->authorise('core.edit.state',	'com_edocman.category.'.$item->id);

					$query->clear();
					$query->select('count(id)')->from('#__edocman_categories')->where('parent_id = "'.$item->id.'"');
					$db->setQuery($query);
					$count = $db->loadResult();
					if($count > 0)
					{
						$icon = "edicon edicon-folder-open";
					}
					else
					{
						$icon = "edicon edicon-folder";
					}
				
					// Get the parents of item for sorting
					if ($item->level > 1)
					{
						$parentsStr = "";
						$_currentParentId = $item->parent_id;
						$parentsStr = " " . $_currentParentId;
						for ($i2 = 0; $i2 < $item->level; $i2++)
						{
							foreach ($this->ordering as $k => $v)
							{
								$v = implode("-", $v);
								$v = "-" . $v . "-";
								if (strpos($v, "-" . $_currentParentId . "-") !== false)
								{
									$parentsStr .= " " . $k;
									$_currentParentId = $k;
									break;
								}
							}
						}
					}
					else
					{
						$parentsStr = "";
					}
					?>
					<tr class="row<?php echo $i % 2; ?>" data-draggable-group="<?php echo $item->parent_id; ?>" sortable-group-id="<?php echo $item->parent_id; ?>" item-id="<?php echo $item->id ?>" parents="<?php echo $parentsStr ?>" level="<?php echo $item->level ?>">
						<td class="order nowrap center hidden-phone">
							<?php
							$iconClass = '';
							if (!$canChange)
							{
								$iconClass = ' inactive';
							}
							elseif (!$saveOrder)
							{
								$iconClass = ' inactive tip-top hasTooltip" title="' . HTMLHelper::tooltipText('JORDERINGDISABLED');
							}
							?>
							<span class="sortable-handler<?php echo $iconClass ?>">
								<span class="icon-menu"></span>
							</span>
							<?php if ($canChange && $saveOrder) : ?>
								<input type="text" style="display:none" name="order[]" size="5" value="<?php echo $orderkey + 1; ?>" />
							<?php endif; ?>
						</td>
						<td class="center">
							<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
						</td>
						<td class="icontd">
							<a href="index.php?option=com_edocman&view=documents&filter_category_id=<?php echo $item->id;?>" target="_blank" title="<?php echo Text::_('EDOCMAN_MANAGE_DOCUMENTS')?>">
								<i class="<?php echo $icon; ?>"></i>
							</a>
						</td>
						<td>
							<?php $prefix = LayoutHelper::render('joomla.html.treeprefix', ['level' => $item->level]); ?>
                            <?php //echo $prefix; ?>
							<?php if ($item->checked_out) : ?>
								<?php echo HTMLHelper::_('jgrid.checkedout', $i, $item->editor, $item->checked_out_time, 'categories.', $canCheckin); ?>
							<?php endif; ?>
							<?php if ($canEdit || $canEditOwn) : ?>
								<a href="<?php echo Route::_('index.php?option=com_edocman&task=category.edit&id='.$item->id);?>">
									 <?php echo $item->treename ? $item->treename : $item->title; ?></a>
							<?php else : ?>
								<?php echo $item->treename ? $item->treename : $item->title; ?>
							<?php endif; ?>
							<p class="smallsub" style="font-size:11px;color:grey;">
								<?php echo Text::sprintf('JGLOBAL_LIST_ALIAS', $this->escape($item->alias));?></p>
						</td>									
						<td class="center">
							<a href="index.php?option=com_edocman&view=documents&filter_category_id=<?php echo $item->id;?>" target="_blank" title="<?php echo Text::_('EDOCMAN_MANAGE_DOCUMENTS')?>">
								<?php echo $item->total_documents; ?>
							</a>
						</td>
						<?php
						if ($config->activate_multilingual_feature)
						{
							?>
							<td class="center">
								<?php 
								if($item->language == "*")
								{
									echo Text::_("JALL");
								}
								else
								{
									echo $item->language;
								}
							?>
							</td>
							<?php
						}
						?>
						<td class="center">
							<div class="btn-group">
								<?php echo HTMLHelper::_('jgrid.published', $item->published, $i, '', $canChange);?>
								<?php
								if(!EdocmanHelper::isJoomla4())
								{
								?>
								<a class="btn btn-micro active hasTooltip" title="<?php echo Text::_('EDOCMAN_EDIT');?>" href="index.php?option=com_edocman&task=category.edit&cid[]=<?php echo $item->id;?>" data-original-title="<?php echo Text::_('EDOCMAN_EDIT');?>">
									<span class="icon-edit"></span>
								</a>
								<?php } ?>
							</div>
						</td>
						<td class="center">
							<?php
                            if($item->accesspicker == 0) 
							{
                                echo $item->access_level;
							}
                            elseif($item->accesspicker == 1 )
							{
                                echo EDocmanHelper::getAccessGroup(0, $item->id);
                            }
							elseif($item->accesspicker == 2)
							{
								if($item->access > 0) 
								{
									echo $item->access_level;
								}
								else
								{
									echo EDocmanHelper::getAccessGroup(1, $item->id);
								}
							}
                            ?>
						</td>
						<td class="center">
							<?php echo $item->id; ?>
						</td>
					</tr>
					<?php
					$k = 1 - $k;
				}
				?>
				</tbody>
			</table>
		</div>
	</div>
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="boxchecked" value="0" />
	<input type="hidden" name="filter_order" value="<?php echo $this->state->filter_order; ?>" />
	<input type="hidden" name="filter_order_Dir" value="<?php echo $this->state->filter_order_Dir; ?>" />
	<input type="hidden" id="filter_full_ordering" name="filter_full_ordering" value="" />
	<?php echo HTMLHelper::_( 'form.token' ); ?>
</form>