<?php

namespace {
/*
 * This file is part of Contao.
 *
 * (c) Leo Feyer
 *
 * @license LGPL-3.0-or-later
 */
$GLOBALS['TL_DCA']['tl_form_field'] = array(
    // Config
    'config' => array('dataContainer' => \Contao\DC_Table::class, 'enableVersioning' => \true, 'ptable' => 'tl_form', 'markAsCopy' => 'label', 'onload_callback' => array(array('tl_form_field', 'checkPermission'), array('tl_form_field', 'filterFormFields')), 'sql' => array('keys' => array('id' => 'primary', 'pid,invisible,sorting' => 'index'))),
    // List
    'list' => array('sorting' => array('mode' => 4, 'fields' => array('sorting'), 'panelLayout' => 'filter;search,limit', 'headerFields' => array('title', 'tstamp', 'formID', 'storeValues', 'sendViaEmail', 'recipient', 'subject'), 'child_record_callback' => array('tl_form_field', 'listFormFields')), 'global_operations' => array('all' => array('href' => 'act=select', 'class' => 'header_edit_all', 'attributes' => 'onclick="Backend.getScrollOffset()" accesskey="e"')), 'operations' => array('edit' => array('href' => 'act=edit', 'icon' => 'edit.svg', 'button_callback' => array('tl_form_field', 'disableButton')), 'copy' => array('href' => 'act=paste&amp;mode=copy', 'icon' => 'copy.svg', 'attributes' => 'onclick="Backend.getScrollOffset()"', 'button_callback' => array('tl_form_field', 'disableButton')), 'cut' => array('href' => 'act=paste&amp;mode=cut', 'icon' => 'cut.svg', 'attributes' => 'onclick="Backend.getScrollOffset()"'), 'delete' => array('href' => 'act=delete', 'icon' => 'delete.svg', 'attributes' => 'onclick="if(!confirm(\'' . $GLOBALS['TL_LANG']['MSC']['deleteConfirm'] . '\'))return false;Backend.getScrollOffset()"', 'button_callback' => array('tl_form_field', 'disableButton')), 'toggle' => array('icon' => 'visible.svg', 'attributes' => 'onclick="Backend.getScrollOffset();return AjaxRequest.toggleVisibility(this,%s)"', 'button_callback' => array('tl_form_field', 'toggleIcon')), 'show' => array('href' => 'act=show', 'icon' => 'show.svg'))),
    // Palettes
    'palettes' => array('__selector__' => array('type', 'multiple', 'storeFile', 'imageSubmit', 'rgxp'), 'default' => '{type_legend},type', 'explanation' => '{type_legend},type;{text_legend},text;{expert_legend:hide},class;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'fieldsetStart' => '{type_legend},type;{fconfig_legend},label;{expert_legend:hide},class;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'fieldsetStop' => '{type_legend},type;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'html' => '{type_legend},type;{text_legend},html;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'text' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{expert_legend:hide},class,value,minlength,maxlength,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'textdigit' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{expert_legend:hide},class,value,minval,maxval,step,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'password' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{expert_legend:hide},class,value,minlength,maxlength,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'textarea' => '{type_legend},type,name,label;{fconfig_legend},mandatory,rgxp,placeholder;{size_legend},size;{expert_legend:hide},class,value,minlength,maxlength,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'select' => '{type_legend},type,name,label;{fconfig_legend},mandatory,multiple;{options_legend},options;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'radio' => '{type_legend},type,name,label;{fconfig_legend},mandatory;{options_legend},options;{expert_legend:hide},class;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'checkbox' => '{type_legend},type,name,label;{fconfig_legend},mandatory;{options_legend},options;{expert_legend:hide},class;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'upload' => '{type_legend},type,name,label;{fconfig_legend},mandatory,extensions,maxlength;{store_legend:hide},storeFile;{expert_legend:hide},class,accesskey,tabindex,fSize;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'range' => '{type_legend},type,name,label;{fconfig_legend},mandatory;{expert_legend:hide},class,value,minval,maxval,step,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'hidden' => '{type_legend},type,name,value;{fconfig_legend},mandatory,rgxp;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'captcha' => '{type_legend},type,label;{fconfig_legend},placeholder;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible', 'submit' => '{type_legend},type,slabel;{image_legend:hide},imageSubmit;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl;{invisible_legend:hide},invisible'),
    // Subpalettes
    'subpalettes' => array('multiple' => 'mSize', 'storeFile' => 'uploadFolder,useHomeDir,doNotOverwrite', 'imageSubmit' => 'singleSRC'),
    // Fields
    'fields' => array('id' => array('sql' => "int(10) unsigned NOT NULL auto_increment"), 'pid' => array('foreignKey' => 'tl_form.title', 'sql' => "int(10) unsigned NOT NULL default 0", 'relation' => array('type' => 'belongsTo', 'load' => 'lazy')), 'sorting' => array('sql' => "int(10) unsigned NOT NULL default 0"), 'tstamp' => array('sql' => "int(10) unsigned NOT NULL default 0"), 'type' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'select', 'options_callback' => array('tl_form_field', 'getFields'), 'eval' => array('helpwizard' => \true, 'submitOnChange' => \true, 'tl_class' => 'w50'), 'reference' => &$GLOBALS['TL_LANG']['FFL'], 'sql' => array('name' => 'type', 'type' => 'string', 'length' => 64, 'default' => 'text')), 'label' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'name' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'rgxp' => 'fieldname', 'spaceToUnderscore' => \true, 'maxlength' => 64, 'tl_class' => 'w50 clr'), 'sql' => "varchar(64) NOT NULL default ''"), 'text' => array('exclude' => \true, 'search' => \true, 'inputType' => 'textarea', 'eval' => array('rte' => 'tinyMCE', 'helpwizard' => \true), 'explanation' => 'insertTags', 'sql' => "text NULL"), 'html' => array('exclude' => \true, 'search' => \true, 'inputType' => 'textarea', 'eval' => array('mandatory' => \true, 'allowHtml' => \true, 'class' => 'monospace', 'rte' => 'ace|html'), 'sql' => "text NULL"), 'options' => array('exclude' => \true, 'inputType' => 'optionWizard', 'eval' => array('mandatory' => \true, 'allowHtml' => \true), 'xlabel' => array(array('tl_form_field', 'optionImportWizard')), 'sql' => "blob NULL"), 'mandatory' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'sql' => "char(1) NOT NULL default ''"), 'rgxp' => array('exclude' => \true, 'inputType' => 'select', 'options' => array('digit', 'alpha', 'alnum', 'extnd', 'date', 'time', 'datim', 'phone', 'email', 'url'), 'reference' => &$GLOBALS['TL_LANG']['tl_form_field'], 'eval' => array('helpwizard' => \true, 'includeBlankOption' => \true, 'submitOnChange' => \true, 'tl_class' => 'w50'), 'sql' => "varchar(32) NOT NULL default ''"), 'placeholder' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('decodeEntities' => \true, 'maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'minlength' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'natural', 'tl_class' => 'w50'), 'sql' => "int(10) unsigned NOT NULL default 0"), 'maxlength' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'natural', 'tl_class' => 'w50'), 'sql' => "int(10) unsigned NOT NULL default 0"), 'minval' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'digit', 'tl_class' => 'w50'), 'sql' => "varchar(10) NOT NULL default ''"), 'maxval' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'digit', 'tl_class' => 'w50'), 'sql' => "varchar(10) NOT NULL default ''"), 'step' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'digit', 'tl_class' => 'w50'), 'sql' => "varchar(10) NOT NULL default ''"), 'size' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'multiple' => \true, 'size' => 2, 'rgxp' => 'natural', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default 'a:2:{i:0;i:4;i:1;i:40;}'"), 'multiple' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true, 'tl_class' => 'clr'), 'sql' => "char(1) NOT NULL default ''"), 'mSize' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'natural'), 'sql' => "smallint(5) unsigned NOT NULL default 0"), 'extensions' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'rgxp' => 'extnd', 'maxlength' => 255, 'tl_class' => 'w50'), 'save_callback' => array(array('tl_form_field', 'checkExtensions')), 'sql' => "varchar(255) NOT NULL default 'jpg,jpeg,gif,png,pdf,doc,docx,xls,xlsx,ppt,pptx'"), 'storeFile' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true), 'sql' => "char(1) NOT NULL default ''"), 'uploadFolder' => array('exclude' => \true, 'inputType' => 'fileTree', 'eval' => array('fieldType' => 'radio', 'tl_class' => 'clr'), 'sql' => "binary(16) NULL"), 'useHomeDir' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50'), 'sql' => "char(1) NOT NULL default ''"), 'doNotOverwrite' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50'), 'sql' => "char(1) NOT NULL default ''"), 'class' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'value' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('decodeEntities' => \true, 'maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'accesskey' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'alnum', 'maxlength' => 1, 'tl_class' => 'w50'), 'sql' => "char(1) NOT NULL default ''"), 'tabindex' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'natural', 'tl_class' => 'w50'), 'sql' => "smallint(5) unsigned NOT NULL default 0"), 'fSize' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'natural', 'tl_class' => 'w50'), 'sql' => "smallint(5) unsigned NOT NULL default 0"), 'customTpl' => array('exclude' => \true, 'inputType' => 'select', 'options_callback' => array('tl_form_field', 'getFormFieldTemplates'), 'eval' => array('chosen' => \true, 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'slabel' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'maxlength' => 255, 'tl_class' => 'w50 clr'), 'sql' => "varchar(255) NOT NULL default ''"), 'imageSubmit' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true), 'sql' => "char(1) NOT NULL default ''"), 'singleSRC' => array('exclude' => \true, 'inputType' => 'fileTree', 'eval' => array('fieldType' => 'radio', 'filesOnly' => \true, 'mandatory' => \true, 'tl_class' => 'clr'), 'sql' => "binary(16) NULL"), 'invisible' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'sql' => "char(1) NOT NULL default ''")),
);
/**
 * Provide miscellaneous methods that are used by the data configuration array.
 *
 * @author Leo Feyer <https://github.com/leofeyer>
 */
