Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Field/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/Field/RecordselectField.php

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

namespace TechFry\Library\Field;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Form\Field\ModalSelectField;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;

/*
EXAMPLE

<field 
    name="element" 
    type="recordselect" 
    label="COM_TFPAGEBUILDER_ELEMENT" 
    component="tfpagebuilder" 
    views="elements"
    view="element"
    sql_title_table="tfpb_elements"
    new="true"
    edit="true"
    addfieldprefix="TechFry\Library\Field" />
*/

class RecordselectField extends ModalSelectField
{
    protected $type = 'Recordselect';

    public function setup(\SimpleXMLElement $element, $value, $group = null)
    {
        // Modal field with select, new, edit, clear butttons
        
        $result = parent::setup($element, $value, $group);

        // Get component and view
        $option = 'com_' . (string) $this->element['component'];
        $views = (string) $this->element['views'];
        $view = (string) $this->element['view'];

        // Database Table
        $this->sql_title_table = '#__' . $this->sql_title_table;
        $this->sql_title_column = 'title';
        $this->sql_title_key = 'id';

        // Prepare Urls
        $linkRecords = (new Uri())->setPath(Uri::base(true) . '/index.php');
        $linkRecords->setQuery([
            'option' => $option,
            'view' => $views,
            'layout' => 'modal',
            'tmpl' => 'component',
            Session::getFormToken() => 1,
        ]);

        $linkRecord = clone $linkRecords;
        $linkRecord->setVar('view', $view);

        $urlEdit = clone $linkRecord;
        $urlEdit->setVar('task', $view . '.edit');

        $urlNew = clone $linkRecord;
        $urlNew->setVar('task', $view . '.add');

        $urlSelect = $linkRecords;

        $this->urls['select'] = (string) $urlSelect;
        $this->urls['new'] = (string) $urlNew;
        $this->urls['edit'] = (string) $urlEdit;

        // Prepare titles
        $this->modalTitles['select'] = Text::_('JSELECT') . ' ' . Text::_($this->element['label']);
        $this->modalTitles['new'] = Text::_('JACTION_CREATE') . ' ' . Text::_($this->element['label']);
        $this->modalTitles['edit'] = Text::_('JACTION_EDIT') . ' ' . Text::_($this->element['label']);

        $this->hint = $this->hint ?: Text::_('JSELECT') . ' ' . Text::_($this->element['label']);

        return $result;
    }
}