<?php

/**
 * @package		SP Transfer
 * @subpackage	Components
 * @copyright	SP CYEND - All rights reserved.
 * @author		SP CYEND
 * @link		http://www.cyend.com
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controlleradmin library
jimport('joomla.application.component.controlleradmin');

/**
 * SPTransfers Controller
 */
class SPTransferControllerTables extends JControllerAdmin {

    function fix() {
        // Check for request forgeries
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $factory = new CYENDFactory();

        //Validate Input IDs
        $input_ids = JRequest::getVar('input_ids', array(), '', 'array');
        $input_ids = $this->validateInputIDs($input_ids);
        if (!$input_ids) {
            $this->setRedirect('index.php?option=com_sptransfer', JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS'), 'error');
            return false;
        }

        //Initial tasks
        //Disable warnings
        error_reporting(E_ERROR | E_PARSE);
        set_time_limit(0);

        //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 = JRequest::getVar('cid', array(), '', 'array');

        // Get the model.
        $model = $factory->getModel('Tables', 'SPTransferModel');
        
        //Main Loop
        foreach ($ids as $i => $id) {
            if (!($item = $model->getItem($id)))
                JError::raiseWarning(500, $model->getError());
            $modelContent = $this->getModel($item->extension_name, 'SPTransferModel');
            $modelContent->init($item);
            $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>');
        $this->setRedirect('index.php?option=com_sptransfer&view=monitoring_log', JText::_("COM_SPTRANSFER_COMPLETED"));
    }

    function transfer() {
        // Check for request forgeries
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $factory = new CYENDFactory();

        //Validate Input IDs
        $input_ids = JRequest::getVar('input_ids', array(), '', 'array');
        $input_ids = $this->validateInputIDs($input_ids);
        if (!$input_ids) {
            $this->setRedirect('index.php?option=com_sptransfer', JText::_('COM_SPTRANSFER_MSG_ERROR_INVALID_IDS'), 'error');
            return false;
        }

        //Initial tasks
        //Disable warnings
        error_reporting(E_ERROR | E_PARSE);
        set_time_limit(0);

        //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 = JRequest::getVar('cid', array(), '', 'array');

        // Get the model.
        $model = $factory->getModel('Tables', 'SPTransferModel');

        //Main Loop
        foreach ($ids as $i => $id) {
            if (!($item = $model->getItem($id)))
                JError::raiseWarning(500, $model->getError());
            $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);
        $message = ('<h1>' . JText::_('COM_SPTRANSFER_COMPLETED') . '</h1>');
        $factory->writeLog($message);
        $this->setRedirect('index.php?option=com_sptransfer&view=monitoring_log', JText::_("COM_SPTRANSFER_COMPLETED"));
    }

    function validateInputIDs($input_ids) {
        $return = Array();
        foreach ($input_ids as $i => $ids) {
            if ($ids != "") {
                $ranges = explode(",", $ids);
                foreach ($ranges as $j => $range) {
                    if (preg_match("/^[0-9]*$/", $range)) {
                        $return[$i][] = $range;
                    } else {
                        if (preg_match("/^[0-9]*-[0-9]*$/", $range)) {
                            $nums = explode("-", $range);
                            if ($nums[0] >= $nums[1])
                                return false;
                            for ($k = $nums[0]; $k <= $nums[1]; $k++) {
                                $return[$i][] = $k;
                            }
                        } else {
                            return false;
                        }
                    }
                }
            }
        }
        if (count($return) == 0) {
            return true;
        } else {
            return $return;
        }
    }

}
