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

heimrichhannot / contao-utils-bundle / 6495495336

12 Oct 2023 12:16PM UTC coverage: 22.867% (+1.0%) from 21.909%
6495495336

Pull #70

github

koertho
more php syntax fixes
Pull Request #70: Feature/dca author field

53 of 53 new or added lines in 3 files covered. (100.0%)

1254 of 5484 relevant lines covered (22.87%)

1.55 hits per line

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

81.48
/src/EventListener/DcaAuthorListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener;
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\AuthorFieldOptions;
13
use Symfony\Component\Security\Core\Security;
14

15
class DcaAuthorListener
16
{
17
    /** @var ContaoFramework  */
18
    private $framework;
19
    /** @var Security  */
20
    private $security;
21

22
    public function __construct(ContaoFramework $framework, Security $security)
23
    {
24
        $this->framework = $framework;
2✔
25
        $this->security = $security;
2✔
26
    }
27

28
    /**
29
     * @Hook("loadDataContainer")
30
     */
31
    public function onLoadDataContainer(string $table): void
32
    {
33
        if (!isset(AuthorField::getRegistrations()[$table])) {
1✔
34
            return;
1✔
35
        }
36

37
        $options = AuthorField::getRegistrations()[$table];
1✔
38

39
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
40

41
        $authorField = [
1✔
42
            'exclude' => $options->isExclude(),
1✔
43
            'search' => $options->isSearch(),
1✔
44
            'filter' => $options->isFilter(),
1✔
45
            'inputType' => 'select',
1✔
46
            'eval' => [
1✔
47
                'doNotCopy' => true,
1✔
48
                'mandatory' => true,
1✔
49
                'chosen' => true,
1✔
50
                'includeBlankOption' => true,
1✔
51
                'tl_class' => 'w50'
1✔
52
            ],
1✔
53
            'sql' => "int(10) unsigned NOT NULL default 0",
1✔
54
        ];
1✔
55

56
        if ($options->isUseDefaultLabel()) {
1✔
57
            $authorField['label'] = &$GLOBALS['TL_LANG']['MSC']['utilsBundle']['author'];
1✔
58
        }
59

60
        $authorField['default'] = 0;
1✔
61
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
62
            if ($this->security->getUser() instanceof BackendUser) {
1✔
63
                $authorField['default'] = $this->security->getUser()->id;
×
64
            }
65
            $authorField['foreignKey'] = 'tl_user.name';
1✔
66
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
1✔
67
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
68
            if ($this->security->getUser() instanceof FrontendUser) {
×
69
                $authorField['default'] = $this->security->getUser()->id;
×
70
            }
71
            $authorField['foreignKey'] = "tl_member.CONCAT(firstname,' ',lastname)";
×
72
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
×
73
        }
74

75
        $GLOBALS['TL_DCA'][$table]['fields'][$authorFieldName] = $authorField;
1✔
76
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onConfigCopyCallback'];
1✔
77
    }
78

79

80
    public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
81
    {
82
        $options = AuthorField::getRegistrations()[$dc->table];
1✔
83
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
84

85
        /** @var class-string<Model> $modelClass */
86
        $modelClass = $this->framework->getAdapter(Model::class)->getClassFromTable($dc->table);
1✔
87
        $model = $this->framework->getAdapter($modelClass)->findByPk($insertId);
1✔
88
        if (!$model) {
1✔
89
            return;
×
90
        }
91

92
        $model->{$authorFieldName} = 0;
1✔
93
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
94
            if ($this->security->getUser() instanceof BackendUser) {
1✔
95
                $model->{$authorFieldName} = $this->security->getUser()->id;
1✔
96
            }
97
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
98
            if ($this->security->getUser() instanceof FrontendUser) {
×
99
                $model->{$authorFieldName} = $this->security->getUser()->id;
×
100
            }
101
        }
102
        $model->save();
1✔
103
    }
104

105
    /**
106
     * @param AuthorFieldOptions $options
107
     * @return string
108
     */
109
    protected function getAuthorFieldName(AuthorFieldOptions $options): string
110
    {
111
        if (!$options->hasFieldNamePrefix()) {
2✔
112
            return 'author';
2✔
113
        }
114
        if (str_ends_with($options->getFieldNamePrefix(), '_')) {
1✔
115
            return $options->getFieldNamePrefix() . 'author';
1✔
116
        } else {
117
            return $options->getFieldNamePrefix() . 'Author';
1✔
118
        }
119
    }
120
}
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