<?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
 */

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

HTMLHelper::_('bootstrap.tooltip');
if(!EdocmanHelper::isJoomla4())
{
	HTMLHelper::_('formbehavior.chosen', 'select');
}
$jinput             = Factory::getApplication()->getInput();
$db                 = Factory::getContainer()->get('db');
$config             = EDocmanHelper::getConfig();
$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');
$bootstrapHelper	= $this->bootstrapHelper;
$rowFluildClass		= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class		= $bootstrapHelper->getClassMapping('span12');


$customOptions = array(
    'filtersHidden'       => true,
    'defaultLimit'        => Factory::getApplication()->get('list_limit', 20),
    'searchFieldSelector' => '#filter_search',
    'orderFieldSelector'  => '#filter_full_ordering'
);
HTMLHelper::_('searchtools.form', '#adminForm', $customOptions);
if (count($this->items))
{
    foreach ($this->items as $item)
    {
        $this->ordering[$item->parent_id][] = $item->id;
    }
}
?>
<style>
.edocman-page-heading
{
	font-size: <?php echo ($this->config->heading_font_size == '') ? '30' : $this->config->heading_font_size; ?>px !important;
}
</style>
<form action="<?php echo Route::_('index.php?option=com_edocman&view=managecategories&Itemid='.$jinput->getInt("Itemid"));?>" method="post" name="adminForm" id="adminForm">
    <div class="<?php echo $rowFluildClass; ?>">
        <div class="<?php echo $bootstrapHelper->getClassMapping('span5');?>">
            <h1 class="edocman-page-heading pull-left">
                <?php
                echo Text::_('EDOCMAN_MANAGE_CATEGORIES');
                ?>
            </h1>
        </div>
        <div class="<?php echo $bootstrapHelper->getClassMapping('span7');?>" style="text-align:right;">
            <a href="<?php echo Route::_('index.php?option=com_edocman&view=editcategory&newcat=1&manage=1&id=0&Itemid='.$jinput->getInt("Itemid").'&return='.EdocmanHelper::currentUrl());?>" class="btn btn-success" title="<?php echo Text::_('EDOCMAN_ADD_CATEGORY');?>">
                <i class="edicon edicon-folder-plus"></i>
                <?php echo Text::_('EDOCMAN_NEW_CATEGORY');?>
            </a>
            <?php
            if(Factory::getApplication()->getIdentity()->authorise('core.edit','com_edocman'))
            {
                ?>
                <a onclick="if (document.adminForm.boxchecked.value == 0) { alert('<?php echo Text::_('EDOCMAN_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST'); ?>'); } else { Joomla.submitbutton('editcategory'); }"
                   class="btn btn-secondary">
                    <i class="edicon edicon-pencil"></i>
                    <?php echo Text::_('EDOCMAN_EDIT'); ?>
                </a>
                <?php
            }
            ?>
            <?php
            if(Factory::getApplication()->getIdentity()->authorise('core.delete','com_edocman'))
            {
                ?>
                <a onclick="if (document.adminForm.boxchecked.value == 0) { alert('<?php echo Text::_('EDOCMAN_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST'); ?>'); } else { if (confirm('<?php echo Text::_('EDOCMAN_ARE_YOU_SURE_WANT_TO_DELETE_SELECTED_ITEMS'); ?>')) { Joomla.submitbutton('deletecategory'); } }"
                   class="btn btn-secondary">
                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
					  <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
					  <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
					</svg>
					&nbsp;
                    <?php echo Text::_('EDOCMAN_DELETE'); ?>
                </a>
                <?php
            }
            ?>
        </div>
    </div>
    <div class="<?php echo $rowFluildClass; ?>">
        <div class="<?php echo $bootstrapHelper->getClassMapping('span12');?>">
            <div id="filter-bar" class="btn-toolbar">
                <div class="filter-search btn-group pull-left">
                    <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" title="<?php echo HTMLHelper::tooltipText('EDOCMAN_SEARCH_CATEGORIES_DESC'); ?>" />
                </div>
                <div class="btn-group pull-left">
                    <button type="submit" class="btn btn-primary hasTooltip" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_SUBMIT'); ?>">
						<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
						  <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
						</svg>
					</button>
                    <button type="button" class="btn hasTooltip" title="<?php echo HTMLHelper::tooltipText('JSEARCH_FILTER_CLEAR'); ?>" onclick="document.getElementById('filter_search').value='';this.form.submit();">
						<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x-square-fill" viewBox="0 0 16 16">
						  <path d="M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2zm3.354 4.646L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 1 1 .708-.708z"/>
						</svg>
					</button>
                </div>
                <div class="btn-group pull-right hidden-phone">
                    <?php
                    if (@$config->activate_multilingual_feature)
                    {
                        echo $this->lists['filter_language'];
                    }
                    echo $this->lists['filter_category_id'] ;
                    echo $this->lists['filter_level'] ;
                    echo $this->lists['filter_state'] ;
                    echo $this->lists['filter_access'] ;
                    
                    ?>
                </div>
            </div>
        </div>
    </div>
    <div class="<?php echo $rowFluildClass; ?>">
        <div class="<?php echo $bootstrapHelper->getClassMapping('span12');?>">
            <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>
                    <th class="center">
                        <?php echo HTMLHelper::_('searchtools.sort',  Text::_('EDOCMAN_PUBLISH'), '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
                $k = 0;
                $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) && $canCheckin;

                    $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; ?>" 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 style="width:5px;padding-left:0px;padding-right:0px;padding-top:12px;color:#5871a9;">
                            <i class="<?php echo $icon; ?>"></i>
                        </td>
                        <td width="30%">
                            <?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=editcategory&cid[]='.$item->id.'&manage=1&Itemid='.$jinput->getInt('Itemid',0).'&return='.EdocmanHelper::currentUrl());?>">
                                    <?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('EDOCMAN_LIST_ALIAS', $this->escape($item->alias));?></p>
                        </td>
                        <td class="center">
                            <?php echo $item->total_documents; ?>
                        </td>
                        <td class="center">
                            <div class="btn-group">
                                <?php
                                if($item->published == 1)
                                {
                                    if(Factory::getApplication()->getIdentity()->authorise('core.edit.state','com_edocman'))
                                    {
                                        ?>
                                        <a class="btn btn-micro active hasTooltip" href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','unpublishcategory')" title="<?php echo Text::_('EDOCMAN_UNPUBLISH_ITEM');?>">
                                            <i class="edicon edicon-checkmark" style="color:green"></i>
                                        </a>
                                        <?php
                                    }
                                    else
                                    {
                                        ?>
                                        <a class="btn btn-micro active hasTooltip" href="javascript:void(0);">
                                            <i class="edicon edicon-checkmark" style="color:green"></i>
                                        </a>
                                        <?php
                                    }
                                }
                                else
                                {
                                    if(Factory::getApplication()->getIdentity()->authorise('core.edit.state','com_edocman'))
                                    {
                                        ?>
                                        <a class="btn btn-micro active hasTooltip" href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','publishcategory')" title="<?php echo Text::_('EDOCMAN_PUBLISH_ITEM');?>">
                                            <i class="edicon edicon-cross" style="color:red"></i>
                                        </a>
                                        <?php
                                    }
                                    else
                                    {
                                        ?>
                                        <a class="btn btn-micro active hasTooltip" href="javascript:void(0);">
                                            <i class="edicon edicon-cross" style="color:red"></i>
                                        </a>
                                        <?php
                                    }
                                }
                                ?>
                                <a class="btn btn-micro active hasTooltip" title="<?php echo Text::_('EDOCMAN_EDIT');?>" href="index.php?option=com_edocman&task=editcategory&cid[]=<?php echo $item->id;?>&return=<?php echo EdocmanHelper::currentUrl();?>&manage=1&Itemid=<?php echo $jinput->getInt('Itemid',0); ?>" data-original-title="<?php echo Text::_('EDOCMAN_EDIT');?>">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
									  <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
									  <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
									</svg>
                                </a>
                            </div>
                        </td>
                        <td class="center accesscol" width="20%">
                            <?php
                            if($item->accesspicker == 0)
                            {
                                echo $item->access_level;
                            }else{
                                echo EDocmanHelper::getAccessGroup(0, $item->id);
                            }
                            ?>
                        </td>
                        <td class="center" width="2%">
                            <?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="" />
    <input type="hidden" name="Itemid" id="Itemid" value="<?php echo $jinput->getInt('Itemid',0); ?>" />
	<input type="hidden" name="manage" id="manage" value="1" />
	<input type="hidden" name="return" value="<?php echo EdocmanHelper::currentUrl(); ?>" />
    <?php echo HTMLHelper::_( 'form.token' ); ?>
</form>
