<?php
/**
 * @version		$Id: import.php 115 2009-06-17 07:20:36Z eddieajau $
 * @copyright	Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://www.theartofjoomla.com
 */

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

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

/**
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaControllerImport extends JController
{
	/**
	 * Proxy for getModel
	 */
	function &getModel($name = 'Import')
	{
		return parent::getModel($name, '', array('ignore_request' => true));
	}

	/**
	 * Display the view
	 */
	function display()
	{
	}

	/**
	 * Changes the order of an item
	 */
	function upload()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit(JText::_('JInvalid_token'));

		$srcFile		= JRequest::getVar('src_file',	array(),	'files',	'array');
		$dateColumn		= JRequest::getVar('date_column',	null,	'post');
		$valueColumn	= JRequest::getVar('value_column',	null,	'post');
		$recordId		= JRequest::getVar('record_id',		null,	'post');

		if ($dateColumn === null)
		{
			JError::raiseWarning(500, 'Quanta_Error_Date_column_must_be_supplied');
			$this->setRedirect('index.php?option=com_quanta&view=import&layout=default');
		}

		if ($valueColumn === null)
		{
			JError::raiseWarning(500, 'Quanta_Error_Value_column_must_be_supplied');
			$this->setRedirect('index.php?option=com_quanta&view=import&layout=default');
		}

		if (empty($recordId))
		{
			JError::raiseWarning(500, 'Quanta_Error_Record_id_must_be_supplied');
			$this->setRedirect('index.php?option=com_quanta&view=import&layout=default');
		}

		/*
		if (($timeLimit = JArrayHelper::getValue($params, 'time_limit', 0, 'int')) > 0) {
			set_time_limit($timeLimit);
		}
		*/
		/*
		if (($memLimit = JArrayHelper::getValue($params, 'mem_limit'))) {
			ini_set('memory_limit', $memLimit);
		}
		*/

		$model	= &$this->getModel();
		$model->setState('src_file',		$srcFile);
		$model->setState('date_column',		$dateColumn);
		$model->setState('value_column',	$valueColumn);
		$model->setState('record_id',		$recordId);

		$model->setState('import.delimiter',	',');
		$model->setState('import.enclosure',	'"');

		if (!$model->upload())
		{
			JError::raiseWarning(500, $model->getError());
			$this->setRedirect('index.php?option=com_quanta&view=import&layout=default');
		}
		else {
			$this->setRedirect('index.php?option=com_quanta&view=import&layout=close');
		}
	}
}