<?php

/**
 * @version        1.11.3
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011-2023 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\Router\Route;
use Joomla\CMS\Language\Text;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\CMS\Uri\Uri;
use Joomla\String\StringHelper;

$config             = $this->config;
$session		= Factory::getApplication()->getSession();
if(!$config->onetime_collect){
	$session->set('name','');
	$session->set('email','');
}
$name			= $session->get('name','');
$email			= $session->get('email','');
$btnClass		= $this->bootstrapHelper->getClassMapping('btn');
require_once JPATH_ROOT.'/components/com_edocman/helper/file.class.php' ;
$bootstrapHelper    = $this->bootstrapHelper;

//EDocmanHelperJquery::colorbox('edocman-modal');
//EDocmanHelperJquery::colorbox('modal');

$user  = Factory::getApplication()->getIdentity() ;
$userId = $user->id ;

if ($config->collect_downloader_information && !$userId && ($name == '' || $email == ''))
{
	$showDownloadForm = true;
}
else
{
	$showDownloadForm = false;
}

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 ($config->heading_font_size == '') ? '30px' : $config->heading_font_size; ?>px !important;
}
</style>
<div id="edocman-category-page-default" class="edocman-container <?php echo $bootstrapHelper->getClassMapping('row-fluid'); ?>">
	<form method="post" name="adminForm" id="edocman_form" action="<?php echo Route::_('index.php?option=com_edocman&view=userdocuments&Itemid='.$this->Itemid); ?>">
		<h1 class="edocman-page-heading">
			<?php echo Text::_('EDOCMAN_BOOKMARK_DOCUMENTS'); ?>
		</h1>
		<?php
			if (count($this->items))
			{
			?>
				<table class="table-condensed table-document" id="table-document">
					<thead>
						<tr>
                            <th class="edocman-document-icon-col">
                            </th>
                            <th class="edocman-document-title-col">
                                <?php echo Text::_('EDOCMAN_TITLE'); ?>
                                <?php
                                if($this->state->get('filter_order') == "tbl.title")
                                {
                                    echo $icon;
			                    }
                                ?>
							</th>
							<th class="edocman-table-download-col aligncenter">
								<?php echo Text::_('EDOCMAN_DOWNLOAD'); ?>
							</th>
                            <th class="edocman-table-download-col aligncenter">
                                <?php
                                echo Text::_('EDOCMAN_DELETE');
                                ?>
                            </th>
						</tr>
					</thead>
					<tbody>
						<?php
						$total = 0 ;
						$activeItemid = $this->defaultItemid;
						$catId = 0;
						for ($i = 0 , $n = count($this->items) ; $i < $n; $i++)
						{
							$catId = $categoryId;
							$item = $this->items[$i] ;
							$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);
							$canEdit	            = $user->authorise('core.edit',			'com_edocman.document.'.$item->id);
							$canEditOwn	            = $user->authorise('core.edit.own',		'com_edocman.document.'.$item->id) && $item->created_user_id == $userId;
							$canDownload            = $user->authorise('edocman.download', 'com_edocman.document.'.$item->id) ;
							$canDownload    = ($item->created_user_id == $userId && $item->created_user_id > 0) || ( ($item->user_ids == "" || $item->user_ids == 0) && ($canDownload || $canEdit)) || ($item->user_ids != '' && $item->user_ids != '0' && in_array($userId, explode(',', $item->user_ids))) ;
							$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;
							}
							$hide_download_button   = $config->hide_download_button;
							$hide_download          = $category->hide_download;
							if((int)$hide_download == 1)
							{
								$hide_download_button = 1;
							}
							elseif((int)$hide_download == 2)
							{
								$hide_download_button = 0;
							}
							if ($hide_download_button != 1)
							{
								$downloadUrl        = Route::_('index.php?option=com_edocman&task=document.download&id='.$item->id.'&Itemid='.$Itemid) ;
							}
							else
							{
								$downloadUrl        = Route::_('index.php?option=com_edocman&task=document.viewdoc&id='.$item->id.'&Itemid='.$Itemid) ;
							}
							$show_view_button = $config->show_view_button;
							$show_view = $category->show_view;

							if((int)$show_view == 1)
							{
								$show_view_button = 1;
							}
							elseif((int)$show_view == 2)
							{
								$show_view_button = 0;
							}
							if ($config->category_table_show_filetype || $config->category_table_show_filesize)
							{
								require_once JPATH_ROOT.'/components/com_edocman/helper/file.class.php' ;
								$item->data         = new EDocman_File($item->id,$item->filename, $config->documents_path) ;
							}
							$fileName               = $item->filename;
							if($fileName != "")
							{
								$fileExt            = strtolower(EdocmanHelper::getExt($fileName));
							}
							else
							{
								$fileExt            = strtolower(EdocmanHelper::getExt($item->document_url));
							}

							$accept_license         = 0;
							$show_accept_license		= 0;
							if($config->accept_license && ($item->license_id > 0 || EdocmanHelper::getDefaultLicense() > 0))
							{
								if($config->accept_license_one_time)
								{
									$alreadyAcceptLicense	= $session->get('alreadyAcceptLicense',0);
									if($alreadyAcceptLicense == 0)
									{
										$show_accept_license = 1;
									}
									else
									{
										$show_accept_license = 0;
									}
								}
								else
								{
									$show_accept_license = 1; 
								}
							}

							if($show_accept_license == 1 || $item->download_password != "")
							{
								$accept_license = 1;
							}
							if ($item->image != '' && is_file(Path::clean(JPATH_ROOT.'/media/com_edocman/document/thumbs/'.$item->image)))
							{
								$imgSrc     = Uri::base().'media/com_edocman/document/thumbs/'.$item->image ;
							}
							else
							{
								$imgSrc     = '';
							}
							?>
							<tr>
                                <td class="edocman-document-icon-td hidden-phone" data-label="">
                                    <?php 
									EDocmanHelperHtml::showFileIcon($item->data);
									//echo $item->data->fileicon; ?>
                                </td>
								<td data-label="<?php echo Text::_('EDOCMAN_TITLE'); ?>" class="edocman-document-title-td">
									<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
									}
									?>
                                    <div class="clearfix"></div>
                                    <?php
                                    if(($config->show_number_downloaded) && ($item->downloads > 0))
                                    {
                                        ?>
                                        <div class="downloadinformation">
                                            <i class="edicon edicon-download2"></i>&nbsp;<?php echo $item->downloads?> <?php echo Text::_('EDOCMAN_DOWNLOADS');?>
                                        </div>
                                        <?php
                                    }
                                    if($config->category_table_show_filesize == 1 && trim($item->data->size) != "")
                                    {
                                        $tempArr[] = $item->data->size;
                                        ?>
                                        <div class="sizeinformation">
                                            <i class="edicon edicon-database"></i>&nbsp;<?php echo $item->data->size; ?>
                                        </div>
                                        <?php
                                    }
                                    if ($config->show_publish_date)
                                    {
                                        ?>
                                        <div class="dateinformation">
                                            <i class="edicon edicon-calendar"></i>&nbsp;<?php echo HTMLHelper::_('date', $item->created_time, $config->date_format, null); ?>
                                        </div>
                                        <?php
                                    }
                                    ?>
								</td>
								<td class="center edocman-document-desc-col aligncenter" data-label="">
									<?php
										if ($canDownload && $show_view_button == 1 && $item->canView)
										{
											$playextension	= array('mp4','flv','mp3','ogg','ogv','png','jpg','jpeg','gif','m4a','oga');
											$audio_array = array('mp3','ogg');
											$frame_array = array('flv');
											$ext = StringHelper::strtolower(EDocmanHelper::getExt($item)) ;
											if($item->view_url != "")
											{
												$showModal = EdocmanHelper::isPopularViewLink($item->view_url);
											}
											else
											{
												$showModal = true;
											}
											if((in_array($ext,$playextension) || $item->view_url != "") && $showModal && !EDocmanHelper::isDropBoxTurnedOn() && !EDocmanHelper::isAmazonS3TurnedOn())
											{
												$viewUrl = Uri::root()."index.php?option=com_edocman&view=play&id=".$item->id."&tmpl=component";
												if(in_array($ext,$audio_array))
												{
													$audio_player = "rel=\"{handler: 'iframe', size: {x: 300, y: 50}, iframeOptions: {scrolling: 'no'}}\"";
												}
												elseif(in_array($ext,$frame_array))
												{
													$audio_player = "rel=\"{handler: 'iframe', size: {x: 450, y: 300}, iframeOptions: {scrolling: 'no'}}\"";
												}
												else
												{
													$audio_player = "";
												}
												?>
												<?php
												if($config->view_option == 0)
												{
												?>
													<a href="<?php echo $viewUrl; ?>" class="edocman-download-link edocman-modal" data-toggle="modal" <?php echo $audio_player;?> aria-label="<?php echo Text::_('EDOCMAN_VIEW'); ?>">
														<span class="edocman_download_label">
															<i class="edicon edicon-eye"></i>
															<?php echo Text::_('EDOCMAN_VIEW'); ?>
														</span>
													</a>
												<?php
												}
												else
												{
												?>
													<a href="<?php echo $viewUrl; ?>" class="edocman-download-link" target="_blank" data-toggle="modal" <?php echo $audio_player;?> aria-label="<?php echo Text::_('EDOCMAN_VIEW'); ?>">
														<span class="edocman_download_label">
															<i class="edicon edicon-eye"></i>
															<?php echo Text::_('EDOCMAN_VIEW'); ?>
														</span>
													</a>
												<?php
												}
												?>
												<?php
											}
											else 
											{

												$viewUrl = Route::_('index.php?option=com_edocman&task=document.viewdoc&id=' . $item->id . '&Itemid=' . $Itemid);
												?>
												<a class="edocman-download-link" href="<?php echo $viewUrl; ?>"
												   target="_blank" aria-label="<?php echo Text::_('EDOCMAN_VIEW'); ?>">
													<span class="edocman_download_label">
														<i class="edicon edicon-eye"></i>
														<?php echo Text::_('EDOCMAN_VIEW'); ?>
													</span>
												</a>
												<?php
											}
											echo "&nbsp;";
										}

										if($item->document_url != ""){
											if($config->external_download_link == 1){
												$target = "_blank";
											}else{
												$target = "_self";
											}
										}else{
											$target = "_self";
										}
										if ($canDownload && $hide_download_button != 1 && $accept_license == 0)
										{
											if ($showDownloadForm)
											{
												?>
													<a data-toggle="modal" data-document-title="<?php echo $item->title; ?>" class="email-popup edocman-download-link" href="#form-content" id="<?php echo $item->id; ?>" target="<?php echo $target;?>" title="<?php echo Text::_('EDOCMAN_CLICK_HERE_TO_DOWNLOAD_DOCUMENT');?>" aria-label="<?php echo Text::_('EDOCMAN_CLICK_HERE_TO_DOWNLOAD_DOCUMENT'); ?>">
														<span class="edocman_download_label">
															<i class="edicon edicon-cloud-download"></i>
															<?php
															$fileName = $item->filename;
															$fileExt  = strtolower(EdocmanHelper::getExt($fileName));
															echo Text::_('EDOCMAN_DOWNLOAD'); ?>
															<?php
															$tempArr = array();
															if($config->category_table_show_filetype == 1 && $item->document_url == "")
															{
																$tempArr[] = $fileExt;
															}
															if(($item->document_url == "") && (count($tempArr) > 0))
															{
															?>
															(
																<?php echo implode(", ",$tempArr);?>
															)
															<?php
															}
															else
															{
																if($config->category_table_show_filetype == 1)
																{
																?>
																	(<?php echo $fileExt; ?>)
																<?php
																}
															}
															?>
														</span>
													</a>
												<?php
											}
											else
											{
											?>
												<a href="<?php echo $downloadUrl; ?>" class="edocman-download-link" target="<?php echo $target;?>" >
													<span class="edocman_download_label">
														<?php
														if($item->document_url != "")
														{
															?>
																<i class="edicon edicon-link"></i>
															<?php
															echo Text::_('EDOCMAN_OPEN_DOCUMENT');
														}
														else
														{
															?>
																<i class="edicon edicon-cloud-download"></i>
															<?php
															echo Text::_('EDOCMAN_DOWNLOAD');
														}
														?>
														<?php
														$tempArr = array();
														if($config->category_table_show_filetype == 1 && $item->document_url == "")
														{
															$tempArr[] = $fileExt;
														}
														if(($item->document_url == "") && (count($tempArr) > 0))
														{
														?>
														(
															<?php echo implode(", ",$tempArr);?>
														)
														<?php 
														}
														else
														{
															if($config->category_table_show_filetype == 1)
															{
															?>
																(<?php echo $fileExt; ?>)
															<?php
															}
														}
														?>
													</span>
												</a>
											<?php
											}
										}
										elseif(($config->login_to_download) and ((int)$userId == 0) and ($hide_download_button != 1) and ($accept_license == 0))
										{
											if($config->edocman_login == 1)
											{
												EdocmanHelper::generateJoomlaLogin('edocman-download-link edocman-download-btn');
											}
											else
											{
												if(EdocmanHelper::isJoomla4())
												{
													$return = base64_encode(Uri::getInstance()->toString());
												?>
													<a data-toggle="modal" class="modalj4 edocman-download-link edocman-download-btn" href="<?php echo Uri::root();?>index.php?option=com_edocman&view=document&layout=login&id=<?php echo $item->id;?>&tmpl=component&return=<?php echo $return;?>" aria-label="<?php echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); ?>" title="<?php echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD');?>">
												<?php
												}
												else
												{
												?>
													<a data-toggle="modal" class="email-popup edocman-download-link edocman-download-btn" href="#login-form" aria-label="<?php echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); ?>" title="<?php echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD');?>">
												<?php
												}
											}
											?>
											<!--<a data-toggle="modal" class="email-popup edocman-download-link edocman-download-btn" href="#login-form" aria-label="<?php echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); ?>">-->
												<span class="edocman_download_label">
													<?php
													echo Text::_('EDOCMAN_LOGIN_TO_DOWNLOAD'); ?>
												</span>
											</a>
											<?php
										}
										elseif($canDownload && $hide_download_button != 1 && ($accept_license == 1))
										{
											?>
												<a href="<?php echo $url; ?>" class="edocman-download-link edocman-download-btn">
													<span class="edocman_download_label">
														<?php
														if($item->document_url != "")
														{
															echo Text::_('EDOCMAN_OPEN_DOCUMENT');
														}
														else
														{
															echo Text::_('EDOCMAN_DOWNLOAD');
														}
														?>
														<?php
														if($item->document_url == "")
														{
														?>
														(
														<?php echo (($config->category_table_show_filetype == 1 && $item->document_url == "") ? $fileExt:''); ?>
														<?php
														if($config->category_table_show_filetype == 1 && $item->document_url == "" && $config->category_table_show_filesize == 1 && $item->data->size)
														{
															echo ", ";
														}
														?>
														<?php echo ($config->category_table_show_filesize == 1? $item->data->size:''); ?>
														)
														<?php }
														else{
															?>
															(<?php echo $fileExt; ?>)
															<?php
														}
														?>
													</span>
												</a>
											<?php
										}
										?>
									</td>
								<td class="center edocman-table-download-col" data-label="<?php echo Text::_('EDOCMAN_DELETE'); ?>" style="text-align:center;">
									<a class="edocman-download-link" href="javascript:deleteConfirm(<?php echo $item->id; ?>);">
										<i class="edicon edicon-bin white"></i>
									</a>
								</td>
							</tr>
						<?php
						}
						?>
					</tbody>
				</table>
			<?php
			}
			else
			{
				echo Text::_('EDOCMAN_NO_DOCUMENT_IN_BOOKMARK_LIST');
			}
			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="category_id" value="0" />
        <input type="hidden" name="view" value="bookmark" />
		<input type="hidden" name="task" value="" />
        <input type="hidden" name="Itemid" value="<?php echo $this->Itemid; ?>" />
        <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');?>" />
		<?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.getElementById('edocman_form') ;
					form.task.value = 'document.removebookmark';
					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.getElementById('edocman_form') ;
                form.submit();
            }
		</script>
	</form>
</div>
<?php
if ($showDownloadForm  && !EdocmanHelper::isJoomla4())
{
	echo EDocmanHelperHtml::loadCommonLayout('common/modal.php', array('bootstrapHelper' => $bootstrapHelper,'config' => $config));
}

if((!$canDownload && $hide_download_button != 1 && $config->login_to_download && (int)$userId == 0) && !EdocmanHelper::isJoomla4())
{
	echo EDocmanHelperHtml::loadCommonLayout('common/login.php', array('bootstrapHelper' => $bootstrapHelper));
}
?>
