<?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;

require_once dirname(__FILE__) . '/../../../view.php';

class AriSmartContentViewProviderSource extends AriSmartContentAdminView 
{
	var $_title = '';
	var $_helpLink = null;
	
	function getTitle()
	{
		return $this->_title;
	}
	
	function display($item, $providerSettingsFormName, $formatterFormName, $tpl = null) 
	{
		$input = JFactory::getApplication()->input;
		$isPopupMode = $input->get('tmpl') == 'component';
        $directEdit = $input->get('direct', null, 'bool');
        $isSaved = $input->get('saved', null, 'bool');
        $callback = $input->get('callback', null, 'RAW');
        $rurl = '';
		if ($isPopupMode)
			$rurl = $input->get('rurl', null, 'RAW');

		$this->registerScripts();
		$this->setToolbar($item, $isPopupMode, $directEdit);

		AriKernel::import('Joomla.Form.Form');

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

		$providerSettingsForm = null;
		if ($providerSettingsFormName)
		{
			$providerSettingsForm = new AriForm('providerSettings');
			$providerSettingsForm->load(AriSmartContentHelper::getFormPath($providerSettingsFormName));
			$providerSettingsForm->bind($item);
		}

		$formatterForm = null;
		if ($formatterFormName)
		{
			$formatterForm = new AriForm('formatter');
			$formatterForm->load(AriSmartContentHelper::getFormPath($formatterFormName));
			$formatterForm->bind($item->Formatter->Data);
		}

		$sourceSettingsForm = new AriForm('sourceSettings');
		$sourceSettingsForm->load(AriSmartContentHelper::getFormPath($this->getName()));
		$sourceSettingsForm->bind($item->Params);

        $this->assign('item', $item);
        $this->assign('callback', $callback);
		$this->assign('isPopupMode', $isPopupMode);
        $this->assign('directEdit', $directEdit);
        $this->assign('isSaved', $isSaved);
		if ($rurl)
			$this->assign('rurl', $rurl);

		$this->assignRef('commonSettingsForm', $commonSettingsForm);
		$this->assignRef('providerSettingsForm', $providerSettingsForm);
		$this->assignRef('formatterForm', $formatterForm);
		$this->assignRef('sourceSettingsForm', $sourceSettingsForm);

		parent::display($tpl);
	}
	
	function loadTemplate($tpl = null) 
	{
		$tpl = parent::loadTemplate($tpl);
		if (JError::isError($tpl)) 
			return $tpl;
			
		$input = JFactory::getApplication()->input;
		$isPopupMode = $input->get('tmpl') == 'component';
		
		if ($isPopupMode)
		{
			$doc = JFactory::getDocument();
			$tpl = sprintf('<div class="asc-popup-toolbar%2$s">%1$s</div>', $doc->getBuffer('modules', 'toolbar'), J3_0 ? ' j3' : '') . $tpl;
		}
		
		return $tpl;
	}

	function registerScripts()
	{
		$doc = JFactory::getDocument();
		$doc->addScriptDeclaration(';YAHOO.ARISoft.page.viewName = "' . $this->getName() . '";');
	}

	function setToolbar($item, $isPopupMode = false, $directEdit = false)
	{
		$this->disableMainMenu();

		$id = JRequest::getInt('Id');
		$edit = ($id > 0);
		$text = ($edit ? JText::_('COM_ARISMARTCONTENT_LABEL_EDIT') : JText::_('COM_ARISMARTCONTENT_LABEL_NEW'));

		JToolBarHelper::title(JText::_($this->getTitle()) . ':' . ($edit ? ' ' . $item->Name : '') . ' <small><small>[ ' . $text . ' ]</small></small>', 'config.png');

        if (!$isPopupMode || !$directEdit)
		    JToolBarHelper::save();
		
		if (!$isPopupMode || ($isPopupMode && $directEdit))
			JToolBarHelper::apply();

        if (!$isPopupMode && $edit)
            JToolbarHelper::save2copy('save2copy');

        if ($edit)
			JToolBarHelper::cancel('cancel', JText::_('Close'));
		else 
			JToolBarHelper::cancel();

		if ($this->_helpLink)
		{
			JToolBarHelper::divider();
			AriSmartContentToolbarHelper::ariSmartContentHelp($this->_helpLink);
		}
	}
}