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

heimrichhannot / contao-utils-bundle / 8099821218

29 Feb 2024 05:05PM UTC coverage: 22.978% (+0.3%) from 22.703%
8099821218

Pull #74

github

koertho
fixed tests
Pull Request #74: Add date added field

29 of 41 new or added lines in 5 files covered. (70.73%)

8 existing lines in 1 file now uncovered.

1281 of 5575 relevant lines covered (22.98%)

1.54 hits per line

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

83.33
/src/EventListener/DcaField/DateAddedFieldListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\CoreBundle\ServiceAnnotation\Hook;
6
use Contao\DataContainer;
7
use HeimrichHannot\UtilsBundle\Dca\DateAddedField;
8

9
class DateAddedFieldListener extends AbstractDcaFieldListener
10
{
11
    /**
12
     * @Hook("loadDataContainer")
13
     */
14
    public function onLoadDataContainer(string $table): void
15
    {
16
        if (!isset(DateAddedField::getRegistrations()[$table])) {
1✔
17
            return;
1✔
18
        }
19

20
        $GLOBALS['TL_DCA'][$table]['config']['onload_callback'][] = [self::class, 'onLoadCallback'];
1✔
21
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onCopyCallback'];
1✔
22

23
        $GLOBALS['TL_DCA'][$table]['fields']['dateAdded'] = [
1✔
24
            'label'   => &$GLOBALS['TL_LANG']['MSC']['dateAdded'],
1✔
25
            'sorting' => true,
1✔
26
            'eval'    => ['rgxp' => 'datim', 'doNotCopy' => true],
1✔
27
            'sql'     => "int(10) unsigned NOT NULL default '0'",
1✔
28
        ];
1✔
29
    }
30

31
    public function onLoadCallback(DataContainer $dc = null): void
32
    {
33
        if (!$dc || !$dc->id) {
1✔
NEW
34
            return;
×
35
        }
36

37
        $model = $this->getModelInstance($dc->table, (int)$dc->id);
1✔
38
        if (!$model || $model->dateAdded > 0) {
1✔
NEW
39
            return;
×
40
        }
41

42
        $model->dateAdded = time();
1✔
43
        $model->save();
1✔
44
    }
45

46
    public function onCopyCallback(int $insertId, DataContainer $dc): void
47
    {
48
        if (!$dc || !$dc->id) {
1✔
NEW
49
            return;
×
50
        }
51

52
        $model = $this->getModelInstance($dc->table, $insertId);
1✔
53
        if (!$model || $model->dateAdded > 0) {
1✔
NEW
54
            return;
×
55
        }
56

57
        $model->dateAdded = time();
1✔
58
        $model->save();
1✔
59
    }
60
}
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