<?php
/**
 * @package DJ-Catalog2-Yootheme
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

// No direct access to this file
defined('_JEXEC') or die;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Component\ComponentHelper;

/**
 * Script file of HelloWorld module
 */
class plgSystemDjc2yoothemeInstallerScript
{
	/**
	 * Method to install the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function install($parent)
	{
		$html = '<style type="text/css">
				.djex-info { padding: 20px 30px 10px; margin: 0 0 20px 0; background: #e83d57; color: #fff; border: 1px solid #453d38; font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: normal; -webkit-border-radius: 4px; border-radius: 4px; }
				.djex-title { text-transform: uppercase; font-weight: bold; font-size: 14px; }
				.djex-info a:link, .djex-info a:visited, .djex-info a:hover { color:#fff; text-decoration:underline; font-weight: 600; }
				.djex-info img { float: left; margin: 0 30px 10px 0; }
			</style>
			<div class="djex-info"><img src='.Uri::root().'plugins/system/djc2yootheme/assets/djc2-integration-yootheme.png style="width: 25%;">	<p class="djex-title">Thank you for installing  DJC2-YooTheme-Integration plugin!</p>
			<p>The DJC2-YooTheme-Integration is an advanced system plugin for your Joomla and YooTheme! It provides dynamic-content support for DJ-Catalog2 component and many more functions.
			If you want to learn how to use DJC2-YooTheme-Integration plugin please read <a target="_blank" href="https://dj-extensions.com/documentation">Documentation</a> and visit our <a target="_blank" href="https://dj-extensions.com/faq">FAQ Section</a></p>
			<p>Check out our other extensions at <a target="_blank" href="https://dj-extensions.com">DJ-Extensions.com</a></p>
			<div style="clear:both"></div></div>';
		echo $html;
		$db = JFactory::getDbo();
		$query = 'ALTER TABLE `#__djc2_items` ADD `product_layout` mediumtext';
		$db->setQuery($query);
		$db->execute();
		$query = 'ALTER TABLE `#__djc2_producers` ADD `producer_layout` mediumtext';
		$db->setQuery($query);
		$db->execute();
	}

	/**
	 * Method to uninstall the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function uninstall($parent)
	{
		echo '<p>DJC2-YooTheme-Integration has been uninstalled.</p>';
		$db = JFactory::getDbo();
		$query = 'ALTER TABLE `#__djc2_items` DROP COLUMN `product_layout`';
		$db->setQuery($query);
		$db->execute();
		$query = 'ALTER TABLE `#__djc2_producers` DROP COLUMN `producer_layout`';
		$db->setQuery($query);
		$db->execute();

	}

	/**
	 * Method to update the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function update($parent)
	{
		$html = '<style type="text/css">
				.djex-info { padding: 20px 30px 10px; margin: 0 0 20px 0; background: #e83d57; color: #fff; border: 1px solid #453d38; font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: normal; -webkit-border-radius: 4px; border-radius: 4px; }
				.djex-title { text-transform: uppercase; font-weight: bold; font-size: 14px; }
				.djex-info a:link, .djex-info a:visited, .djex-info a:hover { color:#fff; text-decoration:underline; font-weight: 600; }
				.djex-info img { float: left; margin: 0 30px 10px 0; }
			</style>
			<div class="djex-info"><img src='.Uri::root().'plugins/system/djc2yootheme/assets/djc2-integration-yootheme.png style="width: 25%;">	<p class="djex-title">DJC2-YooTheme-Integration plugin has been updated to version: ' . $parent->getManifest()->version .'</p>
			<p>Please read <a target="_blank" href="https://dj-extensions.com/documentation">Changelog</a> and visit our <a target="_blank" href="https://dj-extensions.com/faq">FAQ Section</a></p>
			<p>Check out our other extensions at <a target="_blank" href="https://dj-extensions.com">DJ-Extensions.com</a></p>
			<div style="clear:both"></div></div>';
		echo $html;
		$db = JFactory::getDbo();
		$query = 'ALTER TABLE `#__djc2_producers` ADD COLUMN IF NOT EXISTS `producer_layout` mediumtext';
		$db->setQuery($query);
		$db->execute();
	}

	/**
	 * Method to run before an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function preflight($type, $parent)
	{
		$db = JFactory::getDbo();
		$db->setQuery("SELECT enabled FROM #__extensions WHERE name = 'yootheme' AND type = 'template' ");
		$yootheme_enabled = $db->loadResult();
		if($type == 'update' || $type == 'install'){
			if ($yootheme_enabled == 0) {
				JFactory::getApplication()->enqueueMessage(JText::_('Please install YooTheme Builder before installing DJC2-YooTheme-Integration plugin!'));
				return false;
			}
			if (!JComponentHelper::getComponent('com_djcatalog2', true)->enabled)
			{
				JFactory::getApplication()->enqueueMessage(JText::_('Please install DJ-Catalog2 component before installing DJC2-YooTheme-Integration plugin!'));
				return false;
			}
		}
	}

	/**
	 * Method to run after an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function postflight($type, $parent)
	{
		{
			if($type == 'install'){
				$db = JFactory::getDbo();
				$query = $db->getQuery(true);

				$fields = array(
					$db->quoteName('enabled') . ' = 1',
				);

				$conditions = array(
					$db->quoteName('element') . ' = ' . $db->quote('djc2yootheme'),
					$db->quoteName('type') . ' = ' . $db->quote('plugin')
				);

				$query->update($db->quoteName('#__extensions'))->set($fields)->where($conditions);

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

		}
	}
}