<?php

/**
 * @package         Convert Forms
 * @version         5.2.5-RC2 Free
 * 
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2024 Tassos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\Filesystem\Folder;

require_once __DIR__ . '/script.install.helper.php';

class Com_ConvertFormsInstallerScript extends Com_ConvertFormsInstallerScriptHelper
{
	public $name = 'CONVERTFORMS';
	public $alias = 'convertforms';
	public $extension_type = 'component';

	public function onAfterInstall()
	{
		$this->moveFrontEndImages();
		$this->removeCampaignsAdminMenuItemIfRetired();

		if ($this->install_type == 'update') 
		{
			require_once __DIR__ . '/autoload.php';

			try {
				(new ConvertForms\Migrator($this->installedVersion))->start();
			} catch (\Throwable $th)
			{
			}

			$this->dropIndex('convertforms_conversions', 'email_campaign_id');

			// Drop auto-translation feature with language strings in field options in favor of the new {language.KEY} Smart Tag.
			if (version_compare($this->installedVersion, '2.8.0', '<=')) 
			{
				Factory::getApplication()->enqueueMessage('
					<b>Backwards Compatibility Break:</b> Language strings can no longer be used directly in field options. If you\'re using language strings to produce multilingual forms in one of the following options, you should update your forms to be using the <b>{language.KEY}</b> Smart Tag instead:<br><br>
	
					<ul>
						<li>Field Value</li>
						<li>Field Label</li>
						<li>Field Placeholder</li>
						<li>Field Description</li>
						<li>Dropdown Choice Label</li>
						<li>Radio Button Choice Label</li>
						<li>Checkbox Choice Label</li>
					</ul>', 'warning');
			}
        }
    }

	/**
	 *  Moves front-end based images from /media/ folder to /images/
	 *
	 *  @return  void
	 */
	private function moveFrontEndImages()
	{
		$source      = JPATH_SITE . '/media/com_convertforms/img/convertforms';
		$destination = JPATH_SITE . '/images/convertforms';

		if (!is_dir($source))
		{
			return;
		}

		if (!Folder::copy($source, $destination, null, true))
		{
			return;
		}

		Folder::delete($source);
	}

	/**
	 * Remove the legacy Campaigns menu item for fresh installs and any install
	 * state where the legacy campaign MVC files are no longer present.
	 *
	 * Upgraded installs that still contain the legacy campaign model file keep
	 * the menu item so existing users can continue using campaigns.
	 *
	 * @return  void
	 */
	private function removeCampaignsAdminMenuItemIfRetired()
	{
		if (file_exists(JPATH_ADMINISTRATOR . '/components/com_convertforms/models/campaign.php'))
		{
			return;
		}

		$db = Factory::getDbo();

		$conditions = [
			$db->quoteName('client_id') . ' = 1',
			$db->quoteName('type') . ' = ' . $db->quote('component'),
			$db->quoteName('link') . ' = ' . $db->quote('index.php?option=com_convertforms&view=campaigns'),
		];

		$deleteQuery = $db->getQuery(true)
			->delete($db->quoteName('#__menu'))
			->where($conditions);

		try
		{
			$db->setQuery($deleteQuery)->execute();
		}
		catch (\Throwable $th)
		{
			$updateQuery = $db->getQuery(true)
				->update($db->quoteName('#__menu'))
				->set($db->quoteName('published') . ' = 0')
				->where($conditions);

			$db->setQuery($updateQuery)->execute();
		}
	}
}
