103 lines
3.5 KiB
PHP
103 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Contao\CoreBundle\DataContainer\PaletteManipulator;
|
|
use Contao\DataContainer;
|
|
use Contao\Database;
|
|
use Contao\Input;
|
|
use Contao\StringUtil;
|
|
|
|
if (isset($GLOBALS['TL_DCA']['tl_member']['palettes']) && is_array($GLOBALS['TL_DCA']['tl_member']['palettes'])) {
|
|
foreach (array_keys($GLOBALS['TL_DCA']['tl_member']['palettes']) as $paletteName) {
|
|
if ($paletteName === '__selector__') {
|
|
continue;
|
|
}
|
|
|
|
PaletteManipulator::create()
|
|
->addLegend('organization_legend', 'contact_legend', PaletteManipulator::POSITION_AFTER)
|
|
->addField('organizations', 'organization_legend', PaletteManipulator::POSITION_APPEND)
|
|
->applyToPalette((string) $paletteName, 'tl_member');
|
|
}
|
|
}
|
|
|
|
$GLOBALS['TL_DCA']['tl_member']['config']['onbeforesubmit_callback'][] = static function (array $values): array {
|
|
unset($values['organizations']);
|
|
|
|
return $values;
|
|
};
|
|
|
|
$GLOBALS['TL_DCA']['tl_member']['config']['onsubmit_callback'][] = static function (DataContainer $dc): void {
|
|
if (!$dc->id) {
|
|
return;
|
|
}
|
|
|
|
$postedValues = Input::post('organizations');
|
|
|
|
if (null === $postedValues) {
|
|
return;
|
|
}
|
|
|
|
$organizationIds = array_values(array_unique(array_map('intval', is_array($postedValues) ? $postedValues : [$postedValues])));
|
|
$memberId = (int) $dc->id;
|
|
$db = Database::getInstance();
|
|
$time = time();
|
|
|
|
$db->prepare('DELETE FROM tl_member_organization WHERE member_id=?')
|
|
->execute($memberId);
|
|
|
|
foreach ($organizationIds as $organizationId) {
|
|
$db->prepare('INSERT INTO tl_member_organization (tstamp, member_id, organization_id) VALUES (?, ?, ?)')
|
|
->execute($time, $memberId, $organizationId);
|
|
}
|
|
};
|
|
|
|
$GLOBALS['TL_DCA']['tl_member']['fields']['organizations'] = [
|
|
'label' => &$GLOBALS['TL_LANG']['tl_member']['organizations'],
|
|
'exclude' => true,
|
|
'inputType' => 'select',
|
|
'foreignKey' => 'tl_organization.title',
|
|
'eval' => ['multiple' => true, 'chosen' => true, 'tl_class' => 'clr'],
|
|
'relation' => [
|
|
'type' => 'hasMany',
|
|
'load' => 'lazy',
|
|
'table' => 'tl_organization',
|
|
'field' => 'id',
|
|
],
|
|
'load_callback' => [
|
|
static function ($value, DataContainer $dc): array {
|
|
if (!$dc->id) {
|
|
return [];
|
|
}
|
|
|
|
$result = Database::getInstance()
|
|
->prepare('SELECT organization_id FROM tl_member_organization WHERE member_id=? ORDER BY organization_id')
|
|
->execute($dc->id);
|
|
|
|
return array_map(static fn (array $row): int => (int) $row['organization_id'], $result->fetchAllAssoc());
|
|
},
|
|
],
|
|
'save_callback' => [
|
|
static function ($value, DataContainer $dc): array {
|
|
if (!$dc->id) {
|
|
return [];
|
|
}
|
|
|
|
$memberId = (int) $dc->id;
|
|
$organizationIds = array_values(array_unique(array_map('intval', StringUtil::deserialize($value, true))));
|
|
$db = Database::getInstance();
|
|
$time = time();
|
|
|
|
$db->prepare('DELETE FROM tl_member_organization WHERE member_id=?')
|
|
->execute($memberId);
|
|
|
|
foreach ($organizationIds as $organizationId) {
|
|
$db->prepare('INSERT INTO tl_member_organization (tstamp, member_id, organization_id) VALUES (?, ?, ?)')
|
|
->execute($time, $memberId, $organizationId);
|
|
}
|
|
|
|
return $organizationIds;
|
|
},
|
|
],
|
|
];
|