<?php

/**
 * @version        1.11.1
 * @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\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\CMS\Router\Route;

$user = Factory::getApplication()->getIdentity();
if (count($categories))
{
	$return = base64_encode(Uri::getInstance()->toString());
	if ($categoryId && $config->show_subcategory_text)
	{
	?>
		<h2 class="edocman-heading"><i class="edicon edicon-folder-open"></i>&nbsp;<?php echo Text::_('EDOCMAN_SUB_CATEGORIES'); ?></h2>
	<?php
	}
	if((int) $number_categories > 0)
    {
        $numberColumns = $number_categories;
    }
	elseif (isset($config->number_subcategories))
	{
		$numberColumns = $config->number_subcategories ;
	}
	else
	{
		$numberColumns = 1 ;
	}
	$span = intval(12 / $numberColumns);
	if ($span != 12)
	{
		$spanClass = ' '.$bootstrapHelper->getClassMapping('span'.$span);
	}
	else
	{
		$spanClass = '';
	}
	$rowFluidClass = $bootstrapHelper->getClassMapping('row-fluid');
	?>
	<style>
	h3.edocman-category-title
	{
		font-size: <?php echo ($config->category_title_font_size == '') ? '25' : $config->category_title_font_size; ?>px !important;
	}
	</style>
	<div id="edocman-categories" class="categories-default">
		<div class="<?php echo $rowFluidClass; ?> clearfix">
		<?php
		$j = 0;
		for ($i = 0 , $n = count($categories) ; $i < $n ; $i++)
		{
			$category = $categories[$i];
			if (!$config->show_empty_cat && !$category->total_documents)
			{
				continue ;
			}
			if ($category->image && is_file(Path::clean(JPATH_ROOT.'/media/com_edocman/category/thumbs/'.$category->image)))
			{
				$imgUrl = Uri::base().'media/com_edocman/category/thumbs/'.$category->image;
			}
			else
			{
				if (!isset($config->show_default_category_thumbnail) || $config->show_default_category_thumbnail)
				{
					$imgUrl = Uri::base().'components/com_edocman/assets/images/icons/32x32/folder.png' ;
				}
				else
				{
					$imgUrl = '';
				}
			}
			//if ($numberColumns != 1 && ($i % $numberColumns == 0))
			//{
			?>
			<!--<div class="clearfix">-->
			<?php
			//}
			$j++;
			?>
			<div class="edocman-category<?php echo $spanClass; ?>">
				<div class="edocman-box-heading">
					<div class="edocman-box-heading-title">
						<h3 class="edocman-category-title">
							<?php
							//echo EDocmanHelperRoute::getCategoryRoute($category->id, $Itemid);
							?>
							<a href="<?php echo Route::_(EDocmanHelperRoute::getCategoryRoute($category->id, $Itemid)); ?>" class="edocman-category-title-link" style="display: inline-block;">
								<?php
								if($config->show_icon_beside_title)
								{
								?>
									<i class="edicon edicon-folder"></i>
								<?php } ?>
								<?php
									echo $category->title;
									if ($config->show_number_documents)
									{
									?>
										<small>( <?php echo $category->total_documents ;?> <?php echo $category->total_documents != 1 ? Text::_('EDOCMAN_DOCUMENTS') :  Text::_('EDOCMAN_DOCUMENT') ; ?> )</small>
									<?php
									}
								?>
							</a>
							<?php
								if ($config->enable_rss && $category->id > 0)
								{
								?>
									<span class="edocman-rss-icon">
										<a href="<?php echo Route::_('index.php?option=com_edocman&view=category&id='.$category->id.'&format=feed&type=rss'); ?>"><img src="<?php echo Uri::root().'/components/com_edocman/assets/images/rss.png' ?>" /></a>
									</span>
								<?php
								}
								//edit & delete
								if($categoryId > 0)
								{
									if ($user->authorise('edocman.manage_categories', 'com_edocman') || $user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$categoryId))
									{
										
										$url = Route::_('index.php?option=com_edocman&task=editcategory&cid[]='.$category->id.'&return='.$return.'&Itemid='.$Itemid) ;
										?>
										<a class="documents_table_edit" href="<?php echo $url; ?>" title="<?php echo Text::_('EDOCMAN_EDIT'); ?>">
											<i class="edocman-icon-pencil"></i>
										</a>
										<?php
									}
									if ($user->authorise('edocman.manage_categories', 'com_edocman') || $user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$categoryId))
									{
										?>
										<a class="documents_table_delete" href="javascript:deleteCategoryConfirm(<?php echo $category->id; ?>);" title="<?php echo Text::_('EDOCMAN_DELETE'); ?>">
											<i class="edocman-icon-trash"></i>
										</a>
										<?php
									}
								}
							?>
						</h3>
					</div>
					<div class="edocman-box-heading-tasks documents-category-<?php echo $category->id;?>">
						<div class="edocman-documents-default-title-dropdown-menu-icon" onclick="toggleCategoryMenu('<?php echo $category->id;?>')">
							<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-three-dots-vertical" viewBox="0 0 16 16">
							  <path d="M9.5 13a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0m0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0"/>
							</svg>
						</div>
						<div class="edocman-document-default-title-dropdown-menu-popup" id="menuPopup_category_<?php echo $category->id;?>">
							<a href="<?php echo Route::_(EDocmanHelperRoute::getCategoryRoute($category->id, $Itemid)); ?>" class="edocman-category-gallery-title-link" target="_blank" title="<?php echo $category->title;?>"><?php echo Text::_('EDOCMAN_OPEN_CATEGORY_IN_NEW_TAB')?></a>
							<?php
							if ($user->authorise('edocman.manage_categories', 'com_edocman') || $user->authorise('edocman.manage_subcategories', 'com_edocman.category.'.$category->id))
							{
								$url = Route::_('index.php?option=com_edocman&task=editcategory&cid[]='.$category->id.'&return='.$return.'&Itemid='.$Itemid) ;
							?>
								<a href="<?php echo $url;?>" title="<?php echo Text::_('EDOCMAN_EDIT');?>"><?php echo Text::_('EDOCMAN_EDIT');?></a>
							<?php
							}	
							?>
						</div>
						<script>
						document.addEventListener("click", function(event) {
							var menuContainer = document.querySelector(".documents-category-<?php echo $category->id;?>");
							if (!menuContainer.contains(event.target)) {
								document.getElementById("menuPopup_category_<?php echo $category->id?>").style.display = "none";
							}
						});
						</script>
					</div>
				</div>
				<?php
				if((int)$subscat == 1)
				{
                    if(($category->description || $imgUrl) && $config->show_subcategory_description)
                    {
                    ?>
                        <div class="edocman-description clearfix">
                            <?php
                                if ($imgUrl && $config->show_subcategory_icon)
                                {
                                ?>
                                    <img class="edocman-thumb-left" src="<?php echo $imgUrl; ?>" alt="<?php echo $category->title; ?>" />
                                <?php
                                }
                                if((int)$config->number_cwords == 0)
								{
                                    echo $category->description;
                                }
								else
								{
                                    $description = strip_tags($category->description);
                                    if((int)$config->number_cwords > 0)
									{
                                        $descriptionArr = explode(" ",$description);
                                        if(count($descriptionArr) > (int)$config->cnumber_words)
										{
                                            for($d = 0;$d < (int)$config->number_cwords - 1;$d++)
											{
                                                echo $descriptionArr[$d]." ";
                                            }
                                            echo "..";
                                        }
										else
										{
                                            echo $description;
                                        }
                                    }
									else
									{
                                        echo $description;
                                    }
                                }
                            ?>
                        </div>
                    <?php
                    }
					elseif(! $config->show_subcategory_description && $imgUrl && $config->show_subcategory_icon)
					{
						?>
							<img class="edocman-thumb-left" src="<?php echo $imgUrl; ?>" alt="<?php echo $category->title; ?>" />
						<?php
					}
				}
				elseif($category->description || $imgUrl)
				{
				?>
					<div class="edocman-description clearfix">
						<?php
							if ($imgUrl)
							{
							?>
								<img class="edocman-thumb-left" src="<?php echo $imgUrl; ?>" alt="<?php echo $category->title; ?>" />
							<?php
							}
							$description = strip_tags($category->description);
							if((int)$config->number_cwords > 0){
								$descriptionArr = explode(" ",$description);
								if(count($descriptionArr) > (int)$config->cnumber_words){
									for($d = 0;$d < (int)$config->number_cwords - 1;$d++){
										echo $descriptionArr[$d]." ";
									}
									echo "..";
								}else{
									echo $description;
								}
							}else{
								echo $description;
							}
						?>
					</div>
				<?php
				}
				?>
			</div>
		<?php
			if($j == $numberColumns)
			{
				?>
				</div><div class="<?php echo $rowFluidClass; ?> clearfix">
				<?php
				$j = 0;
			}
		}
		?>
		</div>
	</div>
<?php
}
?>
<script type="text/javascript">
    function deleteCategoryConfirm(id) {
        var msg = "<?php echo Text::_('EDOCMAN_DELETE_CONFIRM'); ?>";
        if (confirm(msg))
        {
            location.href = "<?php echo Uri::root()?>index.php?option=com_edocman&task=deletecategory&cid[]=" + id + "&parentId=<?php echo $categoryId?>&return=<?php echo $return;?>";
        }
    }
	function toggleCategoryMenu(documentId) 
	{
		var menu = document.getElementById("menuPopup_category_" + documentId);
		menu.style.display = (menu.style.display === "block") ? "none" : "block";
	}
</script>
