File size: 4,130 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

namespace Mautic\ReportBundle\Helper;

use Mautic\ReportBundle\Event\ColumnCollectEvent;
use Mautic\ReportBundle\ReportEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class ReportHelper
{
    public function __construct(
        private EventDispatcherInterface $dispatcher
    ) {
    }

    public function getName(): string
    {
        return 'report';
    }

    /**
     * @return string
     */
    public function getReportBuilderFieldType($type)
    {
        return match ($type) {
            'number' => 'int',
            'lookup', 'text', 'url', 'email', 'tel', 'region', 'country', 'locale' => 'string',
            default => $type,
        };
    }

    /**
     * Returns standard form fields such as id, name, publish_up, etc.
     *
     * @param string[] $removeColumns
     *
     * @return array<string,array<string,string>>
     */
    public function getStandardColumns(string $prefix, array $removeColumns = [], string $idLink = ''): array
    {
        $aliasPrefix = str_replace('.', '_', $prefix);
        $columns     = [
            $prefix.'id' => [
                'label' => 'mautic.core.id',
                'type'  => 'int',
                'link'  => $idLink,
                'alias' => "{$aliasPrefix}id",
            ],
            $prefix.'name' => [
                'label' => 'mautic.core.name',
                'type'  => 'string',
                'alias' => "{$aliasPrefix}name",
            ],
            $prefix.'created_by_user' => [
                'label' => 'mautic.core.createdby',
                'type'  => 'string',
                'alias' => "{$aliasPrefix}created_by_user",
            ],
            $prefix.'date_added' => [
                'label' => 'mautic.report.field.date_added',
                'type'  => 'datetime',
                'alias' => "{$aliasPrefix}date_added",
            ],
            $prefix.'modified_by_user' => [
                'label' => 'mautic.report.field.modified_by_user',
                'type'  => 'string',
                'alias' => "{$aliasPrefix}modified_by_user",
            ],
            $prefix.'date_modified' => [
                'label' => 'mautic.report.field.date_modified',
                'type'  => 'datetime',
                'alias' => "{$aliasPrefix}date_modified",
            ],
            $prefix.'description' => [
                'label' => 'mautic.core.description',
                'type'  => 'string',
                'alias' => "{$aliasPrefix}description",
            ],
            $prefix.'publish_up' => [
                'label' => 'mautic.report.field.publish_up',
                'type'  => 'datetime',
                'alias' => "{$aliasPrefix}publish_up",
            ],
            $prefix.'publish_down' => [
                'label' => 'mautic.report.field.publish_down',
                'type'  => 'datetime',
                'alias' => "{$aliasPrefix}publish_down",
            ],
            $prefix.'is_published' => [
                'label' => 'mautic.report.field.is_published',
                'type'  => 'bool',
                'alias' => "{$aliasPrefix}is_published",
            ],
        ];

        if (empty($idLink)) {
            unset($columns[$prefix.'id']['link']);
        }

        foreach ($removeColumns as $c) {
            if (isset($columns[$prefix.$c])) {
                unset($columns[$prefix.$c]);
            }
        }

        return $columns;
    }

    /**
     * @param array<string, mixed> $properties
     *
     * @return array<string, array<string, mixed>>
     */
    public function getMappedObjectColumns(string $object, array $properties = []): array
    {
        $event = new ColumnCollectEvent($object, $properties);
        $this->dispatcher->dispatch($event, ReportEvents::REPORT_ON_COLUMN_COLLECT);

        return array_map(
            function ($item) {
                if (isset($item['type'])) {
                    $item['type'] =  $this->getReportBuilderFieldType($item['type']);
                }

                return $item;
            },
            $event->getColumns()
        );
    }
}