Issues (3627)

EmailBundle/EventListener/DashboardSubscriber.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\EmailBundle\EventListener;
13
14
use Mautic\CoreBundle\Helper\ArrayHelper;
15
use Mautic\DashboardBundle\Entity\Widget;
16
use Mautic\DashboardBundle\Event\WidgetDetailEvent;
17
use Mautic\DashboardBundle\EventListener\DashboardSubscriber as MainDashboardSubscriber;
18
use Mautic\EmailBundle\Form\Type\DashboardEmailsInTimeWidgetType;
19
use Mautic\EmailBundle\Form\Type\DashboardMostHitEmailRedirectsWidgetType;
20
use Mautic\EmailBundle\Form\Type\DashboardSentEmailToContactsWidgetType;
21
use Mautic\EmailBundle\Model\EmailModel;
22
use Symfony\Component\Routing\RouterInterface;
23
24
class DashboardSubscriber extends MainDashboardSubscriber
25
{
26
    /**
27
     * Define the name of the bundle/category of the widget(s).
28
     *
29
     * @var string
30
     */
31
    protected $bundle = 'email';
32
33
    /**
34
     * Define the widget(s).
35
     *
36
     * @var string
37
     */
38
    protected $types = [
39
        'emails.in.time' => [
40
            'formAlias' => DashboardEmailsInTimeWidgetType::class,
41
        ],
42
        'sent.email.to.contacts' => [
43
            'formAlias' => DashboardSentEmailToContactsWidgetType::class,
44
        ],
45
        'most.hit.email.redirects' => [
46
            'formAlias' => DashboardMostHitEmailRedirectsWidgetType::class,
47
        ],
48
        'ignored.vs.read.emails'   => [],
49
        'upcoming.emails'          => [],
50
        'most.sent.emails'         => [],
51
        'most.read.emails'         => [],
52
        'created.emails'           => [],
53
        'device.granularity.email' => [],
54
    ];
55
56
    /**
57
     * Define permissions to see those widgets.
58
     *
59
     * @var array
60
     */
61
    protected $permissions = [
62
        'email:emails:viewown',
63
        'email:emails:viewother',
64
    ];
65
66
    /**
67
     * @var EmailModel
68
     */
69
    protected $emailModel;
70
71
    /**
72
     * @var RouterInterface
73
     */
74
    private $router;
75
76
    public function __construct(EmailModel $emailModel, RouterInterface $router)
77
    {
78
        $this->emailModel = $emailModel;
79
        $this->router     = $router;
80
    }
81
82
    /**
83
     * Set a widget detail when needed.
84
     */
85
    public function onWidgetDetailGenerate(WidgetDetailEvent $event)
86
    {
87
        $this->checkPermissions($event);
88
        $canViewOthers = $event->hasPermission('email:emails:viewother');
89
        $defaultLimit  = $this->getDefaultLimit($event->getWidget());
90
91
        if ('emails.in.time' == $event->getType()) {
92
            $widget     = $event->getWidget();
93
            $params     = $widget->getParams();
94
            $filterKeys = ['flag', 'dataset', 'companyId', 'campaignId', 'segmentId'];
95
96
            if (!$event->isCached()) {
97
                $event->setTemplateData([
98
                    'chartType'   => 'line',
99
                    'chartHeight' => $widget->getHeight() - 80,
100
                    'chartData'   => $this->emailModel->getEmailsLineChartData(
101
                        $params['timeUnit'],
102
                        $params['dateFrom'],
103
                        $params['dateTo'],
104
                        $params['dateFormat'],
105
                        ArrayHelper::select($filterKeys, $params),
106
                        $canViewOthers
107
                    ),
108
                ]);
109
            }
110
111
            $event->setTemplate('MauticCoreBundle:Helper:chart.html.php');
112
            $event->stopPropagation();
113
        }
114
115
        if ('sent.email.to.contacts' == $event->getType()) {
116
            $widget = $event->getWidget();
117
            $params = $widget->getParams();
118
119
            if (!$event->isCached()) {
120
                $headItems  = [
121
                    'mautic.dashboard.label.contact.id',
122
                    'mautic.dashboard.label.contact.email.address',
123
                    'mautic.dashboard.label.contact.open',
124
                    'mautic.dashboard.label.contact.click',
125
                    'mautic.dashboard.label.contact.links.clicked',
126
                    'mautic.dashboard.label.email.id',
127
                    'mautic.dashboard.label.email.name',
128
                    'mautic.dashboard.label.segment.id',
129
                    'mautic.dashboard.label.segment.name',
130
                    'mautic.dashboard.label.company.id',
131
                    'mautic.dashboard.label.company.name',
132
                    'mautic.dashboard.label.campaign.id',
133
                    'mautic.dashboard.label.campaign.name',
134
                    'mautic.dashboard.label.date.sent',
135
                    'mautic.dashboard.label.date.read',
136
                ];
137
138
                $event->setTemplateData(
139
                    [
140
                        'headItems' => $headItems,
141
                        'bodyItems' => $this->emailModel->getSentEmailToContactData(
142
                            ArrayHelper::getValue('limit', $params, $defaultLimit),
143
                            $params['dateFrom'],
144
                            $params['dateTo'],
145
                            ['groupBy' => 'sends', 'canViewOthers' => $canViewOthers],
146
                            ArrayHelper::getValue('companyId', $params),
147
                            ArrayHelper::getValue('campaignId', $params),
148
                            ArrayHelper::getValue('segmentId', $params)
149
                        ),
150
                    ]
151
                );
152
            }
153
154
            $event->setTemplate('MauticEmailBundle:SubscribedEvents:Dashboard/Sent.email.to.contacts.html.php');
155
            $event->stopPropagation();
156
        }
157
158
        if ('most.hit.email.redirects' == $event->getType()) {
159
            $widget = $event->getWidget();
160
            $params = $widget->getParams();
161
162
            if (!$event->isCached()) {
163
                $event->setTemplateData([
164
                    'headItems' => [
165
                        'mautic.dashboard.label.url',
166
                        'mautic.dashboard.label.unique.hit.count',
167
                        'mautic.dashboard.label.total.hit.count',
168
                        'mautic.dashboard.label.email.id',
169
                        'mautic.dashboard.label.email.name',
170
                    ],
171
                    'bodyItems' => $this->emailModel->getMostHitEmailRedirects(
172
                        ArrayHelper::getValue('limit', $params, $defaultLimit),
173
                        $params['dateFrom'],
174
                        $params['dateTo'],
175
                        ['groupBy' => 'sends', 'canViewOthers' => $canViewOthers],
176
                        ArrayHelper::getValue('companyId', $params),
177
                        ArrayHelper::getValue('campaignId', $params),
178
                        ArrayHelper::getValue('segmentId', $params)
179
                    ),
180
                ]);
181
            }
182
183
            $event->setTemplate('MauticEmailBundle:SubscribedEvents:Dashboard/Most.hit.email.redirects.html.php');
184
            $event->stopPropagation();
185
        }
186
187
        if ('ignored.vs.read.emails' == $event->getType()) {
188
            $widget = $event->getWidget();
189
            $params = $widget->getParams();
190
191
            if (!$event->isCached()) {
192
                $event->setTemplateData([
193
                    'chartType'   => 'pie',
194
                    'chartHeight' => $widget->getHeight() - 80,
195
                    'chartData'   => $this->emailModel->getIgnoredVsReadPieChartData($params['dateFrom'], $params['dateTo'], [], $canViewOthers),
196
                ]);
197
            }
198
199
            $event->setTemplate('MauticCoreBundle:Helper:chart.html.php');
200
            $event->stopPropagation();
201
        }
202
203
        if ('upcoming.emails' == $event->getType()) {
204
            $widget = $event->getWidget();
205
            $params = $widget->getParams();
206
            $height = $widget->getHeight();
207
            $limit  = round(($height - 80) / 60);
208
209
            $upcomingEmails = $this->emailModel->getUpcomingEmails($limit, $canViewOthers);
210
211
            $event->setTemplate('MauticDashboardBundle:Dashboard:upcomingemails.html.php');
212
            $event->setTemplateData(['upcomingEmails' => $upcomingEmails]);
213
            $event->stopPropagation();
214
        }
215
216
        if ('most.sent.emails' == $event->getType()) {
217
            if (!$event->isCached()) {
218
                $params = $event->getWidget()->getParams();
219
                $emails = $this->emailModel->getEmailStatList(
220
                    ArrayHelper::getValue('limit', $params, $defaultLimit),
221
                    $params['dateFrom'],
222
                    $params['dateTo'],
223
                    [],
224
                    ['groupBy' => 'sends', 'canViewOthers' => $canViewOthers]
225
                );
226
                $items = [];
227
228
                // Build table rows with links
229
                if ($emails) {
230
                    foreach ($emails as &$email) {
231
                        $emailUrl = $this->router->generate('mautic_email_action', ['objectAction' => 'view', 'objectId' => $email['id']]);
232
                        $row      = [
233
                            [
234
                                'value' => $email['name'],
235
                                'type'  => 'link',
236
                                'link'  => $emailUrl,
237
                            ],
238
                            [
239
                                'value' => $email['count'],
240
                            ],
241
                        ];
242
                        $items[] = $row;
243
                    }
244
                }
245
246
                $event->setTemplateData([
247
                    'headItems' => [
248
                        'mautic.dashboard.label.title',
249
                        'mautic.email.label.sends',
250
                    ],
251
                    'bodyItems' => $items,
252
                    'raw'       => $emails,
253
                ]);
254
            }
255
256
            $event->setTemplate('MauticCoreBundle:Helper:table.html.php');
257
            $event->stopPropagation();
258
        }
259
260
        if ('most.read.emails' == $event->getType()) {
261
            if (!$event->isCached()) {
262
                $params = $event->getWidget()->getParams();
263
                $emails = $this->emailModel->getEmailStatList(
264
                    ArrayHelper::getValue('limit', $params, $defaultLimit),
265
                    $params['dateFrom'],
266
                    $params['dateTo'],
267
                    [],
268
                    ['groupBy' => 'reads', 'canViewOthers' => $canViewOthers]
269
                );
270
                $items = [];
271
272
                // Build table rows with links
273
                if ($emails) {
274
                    foreach ($emails as &$email) {
275
                        $emailUrl = $this->router->generate('mautic_email_action', ['objectAction' => 'view', 'objectId' => $email['id']]);
276
                        $row      = [
277
                            [
278
                                'value' => $email['name'],
279
                                'type'  => 'link',
280
                                'link'  => $emailUrl,
281
                            ],
282
                            [
283
                                'value' => $email['count'],
284
                            ],
285
                        ];
286
                        $items[] = $row;
287
                    }
288
                }
289
290
                $event->setTemplateData([
291
                    'headItems' => [
292
                        'mautic.dashboard.label.title',
293
                        'mautic.email.label.reads',
294
                    ],
295
                    'bodyItems' => $items,
296
                    'raw'       => $emails,
297
                ]);
298
            }
299
300
            $event->setTemplate('MauticCoreBundle:Helper:table.html.php');
301
            $event->stopPropagation();
302
        }
303
304
        if ('created.emails' == $event->getType()) {
305
            if (!$event->isCached()) {
306
                $params = $event->getWidget()->getParams();
307
                $emails = $this->emailModel->getEmailList(
308
                    ArrayHelper::getValue('limit', $params, $defaultLimit),
309
                    $params['dateFrom'],
310
                    $params['dateTo'],
311
                    [],
312
                    ['groupBy' => 'creations', 'canViewOthers' => $canViewOthers]
313
                );
314
                $items = [];
315
316
                // Build table rows with links
317
                if ($emails) {
318
                    foreach ($emails as &$email) {
319
                        $emailUrl = $this->router->generate(
320
                            'mautic_email_action',
321
                            [
322
                                'objectAction' => 'view',
323
                                'objectId'     => $email['id'],
324
                            ]
325
                        );
326
                        $row = [
327
                            [
328
                                'value' => $email['name'],
329
                                'type'  => 'link',
330
                                'link'  => $emailUrl,
331
                            ],
332
                        ];
333
                        $items[] = $row;
334
                    }
335
                }
336
337
                $event->setTemplateData([
338
                    'headItems' => [
339
                        'mautic.dashboard.label.title',
340
                    ],
341
                    'bodyItems' => $items,
342
                    'raw'       => $emails,
343
                ]);
344
            }
345
346
            $event->setTemplate('MauticCoreBundle:Helper:table.html.php');
347
            $event->stopPropagation();
348
        }
349
        if ('device.granularity.email' == $event->getType()) {
350
            $widget = $event->getWidget();
351
            $params = $widget->getParams();
352
353
            if (!$event->isCached()) {
354
                $event->setTemplateData([
355
                    'chartType'   => 'pie',
356
                    'chartHeight' => $widget->getHeight() - 80,
357
                    'chartData'   => $this->emailModel->getDeviceGranularityPieChartData(
358
                        $params['dateFrom'],
359
                        $params['dateTo'],
360
                        $canViewOthers
0 ignored issues
show
The call to Mautic\EmailBundle\Model...anularityPieChartData() has too many arguments starting with $canViewOthers. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

360
                    'chartData'   => $this->emailModel->/** @scrutinizer ignore-call */ getDeviceGranularityPieChartData(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
361
                    ),
362
                ]);
363
            }
364
365
            $event->setTemplate('MauticCoreBundle:Helper:chart.html.php');
366
            $event->stopPropagation();
367
        }
368
    }
369
370
    /**
371
     * Count the row limit from the widget height.
372
     *
373
     * @return int
374
     */
375
    private function getDefaultLimit(Widget $widget)
376
    {
377
        return round((($widget->getHeight() - 80) / 35) - 1);
378
    }
379
}
380