<?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\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;

require_once JPATH_ROOT. '/components/com_edocman/helper/route.php';
if(version_compare(JVERSION, '4.0.0-dev', 'lt'))
{
	HTMLHelper::_('behavior.tooltip');
}
HTMLHelper::_('jquery.framework');
HTMLHelper::_('script','system/multiselect.js',false,true);
$user	= Factory::getApplication()->getIdentity();
$userId	= $user->get('id');

$listOrder	= $this->state->filter_order;
$listDirn	= $this->state->filter_order_Dir;

$bootstrapHelper	= $this->bootstrapHelper;
$rowFluidClass		= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class		= $bootstrapHelper->getClassMapping('span12');
$controlGroupClass	= $bootstrapHelper->getClassMapping('control-group');
$controlLabelClass	= $bootstrapHelper->getClassMapping('control-label');
$controlsClass		= $bootstrapHelper->getClassMapping('controls');

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

$app      = Factory::getApplication();
$editor   = $app->getInput()->getCmd('editor', '');
$function = $app->getInput()->getCmd('function', 'jSelectEdocmanDetails');
$defaultItemid = $app->getInput()->getInt('defaultItemid', '0');
$btnClass = $app->getInput()->getString('btnClass','edocmanxtdlink');
$onclick  = $this->escape($function);
$dItemid  = $app->getInput()->getInt('defaultItemid',0);

$isJoomla4 = EdocmanHelper::isJoomla4();

$app->getDocument()->addScriptOptions('EDEditor', $editor)
	->addScriptOptions('isJoomla4', $isJoomla4)
	->addScriptOptions('defaultItemid', $defaultItemid)
	->addScriptOptions('btnClass', $btnClass)
	->getWebAssetManager()
	->useScript('core');

