<?php

defined('_JEXEC') or die('Restricted access');

class com_payzitoInstallerScript
{
    function postflight($type,$parent)
    {
        $type = strtolower($type);
        if($type == 'uninstall')
        {
            return;
        }

        defined('DS') || define('DS',DIRECTORY_SEPARATOR);

        $db = JFactory::getDbo();
        $db->setQuery("UPDATE `#__extensions` SET `enabled`='1' WHERE `element`='com_payzito' AND `type`='component'");
        $db->execute();

        $installer = new JInstaller();
        $installer->install(JPATH_SITE.DS.'components'.DS.'com_payzito'.DS.'packages'.DS.'payzito_loader');

        $db = JFactory::getDbo();
        $db->setQuery("UPDATE `#__extensions` SET `enabled`='1',`ordering`='2020' WHERE `element`='payzito_loader' AND `type`='plugin' AND `folder`='system'");
        $db->execute();

        $shSrcFile = JPATH_ROOT.DS.'components'.DS.'com_payzito'.DS.'includes'.DS.'cms'.DS.'joomla'.DS.'sh404sef'.DS.'com_payzito.php';
        $shDestFile = JPATH_ROOT .DS. 'components'.DS.'com_sh404sef'.DS.'sef_ext'.DS.'com_payzito.php';
        if(is_dir(dirname($shDestFile)))
        {
            copy($shSrcFile,$shDestFile);
        }

        require_once JPATH_ROOT.DS.'components'.DS.'com_payzito'.DS.'includes'.DS.'setup'.DS.'setup.php';

        $GLOBALS['payzito_setup_ready'] = 1;

        PASetup::deleteReadyFile();
        PASetup::afterInstall();
    }

    function uninstall()
    {
        if(!class_exists('PAGeneral'))
        {
            return;
        }

        $dbFile = PA_CORE_ROOT.DS.'databases'.DS.'uninstall.mysql.sql';
        if(PAGeneral::existFile($dbFile))
        {
            $dbContent = PAGeneral::readFile($dbFile);
            $dbRows = !empty($dbContent) ? explode(';',$dbContent) : [];
            foreach ($dbRows as $row)
            {
                $row = trim($row);
                if(!empty($row))
                {
                    PAGeneral::dbQuery($row);
                }
            }
        }

        PAGeneral::deleteFileIfExist(JPATH_ROOT.DS.'language'.DS.'fa-IR'.DS.'fa-IR.com_payzito.ini');
        PAGeneral::deleteFileIfExist(JPATH_ROOT.DS.'language'.DS.'en-GB'.DS.'en-GB.com_payzito.ini');
        PAGeneral::deleteFileIfExist(JPATH_ROOT.DS.'administrator'.DS.'language'.DS.'fa-IR'.DS.'fa-IR.com_payzito.ini');
        PAGeneral::deleteFileIfExist(JPATH_ROOT.DS.'administrator'.DS.'language'.DS.'en-GB'.DS.'en-GB.com_payzito.ini');
    }
}