Skip to content

Commit d6ab309

Browse files
committed
Update the code to newest standards
1 parent f6ade03 commit d6ab309

22 files changed

Lines changed: 130 additions & 392 deletions

config/listener.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ services:
2020
- "@database_connection"
2121
- "@codefog_tags.manager_registry"
2222
- "@request_stack"
23+
- "@contao.slug"

config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
abstract: true
55
calls:
66
- [setTagFinder, ["@codefog_tags.tag_finder"]]
7-
- [setSourcefinder, ["@codefog_tags.source_finder"]]
7+
- [setSourceFinder, ["@codefog_tags.source_finder"]]
88

99
codefog_tags.tag_finder:
1010
class: Codefog\TagsBundle\Finder\TagFinder

contao/config/config.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,13 @@
1717
'tables' => ['tl_cfg_tag'],
1818
];
1919

20-
/*
21-
* Backend widgets
22-
*/
20+
// Backend widgets
2321
$GLOBALS['BE_FFL']['cfgTags'] = TagsWidget::class;
2422

25-
/*
26-
* Models
27-
*/
23+
// Models
2824
$GLOBALS['TL_MODELS']['tl_cfg_tag'] = TagModel::class;
2925

30-
/*
31-
* Hooks
32-
*/
26+
// Hooks
3327
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = ['codefog_tags.listener.insert_tags', 'onReplaceInsertTags'];
3428

