<?php

namespace {
$GLOBALS['TL_DCA']['tl_member'] = array(
    // Config
    'config' => array('dataContainer' => \Contao\DC_Table::class, 'enableVersioning' => \true, 'onsubmit_callback' => array(array('tl_member', 'storeDateAdded')), 'sql' => array('keys' => array('id' => 'primary', 'username' => 'unique', 'email' => 'index', 'login,disable,start,stop' => 'index'))),
    // List
    'list' => array('sorting' => array('mode' => \Contao\DataContainer::MODE_SORTABLE, 'fields' => array('dateAdded'), 'panelLayout' => 'filter;sort,search,limit'), 'label' => array('fields' => array('', 'firstname', 'lastname', 'username', 'dateAdded'), 'showColumns' => \true, 'label_callback' => array('tl_member', 'addIcon')), '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'), 'copy' => array('href' => 'act=copy', 'icon' => 'copy.svg'), 'delete' => array('href' => 'act=delete', 'icon' => 'delete.svg', 'attributes' => 'onclick="if(!confirm(\'' . ($GLOBALS['TL_LANG']['MSC']['deleteConfirm'] ?? \null) . '\'))return false;Backend.getScrollOffset()"'), 'toggle' => array('href' => 'act=toggle&amp;field=disable', 'icon' => 'visible.svg', 'reverse' => \true), 'show' => array('href' => 'act=show', 'icon' => 'show.svg'), 'su' => array('href' => 'key=su', 'icon' => 'su.svg', 'button_callback' => array('tl_member', 'switchUser')))),
    // Palettes
    'palettes' => array('__selector__' => array('login', 'assignDir'), 'default' => '{personal_legend},firstname,lastname,dateOfBirth,gender;{address_legend:hide},company,street,postal,city,state,country;{contact_legend},phone,mobile,fax,email,website,language;{groups_legend},groups;{login_legend},login;{homedir_legend:hide},assignDir;{account_legend},disable,start,stop'),
    // Subpalettes
    'subpalettes' => array('login' => 'username,password', 'assignDir' => 'homeDir'),
    // Fields
    'fields' => array('id' => array('sql' => "int(10) unsigned NOT NULL auto_increment", 'search' => \true), 'tstamp' => array('sql' => "int(10) unsigned NOT NULL default 0"), 'firstname' => array('exclude' => \true, 'search' => \true, 'sorting' => \true, 'flag' => \Contao\DataContainer::SORT_INITIAL_LETTER_ASC, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'maxlength' => 255, 'feEditable' => \true, 'feGroup' => 'personal', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'lastname' => array('exclude' => \true, 'search' => \true, 'sorting' => \true, 'flag' => \Contao\DataContainer::SORT_INITIAL_LETTER_ASC, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'maxlength' => 255, 'feEditable' => \true, 'feGroup' => 'personal', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'dateOfBirth' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'date', 'datepicker' => \true, 'feEditable' => \true, 'feGroup' => 'personal', 'tl_class' => 'w50 wizard'), 'sql' => "varchar(11) NOT NULL default ''"), 'gender' => array('exclude' => \true, 'inputType' => 'select', 'options' => array('male', 'female', 'other'), 'reference' => &$GLOBALS['TL_LANG']['MSC'], 'eval' => array('includeBlankOption' => \true, 'feEditable' => \true, 'feGroup' => 'personal', 'tl_class' => 'w50'), 'sql' => "varchar(32) NOT NULL default ''"), 'company' => array('exclude' => \true, 'search' => \true, 'sorting' => \true, 'flag' => \Contao\DataContainer::SORT_INITIAL_LETTER_ASC, 'inputType' => 'text', 'eval' => array('maxlength' => 255, 'feEditable' => \true, 'feGroup' => 'address', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'street' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 255, 'feEditable' => \true, 'feGroup' => 'address', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'postal' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 32, 'feEditable' => \true, 'feGroup' => 'address', 'tl_class' => 'w50'), 'sql' => "varchar(32) NOT NULL default ''"), 'city' => array('exclude' => \true, 'filter' => \true, 'search' => \true, 'sorting' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 255, 'feEditable' => \true, 'feGroup' => 'address', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'state' => array('exclude' => \true, 'sorting' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 64, 'feEditable' => \true, 'feGroup' => 'address', 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'country' => array('exclude' => \true, 'filter' => \true, 'sorting' => \true, 'inputType' => 'select', 'eval' => array('includeBlankOption' => \true, 'chosen' => \true, 'feEditable' => \true, 'feGroup' => 'address', 'tl_class' => 'w50'), 'options_callback' => static function () {
        $countries = \Contao\System::getContainer()->get('contao.intl.countries')->getCountries();
        // Convert to lower case for backwards compatibility, to be changed in Contao 5.0
        return \array_combine(\array_map('strtolower', \array_keys($countries)), $countries);
    }, 'sql' => "varchar(2) NOT NULL default ''"), 'phone' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 64, 'rgxp' => 'phone', 'decodeEntities' => \true, 'feEditable' => \true, 'feGroup' => 'contact', 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'mobile' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 64, 'rgxp' => 'phone', 'decodeEntities' => \true, 'feEditable' => \true, 'feGroup' => 'contact', 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'fax' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('maxlength' => 64, 'rgxp' => 'phone', 'decodeEntities' => \true, 'feEditable' => \true, 'feGroup' => 'contact', 'tl_class' => 'w50'), 'sql' => "varchar(64) NOT NULL default ''"), 'email' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'maxlength' => 255, 'rgxp' => 'email', 'unique' => \true, 'decodeEntities' => \true, 'feEditable' => \true, 'feGroup' => 'contact', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'website' => array('exclude' => \true, 'search' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => \Contao\CoreBundle\EventListener\Widget\HttpUrlListener::RGXP_NAME, 'maxlength' => 255, 'feEditable' => \true, 'feGroup' => 'contact', 'tl_class' => 'w50'), 'sql' => "varchar(255) NOT NULL default ''"), 'language' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'select', 'eval' => array('includeBlankOption' => \true, 'chosen' => \true, 'feEditable' => \true, 'feGroup' => 'personal', 'tl_class' => 'w50'), 'options_callback' => static function () {
        return \Contao\System::getContainer()->get('contao.intl.locales')->getLocales(\null, \false);
    }, 'sql' => "varchar(64) NOT NULL default ''"), 'groups' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkboxWizard', 'foreignKey' => 'tl_member_group.name', 'eval' => array('multiple' => \true, 'feEditable' => \true, 'feGroup' => 'login'), 'sql' => "blob NULL", 'relation' => array('type' => 'belongsToMany', 'load' => 'lazy')), 'login' => array('exclude' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true), 'sql' => "char(1) NOT NULL default ''"), 'username' => array('exclude' => \true, 'search' => \true, 'sorting' => \true, 'flag' => \Contao\DataContainer::SORT_INITIAL_LETTER_ASC, 'inputType' => 'text', 'eval' => array('mandatory' => \true, 'unique' => \true, 'rgxp' => 'extnd', 'nospace' => \true, 'maxlength' => 64, 'feEditable' => \true, 'feGroup' => 'login', 'tl_class' => 'w50', 'autocapitalize' => 'off', 'autocomplete' => 'username'), 'sql' => 'varchar(64) BINARY NULL'), 'password' => array('label' => &$GLOBALS['TL_LANG']['MSC']['password'], 'exclude' => \true, 'inputType' => 'password', 'eval' => array('mandatory' => \true, 'preserveTags' => \true, 'minlength' => \Contao\Config::get('minPasswordLength'), 'feEditable' => \true, 'feGroup' => 'login', 'tl_class' => 'w50'), 'save_callback' => array(array('tl_member', 'setNewPassword')), 'sql' => "varchar(255) NOT NULL default ''"), 'assignDir' => array('exclude' => \true, 'inputType' => 'checkbox', 'eval' => array('submitOnChange' => \true), 'sql' => "char(1) NOT NULL default ''"), 'homeDir' => array('exclude' => \true, 'inputType' => 'fileTree', 'eval' => array('fieldType' => 'radio', 'tl_class' => 'clr'), 'sql' => "binary(16) NULL"), 'disable' => array('exclude' => \true, 'toggle' => \true, 'filter' => \true, 'inputType' => 'checkbox', 'sql' => "char(1) NOT NULL default ''"), 'start' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'datim', 'datepicker' => \true, 'tl_class' => 'w50 wizard'), 'sql' => "varchar(10) NOT NULL default ''"), 'stop' => array('exclude' => \true, 'inputType' => 'text', 'eval' => array('rgxp' => 'datim', 'datepicker' => \true, 'tl_class' => 'w50 wizard'), 'sql' => "varchar(10) NOT NULL default ''"), 'dateAdded' => array('label' => &$GLOBALS['TL_LANG']['MSC']['dateAdded'], 'default' => \time(), 'sorting' => \true, 'flag' => \Contao\DataContainer::SORT_DAY_DESC, 'eval' => array('rgxp' => 'datim', 'doNotCopy' => \true), 'sql' => "int(10) unsigned NOT NULL default 0"), 'lastLogin' => array('label' => &$GLOBALS['TL_LANG']['MSC']['lastLogin'], 'eval' => array('rgxp' => 'datim', 'doNotCopy' => \true), 'sql' => "int(10) unsigned NOT NULL default 0"), 'currentLogin' => array('label' => &$GLOBALS['TL_LANG']['MSC']['currentLogin'], 'sorting' => \true, 'flag' => \Contao\DataContainer::SORT_DAY_DESC, 'eval' => array('rgxp' => 'datim', 'doNotCopy' => \true), 'sql' => "int(10) unsigned NOT NULL default 0"), 'loginAttempts' => array('eval' => array('doNotCopy' => \true), 'sql' => "smallint(5) unsigned NOT NULL default 0"), 'locked' => array('eval' => array('rgxp' => 'datim', 'doNotCopy' => \true), 'sql' => "int(10) unsigned NOT NULL default 0"), 'session' => array('eval' => array('doNotShow' => \true, 'doNotCopy' => \true), 'sql' => "blob NULL"), 'secret' => array('eval' => array('doNotShow' => \true, 'doNotCopy' => \true), 'sql' => "binary(128) NULL default NULL"), 'useTwoFactor' => array('eval' => array('isBoolean' => \true, 'doNotCopy' => \true, 'tl_class' => 'w50 m12'), 'sql' => "char(1) NOT NULL default ''"), 'backupCodes' => array('eval' => array('doNotCopy' => \true, 'doNotShow' => \true), 'sql' => "text NULL"), 'trustedTokenVersion' => array('eval' => array('doNotCopy' => \true, 'doNotShow' => \true), 'sql' => "int(10) unsigned NOT NULL default 0")),
);
// Filter disabled groups in the front end (see #6757)
if (\defined('TL_MODE') && \TL_MODE == 'FE') {
    $GLOBALS['TL_DCA']['tl_member']['fields']['groups']['options_callback'] = array('tl_member', 'getActiveGroups');
}
/**
 * Provide miscellaneous methods that are used by the data configuration array.
 */
