<?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\HTML\HTMLHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;

if(!EdocmanHelper::isJoomla4())
{
	HTMLHelper::_('behavior.core');
}
require_once JPATH_ROOT.'/components/com_edocman/helper/file.class.php' ;
$uploadItemId       = EDocmanHelperRoute::findUploadMenuId($this->Itemid);
$bootstrapHelper    = $this->bootstrapHelper;
$config             = $this->config;
$user				= Factory::getApplication()->getIdentity();
$userId             = $user->id;
if($this->state->get('filter_order_Dir') == "asc")
{
    $icon = '<i class="edicon edicon-circle-up"></i>';
}
else
{
    $icon = '<i class="edicon edicon-circle-down"></i>';
}
?>
<style>
.edocman-page-heading
{
	font-size: <?php echo ($this->config->heading_font_size == '') ? '30' : $this->config->heading_font_size; ?>px !important;
}
</style>
<div id="edocman-category-page-default" class="edocman-container <?php echo $bootstrapHelper->getClassMapping('row-fluid'); ?>">
	<div class="<?php echo $bootstrapHelper->getClassMapping('span12'); ?>">
		<form method="post" name="adminForm" id="adminForm" action="<?php echo Route::_('index.php?option=com_edocman&view=userdocuments&Itemid='.$this->Itemid); ?>">
			<div class="<?php echo $bootstrapHelper->getClassMapping('row-fluid'); ?>">
				<div class="<?php echo $bootstrapHelper->getClassMapping('span6'); ?>">
					<h1 class="edocman-page-heading">
						<?php echo Text::_('EDOCMAN_MANAGE_DOCUMENTS'); ?>
					</h1>
				</div>
				<div class="<?php echo $bootstrapHelper->getClassMapping('span6'); ?>" style="text-align:right;">
					<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_DOWNLOAD_SELECTED_ITEMS'); ?>')) { Joomla.submitbutton('document.bulkdownload'); } }"
							 class="edocman_upload_link btn btn-primary">
						<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-download" viewBox="0 0 16 16">
						  <path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5"/>
						  <path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
						</svg>
						&nbsp;<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
					</a>
					<?php
					if($user->authorise('core.create','com_edocman'))
					{
					?>
						<a href="<?php echo Route::_('index.php?option=com_edocman&view=document&layout=edit&Itemid=' . $uploadItemId); ?>" class="edocman_upload_link btn btn-primary"><i class="edicon edicon-upload"></i>&nbsp;<?php echo Text::_('EDOCMAN_UPLOAD'); ?></a>
					<?php
					}
					?>
					<?php
					if($user->authorise('core.delete','com_edocman') || $user->authorise('edocman.deleteown','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('document.delete'); } }"
							 class="edocman_upload_link btn btn-primary">
							<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 $bootstrapHelper->getClassMapping('row-fluid'); ?>">
				
				<div class="<?php echo $bootstrapHelper->getClassMapping('span12'); ?> calendarfilter">
					<div class="ed-filters">
						<div class="filter-fields">
							<div class="filter-field">
								<?php echo HTMLHelper::calendar(Factory::getApplication()->getInput()->getString('filter_date_from'),'filter_date_from','filter_date_from','%Y-%m-%d', array('class'=> $bootstrapHelper->getClassMapping('input-small').' ishort', 'placeholder' => Text::_('EDOCMAN_FROM')));?>
							</div>
							<div class="filter-field">
								<?php echo HTMLHelper::calendar(Factory::getApplication()->getInput()->getString('filter_date_to'),'filter_date_to','filter_date_to','%Y-%m-%d', array('class'=> $bootstrapHelper->getClassMapping('input-small').' ishort', 'placeholder' => Text::_('EDOCMAN_TO')));?>
							</div>
							<div class="filter-field">
								<?php echo $this->lists['categories']; ?>
							</div>
							<div class="filter-field">
								<button onClick="javascript:document.adminForm.submit();" class="submitBtn btn btn-primary" title="<?php echo Text::_('EDOCMAN_FILTER');?>"><?php echo Text::_('EDOCMAN_FILTER');?></button>
							</div>
						</div>
					</div>
				</div>
			</div>
			<?php
				if (count($this->items))
				{
				?>
					<table class="table table-striped table-bordered table-condensed" id="userdocumentstable">
						<thead>
							<tr>
								<th width="2%" class="w-1 center">
									<input type="checkbox" name="toggle" value="" onclick="Joomla.checkAll(this);" />
									<span class="visually-hidden">Check all</span>
								</th>
								<th class="edocman-title-col">
									<a href="javascript:void(0);" onclick="javascript:sortItems('tbl.title');">
										<?php echo Text::_('EDOCMAN_TITLE'); ?>
									</a>
									<?php
									if($this->state->get('filter_order') == "tbl.title")
									{
										echo $icon;
									}
									?>
								</th>
								<th class="edocman-createddate-col center">
									<a href="javascript:void(0);" onclick="javascript:sortItems('tbl.created_time');">
										<?php echo Text::_('EDOCMAN_CREATED_DATE'); ?>
									</a>
									<?php
									if($this->state->get('filter_order') == "tbl.created_time")
									{
										echo $icon;
									}
									?>
								</th>
								<th class="edocman-filetype-col">
									<?php echo Text::_('EDOCMAN_FILE_TYPE'); ?>
								</th>
								<th class="edocman-filesize-col">
									<a href="javascript:void(0);" onclick="javascript:sortItems('tbl.file_size');">
										<?php echo Text::_('EDOCMAN_FILESIZE'); ?>
									</a>
									<?php
									if($this->state->get('filter_order') == "tbl.file_size")
									{
										echo $icon;
									}
									?>
								</th>
								<?php
								if($config->lock_function){
								?>
									<th class="edocman-lock-col" aria-label="<?php echo Text::_('EDOCMAN_DOCUMENT_LOOKED');?>">

									</th>
								<?php
								}
								?>
								<th class="center edocman-edit-delete"><span class="visually-hidden"><?php echo Text::_('EDOCMAN_EDIT_DELETE_DOCUMENTS');?></span></th>
							</tr>
						</thead>
						<tbody>
							<?php
							$total = 0 ;
							$activeItemid = $this->defaultItemid;
							$catId = 0;
							for ($i = 0 , $n = count($this->items) ; $i < $n; $i++)
							{
								$item = $this->items[$i] ;
                                $canEdit					= $user->authorise('core.edit',		'com_edocman.document.'.$item->id);
                                $canDelete					= $user->authorise('core.delete',		'com_edocman.document.'.$item->id);
                                if(!$canDelete)
                                {
                                    $canDelete				= $user->authorise('edocman.deleteown',	'com_edocman.document.'.$item->id) && ($item->created_user_id == $userId);
									if(!$canDelete)
									{
										$canDelete					= $user->authorise('edocman.deleteown',	'com_edocman') && $item->created_user_id == $userId && $item->created_user_id > 0;
									}
                                }
                                $canEditOwn					= $user->authorise('core.edit.own',	'com_edocman') && $item->created_user_id == $userId && $item->created_user_id > 0;
								$catId = $categoryId;
								$item->data = new EDocman_File($item->id,$item->filename, $this->path);
								$Itemid = EDocmanHelperRoute::getDocumentMenuId($item->id, $catId, $activeItemid);
								$url = Route::_('index.php?option=com_edocman&view=document&id='.$item->id.'&catid='.$catId.'&Itemid='.$Itemid);
								?>
								<tr>
									<td class="center">
										<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
									</td>
									<td data-label="<?php echo Text::_('EDOCMAN_TITLE'); ?>">
										<a href="<?php echo $url; ?>" target="_blank"><?php echo $item->title; ?></a>
										<?php
										if(($item->publish_down != "") && ($item->publish_down != "0000-00-00 00:00:00") && (strtotime($item->publish_down) < time())){
											?>
											<span class="expired"><?php echo Text::_('EDOCMAN_EXPIRED');?></span>
											<?php
										}
										?>
										<BR />
										<small>
											<?php
											$category = EdocmanHelper::getDocumentCategory($item->id);
											echo $category->title;
											?>
										</small>
									</td>
									<td class="center" data-label="<?php echo Text::_('EDOCMAN_CREATED_DATE'); ?>">
										<?php echo HTMLHelper::_('date', $item->created_time, $config->date_format, null); ?>
									</td>
									<td data-label="<?php echo Text::_('EDOCMAN_FILE_TYPE'); ?>">
										<?php echo $item->data->mime; ?>
									</td>
									<td data-label="<?php echo Text::_('EDOCMAN_FILESIZE'); ?>">
										<?php echo $item->data->size; ?>
									</td>
									<?php
									if($config->lock_function){
									?>
									<td data-label="">
										<?php
										if($item->is_locked){
											?>
											<span class="red"><?php echo Text::_('EDOCMAN_LOCKED');?></span>
											<?php
										}else{
											?>
											<span class="green"><?php echo Text::_('EDOCMAN_UNLOCKED');?></span>
											<?php
										}
										?>
									</td>
									<?php
									}
									?>
									<td class="center" data-label="<?php echo Text::_('EDOCMAN_EDIT'); ?>/ <?php echo Text::_('EDOCMAN_DELETE'); ?>" style="text-align:center;">
										<?php
										if($canEdit || $canEditOwn)
										{
										?>
										<a class="btn <?php echo $extraButtonClass;?>" title="<?php echo Text::_('EDOCMAN_EDIT_DOCUMENT');?>" aria-label="<?php echo Text::_('EDOCMAN_EDIT_DOCUMENT');?>" href="<?php echo Route::_('index.php?option=com_edocman&view=document&layout=edit&id='.$item->id.'&Itemid='.$Itemid) ; ?>">
											<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil" viewBox="0 0 16 16">
											  <path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/>
											</svg>
										</a>
										<?php
										}
										if($canDelete)
										{
										?>
										<a class="btn <?php echo $extraButtonClass;?>" href="javascript:deleteConfirm(<?php echo $item->id; ?>);" title="<?php echo Text::_('EDOCMAN_DELETE_DOCUMENT');?>" aria-label="<?php echo Text::_('EDOCMAN_DELETE_DOCUMENT');?>">
											<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>
										</a>
										<?php } ?>
									</td>
								</tr>
							<?php
							}
							?>
						</tbody>
					</table>
				<?php
				}
				else
				{
					echo Text::_('EDOCMAN_NO_DOCUMENT_UPLOADED_YET');
				}
				if ($this->pagination->total > $this->pagination->limit)
				{
					?>
					<div class="pagination">
						<?php echo $this->pagination->getPagesLinks(); ?>
					</div>
				<?php
				}

			?>
			<input type="hidden" name="cid[]" value="0" id="document_id" />
			<input type="hidden" name="boxchecked" value="0" />
			<input type="hidden" name="category_id" value="0" />
			<input type="hidden" name="task" value="" />
			<input type="hidden" name="view" value="userdocuments" />
			<input type="hidden" name="filter_order" id="filter_order" value="<?php echo $this->state->get('filter_order');?>" />
			<input type="hidden" name="filter_order_Dir" id="filter_order_Dir" value="<?php echo $this->state->get('filter_order_Dir');?>" />
			<input type="hidden" name="redirect" value="<?php echo (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";?>" />
			<?php echo HTMLHelper::_('form.token'); ?>

			<script type="text/javascript">
				function deleteConfirm(id)
				{
					var msg = "<?php echo Text::_('EDOCMAN_DELETE_CONFIRM'); ?>";
					if (confirm(msg))
					{
						var form = document.adminForm ;
						form.task.value = 'document.delete';
						document.getElementById('document_id').value = id;
						form.submit();
					}
				}
				function sortItems(col)
				{
					var filter_order = document.getElementById('filter_order');
					var filter_order_Dir = document.getElementById('filter_order_Dir');
					if (col == filter_order.value)
					{
						if(filter_order_Dir.value == "asc")
						{
							filter_order_Dir.value = "desc";
						}
						else
						{
							filter_order_Dir.value = "asc";
						}
					}
					else
					{
						filter_order.value = col;
						filter_order_Dir.value = "asc"
					}
					var form = document.adminForm ;
					form.submit();
				}
			</script>
		</form>
	</div>
</div>
