<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace com_eorisis_pixels\framework\lib\core\cms;
\defined('_JEXEC') or die;

use EorisisRules, EorisisAccess, JLoader;
use com_eorisis_pixels\framework\lib\core\app\config as app_config;
use com_eorisis_pixels\framework\lib\core\cms\user as cms_user;
use com_eorisis_pixels\framework\lib\core\cms\application;
use com_eorisis_pixels\framework\lib\core\model\database;

if (\version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisUser', '\\Joomla\\CMS\\User\\User');
	JLoader::registerAlias('EorisisRules', '\\Joomla\\CMS\\Access\\Rules');
	JLoader::registerAlias('EorisisAccess', '\\Joomla\\CMS\\Access\\Access');
}
else
{
	JLoader::registerAlias('EorisisUser', 'JUser');
	JLoader::registerAlias('EorisisRules', 'JAccessRules');
	JLoader::registerAlias('EorisisAccess', 'JAccess');
}

class access
{
	public function __construct() {}

	// --------------------------------------------------

	public static function authorise(string $task, $assetname = null)
	{
		return cms_user::obj()->authorise($task, $assetname);
	}

	// --------------------------------------------------

	public static function auth(string $task)
	{
		return self::authorise($task, app_config::COMPONENT_ELEMENT);
	}

	// -----------------------------------------------------

	public static function new_rules($input = '')
	{
		return new EorisisRules($input);
	}

	// --------------------------------------------------

	public static function asset_id(string $asset_name)
	{
		$dbo = application::dbo();
		$query = $dbo->getQuery(true);
		$query->select($dbo->quoteName('id'));
		$query->from($dbo->quoteName('#__assets'));
		$query->where($dbo->quoteName('name').'='.$dbo->quote($asset_name));
		$dbo->setQuery($query);
		$result = database::get_first_result($dbo);
		if ($result['error'])
		{
			return false;
		}

		$asset_id = (int)$result['record'];
		if ($asset_id)
		{
			return self::asset_rules($asset_id);
		}

		// Fallback to Component rules
		$query = $dbo->getQuery(true);
		$query->select($dbo->quoteName('id'));
		$query->from($dbo->quoteName('#__assets'));
		$query->where($dbo->quoteName('name').'='.$dbo->quote(app_config::COMPONENT_ELEMENT));
		$dbo->setQuery($query);
		$result = database::get_first_result($dbo);
		if ($result['error'])
		{
			return false;
		}

		$asset_id = (int)$result['record'];
		return self::asset_rules($asset_id);
	}

	// -----------------------------------------------------

	protected static function asset_rules($assetKey, $recursive = false, $recursiveParentAsset = true, $preload = true)
	{
		return EorisisAccess::getAssetRules($assetKey, $recursive, $recursiveParentAsset, $preload);
	}
}
