<?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 SPTransferControllerFiles extends JControllerAdmin {
    
    public function getModel($name = 'Files', $prefix = 'SPTransferModel') {
        $model = parent::getModel($name, $prefix, array('ignore_request' => true));
        return $model;
    }

    function browse() {
        // Check for request forgeries
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        $url = 'index.php?option=com_sptransfer&view=files';

        //folder remote
        $folder_remote = JRequest::getVar('folder_remote');
        if (!empty($folder_remote))
            $url .= '&folder_remote=' . $folder_remote;

        //folder local
        $folder_local = JRequest::getVar('folder_local');
        if (!empty($folder_local))
            $url .= '&folder_local=' . $folder_local;

        $this->setRedirect($url);

        return;
    }

    function transfer() {
        // Check for request forgeries
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        
        $factory = new CYENDFactory();
        
        //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>');  
        
        $model = $factory->getModel('Files', 'SPTransferModel');
        if(!$model->transfer()) {
            $this->setRedirect('index.php?option=com_sptransfer&view=monitoring_log',$model->getError(), 'error');
            return false;
        }
        
        // 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"));
        return true;
    }

}

