<?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
 */
defined('_JEXEC') or die;

if (!defined('EORISIS_IS_JINSTALLER'))
{
	define('EORISIS_IS_JINSTALLER', isset($_GET['option']) && in_array($_GET['option'], ['com_installer','com_joomlaupdate']));
}

if (EORISIS_IS_JINSTALLER)
{
	return;
}

// Executing the autoloader in the system plugin precedes
$namespace_path = JPATH_ADMINISTRATOR.'/components/com_eorisis_pixels/framework/lib';
require_once $namespace_path.'/eorisis.php';
$eorisis = new \com_eorisis_pixels\framework\lib\eorisis($namespace_path);
$eorisis->register();

use com_eorisis_pixels\framework\lib\core\app\config as app_config;
use com_eorisis_pixels\framework\lib\core\model\head as head_model;
use com_eorisis_pixels\framework\lib\core\model\body as body_model;
use com_eorisis_pixels\framework\lib\core\model\extension;
use com_eorisis_pixels\framework\lib\core\model\area;
use com_eorisis_pixels\framework\lib\core\cms\application;
use com_eorisis_pixels\framework\lib\core\cms\document as cms_document;

if (!class_exists('EorisisPlugin', false))
{
	if (version_compare(JVERSION, '3.8', '>='))
	{
		JLoader::registerAlias('EorisisPlugin', '\\Joomla\\CMS\\Plugin\\CMSPlugin');
	}
	else
	{
		JLoader::registerAlias('EorisisPlugin', 'JPlugin');
	}
}

class PlgSystemEorisis_pixels extends EorisisPlugin
{
	private static $abort = false;
	private static $is_backend = false;
	private static $exclude_traffic = false;

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

	public function __construct(& $subject, $config)
	{
		parent::__construct($subject, $config);
	}

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

	public function onAfterInitialise()
	{
		if (EORISIS_IS_JINSTALLER)
		{
			return;
		}

		self::$is_backend = application::is_backend();

		if (!extension::is_active_component() or !cms_document::is_html())
		{
			self::$abort = true;
			extension::$abort = true;
		}
	}

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

	public function onBeforeRender()
	{
		if (EORISIS_IS_JINSTALLER or self::$abort or !self::$is_backend)
		{
			return;
		}

		area::process_backend();
	}

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

	public function onBeforeCompileHead()
	{
		// Executes before onAfterRender() on HTML and after onAfterRender() on feeds

		if (EORISIS_IS_JINSTALLER or self::$abort)
		{
			return;
		}

		if (self::$is_backend)
		{
			app_config::init();
			\com_eorisis_pixels\framework\lib\core\cms\head::add_js_uri([
				'name' => 'eorisis-notifications',
				'uri' => app_config::media_uri_relative(true).'/assets/js/notifications.js',
				'version' => true
			]);
			return;
		}

		$head = new head_model;
		$head->process();
		self::$exclude_traffic = $head->exclude_traffic;
	}

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

	public function onAfterRender()
	{
		// Executes before onBeforeCompileHead() on feeds and after onBeforeCompileHead() on HTML

		if (EORISIS_IS_JINSTALLER or self::$abort or self::$is_backend or self::$exclude_traffic)
		{
			return;
		}

		body_model::process();
	}

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

	public function onContentBeforeDelete($context, $data)
	{
		// @param	string	$context	The context for the content passed to the plugin.
		// @param	object	$data		The data relating to the content that was deleted.
		// @return	boolean

		if (EORISIS_IS_JINSTALLER || $context != 'com_categories.category' || !defined('EORISIS_PIXELS_PRO'))
		{
			return;
		}

		return \com_eorisis_pixels\framework\lib\pro\cms\document::on_content_before_delete($data);
	}
}