if(EdocmanHelper::isJoomla4())
{
	$searchBtnClass = "btn-primary";
	$cancelSearchBtnClass = "btn-secondary";
}
else
{
	$searchBtnClass = "";
	$cancelSearchBtnClass = "";
}
?>
<form action="<?php echo Route::_('index.php?option=com_edocman&view=documents&layout=modal&tmpl=component&function='.$function.'&'.Session::getFormToken().'=1'); ?>" method="post" name="adminForm" id="adminForm" onsubmit="return storeInsertOption();">
	<fieldset id="filter-bar">
		<div class="filter-search fltlft pull-left btn-group">
			<div class="input-group input-append">
				<input type="text" name="filter_search" id="filter_search" value="<?php echo $this->escape($this->state->get('filter.search')); ?>" placeholder="Keyword" class="input-medium form-control" />
				<button type="submit" class="btn <?php echo $searchBtnClass;?>"><?php echo Text::_('JSEARCH_FILTER_SUBMIT'); ?></button>
				<button type="button" class="btn <?php echo $cancelSearchBtnClass; ?>" onclick="document.id('filter_search').value='';this.form.submit();"><?php echo Text::_('JSEARCH_FILTER_CLEAR'); ?></button>
			</div>
		</div>
		<div class="filter-select fltrt pull-right">
			<?php echo $this->lists['filter_category_id']; ?>
			<?php
                if ($this->config->activate_multilingual_feature) {
                ?>
              		<select name="filter_language" class="input-small imedium form-select" onchange="this.form.submit()" style="display:inline;">
						<option value=""><?php echo Text::_('JOPTION_SELECT_LANGUAGE');?></option>
						<?php echo HTMLHelper::_('select.options', HTMLHelper::_('contentlanguage.existing', true, true), 'value', 'text', $this->state->get('filter.language'));?>
            		</select>  	
                <?php    
                }
			?>
			<select name="filter_published" class="input-small imedium form-select" onchange="this.form.submit()" style="display:inline;width:180px;">
				<option value=""><?php echo Text::_('JOPTION_SELECT_PUBLISHED');?></option>
				<?php echo HTMLHelper::_('select.options', HTMLHelper::_('jgrid.publishedOptions'), "value", "text", $this->state->filter_published, true);?>
			</select>
		</div>
	</fieldset>
	<div class="clearfix"></div>
	<table class="adminlist table table-striped">
		<thead>
			<tr>
				<th width="1%">
					<input type="checkbox" name="checkall-toggle" value="" onclick="checkAll(this)" />
				</th>      
				<th style="text-align: left;">
					<?php echo HTMLHelper::_('grid.sort',  'Title', 'tbl.title', $listDirn, $listOrder); ?>
				</th>			
				<th width="15%" style="text-align: left;">
					<?php echo HTMLHelper::_('grid.sort',  'Category', 'cat.title', $listDirn, $listOrder); ?>
				</th>	        
				<th width="5%">
					<?php echo HTMLHelper::_('grid.sort',  'JPUBLISHED', 'tbl.published', $listDirn, $listOrder); ?>
				</th>                                
				<th width="5%">
					<?php echo HTMLHelper::_('grid.sort',  'Hits', 'hits', $listDirn, $listOrder); ?>
				</th>
				<th width="5%">
					<?php echo HTMLHelper::_('grid.sort',  'Downloads', 'downloads', $listDirn, $listOrder); ?>
				</th>         
				<th width="10%">
					<?php echo HTMLHelper::_('grid.sort',  'JGRID_HEADING_ACCESS', 'access_level', $listDirn, $listOrder); ?>
				</th>                       
                <th width="1%" class="nowrap">
                    <?php echo HTMLHelper::_('grid.sort',  'JGRID_HEADING_ID', 'tbl.id', $listDirn, $listOrder); ?>
                </th>                
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="8">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php foreach ($this->items as $i => $item) :
			
		$needles = array (
			'document'  => array((int) $item->id)
		);
		$cat = EdocmanHelper::getDocumentCategory($item->id);
		$catId = $cat->id;
		if ($catId)
        {
            $needles['category'] = EDocmanHelperRoute::getCategoriesPath($catId, 'id', false);
            $needles['categories'] = $needles['category'];
        }
		$itemId = EDocmanHelperRoute::findItem($needles, $dItemid);
		?>
			<tr class="row<?php echo $i % 2; ?>">
				<td class="center">
					<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
				</td>    
				<td>
					<?php if ($item->checked_out) : ?>
					<?php echo HTMLHelper::_('jgrid.checkedout', $i, $item->editor, $item->checked_out_time, 'articles.', ''); ?>
    				<?php endif; ?>
					<a class="pointer select-link" href="javascript:void(0)" onclick="if (window.parent) window.parent.jSelectEdocmanDocument('<?php echo $item->id?>', '<?php echo str_replace("'", "\'", $item->title);?>');" data-id="<?php echo $item->id; ?>" data-title="<?php echo $item->title;?>" data-id="<?php echo $item->id; ?>" data-itemid="<?php echo $itemId;?>" data-title="<?php echo $item->title;?>" data-btnclass="<?php echo $btnClass ;?>">
    					<?php echo $item->title; ?>
					</a>			
    				<p class="smallsub">
						<?php echo Text::sprintf('JGLOBAL_LIST_ALIAS', $this->escape($item->alias));?>
					</p>
				</td> 					
				<td>
					<?php echo $item->category_title ; ?>
				</td>			          
			    <td class="center">
				    <?php echo HTMLHelper::_('jgrid.published', $item->published, $i, 'documents.', '', 'cb'); ?>
			    </td>           
			    <td class="center">
			    	<?php echo (int)$item->hits ; ?>
			    </td>
			    <td class="center">
			    	<?php echo (int)$item->downloads ; ?>
			    </td>
			    <td class="center">
				    <?php echo $item->access_level ; ?>
				</td>                        
				<td class="center">
					<?php echo (int) $item->id; ?>
				</td>                
			</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	<div>
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
		<input type="hidden" id="filter_full_ordering" name="filter_full_ordering" value="" />
		<input type="hidden" name="editor" id="editor" value="<?php echo $editor; ?>" />
		<input type="hidden" name="Itemid" value="<?php echo $dItemid; ?>" />
		<input type="hidden" name="btnClass" id="btnclass" value="<?php echo $btnClass; ?>" />
		<?php echo HTMLHelper::_('form.token'); ?>
	</div>