class tl_form_field extends \Contao\Backend
{
    /**
     * Import the back end user object
     */
    public function __construct()
    {
        parent::__construct();
        $this->import('Contao\\BackendUser', 'User');
    }
    /**
     * Check permissions to edit table tl_form_field
     *
     * @throws Contao\CoreBundle\Exception\AccessDeniedException
     */
    public function checkPermission()
    {
        if ($this->User->isAdmin) {
            return;
        }
        /** @var Symfony\Component\HttpFoundation\Session\SessionInterface $objSession */
        $objSession = \Contao\System::getContainer()->get('session');
        // Set root IDs
        if (empty($this->User->forms) || !\is_array($this->User->forms)) {
            $root = array(0);
        } else {
            $root = $this->User->forms;
        }
        $id = \strlen(\Contao\Input::get('id')) ? \Contao\Input::get('id') : \CURRENT_ID;
        // Check current action
        switch (\Contao\Input::get('act')) {
            case 'paste':
            case 'select':
                // Check CURRENT_ID here (see #247)
                if (!\in_array(\CURRENT_ID, $root)) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to access form ID ' . $id . '.');
                }
                break;
            case 'create':
            case 'cut':
            case 'copy':
                $pid = \Contao\Input::get('pid');
                // Get form ID
                if (\Contao\Input::get('mode') == 1) {
                    $objField = $this->Database->prepare("SELECT pid FROM tl_form_field WHERE id=?")->limit(1)->execute(\Contao\Input::get('pid'));
                    if ($objField->numRows < 1) {
                        throw new \Contao\CoreBundle\Exception\AccessDeniedException('Invalid form field ID ' . \Contao\Input::get('pid') . '.');
                    }
                    $pid = $objField->pid;
                }
                if (!\in_array($pid, $root)) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to ' . \Contao\Input::get('act') . ' form field ID ' . $id . ' to form ID ' . $pid . '.');
                }
                if (\Contao\Input::get('act') == 'create') {
                    break;
                }
            // no break
            case 'edit':
            case 'show':
            case 'delete':
            case 'toggle':
                $objField = $this->Database->prepare("SELECT pid FROM tl_form_field WHERE id=?")->limit(1)->execute($id);
                if ($objField->numRows < 1) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Invalid form field ID ' . $id . '.');
                }
                if (!\in_array($objField->pid, $root)) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to ' . \Contao\Input::get('act') . ' form field ID ' . $id . ' of form ID ' . $objField->pid . '.');
                }
                break;
            case 'editAll':
            case 'deleteAll':
            case 'overrideAll':
            case 'cutAll':
            case 'copyAll':
                if (!\in_array($id, $root)) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to access form ID ' . $id . '.');
                }
                $objForm = $this->Database->prepare("SELECT id FROM tl_form_field WHERE pid=?")->execute($id);
                $session = $objSession->all();
                $session['CURRENT']['IDS'] = \array_intersect((array) $session['CURRENT']['IDS'], $objForm->fetchEach('id'));
                $objSession->replace($session);
                break;
            default:
                if (\Contao\Input::get('act')) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Invalid command "' . \Contao\Input::get('act') . '".');
                }
                if (!\in_array($id, $root)) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to access form ID ' . $id . '.');
                }
                break;
        }
    }
    /**
     * Filter the form fields
     */
    public function filterFormFields()
    {
        if ($this->User->isAdmin) {
            return;
        }
        if (empty($this->User->fields)) {
            $GLOBALS['TL_DCA']['tl_form_field']['config']['closed'] = \true;
            $GLOBALS['TL_DCA']['tl_form_field']['config']['notEditable'] = \true;
        } elseif (!\in_array($GLOBALS['TL_DCA']['tl_form_field']['fields']['type']['sql']['default'], $this->User->fields)) {
            $GLOBALS['TL_DCA']['tl_form_field']['fields']['type']['default'] = $this->User->fields[0];
        }
        /** @var Symfony\Component\HttpFoundation\Session\SessionInterface $objSession */
        $objSession = \Contao\System::getContainer()->get('session');
        // Prevent editing form fields with not allowed types
        if (\Contao\Input::get('act') == 'edit' || \Contao\Input::get('act') == 'delete' || \Contao\Input::get('act') == 'paste' && \Contao\Input::get('mode') == 'copy') {
            $objField = $this->Database->prepare("SELECT type FROM tl_form_field WHERE id=?")->execute(\Contao\Input::get('id'));
            if ($objField->numRows && !\in_array($objField->type, $this->User->fields)) {
                throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to modify form fields of type "' . $objField->type . '".');
            }
        }
        // Prevent editing content elements with not allowed types
        if (\Contao\Input::get('act') == 'editAll' || \Contao\Input::get('act') == 'overrideAll' || \Contao\Input::get('act') == 'deleteAll') {
            $session = $objSession->all();
            if (!empty($session['CURRENT']['IDS']) && \is_array($session['CURRENT']['IDS'])) {
                if (empty($this->User->fields)) {
                    $session['CURRENT']['IDS'] = array();
                } else {
                    $objFields = $this->Database->prepare("SELECT id FROM tl_form_field WHERE id IN(" . \implode(',', \array_map('\\intval', $session['CURRENT']['IDS'])) . ") AND type IN(" . \implode(',', \array_fill(0, \count($this->User->fields), '?')) . ")")->execute(...$this->User->fields);
                    $session['CURRENT']['IDS'] = $objFields->fetchEach('id');
                }
                $objSession->replace($session);
            }
        }
        // Prevent copying content elements with not allowed types
        if (\Contao\Input::get('act') == 'copyAll') {
            $session = $objSession->all();
            if (!empty($session['CLIPBOARD']['tl_form_field']['id']) && \is_array($session['CLIPBOARD']['tl_form_field']['id'])) {
                if (empty($this->User->fields)) {
                    $session['CLIPBOARD']['tl_form_field']['id'] = array();
                } else {
                    $objFields = $this->Database->prepare("SELECT id, type FROM tl_form_field WHERE id IN(" . \implode(',', \array_map('\\intval', $session['CLIPBOARD']['tl_form_field']['id'])) . ") AND type IN(" . \implode(',', \array_fill(0, \count($this->User->fields), '?')) . ")")->execute(...$this->User->fields);
                    $session['CLIPBOARD']['tl_form_field']['id'] = $objFields->fetchEach('id');
                }
                $objSession->replace($session);
            }
        }
    }
    /**
     * Add the type of input field
     *
     * @param array $arrRow
     *
     * @return string
     */
    public function listFormFields($arrRow)
    {
        $arrRow['required'] = $arrRow['mandatory'];
        $strClass = $GLOBALS['TL_FFL'][$arrRow['type']];
        if (!\class_exists($strClass)) {
            return '';
        }
        /** @var Contao\Widget $objWidget */
        $objWidget = new $strClass($arrRow);
        $key = $arrRow['invisible'] ? 'unpublished' : 'published';
        $strType = '
<div class="cte_type ' . $key . '">' . $GLOBALS['TL_LANG']['FFL'][$arrRow['type']][0] . ($objWidget->submitInput() && $arrRow['name'] ? ' (' . $arrRow['name'] . ')' : '') . '</div>
<div class="limit_height' . (!\Contao\Config::get('doNotCollapse') ? ' h32' : '') . '">';
        $strWidget = $objWidget->parse();
        $strWidget = \preg_replace('/ name="[^"]+"/i', '', $strWidget);
        $strWidget = \str_replace(array(' type="submit"', ' autofocus', ' required'), array(' type="button"', '', ''), $strWidget);
        if ($objWidget instanceof \FormHidden) {
            return $strType . "\n" . $objWidget->value . "\n</div>\n";
        }
        return $strType . \Contao\StringUtil::insertTagToSrc($strWidget) . '
</div>' . "\n";
    }
    /**
     * Add a link to the option items import wizard
     *
     * @return string
     */
    public function optionImportWizard()
    {
        return ' <a href="' . $this->addToUrl('key=option') . '" title="' . \Contao\StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['ow_import'][1]) . '" onclick="Backend.getScrollOffset()">' . \Contao\Image::getHtml('tablewizard.svg', $GLOBALS['TL_LANG']['MSC']['ow_import'][0]) . '</a>';
    }
    /**
     * Check the configured extensions against the upload types
     *
     * @param mixed                $varValue
     * @param Contao\DataContainer $dc
     *
     * @return string
     */
    public function checkExtensions($varValue, \Contao\DataContainer $dc)
    {
        // Convert the extensions to lowercase
        $varValue = \strtolower($varValue);
        $arrExtensions = \Contao\StringUtil::trimsplit(',', $varValue);
        $arrUploadTypes = \Contao\StringUtil::trimsplit(',', \strtolower(\Contao\Config::get('uploadTypes')));
        $arrNotAllowed = \array_diff($arrExtensions, $arrUploadTypes);
        if (0 !== \count($arrNotAllowed)) {
            throw new \Exception(\sprintf($GLOBALS['TL_LANG']['ERR']['forbiddenExtensions'], \implode(', ', $arrNotAllowed)));
        }
        return $varValue;
    }
    /**
     * Return a list of form fields
     *
     * @return array
     */
    public function getFields()
    {
        $fields = array();
        foreach ($GLOBALS['TL_FFL'] as $k => $v) {
            if ($this->User->hasAccess($k, 'fields')) {
                $fields[] = $k;
            }
        }
        return $fields;
    }
    /**
     * Return all form field templates as array
     *
     * @param Contao\DataContainer $dc
     *
     * @return array
     */
    public function getFormFieldTemplates(\Contao\DataContainer $dc)
    {
        if (\Contao\Input::get('act') == 'overrideAll') {
            return $this->getTemplateGroup('form_');
        }
        $default = 'form_' . $dc->activeRecord->type;
        // Backwards compatibility
        if ($dc->activeRecord->type == 'text') {
            $default = 'form_textfield';
        }
        $arrTemplates = $this->getTemplateGroup('form_' . $dc->activeRecord->type . '_', array(), $default);
        // Backwards compatibility
        if ($dc->activeRecord->type == 'text') {
            $arrTemplates += $this->getTemplateGroup('form_textfield_');
        }
        return $arrTemplates;
    }
    /**
     * Disable the button if the element type is not allowed
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function disableButton($row, $href, $label, $title, $icon, $attributes)
    {
        return $this->User->hasAccess($row['type'], 'fields') ? '<a href="' . $this->addToUrl($href . '&amp;id=' . $row['id']) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label) . '</a> ' : \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
    }
    /**
     * Return the "toggle visibility" button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function toggleIcon($row, $href, $label, $title, $icon, $attributes)
    {
        if (\Contao\Input::get('tid')) {
            $this->toggleVisibility(\Contao\Input::get('tid'), \Contao\Input::get('state') == 1, \func_num_args() <= 12 ? \null : \func_get_arg(12));
            $this->redirect($this->getReferer());
        }
        // Check permissions AFTER checking the tid, so hacking attempts are logged
        if (!$this->User->hasAccess('tl_form_field::invisible', 'alexf')) {
            return '';
        }
        // Disable the button if the element type is not allowed
        if (!$this->User->hasAccess($row['type'], 'fields')) {
            return \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
        }
        $href .= '&amp;tid=' . $row['id'] . '&amp;state=' . $row['invisible'];
        if ($row['invisible']) {
            $icon = 'invisible.svg';
        }
        return '<a href="' . $this->addToUrl($href) . '" title="' . \Contao\StringUtil::specialchars($title) . '"' . $attributes . '>' . \Contao\Image::getHtml($icon, $label, 'data-state="' . ($row['invisible'] ? 0 : 1) . '"') . '</a> ';
    }
    /**
     * Toggle the visibility of a form field
     *
     * @param integer              $intId
     * @param boolean              $blnVisible
     * @param Contao\DataContainer $dc
     */
    public function toggleVisibility($intId, $blnVisible, \Contao\DataContainer $dc = \null)
    {
        // Set the ID and action
        \Contao\Input::setGet('id', $intId);
        \Contao\Input::setGet('act', 'toggle');
        if ($dc) {
            $dc->id = $intId;
            // see #8043
        }
        // Trigger the onload_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_form_field']['config']['onload_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_form_field']['config']['onload_callback'] as $callback) {
                if (\is_array($callback)) {
                    $this->import($callback[0]);
                    $this->{$callback[0]}->{$callback[1]}($dc);
                } elseif (\is_callable($callback)) {
                    $callback($dc);
                }
            }
        }
        // Check the field access
        if (!$this->User->hasAccess('tl_form_field::invisible', 'alexf')) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to publish/unpublish form field ID ' . $intId . '.');
        }
        $objRow = $this->Database->prepare("SELECT * FROM tl_form_field WHERE id=?")->limit(1)->execute($intId);
        if ($objRow->numRows < 1) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Invalid form field ID ' . $intId . '.');
        }
        if (!$this->User->hasAccess($objRow->type, 'fields')) {
            throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to modify form fields of type "' . $objRow->type . '".');
        }
        // Set the current record
        if ($dc) {
            $dc->activeRecord = $objRow;
        }
        $objVersions = new \Contao\Versions('tl_form_field', $intId);
        $objVersions->initialize();
        // Reverse the logic (form fields have invisible=1)
        $blnVisible = !$blnVisible;
        // Trigger the save_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_form_field']['fields']['invisible']['save_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_form_field']['fields']['invisible']['save_callback'] as $callback) {
                if (\is_array($callback)) {
                    $this->import($callback[0]);
                    $blnVisible = $this->{$callback[0]}->{$callback[1]}($blnVisible, $dc);
                } elseif (\is_callable($callback)) {
                    $blnVisible = $callback($blnVisible, $dc);
                }
            }
        }
        $time = \time();
        // Update the database
        $this->Database->prepare("UPDATE tl_form_field SET tstamp={$time}, invisible='" . ($blnVisible ? '1' : '') . "' WHERE id=?")->execute($intId);
        if ($dc) {
            $dc->activeRecord->tstamp = $time;
            $dc->activeRecord->invisible = $blnVisible ? '1' : '';
        }
        // Trigger the onsubmit_callback
        if (\is_array($GLOBALS['TL_DCA']['tl_form_field']['config']['onsubmit_callback'])) {
            foreach ($GLOBALS['TL_DCA']['tl_form_field']['config']['onsubmit_callback'] as $callback) {
                if (\is_array($callback)) {
                    $this->import($callback[0]);
                    $this->{$callback[0]}->{$callback[1]}($dc);
                } elseif (\is_callable($callback)) {
                    $callback($dc);
                }
            }
        }
        $objVersions->create();
        if ($dc) {
            $dc->invalidateCacheTags();
        }
    }
}
}

