<?php
namespace JExtstore\Component\JChat\Administrator\View\Rooms;
/**
 * @package JCHAT::ROOMS::administrator::components::com_jchat
 * @subpackage views
 * @subpackage rooms
 * @author Joomla! Extensions Store
 * @Copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html  
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Filter\OutputFilter;
use Joomla\CMS\Pagination\Pagination;
use JExtstore\Component\JChat\Administrator\Framework\View as JChatView;

/**
 * @package JCHAT::ROOMS::administrator::components::com_jchat
 * @subpackage views
 * @subpackage rooms
 * @since 1.0
 */
class HtmlView extends JChatView {
	// Template view variables
	protected $pagination;
	protected $searchword;
	protected $lists;
	protected $orders;
	protected $items;
	protected $record;
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addEditEntityToolbar() {
		$user		= $this->app->getIdentity();
		$userId		= $user->get('id');
		$isNew		= ($this->record->id == 0);
		$checkedOut	= !($this->record->checked_out == 0 || $this->record->checked_out == $userId);
		$toolbarHelperTitle = $isNew ? 'COM_JCHAT_ROOM_NEW' : 'COM_JCHAT_ROOM_EDIT';
	
		$doc = $this->app->getDocument();
		ToolbarHelper::title( Text::_( $toolbarHelperTitle ), 'jchat' );
	
		if ($isNew)  {
			// For new records, check the create permission.
			if ($isNew && ($user->authorise('core.create', 'com_jchat'))) {
				ToolbarHelper::apply( 'rooms.applyEntity', 'JAPPLY');
				ToolbarHelper::save( 'rooms.saveEntity', 'JSAVE');
				ToolbarHelper::save2new( 'rooms.saveEntity2New');
			}
		} else {
			// Can't save the record if it's checked out.
			if (!$checkedOut) {
				// Since it's an existing record, check the edit permission, or fall back to edit own if the owner.
				if ($user->authorise('core.edit', 'com_jchat')) {
					ToolbarHelper::apply( 'rooms.applyEntity', 'JAPPLY');
					ToolbarHelper::save( 'rooms.saveEntity', 'JSAVE');
					ToolbarHelper::save2new( 'rooms.saveEntity2New');
				}
			}
		}
			
		ToolbarHelper::custom('rooms.cancelEntity', 'cancel', 'cancel', 'JCANCEL', false);
	}
	
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addDisplayToolbar() {
		$user = $this->app->getIdentity();
		ToolbarHelper::title(  Text::_('COM_JCHAT_MAINTITLE_TOOLBAR') . Text::_( 'COM_JCHAT_LIST_ROOMS' ), 'jchat' );
		ToolbarHelper::addNew('rooms.editEntity', 'COM_JCHAT_ADD_ROOM');
		ToolbarHelper::editList('rooms.editEntity', 'COM_JCHAT_EDIT_ROOM');
		ToolbarHelper::deleteList(Text::_('COM_JCHAT_DELETE_ROOM'), 'rooms.deleteEntity');
		ToolbarHelper::custom('cpanel.display', 'home', 'home', 'COM_JCHAT_CPANEL', false);
	}
	
	/**
	 * Default display listEntities
	 *        	
	 * @access public
	 * @param string $tpl
	 * @return void
	 */
	public function display($tpl = 'list') {
		// Get main records
		$rows = $this->get ( 'Data' );
		$lists = $this->get ( 'Filters' );
		$total = $this->get ( 'Total' );
		
		$doc = $this->app->getDocument();
		$this->loadJQuery($doc);
		$this->loadBootstrap($doc);
		
		$orders = array ();
		$orders ['order'] = $this->getModel ()->getState ( 'order' );
		$orders ['order_Dir'] = $this->getModel ()->getState ( 'order_dir' );
		// Pagination view object model state populated
		$pagination = new Pagination ( $total, $this->getModel ()->getState ( 'limitstart' ), $this->getModel ()->getState ( 'limit' ) );
		
		$this->pagination = $pagination;
		$this->searchword = $this->getModel ()->getState ( 'searchword' );
		$this->lists = $lists;
		$this->orders = $orders;
		$this->items = $rows;
		
		// Aggiunta toolbar
		$this->addDisplayToolbar();
		
		parent::display ($tpl);
	}
	
	/**
	 * Edit entity view
	 *
	 * @access public
	 * @param Object& $row the item to edit
	 * @return void
	 */
	public function editEntity($row) {
		// Sanitize HTML Object2Form
		OutputFilter::objectHTMLSafe( $row );
		
		// Load JS Client App dependencies
		$doc = $this->app->getDocument();
		$base = Uri::root();
		$this->loadJQuery($doc);
		$this->loadBootstrap($doc);
		$this->loadValidation($doc);
		
		// Load specific JS App
		$doc->getWebAssetManager()->addInlineScript("
					Joomla.submitbutton = function(pressbutton) {
						if(!jQuery.fn.validation) {
							jQuery.extend(jQuery.fn, jchatjQueryBackup.fn);
						}
						jQuery('#adminForm').validation();
				
						if (pressbutton == 'rooms.cancelEntity') {
							jQuery('#adminForm').off();
							Joomla.submitform( pressbutton );
							return true;
						}
				
						if(jQuery('#adminForm').validate()) {
							Joomla.submitform( pressbutton );
							return true;
						}
						return false;
					}
				");
		
		$lists = $this->getModel()->getLists($row);
		$this->record = $row;
		$this->lists = $lists;
		
		// Aggiunta toolbar
		$this->addEditEntityToolbar();
		
		parent::display ( 'edit' );
	}
}