class tl_member extends \Contao\Backend
{
    /**
     * Import the back end user object
     */
    public function __construct()
    {
        parent::__construct();
        $this->import(\Contao\BackendUser::class, 'User');
    }
    /**
     * Filter disabled groups
     *
     * @return array
     */
    public function getActiveGroups()
    {
        $arrGroups = array();
        $objGroup = \Contao\MemberGroupModel::findAllActive();
        if ($objGroup !== \null) {
            while ($objGroup->next()) {
                $arrGroups[$objGroup->id] = $objGroup->name;
            }
        }
        return $arrGroups;
    }
    /**
     * Add an image to each record
     *
     * @param array         $row
     * @param string        $label
     * @param DataContainer $dc
     * @param array         $args
     *
     * @return array
     */
    public function addIcon($row, $label, \Contao\DataContainer $dc, $args)
    {
        $image = 'member';
        $disabled = $row['start'] !== '' && $row['start'] > \time() || $row['stop'] !== '' && $row['stop'] <= \time();
        if ($row['useTwoFactor']) {
            $image .= '_two_factor';
        }
        if ($disabled || $row['disable']) {
            $image .= '_';
        }
        $args[0] = \sprintf('<div class="list_icon_new" style="background-image:url(\'%s\')" data-icon="%s" data-icon-disabled="%s">&nbsp;</div>', \Contao\Image::getPath($image), \Contao\Image::getPath($disabled ? $image : \rtrim($image, '_')), \Contao\Image::getPath(\rtrim($image, '_') . '_'));
        return $args;
    }
    /**
     * Generate a "switch account" button and return it as string
     *
     * @param array  $row
     * @param string $href
     * @param string $label
     * @param string $title
     * @param string $icon
     *
     * @return string
     */
    public function switchUser($row, $href, $label, $title, $icon)
    {
        $blnCanSwitchUser = $this->User->isAdmin || !empty($this->User->amg) && \is_array($this->User->amg);
        if (!$blnCanSwitchUser) {
            return '';
        }
        if (!$row['login'] || !$row['username'] || !$this->User->isAdmin && \count(\array_intersect(\Contao\StringUtil::deserialize($row['groups'], \true), $this->User->amg)) < 1) {
            return \Contao\Image::getHtml(\preg_replace('/\\.svg$/i', '_.svg', $icon)) . ' ';
        }
        $url = \Contao\System::getContainer()->get('router')->generate('contao_backend_preview', array('user' => $row['username']));
        return '<a href="' . \Contao\StringUtil::specialcharsUrl($url) . '" title="' . \Contao\StringUtil::specialchars($title) . '" target="_blank">' . \Contao\Image::getHtml($icon, $label) . '</a> ';
    }
    /**
     * Call the "setNewPassword" callback
     *
     * @param string                    $strPassword
     * @param DataContainer|MemberModel $user
     *
     * @return string
     */
    public function setNewPassword($strPassword, $user)
    {
        // Return if there is no user (e.g. upon registration)
        if (!$user) {
            return $strPassword;
        }
        $objUser = $this->Database->prepare("SELECT * FROM tl_member WHERE id=?")->limit(1)->execute($user->id);
        // HOOK: set new password callback
        if ($objUser->numRows && isset($GLOBALS['TL_HOOKS']['setNewPassword']) && \is_array($GLOBALS['TL_HOOKS']['setNewPassword'])) {
            foreach ($GLOBALS['TL_HOOKS']['setNewPassword'] as $callback) {
                $this->import($callback[0]);
                $this->{$callback[0]}->{$callback[1]}($objUser, $strPassword);
            }
        }
        return $strPassword;
    }
    /**
     * Store the date when the account has been added
     *
     * @param DataContainer|FrontendUser $dc
     */
    public function storeDateAdded($dc)
    {
        // Front end call
        if (!$dc instanceof \Contao\DataContainer) {
            return;
        }
        // Return if there is no active record (override all)
        if (!$dc->activeRecord || $dc->activeRecord->dateAdded > 0) {
            return;
        }
        // Fallback solution for existing accounts
        if ($dc->activeRecord->lastLogin > 0) {
            $time = $dc->activeRecord->lastLogin;
        } else {
            $time = \time();
        }
        $this->Database->prepare("UPDATE tl_member SET dateAdded=? WHERE id=?")->execute($time, $dc->id);
    }
}
}

