<?php

namespace {
/*
 * This file is part of Contao.
 *
 * (c) Leo Feyer
 *
 * @license LGPL-3.0-or-later
 */
$GLOBALS['TL_DCA']['tl_form'] = array(
    // Config
    'config' => array('dataContainer' => \Contao\DC_Table::class, 'switchToEdit' => \true, 'enableVersioning' => \true, 'ctable' => array('tl_form_field'), 'markAsCopy' => 'title', 'onload_callback' => array(array('tl_form', 'checkPermission')), 'oncreate_callback' => array(array('tl_form', 'adjustPermissions')), 'oncopy_callback' => array(array('tl_form', 'adjustPermissions')), 'sql' => array('keys' => array('id' => 'primary', 'alias' => 'index'))),
    // List
    'list' => array('sorting' => array('mode' => 1, 'fields' => array('title'), 'flag' => 1, 'panelLayout' => 'filter;search,limit'), 'label' => array('fields' => array('title', 'formID'), 'format' => '%s <span style="color:#999;padding-left:3px">[%s]</span>'), 'global_operations' => array('all' => array('href' => 'act=select', 'class' => 'header_edit_all', 'attributes' => 'onclick="Backend.getScrollOffset()" accesskey="e"')), 'operations' => array('edit' => array('href' => 'table=tl_form_field', 'icon' => 'edit.svg'), 'editheader' => array('href' => 'act=edit', 'icon' => 'header.svg', 'button_callback' => array('tl_form', 'editHeader')), 'copy' => array('href' => 'act=copy', 'icon' => 'copy.svg', 'button_callback' => array('tl_form', 'copyForm')), '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', 'deleteForm')), 'show' => array('href' => 'act=show', 'icon' => 'show.svg'))),
    // Palettes
    'palettes' => array('__selector__' => array('sendViaEmail', 'storeValues'), 'default' => '{title_legend},title,alias,jumpTo;{config_legend},allowTags;{email_legend},sendViaEmail;{store_legend:hide},storeValues;{template_legend:hide},customTpl;{expert_legend:hide},method,novalidate,attributes,formID'),
    // Subpalettes
    'subpalettes' => array('sendViaEmail' => 'recipient,subject,format,skipEmpty', 'storeValues' => 'targetTable'),
    // Fields
    'fields' => array('id' => array('sql' => "int(10) unsigned NOT NULL auto_increment"), 'tstamp' => array('sql' => "int(10) unsigned NOT NULL default 0"), 'title' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'maxlength' => 255, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'alias' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'alias', 'doNotCopy' => \true, 'maxlength' => 255, 'tl_class' => 'w50'), 'save_callback' => array(array('tl_form', 'generateAlias')), 'sql' => "varchar(255) BINARY NOT NULL default ''"), 'jumpTo' => array('exclude' => \true, 'inputType' => 'pageTree', 'foreignKey' => 'tl_page.title', 'eval' => array('fieldType' => 'radio', 'tl_class' => 'clr'), 'sql' => "int(10) unsigned NOT NULL default 0", 'relation' => array('type' => 'hasOne', 'load' => 'lazy')), 'sendViaEmail' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true), 'sql' => "char(1) NOT NULL default ''"), 'recipient' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'maxlength' => 1022, 'rgxp' => 'emails', 'tl_class' => 'w50'), 'sql' => "varchar(1022) NOT NULL default ''"), 'subject' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'maxlength' => 255, 'decodeEntities' => \true, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'format' => array('exclude' => \true, 'inputType' => 'select', 'options' => array('raw', 'xml', 'csv', 'email'), 'reference' => &$GLOBALS['TL_LANG']['tl_form'], 'eval' => array('helpwizard' => \true, 'tl_class' => 'w50'), 'sql' => "varchar(12) NOT NULL default 'raw'"), 'skipEmpty' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50 m12'), 'sql' => "char(1) NOT NULL default ''"), 'storeValues' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true), 'sql' => "char(1) NOT NULL default ''"), 'targetTable' => array('exclude' => \true, 'search' => \true, 'inputType' => 'select', 'options_callback' => array('tl_form', 'getAllTables'), 'eval' => array('chosen' => \true, 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'customTpl' => array('exclude' => \true, 'inputType' => 'select', 'options_callback' => static function () {
        return \Contao\Controller::getTemplateGroup('form_wrapper_', array(), 'form_wrapper');
    }, 'eval' => array('chosen' => \true, 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'method' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'select', 'options' => array('POST', 'GET'), 'eval' => array('tl_class' => 'w50'), 'sql' => "varchar(12) NOT NULL default 'POST'"), 'novalidate' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50 m12'), 'sql' => "char(1) NOT NULL default ''"), 'attributes' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('multiple' => \true, 'size' => 2, 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'formID' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('nospace' => \true, 'doNotCopy' => \true, 'maxlength' => 64, 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'allowTags' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('tl_class' => 'w50'), '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 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
     *
     * @throws Contao\CoreBundle\Exception\AccessDeniedException
     */
    public function checkPermission()
    {
        if ($this->User->isAdmin) {
            return;
        }
        // Set root IDs
        if (empty($this->User->forms) || !\is_array($this->User->forms)) {
            $root = array(0);
        } else {
            $root = $this->User->forms;
        }
        $GLOBALS['TL_DCA']['tl_form']['list']['sorting']['root'] = $root;
        // Check permissions to add forms
        if (!$this->User->hasAccess('create', 'formp')) {
            $GLOBALS['TL_DCA']['tl_form']['config']['closed'] = \true;
            $GLOBALS['TL_DCA']['tl_form']['config']['notCreatable'] = \true;
            $GLOBALS['TL_DCA']['tl_form']['config']['notCopyable'] = \true;
        }
        // Check permissions to delete forms
        if (!$this->User->hasAccess('delete', 'formp')) {
            $GLOBALS['TL_DCA']['tl_form']['config']['notDeletable'] = \true;
        }
        /** @var Symfony\Component\HttpFoundation\Session\SessionInterface $objSession */
        $objSession = \Contao\System::getContainer()->get('session');
        // Check current action
        switch (\Contao\Input::get('act')) {
            case 'select':
                // Allow
                break;
            case 'create':
                if (!$this->User->hasAccess('create', 'formp')) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to create forms.');
                }
                break;
            case 'edit':
            case 'copy':
            case 'delete':
            case 'show':
                if (!\in_array(\Contao\Input::get('id'), $root) || \Contao\Input::get('act') == 'delete' && !$this->User->hasAccess('delete', 'formp')) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to ' . \Contao\Input::get('act') . ' form ID ' . \Contao\Input::get('id') . '.');
                }
                break;
            case 'editAll':
            case 'deleteAll':
            case 'overrideAll':
            case 'copyAll':
                $session = $objSession->all();
                if (\Contao\Input::get('act') == 'deleteAll' && !$this->User->hasAccess('delete', 'formp')) {
                    $session['CURRENT']['IDS'] = array();
                } else {
                    $session['CURRENT']['IDS'] = \array_intersect((array) $session['CURRENT']['IDS'], $root);
                }
                $objSession->replace($session);
                break;
            default:
                if (\Contao\Input::get('act')) {
                    throw new \Contao\CoreBundle\Exception\AccessDeniedException('Not enough permissions to ' . \Contao\Input::get('act') . ' forms.');
                }
                break;
        }
    }
    /**
     * Add the new form to the permissions
     *
     * @param $insertId
     */
    public function adjustPermissions($insertId)
    {
        // The oncreate_callback passes $insertId as second argument
        if (\func_num_args() == 4) {
            $insertId = \func_get_arg(1);
        }
        if ($this->User->isAdmin) {
            return;
        }
        // Set root IDs
        if (empty($this->User->forms) || !\is_array($this->User->forms)) {
            $root = array(0);
        } else {
            $root = $this->User->forms;
        }
        // The form is enabled already
        if (\in_array($insertId, $root)) {
            return;
        }
        /** @var Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface $objSessionBag */
        $objSessionBag = \Contao\System::getContainer()->get('session')->getBag('contao_backend');
        $arrNew = $objSessionBag->get('new_records');
        if (\is_array($arrNew['tl_form']) && \in_array($insertId, $arrNew['tl_form'])) {
            // Add the permissions on group level
            if ($this->User->inherit != 'custom') {
                $objGroup = $this->Database->execute("SELECT id, forms, formp FROM tl_user_group WHERE id IN(" . \implode(',', \array_map('\\intval', $this->User->groups)) . ")");
                while ($objGroup->next()) {
                    $arrFormp = \Contao\StringUtil::deserialize($objGroup->formp);
                    if (\is_array($arrFormp) && \in_array('create', $arrFormp)) {
                        $arrForms = \Contao\StringUtil::deserialize($objGroup->forms, \true);
                        $arrForms[] = $insertId;
                        $this->Database->prepare("UPDATE tl_user_group SET forms=? WHERE id=?")->execute(\serialize($arrForms), $objGroup->id);
                    }
                }
            }
            // Add the permissions on user level
            if ($this->User->inherit != 'group') {
                $objUser = $this->Database->prepare("SELECT forms, formp FROM tl_user WHERE id=?")->limit(1)->execute($this->User->id);
                $arrFormp = \Contao\StringUtil::deserialize($objUser->formp);
                if (\is_array($arrFormp) && \in_array('create', $arrFormp)) {
                    $arrForms = \Contao\StringUtil::deserialize($objUser->forms, \true);
                    $arrForms[] = $insertId;
                    $this->Database->prepare("UPDATE tl_user SET forms=? WHERE id=?")->execute(\serialize($arrForms), $this->User->id);
                }
            }
            // Add the new element to the user object
            $root[] = $insertId;
            $this->User->forms = $root;
        }
    }
    /**
     * Auto-generate a form alias if it has not been set yet
     *
     * @param mixed                $varValue
     * @param Contao\DataContainer $dc
     *
     * @return mixed
     *
     * @throws Exception
     */
    public function generateAlias($varValue, \Contao\DataContainer $dc)
    {
        $aliasExists = function (string $alias) use($dc) : bool {
            return $this->Database->prepare("SELECT id FROM tl_form WHERE alias=? AND id!=?")->execute($alias, $dc->id)->numRows > 0;
        };
        // Generate an alias if there is none
        if (!$varValue) {
            $varValue = \Contao\System::getContainer()->get('contao.slug')->generate($dc->activeRecord->title, \Contao\Input::post('jumpTo') ?: $dc->activeRecord->jumpTo, $aliasExists);
        } elseif (\preg_match('/^[1-9]\\d*$/', $varValue)) {
            throw new \Exception(\sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $varValue));
        } elseif ($aliasExists($varValue)) {
            throw new \Exception(\sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue));
        }
        return $varValue;
    }
    /**
     * Get all tables and return them as array
     *
     * @return array
     */
    public function getAllTables()
    {
        // Return allowlisted tables if defined
        if (!empty($GLOBALS['TL_DCA']['tl_form']['fields']['targetTable']['options'])) {
            return $GLOBALS['TL_DCA']['tl_form']['fields']['targetTable']['options'];
        }
        $GLOBALS['TL_DCA']['tl_form']['fields']['targetTable']['label'][1] = '<span style="color: #c33;">' . \sprintf($GLOBALS['TL_LANG']['tl_form']['targetTableMissingAllowlist'], "\$GLOBALS['TL_DCA']['tl_form']['fields']['targetTable']['options']") . '</span>';
        if (!$this->User->isAdmin) {
            return array();
        }
        $arrTables = $this->Database->listTables();
        $arrViews = \Contao\System::getContainer()->get('database_connection')->getSchemaManager()->listViews();
        if (!empty($arrViews)) {
            $arrTables = \array_merge($arrTables, \array_keys($arrViews));
            \natsort($arrTables);
        }
        return \array_values($arrTables);
    }
    /**
     * Return the edit header button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function editHeader($row, $href, $label, $title, $icon, $attributes)
    {
        return $this->User->canEditFieldsOf('tl_form') ? '<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 copy form button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function copyForm($row, $href, $label, $title, $icon, $attributes)
    {
        return $this->User->hasAccess('create', 'formp') ? '<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 delete form button
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     * @param string $attributes
     *
     * @return string
     */
    public function deleteForm($row, $href, $label, $title, $icon, $attributes)
    {
        return $this->User->hasAccess('delete', 'formp') ? '<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)) . ' ';
    }
}
}

