1010use Codefog \TagsBundle \ManagerRegistry ;
1111use Codefog \TagsBundle \Model \TagModel ;
1212use Contao \Controller ;
13+ use Contao \CoreBundle \DependencyInjection \Attribute \AsCallback ;
14+ use Contao \CoreBundle \Slug \Slug ;
1315use Contao \Database ;
1416use Contao \DataContainer ;
17+ use Contao \DC_Table ;
1518use Contao \StringUtil ;
1619use Contao \System ;
1720use Contao \Versions ;
1821use Doctrine \DBAL \Connection ;
1922use Symfony \Component \HttpFoundation \RequestStack ;
2023use Symfony \Component \HttpFoundation \Session \Attribute \AttributeBagInterface ;
2124
22- class TagListener
25+ readonly class TagListener
2326{
24- /**
25- * TagListener constructor.
26- */
2727 public function __construct (
28- private readonly Connection $ db ,
29- private readonly ManagerRegistry $ registry ,
30- private readonly RequestStack $ requestStack ,
28+ private Connection $ connection ,
29+ private ManagerRegistry $ registry ,
30+ private RequestStack $ requestStack ,
31+ private Slug $ slug ,
3132 ) {
3233 }
3334
34- /**
35- * On load callback.
36- */
35+ #[AsCallback('tl_cfg_tag ' , 'config.onload ' )]
3736 public function onLoadCallback (DataContainer $ dc ): void
3837 {
3938 if (!($ dc instanceof Driver)) {
@@ -57,7 +56,7 @@ public function onLoadCallback(DataContainer $dc): void
5756 }
5857
5958 // Append all other tags
60- foreach ($ this ->db -> query ("SELECT id FROM {$ dc ->table }" )-> fetchFirstColumn ( ) as $ id ) {
59+ foreach ($ this ->connection -> fetchFirstColumn ("SELECT id FROM {$ dc ->table }" ) as $ id ) {
6160 if (!\array_key_exists ($ id , $ ids )) {
6261 $ ids [$ id ] = 0 ;
6362 }
@@ -90,9 +89,7 @@ public function onLoadCallback(DataContainer $dc): void
9089 $ dc ->setFirstOrderBy ('name ' );
9190 }
9291
93- /**
94- * On generate panel callback.
95- */
92+ #[AsCallback('tl_cfg_tag ' , 'list.sorting.panel_callback.cfg_sort ' )]
9693 public function onPanelCallback (DataContainer $ dc ): string
9794 {
9895 /** @var AttributeBagInterface $bag */
@@ -137,12 +134,8 @@ public function onPanelCallback(DataContainer $dc): string
137134</div> ' ;
138135 }
139136
140- /**
141- * On label callback.
142- *
143- * @param string $label
144- */
145- public function onLabelCallback (array $ row , $ label , DataContainer $ dc , array $ args ): array
137+ #[AsCallback('tl_cfg_tag ' , 'list.label.label ' )]
138+ public function onLabelCallback (array $ row , string $ label , DataContainer $ dc , array $ args ): array
146139 {
147140 if ($ row ['source ' ]) {
148141 $ manager = $ this ->registry ->get ($ row ['source ' ]);
@@ -155,12 +148,8 @@ public function onLabelCallback(array $row, $label, DataContainer $dc, array $ar
155148 return $ args ;
156149 }
157150
158- /**
159- * On buttons callback.
160- *
161- * @return array
162- */
163- public function onButtonsCallback (array $ buttons , DataContainer $ dc )
151+ #[AsCallback('tl_cfg_tag ' , 'select.buttons ' )]
152+ public function onButtonsCallback (array $ buttons , DataContainer $ dc ): array
164153 {
165154 $ request = $ this ->requestStack ->getCurrentRequest ();
166155
@@ -196,7 +185,7 @@ public function onButtonsCallback(array $buttons, DataContainer $dc)
196185 $ versions ->initialize ();
197186
198187 // Store the new alias
199- $ this ->db ->update ($ dc ->table , ['alias ' => $ alias ], ['id ' => $ tagModel ->id ]);
188+ $ this ->connection ->update ($ dc ->table , ['alias ' => $ alias ], ['id ' => $ tagModel ->id ]);
200189
201190 // Create a new version
202191 $ versions ->create ();
@@ -212,9 +201,7 @@ public function onButtonsCallback(array $buttons, DataContainer $dc)
212201 return $ buttons ;
213202 }
214203
215- /**
216- * On source options callback.
217- */
204+ #[AsCallback('tl_cfg_tag ' , 'fields.source.options ' )]
218205 public function onSourceOptionsCallback (): array
219206 {
220207 $ options = [];
@@ -228,31 +215,27 @@ public function onSourceOptionsCallback(): array
228215 return $ options ;
229216 }
230217
231- /**
232- * On alias save callback.
233- *
234- * @throws \RuntimeException
235- */
218+ #[AsCallback('tl_cfg_tag ' , 'fields.alias.save ' )]
236219 public function onAliasSaveCallback (string $ value , DataContainer $ dc ): string
237220 {
238- $ autoAlias = false ;
239-
240- // Generate alias if there is none
241- if (!$ value ) {
242- $ autoAlias = true ;
243- $ value = StringUtil::generateAlias ($ dc ->activeRecord ->name );
221+ if ($ dc instanceof DC_Table) {
222+ $ activeRecord = $ dc ->getActiveRecord ();
223+ } else {
224+ $ activeRecord = $ dc ->getCurrentRecord ();
244225 }
245226
246- $ existingAliases = $ this ->db ->fetchOne ("SELECT COUNT(*) FROM {$ dc ->table } WHERE alias=? AND source=? " , [$ value , $ dc ->activeRecord ->source ]);
247-
248- // Check whether the record alias exists
249- if ($ existingAliases > 1 && !$ autoAlias ) {
250- throw new \RuntimeException (\sprintf ($ GLOBALS ['TL_LANG ' ]['ERR ' ]['aliasExists ' ], $ value ));
227+ if (null === $ activeRecord ) {
228+ return $ value ;
251229 }
252230
253- // Add ID to alias
254- if ($ existingAliases > 0 && $ autoAlias ) {
255- $ value .= '- ' .$ dc ->id ;
231+ $ aliasExists = fn (string $ alias ) => false !== $ this ->connection ->fetchOne ("SELECT id FROM {$ dc ->table } WHERE id!=? AND alias=? AND source=? " , [$ dc ->id , $ value , $ activeRecord ['source ' ]]);
232+
233+ if (!$ value ) {
234+ $ value = $ this ->slug ->generate ($ activeRecord ['headline ' ], duplicateCheck: $ aliasExists );
235+ } elseif (preg_match ('/^[1-9]\d*$/ ' , $ value )) {
236+ throw new \Exception (\sprintf ($ GLOBALS ['TL_LANG ' ]['ERR ' ]['aliasNumeric ' ], $ value ));
237+ } elseif ($ aliasExists ($ value )) {
238+ throw new \Exception (\sprintf ($ GLOBALS ['TL_LANG ' ]['ERR ' ]['aliasExists ' ], $ value ));
256239 }
257240
258241 return $ value ;
0 commit comments