namespace {
if (@\class_exists("tl_member")) {
    \Contao\CoreBundle\DataContainer\PaletteManipulator::create()->addLegend('c4g_maps_legend', 'homedir_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_BEFORE)->addField(array('c4g_loc_geox', 'c4g_loc_geoy', 'c4g_loc_label', 'c4g_locstyle'), 'c4g_maps_legend', \Contao\CoreBundle\DataContainer\PaletteManipulator::POSITION_APPEND)->applyToPalette('default', 'tl_member');
    $GLOBALS['TL_DCA']['tl_member']['fields']['c4g_loc_geox'] = ['exclude' => \true, 'inputType' => 'text', 'eval' => ['mandatory' => \false, 'maxlength' => 20, 'tl_class' => 'w50 wizard', 'feEditable' => \true], 'save_callback' => [['tl_member_c4g_maps', 'setLocLon']], 'wizard' => [['\\con4gis\\MapsBundle\\Classes\\GeoPicker', 'getPickerLink']], 'sql' => "varchar(20) NOT NULL default ''"];
    $GLOBALS['TL_DCA']['tl_member']['fields']['c4g_loc_geoy'] = ['exclude' => \true, 'inputType' => 'text', 'eval' => ['mandatory' => \false, 'maxlength' => 20, 'tl_class' => 'w50 wizard', 'feEditable' => \true], 'save_callback' => [['tl_member_c4g_maps', 'setLocLat']], 'wizard' => [['\\con4gis\\MapsBundle\\Classes\\GeoPicker', 'getPickerLink']], 'sql' => "varchar(20) NOT NULL default ''"];
    $GLOBALS['TL_DCA']['tl_member']['fields']['c4g_loc_label'] = ['exclude' => \true, 'inputType' => 'text', 'eval' => ['tl_class' => 'clr'], 'sql' => "varchar(100) NOT NULL default ''"];
    $GLOBALS['TL_DCA']['tl_member']['fields']['c4g_locstyle'] = ['exclude' => \true, 'inputType' => 'select', 'options_callback' => ['tl_member_c4g_maps', 'getLocStyles'], 'eval' => ['chosen' => \true, 'includeBlankOption' => \true], 'sql' => "int(10) unsigned NOT NULL default '0'", 'xlabel' => [['tl_member_c4g_maps', 'locstylesLink']]];
}
/**
 * Class tl_member_c4g_maps
 */
class tl_member_c4g_maps extends \Contao\Backend
{
    /**
     * @param DC_Table $dc
     * @return array
     */
    public function getLocStyles(\Contao\DC_Table $dc)
    {
        $locStyles = $this->Database->prepare("SELECT id,name FROM tl_c4g_map_locstyles ORDER BY name")->execute();
        $return[''] = '-';
        while ($locStyles->next()) {
            $return[$locStyles->id] = $locStyles->name;
        }
        return $return;
    }
    /**
     * Validate Longitude
     */
    public function setLocLon($varValue)
    {
        if ($varValue != 0) {
            if (!\con4gis\MapsBundle\Classes\Utils::validateLon($varValue)) {
                throw new \Exception($GLOBALS['TL_LANG']['c4g_maps']['geox_invalid']);
            }
        }
        return $varValue;
    }
    /**
     * Validate Latitude
     */
    public function setLocLat($varValue)
    {
        if ($varValue != 0) {
            if (!\con4gis\MapsBundle\Classes\Utils::validateLat($varValue)) {
                throw new \Exception($GLOBALS['TL_LANG']['c4g_maps']['geoy_invalid']);
            }
        }
        return $varValue;
    }
    public function locstylesLink(\Contao\DataContainer $dc)
    {
        $requestToken = \Contao\System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue();
        return ' <a href="contao/main.php?do=c4g_map_locstyles&amp;table=tl_c4g_map_locstyles&amp;id=' . $dc->activeRecord->pid . '&amp;popup=1&amp;nb=1&amp;rt=' . $requestToken . '" title="' . \Contao\StringUtil::specialchars($GLOBALS['TL_LANG']['tl_member']['editLocstyles'][0]) . '" onclick="Backend.openModalIframe({\'title\':\'' . \Contao\StringUtil::specialchars(\str_replace("'", "\\'", $GLOBALS['TL_LANG']['tl_member']['editLocstyles'][0])) . '\',\'url\':this.href});return false">' . \Contao\Image::getHtml('edit.svg') . '</a>';
    }
}
}
