<?php

/** 
 *------------------------------------------------------------------------------
 * @package       T4 Page Builder for Joomla!
 *------------------------------------------------------------------------------
 * @copyright     Copyright (C) 2004-2020 JoomlArt.com. All Rights Reserved.
 * @license       GNU General Public License version 2 or later; see LICENSE.txt
 * @authors       JoomlArt
 * @forum:		  https://www.joomlart.com/forums/t/t4-builder
 * @Link:         https://demo.t4-builder.joomlart.com/
 *------------------------------------------------------------------------------
 */
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\MVC\View\HtmlView;
use Joomla\CMS\Layout\FileLayout;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Access\Exception\NotAllowed;

class T4pagebuilderViewMenu extends HtmlView
{
		
	/**
	 * The JForm object
	 *
	 * @var  JForm
	 */
	protected $form;

	/**
	 * The active item
	 *
	 * @var  object
	 */
	protected $item;

	/**
	 * The model state
	 *
	 * @var  object
	 */
	protected $state;

	/**
	 * The actions the user is authorised to perform
	 *
	 * @var  JObject
	 */
	protected $canDo;

	/**
	 * Execute and display a template script.
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  mixed  A string if successful, otherwise an Error object.
	 *
	 * @since   1.6
	 */

	function display($tpl=null)
	{
		// Get the Data
		$this->form = $this->get('Form');
		$this->item = $this->get('Item');
		$input = Factory::getApplication()->input;
		$opt = $input->get('opt');
		if ($opt) {
			$option = json_decode( base64_decode($opt) );
			$title = isset($option->title) ? $option->title: '';
			$alias = isset($option->alias) ? $option->alias: '';
			$this->form->setValue('title','', $title);
			$this->form->setValue('alias','', $alias);
		}
		
		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			throw new NotAllowed(implode('<br />', $errors), 500);
 
			return false;
		}
		
		// Set the toolbar
		$this->addToolBar();
		parent::display($tpl);
	}
	
	protected function addToolBar()
	{
		$input = Factory::getApplication()->input;
 
		// Hide Joomla Administrator Main menu
		$input->set('hidemainmenu', true);
 
		$isNew = ($this->item->id == 0);
		
		ToolbarHelper::apply('menu.apply');
		
		ToolbarHelper::save('menu.save');
		
		ToolbarHelper::cancel(
			'menu.cancel',
			$isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE'
		);
		
		if ($isNew)
		{
			$title = 'New Page';
		}
		else
		{
			$title = 'Edit Page';
			
			$bar = Toolbar::getInstance('toolbar');
		
			$layout = new FileLayout('toolbar.live');

			$bar->appendButton('Custom', $layout->render(array()), '');
		}
 
		ToolbarHelper::title($title, 'Ja Builder');
	}
}




?>