<?php
/**
 * @package	AcyMailing for Joomla!
 * @version	5.8.0
 * @author	acyba.com
 * @copyright	(C) 2009-2017 ACYBA S.A.R.L. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><?php
jimport('joomla.application.component.controller');
jimport('joomla.application.component.view');

include_once(rtrim(JPATH_ADMINISTRATOR, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_acymailing'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'helper.php');

if(defined('JDEBUG') AND JDEBUG) acymailing_displayErrors();

$view = acymailing_getVar('cmd', 'view');
if(!empty($view) AND !acymailing_getVar('cmd', 'ctrl')){
	acymailing_setVar('ctrl', $view);
	$layout = acymailing_getVar('cmd', 'layout');
	if(!empty($layout)){
		acymailing_setVar('task', $layout);
	}
}
$taskGroup = acymailing_getVar('cmd', 'ctrl', acymailing_getVar('cmd', 'gtask', 'lists'));

global $Itemid;
if(empty($Itemid)){
	$urlItemid = acymailing_getVar('int', 'Itemid');
	if(!empty($urlItemid)) $Itemid = $urlItemid;
}


$config = acymailing_config();

acymailing_addScript(false, ACYMAILING_JS.'acymailing.js?v='.str_replace('.', '', $config->get('version')));


$cssFrontend = $config->get('css_frontend', 'default');
if(!empty($cssFrontend)){
	acymailing_addStyle(false, ACYMAILING_CSS.'component_'.$cssFrontend.'.css?v='.filemtime(ACYMAILING_MEDIA.'css'.DS.'component_'.$cssFrontend.'.css'));
}

if($taskGroup == 'newsletter') $taskGroup = 'frontnewsletter';

if(!file_exists(ACYMAILING_CONTROLLER_FRONT.$taskGroup.'.php') || !include(ACYMAILING_CONTROLLER_FRONT.$taskGroup.'.php')){
	return acymailing_raiseError(E_ERROR, 404, 'Page not found : '.$taskGroup);
}

$className = ucfirst($taskGroup).'Controller';
$classGroup = new $className();
acymailing_setVar('view', $classGroup->getName());

$action = acymailing_getVar('cmd', 'task');
if(empty($action)){
	$action = acymailing_getVar('cmd', 'defaulttask');
	acymailing_setVar('task', $action);
}

$classGroup->execute($action);
$classGroup->redirect();
if(acymailing_getVar('string', 'tmpl') !== 'component' && !in_array(acymailing_getVar('cmd', 'task'), array('unsub', 'saveunsub', 'optout', 'out', 'view'))){
	echo acymailing_footer();
}