3529
if (is_array($GLOBALS['TL_HOOKS']['loadDataContainer'] ?? null)) {

contao/dca/tl_cfg_tag.php

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Codefog\TagsBundle\Driver;
66
use Contao\DataContainer;
7+
use Doctrine\DBAL\Types\Types;
78

89
/*
910
* Tags Bundle for Contao Open Source CMS.
@@ -19,9 +20,6 @@
1920
'dataContainer' => Driver::class,
2021
'enableVersioning' => true,
2122
'notCopyable' => true,
22-
'onload_callback' => [
23-
['codefog_tags.listener.data_container.tag', 'onLoadCallback'],
24-
],
2523
'sql' => [
2624
'keys' => [
2725
'id' => 'primary',
@@ -38,47 +36,10 @@
3836
'fields' => ['name'],
3937
'flag' => DataContainer::SORT_INITIAL_LETTER_ASC,
4038
'panelLayout' => 'filter;cfg_sort,search,limit',
41-
'panel_callback' => [
42-
'cfg_sort' => ['codefog_tags.listener.data_container.tag', 'onPanelCallback'],
43-
],
4439
],
4540
'label' => [
4641
'fields' => ['name', 'source', 'total'],
4742
'showColumns' => true,
48-
'label_callback' => ['codefog_tags.listener.data_container.tag', 'onLabelCallback'],
49-
],
50-
'global_operations' => [
51-
'all' => [
52-
'label' => &$GLOBALS['TL_LANG']['MSC']['all'],
53-
'href' => 'act=select',
54-
'class' => 'header_edit_all',
55-
'attributes' => 'onclick="Backend.getScrollOffset()" accesskey="e"',
56-
],
57-
],
58-
'operations' => [
59-
'edit' => [
60-
'label' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['edit'],
61-
'href' => 'act=edit',
62-
'icon' => 'edit.gif',
63-
],
64-
'delete' => [
65-
'label' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['delete'],
66-
'href' => 'act=delete',
67-
'icon' => 'delete.gif',
68-
'attributes' => 'onclick="if(!confirm(\''.($GLOBALS['TL_LANG']['MSC']['deleteConfirm'] ?? null).'\'))return false;Backend.getScrollOffset()"',
69-
],
70-
'show' => [
71-
'label' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['show'],
72-
'href' => 'act=show',
73-
'icon' => 'show.gif',
74-
],
75-
],
76-
],
77-
78-
// Select
79-
'select' => [
80-
'buttons_callback' => [
81-
['codefog_tags.listener.data_container.tag', 'onButtonsCallback'],
8243
],
8344
],
8445

@@ -90,47 +51,32 @@
9051
// Fields
9152
'fields' => [
9253
'id' => [
93-
'sql' => ['type' => 'integer', 'unsigned' => true, 'autoincrement' => true],
54+
'sql' => ['type' => Types::INTEGER, 'unsigned' => true, 'autoincrement' => true],
9455
],
9556
'tstamp' => [
96-
'sql' => ['type' => 'integer', 'unsigned' => true],
57+
'sql' => ['type' => Types::INTEGER, 'unsigned' => true],
9758
],
9859
'total' => [
9960
'label' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['count'],
10061
],
10162
'name' => [
102-
'label' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['name'],
103-
'exclude' => true,
10463
'search' => true,
10564
'inputType' => 'text',
106-
'eval' => [
107-
'mandatory' => true,
108-
'alnum' => true,
109-
'maxlength' => 255,
110-
'tl_class' => 'w50',
111-
],
112-
'sql' => ['type' => 'string', 'length' => 64, 'default' => ''],
65+
'eval' => ['mandatory' => true, 'alnum' => true, 'maxlength' => 255, 'tl_class' => 'w50'],
66+
'sql' => ['type' => Types::STRING, 'length' => 64, 'default' => ''],
11367
],
11468
'source' => [
115-
'label' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['source'],
116-
'exclude' => true,
11769
'filter' => true,
11870
'inputType' => 'select',
119-
'options_callback' => ['codefog_tags.listener.data_container.tag', 'onSourceOptionsCallback'],
12071
'reference' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['sourceRef'],
12172
'eval' => ['mandatory' => true, 'includeBlankOption' => true, 'tl_class' => 'w50'],
122-
'sql' => ['type' => 'string', 'length' => 64, 'notnull' => false],
73+
'sql' => ['type' => Types::STRING, 'length' => 64, 'notnull' => false],
12374
],
12475
'alias' => [
125-
'label' => &$GLOBALS['TL_LANG']['tl_cfg_tag']['alias'],
126-
'exclude' => true,
12776
'search' => true,
12877
'inputType' => 'text',
12978
'eval' => ['rgxp' => 'alias', 'maxlength' => 128, 'tl_class' => 'w50'],
130-
'save_callback' => [
131-
['codefog_tags.listener.data_container.tag', 'onAliasSaveCallback'],
132-
],
133-
'sql' => ['type' => 'string', 'length' => 128, 'default' => ''],
79+
'sql' => ['type' => Types::STRING, 'length' => 128, 'default' => ''],
13480
],
13581
],
13682
];

src/ContaoManager/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class Plugin implements BundlePluginInterface
1515
{
16-
public function getBundles(ParserInterface $parser)
16+
public function getBundles(ParserInterface $parser): array
1717
{
1818
return [
1919
BundleConfig::create(CodefogTagsBundle::class)->setLoadAfter([ContaoCoreBundle::class, CodefogHasteBundle::class]),

src/DependencyInjection/CodefogTagsExtension.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
2424
}
2525
}
2626

27-
/**
28-
* Create the manager.
29-
*/
3027
private function createManager(string $name, array $config, ContainerBuilder $container): void
3128
{
3229
$id = \sprintf('codefog_tags.manager.%s', $name);

src/DependencyInjection/Compiler/ManagerPass.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\DependencyInjection\Reference;
1010

11-
class ManagerPass implements CompilerPassInterface
11+
readonly class ManagerPass implements CompilerPassInterface
1212
{
13-
public const TAG_NAME = 'codefog_tags.manager';
14-
15-
/**
16-
* ManagerPass constructor.
17-
*
18-
* @param string $registryName
19-
*/
20-
public function __construct(private $registryName)
13+
public const string TAG_NAME = 'codefog_tags.manager';
14+
15+
public function __construct(private string $registryName)
2116
{
2217
}
2318

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@ class Configuration implements ConfigurationInterface
1212
public function getConfigTreeBuilder(): TreeBuilder
1313
{
1414
$treeBuilder = new TreeBuilder('codefog_tags');
15-
16-
if (method_exists($treeBuilder, 'getRootNode')) {
17-
$rootNode = $treeBuilder->getRootNode();
18-
} else {
19-
// BC layer for symfony/config 4.1 and older
20-
$rootNode = $treeBuilder->getRootNode();
21-
}
22-
15+
$rootNode = $treeBuilder->getRootNode();
2316
$rootNode
2417
->children()
2518
->arrayNode('managers')

src/EventListener/DataContainer/TagListener.php

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@
1010
use Codefog\TagsBundle\ManagerRegistry;
1111
use Codefog\TagsBundle\Model\TagModel;
1212
use Contao\Controller;
13+
use Contao\CoreBundle\DependencyInjection\Attribute\AsCallback;
14+
use Contao\CoreBundle\Slug\Slug;
1315
use Contao\Database;
1416
use Contao\DataContainer;
17+
use Contao\DC_Table;
1518
use Contao\StringUtil;
1619
use Contao\System;
1720
use Contao\Versions;
1821
use Doctrine\DBAL\Connection;
1922
use Symfony\Component\HttpFoundation\RequestStack;
2023
use 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

Comments
 (0)