-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters