<?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
	}
	$numberColumns = 2 ;
	$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-blog">
		<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 = '';
				}
			}
			
			
			$j++;
			?>
			<div class="edocman-category<?php echo $spanClass; ?>">
				<div class="edocman-blog-box">
					<a href="<?php echo Route::_(EDocmanHelperRoute::getCategoryRoute($category->id, $Itemid)); ?>" class="edocman-blog-box-link">
						<?php
						if($imgUrl != "")
						{
						?>
							<div class="edocman-blog-box-left">
								<img class="edocman-thumb-left" src="<?php echo $imgUrl; ?>" alt="<?php echo $category->title; ?>" />
							</div>
						<?php
						}	
						?>
						<div class="edocman-blog-box-right">
							<div class="edocman-blog-box-right-heading">
								<?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
									}
								?>
							</div>
							<div class="edocman-blog-box-right-description">
								<?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>
						</div>
					</a>
				</div>
			</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;?>";
        }
    }
</script>
