<?php


/**
 * @package     pkg_dearflip
 * @subpackage  com_dearflip
 * @since       1.0.0
 * @copyright   Copyright © 2007 Free Software Foundation, Inc. All rights reserved.
 * @license   GNU General Public License version 3 or later; see https://www.gnu.org/licenses/gpl-3.0.en.html
 */

// No direct access to this file
defined( '_JEXEC' ) or die( 'Restricted access' );
use Joomla\CMS\Document\Document;

/**
 * Dearflipcat View
 *
 * @since  0.0.1
 */
class DearflipViewDearflipcat extends JViewLegacy {
  /**
   * View form
   *
   * @var         form
   */
  protected $form = null;
  
  /**
   * Display the Dearflipcat view
   *
   * @param string $tpl The name of the template file to parse; automatically searches through the template paths.
   *
   * @return  void
   */
  public function display( $tpl = null ) {
    // Get the Data
    $this->form = $this->get( 'Form' );
    $this->item = $this->get( 'Item' );
    
    
    // Check for errors.
    if ( count( $errors = $this->get( 'Errors' ) ) ) {
      JFactory::getApplication()->enqueueMessage();
      
      return false;
      
    }
    
    
    // Set the toolbar
    $this->addToolBar();
    
    // Display the template
    parent::display( $tpl );
    
    // Set the document
      $document =JFactory::getDocument();
    $this->setDocument($document);
  }
  
  /**
   * Add the page title and toolbar.
   *
   * @return  void
   *
   * @since   1.6
   */
  
  
  protected function addToolBar() {
    $input = JFactory::getApplication()->input;
    
    // Hide Joomla Administrator Main menu
    $input->set( 'hidemainmenu', true );
    
    $isNew = ( $this->item->id == 0 );
    
    if ( $isNew ) {
      $title = JText::_( 'COM_DEARFLIP_CATEGORY_MANAGER_DEARFLIP_NEW' );
    } else {
      $title = JText::_( 'COM_DEARFLIP_CATEGORY_MANAGER_DEARFLIP_EDIT' );
    }
    
    JToolbarHelper::title( $title, 'dearflipcat' );
    JToolbarHelper::apply( 'dearflipcat.apply' );
    JToolbarHelper::save( 'dearflipcat.save' );
    JToolbarHelper::cancel(
        'dearflipcat.cancel',
        $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE'
    );
  }
  
  /**
   * Method to set up the document properties
   *
   * @return void
   */
  public function setDocument(Document $document) : void {
      $this->item = $this->get( 'Item' );
    $isNew    = ( $this->item->id < 1 );
    $document = JFactory::getDocument();
    $document->setTitle( $isNew ? JText::_( 'COM_DEARFLIP_DEARFLIP_CREATING' ) :
        JText::_( 'COM_DEARFLIP_DEARFLIP_CATEGORY_EDITING' ) );
  }
}



