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

heimrichhannot / contao-utils-bundle / 8647844091

11 Apr 2024 01:49PM UTC coverage: 71.88% (+1.1%) from 70.826%
8647844091

push

github

koertho
adjust #83 for v3 and php8

5 of 5 new or added lines in 1 file covered. (100.0%)

17 existing lines in 2 files now uncovered.

887 of 1234 relevant lines covered (71.88%)

3.37 hits per line

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

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

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\BackendUser;
6
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
7
use Contao\DataContainer;
8
use Contao\FrontendUser;
9
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
10
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldConfiguration;
11
use Symfony\Component\Security\Core\Security;
12

13
class DcaAuthorListener extends AbstractDcaFieldListener
14
{
15
    #[AsHook("loadDataContainer")]
16
    public function onLoadDataContainer(string $table): void
17
    {
18
        if (!isset(AuthorField::getRegistrations()[$table])) {
1✔
19
            return;
1✔
20
        }
21

22
        $options = AuthorField::getRegistrations()[$table];
1✔
23
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
24
        $security = $this->container->get('security.helper');
1✔
25

26
        $authorField = [
1✔
27
            'inputType' => 'select',
1✔
28
            'eval' => [
1✔
29
                'doNotCopy' => true,
1✔
30
                'mandatory' => true,
1✔
31
                'chosen' => true,
1✔
32
                'includeBlankOption' => true,
1✔
33
                'tl_class' => 'w50'
1✔
34
            ],
1✔
35
            'sql' => "int(10) unsigned NOT NULL default 0",
1✔
36
        ];
1✔
37

38
        $this->applyDefaultFieldAdjustments($authorField, $options);
1✔
39

40
        if ($options->isUseDefaultLabel()) {
1✔
41
            $authorField['label'] = &$GLOBALS['TL_LANG']['MSC']['utilsBundle']['author'];
1✔
42
        }
43

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

59
        $GLOBALS['TL_DCA'][$table]['fields'][$authorFieldName] = $authorField;
1✔
60
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onConfigCopyCallback'];
1✔
61
    }
62

63

64
    public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
65
    {
66
        $options = AuthorField::getRegistrations()[$dc->table];
1✔
67
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
68
        $security = $this->container->get('security.helper');
1✔
69

70
        $model = $this->getModelInstance($dc->table, $insertId);
1✔
71
        if (!$model) {
1✔
UNCOV
72
            return;
×
73
        }
74

75
        $model->{$authorFieldName} = 0;
1✔
76
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
77
            if ($security->getUser() instanceof BackendUser) {
1✔
UNCOV
78
                $model->{$authorFieldName} = $security->getUser()->id;
×
79
            }
UNCOV
80
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
UNCOV
81
            if ($security->getUser() instanceof FrontendUser) {
×
UNCOV
82
                $model->{$authorFieldName} = $security->getUser()->id;
×
83
            }
84
        }
85
        $model->save();
1✔
86
    }
87

88
    /**
89
     * @param AuthorFieldConfiguration $options
90
     * @return string
91
     */
92
    protected function getAuthorFieldName(AuthorFieldConfiguration $options): string
93
    {
94
        if (!$options->hasFieldNamePrefix()) {
2✔
95
            return 'author';
2✔
96
        }
97
        if (str_ends_with($options->getFieldNamePrefix(), '_')) {
1✔
98
            return $options->getFieldNamePrefix() . 'author';
1✔
99
        } else {
100
            return $options->getFieldNamePrefix() . 'Author';
1✔
101
        }
102
    }
103

104
    public static function getSubscribedServices(): array
105
    {
UNCOV
106
        $services = parent::getSubscribedServices();
×
UNCOV
107
        $services['security.helper'] = Security::class;
×
UNCOV
108
        return $services;
×
109
    }
110
}
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

© 2025 Coveralls, Inc