• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 15017483614

14 May 2025 09:41AM UTC coverage: 25.302% (-54.2%) from 79.482%
15017483614

Pull #96

github

koertho
backport anonymize utils
Pull Request #96: Backport anonymize utils

6 of 7 new or added lines in 2 files covered. (85.71%)

248 existing lines in 22 files now uncovered.

1488 of 5881 relevant lines covered (25.3%)

1.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.19
/src/EventListener/DcaField/DcaAuthorListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\BackendUser;
6
use Contao\CoreBundle\Framework\ContaoFramework;
7
use Contao\CoreBundle\ServiceAnnotation\Hook;
8
use Contao\DataContainer;
9
use Contao\FrontendUser;
10
use Contao\Model;
11
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
12
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldConfiguration;
13
use Symfony\Component\Security\Core\Security;
14

15
class DcaAuthorListener extends AbstractDcaFieldListener
16
{
17
    /**
18
     * @Hook("loadDataContainer")
19
     */
20
    public function onLoadDataContainer(string $table): void
21
    {
22
        if (!isset(AuthorField::getRegistrations()[$table])) {
1✔
23
            return;
1✔
24
        }
25

26
        $options = AuthorField::getRegistrations()[$table];
1✔
27
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
28
        $security = $this->container->get('security.helper');
1✔
29

30
        $authorField = [
1✔
31
            'exclude' => $options->isExclude(),
1✔
32
            'search' => $options->isSearch(),
1✔
33
            'filter' => $options->isFilter(),
1✔
34
            'inputType' => 'select',
1✔
35
            'eval' => [
1✔
36
                'doNotCopy' => true,
1✔
37
                'mandatory' => true,
1✔
38
                'chosen' => true,
1✔
39
                'includeBlankOption' => true,
1✔
40
                'tl_class' => 'w50'
1✔
41
            ],
1✔
42
            'sql' => "int(10) unsigned NOT NULL default 0",
1✔
43
        ];
1✔
44

45
        $this->applyDefaultFieldAdjustments($authorField, $options);
1✔
46

47
        if ($options->isUseDefaultLabel()) {
1✔
48
            $authorField['label'] = &$GLOBALS['TL_LANG']['MSC']['utilsBundle']['author'];
1✔
49
        }
50

51
        $authorField['default'] = 0;
1✔
52
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
53
            if ($security->getUser() instanceof BackendUser) {
1✔
54
                $authorField['default'] = $security->getUser()->id;
×
55
            }
56
            $authorField['foreignKey'] = 'tl_user.name';
1✔
57
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
1✔
58
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
59
            if ($security->getUser() instanceof FrontendUser) {
×
UNCOV
60
                $authorField['default'] = $security->getUser()->id;
×
61
            }
UNCOV
62
            $authorField['foreignKey'] = "tl_member.CONCAT(firstname,' ',lastname)";
×
UNCOV
63
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
×
64
        }
65

66
        $GLOBALS['TL_DCA'][$table]['fields'][$authorFieldName] = $authorField;
1✔
67
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onConfigCopyCallback'];
1✔
68
    }
69

70

71
    public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
72
    {
73
        $options = AuthorField::getRegistrations()[$dc->table];
1✔
74
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
75
        $security = $this->container->get('security.helper');
1✔
76

77
        $model = $this->getModelInstance($dc->table, $insertId);
1✔
78
        if (!$model) {
1✔
UNCOV
79
            return;
×
80
        }
81

82
        $model->{$authorFieldName} = 0;
1✔
83
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
84
            if ($security->getUser() instanceof BackendUser) {
1✔
85
                $model->{$authorFieldName} = $security->getUser()->id;
1✔
86
            }
87
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
88
            if ($security->getUser() instanceof FrontendUser) {
×
89
                $model->{$authorFieldName} = $security->getUser()->id;
×
90
            }
91
        }
92
        $model->save();
1✔
93
    }
94

95
    /**
96
     * @param AuthorFieldConfiguration $options
97
     * @return string
98
     */
99
    protected function getAuthorFieldName(AuthorFieldConfiguration $options): string
100
    {
101
        if (!$options->hasFieldNamePrefix()) {
2✔
102
            return 'author';
2✔
103
        }
104
        if (str_ends_with($options->getFieldNamePrefix(), '_')) {
1✔
105
            return $options->getFieldNamePrefix() . 'author';
1✔
106
        } else {
107
            return $options->getFieldNamePrefix() . 'Author';
1✔
108
        }
109
    }
110

111
    public static function getSubscribedServices(): array
112
    {
113
        $services = parent::getSubscribedServices();
×
114
        $services['security.helper'] = Security::class;
×
115
        return $services;
×
116
    }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc