Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Front/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Front/TfViewForm.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\View\Front;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use TechFry\Library\View\JForm\TField;

class TfViewForm extends TfView
{
    // View class for frontend forms
    
    public $form;
  
    public $item;
  
    public $loadData = true;
  
    public $fileUpload = false;
  
    public $is_new;

    public $controller_name;
  
    public $button = [];

    public function initiate_form()
    {
        $this->init_view1();
        
        // Get data from model
        $this->form = $this->get('Form');
        if ($this->loadData)
        {
            $this->item = $this->get('Item');
        }

        $this->init_view2();
    }
    
    // Function to start displaying form layout
    public function start_form_layout()
    {
        $wa = $this->document->getWebAssetManager();
		$wa->useScript('keepalive')
    		->useScript('form.validate');
      
      	$link = 'index.php?option=' . $this->option . '&view=' . $this->view_name;
        if (isset($this->item->id))
        {
            $link .= '&id=' . (int) $this->item->id;
        }
        
        echo '<form action="' . Route::_($link) . '" method="post" name="adminForm" id="adminForm" class="form-validate"';
        if ($this->fileUpload)
        {
            echo ' enctype="multipart/form-data"';
        }
        echo '>';
    }
    
    // Function to finish displaying form layout
    public function end_form_layout()
    {   
        // Captcha Field
        if ($this->params->get('show_captcha', 0) && !$this->juser->id)
        {
            $num1 = rand(1, 9);
            $num2 = rand(1, 9);
            $answer = $num1 + $num2;
        
            $math = $num1 . ' + ' . $num2 . ' = ?';
            $hint = Text::_(strtoupper($this->option) . '_CAPTCHA') . ': ' . $num1 . ' + ' . $num2 . ' equals to';
        
            $this->form->setFieldAttribute('captcha', 'label', Text::_(strtoupper($this->option) . '_CAPTCHA') . ': ' . $math);
            $this->form->setFieldAttribute('captcha', 'hint', $hint);
            $this->form->setFieldAttribute('captcha', 'required', 'true');
          
          	$captcha_field = $this->form->getField('captcha');
          	if ($captcha_field)
            {
              	$f = new TField($captcha_field, array('show_label' => true));
                echo $f->get_output();
            }

            echo '<input type="hidden" name="jform[answer]" value="' . $answer . '" />';
        }
        
        // Custom Redirect
        if (!empty($this->params->get('custom_redirect', '')))
        {
            $this->form->setValue('custom_redirect', null, $this->params->get('custom_redirect'));
        }
        else
        {
            $this->form->setValue('custom_redirect', null, Uri::getInstance());
        }
        
        // Custom Message
        if (!empty($this->params->get('custom_message', '')))
        {
            $this->form->setValue('custom_message', null, $this->params->get('custom_message'));
        }
        
        echo $this->form->renderFieldset('details');
        
        echo '<input type="hidden" name="option" value="' . $this->option . '" />';

        $controller = $this->controller_name ?? $this->view_name;
        $task = $controller . '.save';
        
        echo '<input type="hidden" name="task" value="' . $task . '" />';

        echo HTMLHelper::_('form.token');

        // Submit Button
        if (empty($this->button))
        {   
            $this->button['button_text'] = $this->params->get('button_text', '') ?: Text::_('JSUBMIT');
            $this->button['button_class'] = $this->params->get('button_class', '') ?: 'btn btn-primary';
            $this->button['button_icon'] = $this->params->get('button_icon', '');
        }
          
        echo '<button type="submit" class="me-2 ' . $this->button['button_class'] . '">';
            if (isset($this->button['button_icon']) && $this->button['button_icon'])
            {
                echo '<span class="' . $this->button['button_icon'] . '" aria-hidden="true"></span> ';
            }
            echo $this->button['button_text'];
        echo '</button>';
		
        echo '</form>';
    }
    
    // Renders fieldset legend (label), description and all fields (Depreciated : Use TFieldset)
    public function render_fieldset($fieldset, $show_label = true)
    {
    	$output = '';
      
      	if (isset($fieldset->label) && !empty($fieldset->label) && $show_label)
      	{
        	$output .= '<fieldset class="border border-primary p-3 mb-2 shadow">';
        	$output .= '<legend class="float-none w-auto p-2">' . Text::_($fieldset->label) . '</legend>';
      	}

      	if (isset($fieldset->description) && !empty($fieldset->description))
      	{
        	$output .= '<div class="alert bg-light alert-info">';
        	$output .= '<span class="fas fa-info-circle" aria-hidden="true"></span> ' . Text::_($fieldset->description);
        	$output .= '</div>';
      	}

      	$output .= $this->form->renderFieldset($fieldset->name);
        
        if (isset($fieldset->label) && !empty($fieldset->label) && $show_label)
        {
            $output .= '</fieldset>';
        }
        
        return $output;
    }
    
    protected function _prepareDocument()
    {
        $app = Factory::getApplication();
        $title = null;
        
        // 01. Page heading of document
		$menus = $app->getMenu();
		$menu = $menus->getActive();
		if ($menu)
		{
			$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
		}
        
        // 02. Title of document
        $title = $this->params->get('page_title', $menu->title);
        
        $this->setDocumentTitle($title);
    }
}