<?php
/*
 * Software Name: Eorisis Setup
 * Software URI: https://eorisis.com/shop/software/joomla/setup
 * 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 eorisis_setup\framework\install\lib\core\model;
\defined('_JEXEC') or die;

use version_compare, str_replace;
use eorisis_setup\framework\install\lib\core\cms\application;
use eorisis_setup\framework\install\lib\core\model\database;
use eorisis_setup\framework\install\lib\core\model\setup;
use eorisis_setup\framework\install\lib\core\model\data;

class updates
{
	public function __construct() {}

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

	public static function process(array $updates)
	{
		if (!data::$ext_version_old || setup::$setup_type != 'update')
		{
			return;
		}

		foreach ($updates as $version)
		{
			if (version_compare(data::$ext_version_old, $version, '>='))
			{
				continue;
			}

			$class = '\\eorisis_setup\\framework\\install\\lib\\core\\update\\v'.str_replace('.', '_', $version);
			$update = new $class;
			$update->execute($version);
		}
	}

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

	public static function remove_discontinued_sites(string $extension) : bool
	{
		$dbo = application::dbo();
		$query = $dbo->getQuery(true);
		$query->delete($dbo->quoteName('#__update_sites'));
		$query->where([
			$dbo->quoteName('location').' LIKE '.$dbo->quote('%updates.eorisis.net/eorisis-'.$extension.'/%'),
			$dbo->quoteName('location').' LIKE '.$dbo->quote('%eorisis.com/updates/eorisis-'.$extension.'/%'),
			$dbo->quoteName('location').' LIKE '.$dbo->quote('%eorisis.com/updates/eorisis-piwik/%')
		], 'OR');
		$dbo->setQuery($query);
		$result = database::execute($dbo);
		return !$result['error'];
	}
}