</form>
<div class="<?php echo $rowFluidClass; ?>">
<form  action="<?php echo Route::_('index.php?option=com_edocman&task=document.saveDocument'); ?>" method="post" name="upload-document-form" id="item-form" class="form form-horizontal upload-new-document" enctype="multipart/form-data">
	<div class="<?php echo $controlGroupClass; ?>">
		<label class="<?php echo $controlLabelClass; ?>" for="title">
			<?php echo Text::_('JGLOBAL_TITLE'); ?>
		</label>
		<div class="<?php echo $controlsClass; ?>">
			<input type="text" required="required" size="40" class="input-xlarge form-control" value="" id="title" name="title" />
		</div>
	</div>
	<div class="<?php echo $controlGroupClass; ?>">
		<label class="<?php echo $controlLabelClass; ?>" for="category_id">
			<?php echo Text::_('EDOCMAN_CATEGORY'); ?>
		</label>
		<div class="<?php echo $controlsClass; ?>">
			<?php echo $this->lists['category_id']; ?>
		</div>
	</div>
	<div class="<?php echo $controlGroupClass; ?>">
		<label class="<?php echo $controlLabelClass; ?>" for="access">
			<?php echo Text::_('JFIELD_ACCESS_LABEL'); ?>
		</label>
		<div class="<?php echo $controlsClass; ?>">
			<?php echo EdocmanHelper::getChoicesJsSelect($this->lists['access']); ?>
		</div>
	</div>
	<div class="<?php echo $controlGroupClass; ?>">
		<label class="<?php echo $controlLabelClass; ?>" for="published">
			<?php echo Text::_('JSTATUS'); ?>
		</label>
		<?php
		if (version_compare(JVERSION, '3.0', 'lt'))
		{
			?>
			<div class="<?php echo $controlsClass; ?>">
				<?php echo $this->lists['published']; ?>
			</div>
		<?php
		}
		else
		{
			echo $this->lists['published'];
		}
		?>
	</div>
	<div class="<?php echo $controlGroupClass; ?>">
		<label class="<?php echo $controlLabelClass; ?>" for="filename">
			<?php echo Text::_('EDOCMAN_FILE'); ?>
		</label>
		<div class="<?php echo $controlsClass; ?>">
			<input type="file" size="60" class="inputbox form-control" value="" id="filename" name="filename" />
		</div>
	</div>
	<div class="<?php echo $controlGroupClass; ?>">
		<label class="<?php echo $controlLabelClass; ?>" for="description">
			<?php echo Text::_('JGLOBAL_DESCRIPTION'); ?>
		</label>
		<div class="<?php echo $controlsClass; ?>">
			<textarea id="description" name="description" class="input-xlarge form-control" cols="60" rows="5"><?php echo $this->input->get('description', '', 'none'); ?></textarea>
		</div>
	</div>
	<div class="<?php echo $controlGroupClass; ?>">
		<button type="button" class="btn btn-small btn-primary" onclick="saveDocument();">
			<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-save" viewBox="0 0 16 16">
			  <path d="M2 1a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1H9.5a1 1 0 0 0-1 1v7.293l2.646-2.647a.5.5 0 0 1 .708.708l-3.5 3.5a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L7.5 9.293V2a2 2 0 0 1 2-2H14a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2a2 2 0 0 1 2-2h2.5a.5.5 0 0 1 0 1H2z"/>
			</svg>
			<?php echo Text::_('EDOCMAN_UPLOAD'); ?>
		</button>
	</div>
	<input type="hidden" name="id" value="0" />
	<input type="hidden" name="btnClass" id="btnclass" value="<?php echo $btnClass; ?>" />
	<input type="hidden" name="defaultItemId" id="defaultItemId" value="<?php echo $defaultItemid; ?>" />
	<input type="hidden" name="onclick" id="onclick" value="<?php echo $onclick; ?>" />
	<input type="hidden" name="editor" id="editor" value="<?php echo $editor; ?>" />
	<input type="hidden" name="Itemid" value="<?php echo $dItemid; ?>" />
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
</div>
<script type="text/javascript">
	(function($){
		$(document).ready(function(){
			changeOption = (function(value){
				if(value == 1)
				{
					$('#adminForm').hide();
					$('#item-form').show();
				}
				else
				{
					$('#item-form').hide();
					$('#adminForm').show();
				}
			})

			saveDocument = (function(){
				if($( "#title" ).val() == '')
				{
					$( "#title" ).css("border","1px solid #f11");
					$( "#title" ).focus();
				}
				else if($("#category_id").val() == 0)
				{
					$("#category_id").css("border","1px solid #f11");
					$("#category_id").val().focus();
				}
				else
				{
					$('#item-form').submit();
				}
			})

			storeInsertOption = (function(){
				document.adminForm.choose_document_option.value = $('input:radio[name=choose_document_option]:checked').val();
				return true;

			})

			var selectedOption = $('input:radio[name=choose_document_option]:checked').val();
			changeOption(selectedOption);
		})
	})(jQuery)
</script>
