Skip to content

Commit

Permalink
Alias generation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoglo committed Apr 11, 2024
1 parent 0c5ffe6 commit ce66f3a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
10 changes: 9 additions & 1 deletion contao/dca/tl_member.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

// Extend the default palette
PaletteManipulator::create()
->addField(['avatar'], 'personal_legend', PaletteManipulator::POSITION_APPEND)
->addField('avatar', 'personal_legend', PaletteManipulator::POSITION_APPEND)
->addField('alias', 'avatar')
->applyToPalette('default', 'tl_member')
;

Expand All @@ -45,3 +46,10 @@
],
'sql' => "binary(16) NULL"
];

$GLOBALS['TL_DCA']['tl_member']['fields']['alias'] = [
'search' => true,
'inputType' => 'text',
'eval' => ['rgxp'=>'alias', 'doNotCopy'=>true, 'unique'=>true, 'maxlength'=>255, 'tl_class'=>'w50'],
'sql' => "varchar(255) BINARY NOT NULL default ''"
];
10 changes: 9 additions & 1 deletion contao/languages/de/tl_member.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<source>Here you can choose a profile picture for the member.</source>
<target>Hier können Sie ein Profilbild für das Mitglied auswählen.</target>
</trans-unit>
<trans-unit id="tl_member.alias.0">
<source>Member alias</source>
<target>Mitgliedsalias</target>
</trans-unit>
<trans-unit id="tl_member.alias.1">
<source>The member alias is a unique reference to the news item which can be called instead of its numeric ID.</source>
<target>Der Mitgliedsalias ist eine eindeutige Referenz, die anstelle der numerischen Mitglieds-ID aufgerufen werden kann.</target>
</trans-unit>
<trans-unit id="tl_member.settings.0">
<source>Settings</source>
<target>Einstellungen</target>
Expand All @@ -19,4 +27,4 @@
</trans-unit>
</body>
</file>
</xliff>
</xliff>
8 changes: 7 additions & 1 deletion contao/languages/en/tl_member.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<trans-unit id="tl_member.avatar.1">
<source>Here you can choose an avatar for the member.</source>
</trans-unit>
<trans-unit id="tl_member.alias.0">
<source>Member alias</source>
</trans-unit>
<trans-unit id="tl_member.alias.1">
<source>The member alias is a unique reference to the news item which can be called instead of its numeric ID.</source>
</trans-unit>
<trans-unit id="tl_member.settings.0">
<source>Settings</source>
</trans-unit>
Expand All @@ -15,4 +21,4 @@
</trans-unit>
</body>
</file>
</xliff>
</xliff>
43 changes: 43 additions & 0 deletions src/EventListener/DataContainer/MemberFieldsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Oveleon\ContaoMemberExtensionBundle\EventListener\DataContainer;

use Contao\CoreBundle\DependencyInjection\Attribute\AsCallback;
use Contao\Database;
use Contao\DataContainer;
use Contao\MemberModel;
use Contao\System;
use Exception;

class MemberFieldsListener
{
/**
* @throws Exception
*/
#[AsCallback(table: 'tl_member', target: 'fields.alias.save')]
public function generateAlias($varValue, DataContainer $dc): string
{
$aliasExists = static function (string $alias) use ($dc): bool {
$result = Database::getInstance()
->prepare("SELECT id FROM tl_member WHERE alias=? AND id!=?")
->execute($alias, $dc->id);

return $result->numRows > 0;
};

if (!$varValue)
{
$varValue = $dc->activeRecord->firstname . '_' . $dc->activeRecord->lastname . ($aliasExists ? '_' . $dc->activeRecord->id : '');
}
if (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;
}
}
1 change: 0 additions & 1 deletion src/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public static function processAvatar(?MemberModel $objMember, ?array $arrData):
return;
}

// ToDo: remove $_SESSION when contao 4.13 support ends (Contao ^5.* is not possible with Contao 4.* support)
$maxlength_kb = FileUpload::getMaxUploadSize();
//$maxlength_kb_readable = System::getReadableSize($maxlength_kb);

Expand Down

0 comments on commit ce66f3a

Please sign in to comment.