<?php
/**
 * @version        1.9.7
 * @package        Joomla
 * @subpackage     Edocman
 * @author         Tuan Pham Ngoc
 * @copyright      Copyright (C) 2011 - 2025 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

// No direct access
defined('_JEXEC') or die();
use Joomla\CMS\Factory;
use Joomla\String\StringHelper;
use Joomla\CMS\Language\Text;
use Joomla\Filesystem\Path;
/**
 * Category controller class.
 */
class EDocmanControllerCategory extends EDocmanController
{
	protected function allowAdd($data = array())
	{
		$user = Factory::getApplication()->getIdentity();

		if (isset($data['parent_id']))
		{
			$categoryId = (int) $data['parent_id'];
		}
		else
		{
			$categoryId = 0;
		}

		if ($categoryId)
		{
			return $user->authorise('core.create', 'com_edocman.category.' . $categoryId);
		}
		else
		{
			return $user->authorise('core.create', 'com_edocman') || count(EDocmanHelper::getAuthorisedCategories('core.create'));
		}
	}

	/**
	 * Method to check whether edit function is allowed for the given category
	 *
	 * @see OSControllerAdmin::allowEdit()
	 */
	function allowEdit($data = array())
	{
		$id   = isset($data['id']) ? (int) $data['id'] : 0;
		$user = Factory::getApplication()->getIdentity();
		if ($id)
		{
			if ($user->authorise('core.edit', 'com_edocman.category.' . $id))
			{
				return true;
			}

			if ($user->authorise('core.edit.own', 'com_edocman.category.' . $id))
			{
				$item = $this->getModel()->getData();

				return $item->created_user_id == $user->id;
			}

			return false;
		}
		else
		{
			return parent::allowEdit($data);
		}
	}

	function allowEditState($id)
	{
		if ((int) $id > 0)
		{
			return Factory::getApplication()->getIdentity()->authorise('core.edit.state', 'com_edocman.category.' . (int) $id);
		}
		else
		{
			return parent::allowEditState($id);
		}
	}

	function createAliases()
	{
		$db = Factory::getContainer()->get('db');
		$db->setQuery("Select id, title from #__edocman_categories where `alias` = ''");
		$rows = $db->loadObjectList();
		if(count($rows))
		{
			foreach($rows as $row)
			{
				$alias = $row->title;
				$alias = strtolower($alias);
				$alias = str_replace(" ", "", $alias);
				$db->setQuery('Select count(id) from #__edocman_categories where alias = '.$db->quote($alias));
				$found = (int) $db->loadResult();
				if ($found)
				{
					$alias = StringHelper::increment($alias, 'dash');
				}
				$db->setQuery("Update #__edocman_categories set `alias` = '$alias' where id = '$row->id'");
				$db->execute();
			}
		}
	}

	function updateCategoryPaths()
	{
		$config		= EdocmanHelper::getConfig();
		$db			= Factory::getContainer()->get('db');
		$db->setQuery("Select * from `#__edocman_categories` where parent_id = '0'");
		$categories = $db->loadObjectList();
		foreach($categories as $category)
		{
			$path	= $category->path;
			if(! is_dir(Path::clean($config->documents_path.'/'.$path)))
			{
				//JFolder::create($config->documents_path.'/'.$path);
			}
			EdocmanHelper::updateSubCategoryPaths($category->id);
		}
	}

	function generateCategoriesJson()
	{
		
		$firstText = Text::_('EDOCMAN_SELECT_CATEGORY');
		
		EdocmanHelper::jsonCategories($firstText, 1);
	}
}
