<?php

// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

class SPStgSrvControllerInstaller extends JControllerLegacy {

    public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true)) {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }
    
    public function tables_transfer() {
        //login
        $app = JFactory::getApplication();
        
        error_reporting(0);

        $model_transfer = $this->getModel('transfer');
        $model_installer = $this->getModel('installer');

        $data['comm_id'] = JRequest::getString('comm_id');  
        
        //get post
        $ip = $model_installer->getIP();
        $message = $model_transfer->getPost($data);        
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }
        $post = $model_transfer->post;
        
        //includes and load transfer controller and model
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/com.php');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/tables/log.php');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/controllers/tables.php');
        $controllerSPTransferTables = CYENDFactory::getModel('Tables', 'SPTransferController');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/tables.php');        
        $modelSPTransferTables = CYENDFactory::getModel('Tables', 'SPTransferModel');

        //validate post
        $input_ids = $controllerSPTransferTables->validateInputIDs($post->input_ids);
        if (!$input_ids) {
            echo JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS');
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS');
            $data['ip'] = $ip;
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }
        
        //Initial tasks
        //Disable warnings
        error_reporting(E_ERROR | E_PARSE);
        set_time_limit(0);
        $factory = new CYENDFactory();

        //monitor log         
        $factory->writeLog('<META HTTP-EQUIV="REFRESH" CONTENT="15">', 'w'); // create monitor log        
        $factory->writeLog('<link rel="stylesheet" href="'.JURI::base().'templates/isis/css/template.css" type="text/css" />');
        $factory->writeLog('<div class="well well-large">');
        $factory->writeLog('<p>* This log will be automatically refreshed every 15 seconds.</p>');
        $factory->writeLog('<h1>' . JText::_(COM_SPTRANSFER_START) . '</h1>');      

        // Main Loop within extensions
        //Get ids
        $ids = $post->cid;

        //Main Loop
        foreach ($ids as $id) {
            if (!($item = $modelSPTransferTables->getItem($id)))
                JError::raiseWarning(500, $modelSPTransferTables->getError());
            require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/'.$item->extension_name.'.php');
            $modelContent = $factory->getModel($item->extension_name, 'SPTransferModel');
            $modelContent->init($item);
            echo $modelContent->{$item->name}($input_ids[$id - 1]);
        }

        // Finish
        //enable warnings
        error_reporting(E_ALL);
        set_time_limit(30);
        $factory->writeLog('<h1>' . JText::_('COM_SPTRANSFER_COMPLETED') . '</h1>');        

        $data['ip'] = $ip;
        $data['result'] = 1;
        $data['message'] = JText::_('COM_SPSTGSRV_INSTALL_SUCCESS');
        $app->logout();
        $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=success');        
        return true;
    }
    
    public function tables_fix() {
        
        //login
        $app = JFactory::getApplication();
        
        error_reporting(0);

        $model_transfer = $this->getModel('transfer');
        $model_installer = $this->getModel('installer');

        $params = &JComponentHelper::getParams('com_spstgsrv');
        $data['package_path'] = $params->get('package_path').'components/com_spstgmng/packages/';
        $data['comm_id'] = JRequest::getString('comm_id');  
        
        //get post
        $ip = $model_installer->getIP();
        $message = $model_transfer->getPost($data);        
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }
        $post = $model_transfer->post;
        
        //includes and load transfer controller and model
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/com.php');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/tables/log.php');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/controllers/tables.php');
        $controllerSPTransferTables = CYENDFactory::getModel('Tables', 'SPTransferController');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/tables.php');        
        $modelSPTransferTables = CYENDFactory::getModel('Tables', 'SPTransferModel');

        //validate post
        $input_ids = $controllerSPTransferTables->validateInputIDs($post->input_ids);
        if (!$input_ids) {
            echo JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS');
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS');
            $data['ip'] = $ip;
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }
        
        //Initial tasks
        //Disable warnings
        error_reporting(E_ERROR | E_PARSE);
        set_time_limit(0);
        $factory = new CYENDFactory();

        //monitor log         
        $factory->writeLog('<META HTTP-EQUIV="REFRESH" CONTENT="15">', 'w'); // create monitor log        
        $factory->writeLog('<link rel="stylesheet" href="'.JURI::base().'templates/isis/css/template.css" type="text/css" />');
        $factory->writeLog('<div class="well well-large">');
        $factory->writeLog('<p>* This log will be automatically refreshed every 15 seconds.</p>');
        $factory->writeLog('<h1>' . JText::_(COM_SPTRANSFER_START) . '</h1>');      
        
        // Main Loop within extensions
        //Get ids
        $ids = $post->cid;

        //Main Loop
        foreach ($ids as $id) {
            if (!($item = $modelSPTransferTables->getItem($id)))
                JError::raiseWarning(500, $modelSPTransferTables->getError());
            require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/'.$item->extension_name.'.php');
            $modelContent = $factory->getModel($item->extension_name, 'SPTransferModel');
            $modelContent->init($item);
            //echo $modelContent->{$item->name}($input_ids[$id - 1]);
            $modelContent->{$item->name . '_fix'}($input_ids[$id - 1]);
        }

        // Finish
        //enable warnings
        error_reporting(E_ALL);
        set_time_limit(30);
        $factory->writeLog('<h1>' . JText::_('COM_SPTRANSFER_COMPLETED') . '</h1>');        

        $data['ip'] = $ip;
        $data['result'] = 1;
        $data['message'] = JText::_('COM_SPSTGSRV_INSTALL_SUCCESS');
        $app->logout();
        $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=success');        
        return true;
    }

    public function database_transfer() {
        //login
        $app = JFactory::getApplication();

        error_reporting(0);

        $model_transfer = $this->getModel('transfer');
        $model_installer = $this->getModel('installer');

        $data['comm_id'] = JRequest::getString('comm_id');  
        
        //get post
        $ip = $model_installer->getIP();
        $message = $model_transfer->getPost($data);        
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }
        $post = $model_transfer->post;

        //includes and load transfer controller and model
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/com.php');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/tables/log.php');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/controllers/database.php');
        $controllerSPTransferDatabase = CYENDFactory::getModel('Database', 'SPTransferController');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/database.php');        
        $modelSPTransferDatabase = CYENDFactory::getModel('Database', 'SPTransferModel');
        require_once(JPATH_ADMINISTRATOR.'/components/com_sptransfer/models/com_database.php');        
        $modelContent = CYENDFactory::getModel('Com_Database', 'SPTransferModel');
                
        $params = JComponentHelper::getParams('com_sptransfer');
        if (!($source_db = $controllerSPTransferDatabase->connect($params))) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPTRANSFER_MSG_DB');
            $data['ip'] = $ip;
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;        
        }

        //validate post
        $input_ids = $controllerSPTransferDatabase->validateInputIDs($post->input_ids);
        $input_prefixes = $post->input_prefixes;
        $input_names = $post->input_names;

        if (!$input_ids) {
            echo JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS');
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS');
            $data['ip'] = $ip;
            $app->logout();            
            $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=fail&message='.$data['message']);            
            return false;
        }
        
        //Initial tasks
        //Disable warnings
        error_reporting(E_ERROR | E_PARSE);
        set_time_limit(0);
        $factory = new CYENDFactory();

        //monitor log         
        $factory->writeLog('', 'w'); // create monitor log        
        $factory->writeLog('<META HTTP-EQUIV="REFRESH" CONTENT="15"><link rel="stylesheet" href="' . JURI::base() . 'templates/bluestork/css/template.css" type="text/css" />');
        $factory->writeLog('<div class="m">');
        $factory->writeLog('<p>* This log will be automatically refreshed every 15 seconds.</p>');
        $factory->writeLog('<h1>' . JText::_(COM_SPTRANSFER_START) . '</h1>');   

        // Main Loop within extensions
        //Get ids
        $ids = $post->cid;

        //Main Loop
        foreach ($ids as $id) {
            $table_name = $input_prefixes[$id-1].'_'.$input_names[$id-1];
            $item = $modelSPTransferDatabase->getItem($table_name);

            if (is_null($item)) {
                //Insert new item in tables
                $item = $modelSPTransferDatabase->newItem($table_name);
            }
            if (is_null($item)) {
                $message = ('<p>' . JText::plural('COM_SPTRANSFER_DATABASE_FAILED', $table_name) . '</p>');
                $controllerSPTransferDatabase->writeLog($message);
                continue;
            }

            $modelContent->init($source_db, $item);
            if (!$modelContent->setTable($input_prefixes[$id-1], $input_names[$id-1])) {
                $message = ('<p>' . JText::plural('COM_SPTRANSFER_DATABASE_FAILED', $table_name) . '</p>');
                $controllerSPTransferDatabase->writeLog($message);
                continue;
            }            
            $modelContent->content($input_ids[$id - 1], $input_prefixes[$id-1], $input_names[$id-1]);
        }

        // Finish
        //enable warnings
        error_reporting(E_ALL);
        set_time_limit(30);
        $factory->writeLog('<h1>' . JText::_('COM_SPTRANSFER_COMPLETED') . '</h1>');        

        $data['ip'] = $ip;
        $data['result'] = 1;
        $data['message'] = JText::_('COM_SPSTGSRV_INSTALL_SUCCESS');
        $app->logout();
        $app->redirect(JURI::root().'index.php?option=com_spstgsrv&tmpl=component&view=success');        
        return true;
    }
    
}

?>