namespace {
$GLOBALS['TL_DCA']['tl_form_field']['config']['onload_callback'][] = ['netzmacht.contao_form_designer.listener.dca.form_field', 'initialize'];
$GLOBALS['TL_DCA']['tl_form_field']['fields']['helpMessage'] = ['exclude' => \true, 'inputType' => 'text', 'search' => \true, 'eval' => ['maxlength' => 255, 'tl_class' => 'long clr'], 'sql' => "varchar(255) NOT NULL default ''"];
$GLOBALS['TL_DCA']['tl_form_field']['fields']['controlClass'] = ['exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => ['maxlength' => 255, 'tl_class' => 'w50'], 'sql' => "varchar(255) NOT NULL default ''"];
$GLOBALS['TL_DCA']['tl_form_field']['fields']['formLayout'] = ['inputType' => 'select', 'eval' => ['tl_class' => 'w50', 'includeBlankOption' => \true, 'chosen' => \true], 'options_callback' => ['netzmacht.contao_form_designer.listener.dca.module', 'getFormLayoutOptions'], 'foreignKey' => 'tl_form_layout.title', 'relation' => ['type' => 'belongsTo', 'load' => 'lazy'], 'sql' => "int(10) unsigned NOT NULL default '0'"];
$GLOBALS['TL_DCA']['tl_form_field']['fields']['controlTemplate'] = ['exclude' => \true, 'label' => &$GLOBALS['TL_LANG']['tl_form_layout']['control'], 'inputType' => 'select', 'options_callback' => ['netzmacht.contao_form_designer.listener.dca.form_layout', 'getControlTemplates'], 'eval' => ['includeBlankOption' => \true, 'chosen' => \true, 'tl_class' => 'clr w50'], 'sql' => "varchar(255) NOT NULL default ''"];
$GLOBALS['TL_DCA']['tl_form_field']['fields']['layoutTemplate'] = ['exclude' => \true, 'inputType' => 'select', 'options_callback' => ['netzmacht.contao_form_designer.listener.dca.form_layout', 'getLayoutTemplates'], 'eval' => ['includeBlankOption' => \true, 'chosen' => \true, 'tl_class' => 'w50'], 'sql' => "varchar(255) NOT NULL default ''"];
}

namespace {
// alter palette for use with reCaptcha V3
$GLOBALS['TL_DCA']['tl_form_field']['config']['onload_callback'][] = function () {
    if (\Config::get('recaptchaType') != 'recaptcha3') {
        return;
    }
    $GLOBALS['TL_DCA']['tl_form_field']['palettes']['captcha'] = \str_replace('{fconfig_legend},', '{fconfig_legend},recaptcha3_threshold,recaptcha3_action,', $GLOBALS['TL_DCA']['tl_form_field']['palettes']['captcha']);
};
$GLOBALS['TL_DCA']['tl_form_field']['fields'] += ['recaptcha3_threshold' => ['label' => &$GLOBALS['TL_LANG']['tl_form_field']['recaptcha3_threshold'], 'inputType' => 'text', 'sql' => "varchar(8) NOT NULL default ''", 'eval' => ['tl_class' => 'w50 clr']], 'recaptcha3_action' => ['label' => &$GLOBALS['TL_LANG']['tl_form_field']['recaptcha3_action'], 'inputType' => 'text', 'sql' => "varchar(120) NOT NULL default ''", 'eval' => ['tl_class' => 'w50', 'rgxp' => 'recaptcha']]];
}