namespace {
$GLOBALS['TL_DCA']['tl_form']['config']['onload_callback'][] = ['netzmacht.contao_form_designer.listener.dca.form', 'preparePalette'];
$GLOBALS['TL_DCA']['tl_form']['fields']['formLayout'] = ['label' => &$GLOBALS['TL_LANG']['tl_form']['formLayout'], 'inputType' => 'select', 'eval' => ['tl_class' => 'w50', 'includeBlankOption' => \true, 'chosen' => \true], 'options_callback' => ['netzmacht.contao_form_designer.listener.dca.form', 'getFormLayoutOptions'], 'foreignKey' => 'tl_form_layout.title', 'relation' => ['type' => 'belongsTo', 'load' => 'lazy'], 'sql' => "int(10) unsigned NOT NULL default '0'"];
}

namespace {
/**
 * notification_center extension for Contao Open Source CMS
 *
 * @copyright  Copyright (c) 2008-2015, terminal42
 * @author     terminal42 gmbh <info@terminal42.ch>
 * @license    LGPL
 */
/**
 * Palettes
 */
$GLOBALS['TL_DCA']['tl_form']['palettes']['default'] = \str_replace('sendViaEmail;', 'nc_notification,sendViaEmail;', $GLOBALS['TL_DCA']['tl_form']['palettes']['default']);
/**
 * Fields
 */
$GLOBALS['TL_DCA']['tl_form']['fields']['nc_notification'] = array('label' => &$GLOBALS['TL_LANG']['tl_form']['nc_notification'], 'exclude' => \true, 'inputType' => 'select', 'options_callback' => array('NotificationCenter\\tl_form', 'getNotificationChoices'), 'eval' => array('includeBlankOption' => \true, 'chosen' => \true, 'tl_class' => 'clr w50'), 'sql' => "int(10) unsigned NOT NULL default '0'");
$GLOBALS['TL_DCA']['tl_form']['fields']['sendViaEmail']['eval']['tl_class'] = 'clr w50';
}
