<?php
/*
 *
 * @package		ARI Smart Content
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die;

AriKernel::import('Application.ARISmartContent.Controllers.Controller');
//provider_database_group_connection
class AriSmartContentProviderGroupBase extends AriSmartContentController 
{
	var $_itemListName;
	var $_itemName = null;
	var $_group = null;

	protected function getItemName()
	{
		if (is_null($this->_itemName))
			$this->_itemName = $this->getOriginalName();

		return $this->_itemName;
	}
	
	protected function getItemListName()
	{
		if (is_null($this->_itemListName))
		{
			$name = $this->getOriginalName();
			$this->_itemListName = $name . 'list';
		}

		return $this->_itemListName;
	}
	
	protected function getGroup()
	{
		if (is_null($this->_group))
		{
			$itemName = $this->getItemName();
			$itemName = explode('_', $itemName);
			$this->_group = $itemName[1] . '_' . $itemName[3];
		}

		return $this->_group;
	}
	
	protected function getModelInstance()
	{
		return $this->getModel('Providersettings');
	}
	
	function display() 
	{
		$this->redirect('index.php?option=com_arismartcontent&view=' . $this->getItemListName());
	}
	
	function add() 
	{
		$model = $this->getModelInstance();
		$item = $model->getTable();

		$data = $this->getRequestData();
		if (!is_null($data))
			$item->bind($data);

		$view = $this->getView($this->getOriginalName());
		$view->display($item);
	}

	function edit()
	{
		$id = JRequest::getInt('Id');

		$model = $this->getModelInstance();
		$item = $model->getSettings($id, $this->getGroup());
		if (is_null($item))
		{
			JError::raiseError(
				500, 
				JText::sprintf(
					'COM_ARISMARTCONTENT_ERROR_LOAD_PROVIDERSETTINGS', 
					__CLASS__ . '::' . __FUNCTION__ . '()', 
					$id
				)
			);
			
			return ;
		}

		$data = $this->getRequestData();
		if (!is_null($data))
			$item->bind($data);

		$view = $this->getView($this->getOriginalName());
		$view->display($item);
	}

	function apply()
	{
		JRequest::checkToken() or jexit('Invalid Token');

		$item = $this->_save();
		$this->redirect('index.php?option=com_arismartcontent&view=' . $this->getItemName() . '&task=edit&Id=' . $item->Id . '&__MSG=COM_ARISMARTCONTENT_COMPLETE_PROVIDERGROUPSAVE');
	}

	function save()
	{
		JRequest::checkToken() or jexit('Invalid Token');
		
		$this->_save();
		$this->redirect('index.php?option=com_arismartcontent&view=' . $this->getItemName() . '&__MSG=COM_ARISMARTCONTENT_COMPLETE_PROVIDERGROUPSAVE');
	}

	function cancel()
	{
		$this->redirect('index.php?option=com_arismartcontent&view=' . $this->getItemListName());
	}
	
	function ajaxIsNameUnique()
	{
		$model = $this->getModelInstance();
		
		$name = JRequest::getString('name');
		$id = JRequest::getInt('id');
		
		return $model->isUniqueName($name, $id, $this->getGroup());
	}
	
	function _save($redirectOnError = true) 
	{
		$model = $this->getModelInstance(); 
		$data = JRequest::getVar('params', null, 'default', 'none', JREQUEST_ALLOWRAW);
		$settings = JRequest::getVar('settings', null, 'default', 'none', JREQUEST_ALLOWRAW);

		$id = AriUtils::getParam($data, 'Id', 0);
		
		AriKernel::import('Joomla.Form.Form');

		$commonSettingsForm = new AriForm('commonSettings');
		$commonSettingsForm->load(AriSmartContentHelper::getFormPath('provider', 'group'));

		$groupSettingsForm = new AriForm('groupSettings');
		$groupSettingsForm->load(AriSmartContentHelper::getFormPath($this->getOriginalName()));

		$commonSettingsForm->bind($data);
		$groupSettingsForm->bind($settings);

		$cleanData = $commonSettingsForm->toArray();
		$cleanSettings = $groupSettingsForm->toArray();
		
		$cleanData['_GroupName'] = $this->getGroup();

		if (!$commonSettingsForm->validate($cleanData) ||
			!$groupSettingsForm->validate($cleanSettings))
		{
			if ($redirectOnError)
			{
				$requestData = $data;
				$requestData['Params'] = $settings;
				$this->setRequestData($requestData);
				if ($id > 0)
					$this->redirect('index.php?option=com_arismartcontent&view=' . $this->getItemName() . '&task=edit&__MSG=COM_ARISMARTCONTENT_ERROR_ENTITYSAVE&Id=' . $id);
				else
					$this->redirect('index.php?option=com_arismartcontent&view=' . $this->getItemName() . '&task=add&__MSG=COM_ARISMARTCONTENT_ERROR_ENTITYSAVE');
			}

			return null;
		}

		return $model->saveSettings(
			$cleanData, 
			$cleanSettings,
			$this->getGroup()
		);
	}
}