<?php
/**
 * @package    VP Advanced User
 * @author     Abhishek Das <info@virtueplanet.com>
 * @copyright  Copyright (C) 2012-2019 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

/**
* View to edit
*/
class VPAdvancedUserViewEmailtemplate extends JViewLegacy
{
	protected $state;
	
	protected $params;
	
	protected $item;
	
	protected $form;

	/**
	* Display the view
	*/
	public function display($tpl = null)
	{
		$this->state  = $this->get('State');
		$this->item   = $this->get('Item');
		$this->form   = $this->get('Form');
		$this->params = $this->state->get('params');

		// Check for errors.
		if (count($errors = $this->get('Errors'))) 
		{
			throw new Exception(implode("\n", $errors));
		}

		$this->addToolbar();
		
		parent::display($tpl);
	}

	/**
	* Add the page title and toolbar.
	*/
	protected function addToolbar()
	{
		JFactory::getApplication()->input->set('hidemainmenu', true);
		
		$user       = JFactory::getUser();
		$isNew      = ($this->item->id == 0);
		$canDo      = VPAdvancedUserHelper::getActions();
		$checkedOut = !($this->item->checked_out == 0 || $this->item->checked_out == $user->get('id'));

		if($isNew)
		{
			JToolBarHelper::title(JText::_('COM_VPADVANCEDUSER') . ': ' . JText::_('COM_VPADVANCEDUSER_TITLE_ADD_EMAIL_TEMPLATE'), 'envelope emailtemplate');
		}
		else
		{
			JToolBarHelper::title(JText::_('COM_VPADVANCEDUSER') . ': ' . JText::_('COM_VPADVANCEDUSER_TITLE_EDIT_EMAIL_TEMPLATE') . ' - ' . $this->item->name, 'envelope emailtemplate');
		}

		// If not checked out, can save the item.
		if (!$checkedOut && ($canDo->get('core.edit') || ($canDo->get('core.create'))))
		{
			JToolBarHelper::apply('emailtemplate.apply', 'JTOOLBAR_APPLY');
			JToolBarHelper::save('emailtemplate.save', 'JTOOLBAR_SAVE');
		}

		if (!$checkedOut && ($canDo->get('core.create')))
		{
			JToolbarHelper::save2new('emailtemplate.save2new');
		}

		// If an existing item, can save to a copy.
		if (!$isNew && $canDo->get('core.create'))
		{
			JToolbarHelper::save2copy('emailtemplate.save2copy');
		}

		// Button for version control
		if ($this->state->params->get('save_history', 1) && $user->authorise('core.edit'))
		{
			JToolbarHelper::versions('com_vpadvanceduser.emailtemplate', $this->item->id);
		}

		if (empty($this->item->id))
		{
			JToolBarHelper::cancel('emailtemplate.cancel', 'JTOOLBAR_CANCEL');
		}
		else
		{
			JToolBarHelper::cancel('emailtemplate.cancel', 'JTOOLBAR_CLOSE');
		}
	}
}
