<?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;

use com_eorisis_pixels\framework\install_package\lib\core\model\language as language_model;
use com_eorisis_pixels\framework\install_package\lib\core\model\install;
use com_eorisis_pixels\framework\install_package\lib\core\model\message;
use com_eorisis_pixels\framework\install_package\lib\core\model\setup;

if (version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisFactory', '\\Joomla\\CMS\\Factory');
}
else
{
	JLoader::registerAlias('EorisisFactory', 'JFactory');
}

if (PHP_VERSION_ID >= 70000)
{
	$current_path = str_replace('\\', '/', __DIR__);

	if (strpos((string)$current_path, '/administrator/manifests/packages') !== false)
	{
		$namespace_path = JPATH_ADMINISTRATOR.'/components/com_eorisis_pixels/framework/install_package/lib';
	}
	else
	{
		$namespace_path = $current_path.'/lib';
	}

	// Register autoloader here otherwise php 5.6 will crash
	require_once $namespace_path.'/eorisis.php';
	$eorisis = new \com_eorisis_pixels\framework\install_package\lib\eorisis($namespace_path);
	$eorisis->register();

	setup::$extension = 'pixels';
	setup::$current_path = $current_path;

	language_model::$path = $current_path;
	language_model::load('install');
}

class Pkg_eorisis_pixelsInstallerScript
{
	private static $min_php_version = '7.0.0';
	private static $is_discover_install = false;

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

	public function preflight($type, $parent)
	{
		// Preflight is executed prior to any Joomla install,
		// update or discover_install actions (or uninstall for plugins)
		// --------------------------------------------------

		defined('EORISIS_PKG_INSTALL') or define('EORISIS_PKG_INSTALL', 1);

		$type = strtolower($type);
		if ($type == 'uninstall')
		{
			// Do not continue in preflight
			return;
		}

		// Minimum PHP Version Check
		if (!$this->min_php_version())
		{
			return false;
		}

		$setup = new setup;
		self::$is_discover_install = $setup->is_discover_install($type);

		if (self::$is_discover_install or !$setup->checks($type, $parent))
		{
			return false;
		}
	}

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

	public function install($parent)
	{
		// In packages, during manual install,
		// even an update fires the install() function
		// --------------------------------------------------

		if (!$this->min_php_version() or self::$is_discover_install)
		{
			return false;
		}

		message::set_html();

		if (!setup::$continue)
		{
			return false;
		}

		// Old Joomla versions fire this method even on updates
		if (setup::$execute_db_install)
		{
			$install = new install;
			$install->execute();
		}
	}

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

	public function update($parent)
	{
		// In packages, function update() works only while updating from Joomla Extension Update
		// Not during manual install. see: install()
		// --------------------------------------------------

		if (!$this->min_php_version() or self::$is_discover_install)
		{
			return false;
		}

		if (setup::$update_multi)
		{
			\com_eorisis_pixels\framework\install_package\lib\core\model\document::$html .= '<hr>';
		}

		message::set_html();

		if (!setup::$continue)
		{
			return false;
		}

		if (setup::$execute_db_install)
		{
			$install = new install;
			$install->execute();
		}
	}

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

	public function uninstall($parent)
	{
		// Uninstall cannot cause an abort of the Joomla uninstall action
		// returning false does nothing
		$uninstall = new \com_eorisis_pixels\framework\install_package\lib\core\model\uninstall;
		$uninstall->execute();
	}

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

	public function postflight($type, $parent)
	{
		// postflight() Executes after Install or Update (and uninstall in Joomla > 4)
		// --------------------------------------------------

		$type = strtolower($type);
		if ($type == 'uninstall')
		{
			// Do not continue in postflight
			return;
		}

		if (!$this->min_php_version() or self::$is_discover_install)
		{
			return false;
		}

		if (!setup::$continue)
		{
			return false;
		}

		// Enable the system plugin
		\com_eorisis_pixels\framework\install_package\lib\core\cms\plugin::enable('eorisis_pixels','system');

		// Updates
		$updates_model = new \com_eorisis_pixels\framework\install_package\lib\core\model\updates;
		$updates_model->process(['3.3.0']);

		$setup = new setup;
		$setup->final_cleanup();

		// Setup Installer Plugin
		$setup->setup_plugin_custom_install();
	}

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

	private function min_php_version() : bool
	{
		if (version_compare(PHP_VERSION, self::$min_php_version, '>='))
		{
			return true;
		}

		$txt  = 'Your server is running PHP version '.PHP_VERSION.'. ';
		$txt .= 'The minimum PHP version required is '.self::$min_php_version.'.<br>';
		$txt .= 'Request from your server host to upgrade your PHP version.';
		EorisisFactory::getApplication()->enqueueMessage($txt, 'warning');
		return false;
	}
}
