<?php
// namespace administrator\components\com_jmap\views\aigenerator;
/**
 * @package JMAP::LINKS::administrator::components::com_jmap
 * @subpackage views
 * @subpackage links
 * @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' );

/**
 * @package JMAP::PATTERNS::administrator::components::com_jmap
 * @subpackage views
 * @subpackage links
 * * @since 1.8
 */
class JMapViewPatterns extends JMapView {
	// Template view variables
	protected $pagination;
	protected $searchword;
	protected $urlRewriting;
	protected $orders;
	protected $lists;
	protected $items;
	protected $isMultiLanguage;
	protected $urischeme;
	protected $componentParams;
	protected $record;
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addEditEntityToolbar() {
		$user		= JFactory::getUser();
		$userId		= $user->get('id');
		$isNew		= ($this->record->id == 0);
		$checkedOut	= !($this->record->checked_out == 0 || $this->record->checked_out == $userId);
		$JToolbarHelperTitle = $isNew ? 'COM_JMAP_PATTERNS_REPLACEMENT_NEW' : 'COM_JMAP_PATTERNS_REPLACEMENT_EDIT';
		
		JToolbarHelper::title( JText::_( $JToolbarHelperTitle ), 'jredirects' );
		
		if ($isNew)  {
			// For new records, check the create permission.
			if ($isNew && ($user->authorise('core.create', 'com_jmap'))) {
				JToolbarHelper::apply( 'patterns.applyEntity', 'JAPPLY');
				JToolbarHelper::save( 'patterns.saveEntity', 'JSAVE');
				JToolBarHelper::save2new( 'aigenerator.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_jmap')) {
					JToolbarHelper::apply( 'patterns.applyEntity', 'JAPPLY');
					JToolbarHelper::save( 'patterns.saveEntity', 'JSAVE');
					JToolBarHelper::save2new( 'aigenerator.saveEntity2New');
				}
			}
		}
		
		JToolbarHelper::custom('patterns.cancelEntity', 'cancel', 'cancel', 'JCANCEL', false);
	}
	
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addDisplayToolbar() {
		$user = JFactory::getUser();
		JToolbarHelper::title( JText::_('COM_JMAP_PATTERNS_REPLACEMENT_TITLE' ), 'jredirects' );
		
		// Access check.
		if ($user->authorise('core.create')) {
			JToolbarHelper::addNew('patterns.editEntity', 'COM_JMAP_NEW_PATTERN');
		}
		
		if ($user->authorise('core.edit')) {
			JToolbarHelper::editList('patterns.editentity', 'COM_JMAP_EDIT_PATTERN');
		}
		
		if ($user->authorise('core.delete') && $user->authorise('core.edit')) {
			JToolbarHelper::deleteList('COM_JMAP_DELETE_ENTITY', 'patterns.deleteentity');
		}
		
		JToolbarHelper::custom('cpanel.display', 'home', 'home', 'COM_JMAP_CPANEL', false);
	}
	
	/**
	 * Default display listEntities
	 *
	 * @access public
	 * @param string $tpl
	 * @return void
	 */
	public function display($tpl = null) {
		// Get main records
		$rows = $this->get ( 'Data' );
		$total = $this->get ( 'Total' );
		$lists = $this->get('Filters');
		
		$doc = JFactory::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 JPagination ( $total, $this->getModel ()->getState ( 'limitstart' ), $this->getModel ()->getState ( 'limit' ) );
		
		$this->user = JFactory::getUser();
		$this->pagination = $pagination;
		$this->searchword = $this->getModel ()->getState ( 'searchword' );
		$this->option = $this->getModel ()->getState ( 'option' );
		$this->urlRewriting = $this->app->get('sef_rewrite', 0) ? '' : 'index.php/';
		$this->orders = $orders;
		$this->lists = $lists;
		$this->items = $rows;
		
		// Aggiunta toolbar
		$this->addDisplayToolbar();
		
		parent::display ( 'list' );
	}
	
	/**
	 * Edit entity view
	 *
	 * @access public
	 * @param Object& $row the item to edit
	 * @return void
	 */
	public function editEntity(&$row) {
		// Sanitize HTML Object2Form
		JFilterOutput::objectHTMLSafe( $row );
		
		// Detect uri scheme
		$instance = JUri::getInstance();
		$this->urischeme = $instance->isSSL() ? 'https' : 'http';
		
		// Load JS Client App dependencies
		$doc = JFactory::getDocument();
		$base = JUri::root();
		$this->loadJQuery($doc);
		$this->loadBootstrap($doc);
		$this->loadValidation($doc);
		
		// Inject js translations
		/*$translations = array( '' );
		 $this->injectJsTranslations($translations, $doc);*/
		
		// Load specific JS App
		$doc->addScriptDeclaration("
						Joomla.submitbutton = function(pressbutton) {
							if(!jQuery.fn.validation) {
								jQuery.extend(jQuery.fn, jredirectsjQueryBackup.fn);
							}
				
							jQuery('#adminForm').validation();
				
							if (pressbutton == 'patterns.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->option = $this->getModel ()->getState ( 'option' );
		$this->componentParams = $this->getModel()->getComponentParams();
		$this->record = $row;
		$this->lists = $lists;
		
		// Aggiunta toolbar
		$this->addEditEntityToolbar();
		
		parent::display ( 'edit' );
	}
}