1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,44 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Oveleon\ContaoMemberExtensionBundle\EventListener\DataContainer; |
|
4 |
+ |
|
5 |
+use Contao\CoreBundle\DependencyInjection\Attribute\AsCallback; |
|
6 |
+use Contao\Database; |
|
7 |
+use Contao\DataContainer; |
|
8 |
+use Contao\MemberModel; |
|
9 |
+use Contao\System; |
|
10 |
+use Exception; |
|
11 |
+ |
|
12 |
+class MemberFieldsListener |
|
13 |
+{ |
|
14 |
+ /** |
|
15 |
+ * @throws Exception |
|
16 |
+ */ |
|
17 |
+ #[AsCallback(table: 'tl_member', target: 'fields.alias.save')] |
|
18 |
+ public function generateAlias($varValue, DataContainer $dc): string |
|
19 |
+ { |
|
20 |
+ $aliasExists = static function (string $alias) use ($dc): bool { |
|
21 |
+ $result = Database::getInstance() |
|
22 |
+ ->prepare("SELECT id FROM tl_member WHERE alias=? AND id!=?") |
|
23 |
+ ->execute($alias, $dc->id); |
|
24 |
+ |
|
25 |
+ return $result->numRows > 0; |
|
26 |
+ }; |
|
27 |
+ |
|
28 |
+ if (!$varValue) |
|
29 |
+ { |
|
30 |
+ // ToDo - use slug generator for aliases |
|
31 |
+ $varValue = str_replace(' ','-', $dc->activeRecord->firstname) . '_' . str_replace(' ','-', $dc->activeRecord->lastname) . ($aliasExists ? '_' . $dc->activeRecord->id : ''); |
|
32 |
+ } |
|
33 |
+ if (preg_match('/^[1-9]\d*$/', $varValue)) |
|
34 |
+ { |
|
35 |
+ throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasNumeric'], $varValue)); |
|
36 |
+ } |
|
37 |
+ elseif ($aliasExists($varValue)) |
|
38 |
+ { |
|
39 |
+ throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['aliasExists'], $varValue)); |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ return $varValue; |
|
43 |
+ } |
|
44 |
+} |