32
32
use Xibo \Factory \LayoutFactory ;
33
33
use Xibo \Factory \MediaFactory ;
34
34
use Xibo \Factory \ReportScheduleFactory ;
35
+ use Xibo \Factory \DisplayGroupFactory ;
35
36
use Xibo \Helper \ApplicationState ;
36
37
use Xibo \Helper \DateFormatHelper ;
37
38
use Xibo \Helper \SanitizerService ;
@@ -69,6 +70,11 @@ class ProofOfPlay implements ReportInterface
69
70
*/
70
71
private $ reportScheduleFactory ;
71
72
73
+ /**
74
+ * @var DisplayGroupFactory
75
+ */
76
+ private $ displayGroupFactory ;
77
+
72
78
/**
73
79
* @var SanitizerService
74
80
*/
@@ -94,6 +100,7 @@ public function setFactories(ContainerInterface $container)
94
100
$ this ->mediaFactory = $ container ->get ('mediaFactory ' );
95
101
$ this ->layoutFactory = $ container ->get ('layoutFactory ' );
96
102
$ this ->reportScheduleFactory = $ container ->get ('reportScheduleFactory ' );
103
+ $ this ->displayGroupFactory = $ container ->get ('displayGroupFactory ' );
97
104
$ this ->sanitizer = $ container ->get ('sanitizerService ' );
98
105
99
106
return $ this ;
@@ -325,6 +332,12 @@ public function getResults(SanitizerInterface $sanitizedParams)
325
332
$ operator = $ sanitizedParams ->getString ('logicalOperator ' , ['default ' => 'OR ' ]);
326
333
$ parentCampaignId = $ sanitizedParams ->getInt ('parentCampaignId ' );
327
334
335
+ // Group the data by display, display group, or by tag
336
+ $ groupBy = $ sanitizedParams ->getString ('groupBy ' );
337
+
338
+ // Used with groupBy in case we want to filter by specific display groups only
339
+ $ displayGroupIds = $ sanitizedParams ->getIntArray ('displayGroupId ' , ['default ' => []]);
340
+
328
341
// Display filter.
329
342
try {
330
343
// Get an array of display id this user has access to.
@@ -472,7 +485,8 @@ public function getResults(SanitizerInterface $sanitizedParams)
472
485
$ tags ,
473
486
$ tagsType ,
474
487
$ exactTags ,
475
- $ operator
488
+ $ operator ,
489
+ $ groupBy
476
490
);
477
491
}
478
492
@@ -505,7 +519,10 @@ public function getResults(SanitizerInterface $sanitizedParams)
505
519
$ entry ['minStart ' ] = Carbon::createFromTimestamp ($ row ['minStart ' ])->format (DateFormatHelper::getSystemFormat ());
506
520
$ entry ['maxEnd ' ] = Carbon::createFromTimestamp ($ row ['maxEnd ' ])->format (DateFormatHelper::getSystemFormat ());
507
521
$ entry ['mediaId ' ] = $ sanitizedRow ->getInt ('mediaId ' );
508
-
522
+ $ entry ['displayGroup ' ] = $ sanitizedRow ->getString ('displayGroup ' );
523
+ $ entry ['displayGroupId ' ] = $ sanitizedRow ->getInt ('displayGroupId ' );
524
+ $ entry ['tagName ' ] = $ sanitizedRow ->getString ('tagName ' );
525
+ $ entry ['tagId ' ] = $ sanitizedRow ->getInt ('tagId ' );
509
526
$ rows [] = $ entry ;
510
527
}
511
528
@@ -541,6 +558,7 @@ public function getResults(SanitizerInterface $sanitizedParams)
541
558
* @param $tags string
542
559
* @param $tagsType string
543
560
* @param $exactTags mixed
561
+ * @param $groupBy string
544
562
* @return array[array result, date periodStart, date periodEnd, int count, int totalStats]
545
563
*/
546
564
private function getProofOfPlayReportMySql (
@@ -555,15 +573,15 @@ private function getProofOfPlayReportMySql(
555
573
$ tags ,
556
574
$ tagsType ,
557
575
$ exactTags ,
558
- $ logicalOperator
576
+ $ logicalOperator ,
577
+ $ groupBy
559
578
) {
560
579
$ fromDt = $ fromDt ->format ('U ' );
561
580
$ toDt = $ toDt ->format ('U ' );
562
581
563
582
// Media on Layouts Ran
564
583
$ select = '
565
584
SELECT stat.type,
566
- display.Display,
567
585
stat.parentCampaignId,
568
586
campaign.campaign as parentCampaign,
569
587
IFNULL(layout.Layout,
@@ -581,10 +599,23 @@ private function getProofOfPlayReportMySql(
581
599
stat.tag,
582
600
stat.layoutId,
583
601
stat.mediaId,
584
- stat.widgetId,
585
- stat.displayId
602
+ stat.widgetId
586
603
' ;
587
604
605
+ // We get the ID and name - either by display, display group or tag
606
+ if ($ groupBy === 'display ' ) {
607
+ $ select .= ', display.Display, stat.displayId ' ;
608
+ } else if ($ groupBy === 'displayGroup ' ) {
609
+ $ select .= ', displaydg.displayGroup, displaydg.displayGroupId ' ;
610
+ } else if ($ groupBy === 'tag ' ) {
611
+ if ($ tagsType === 'dg ' || $ tagsType === 'media ' ) {
612
+ $ select .= ', taglink.value, taglink.tagId ' ;
613
+ } else {
614
+ // For layouts, we need to manually select taglink.tag
615
+ $ select .= ', taglink.tag AS value, taglink.tagId ' ;
616
+ }
617
+ }
618
+
588
619
$ body = '
589
620
FROM stat
590
621
LEFT OUTER JOIN display
@@ -615,6 +646,17 @@ private function getProofOfPlayReportMySql(
615
646
}
616
647
}
617
648
649
+ if ($ groupBy === 'displayGroup ' ) {
650
+ // Group the data by display group
651
+ $ body .= 'INNER JOIN `lkdisplaydg` AS linkdg
652
+ ON linkdg.DisplayID = display.displayid
653
+ INNER JOIN `displaygroup` AS displaydg
654
+ ON displaydg.displaygroupId = linkdg.displaygroupId
655
+ AND `displaydg`.isDisplaySpecific = 0 ' ;
656
+ } else if ($ groupBy === 'tag ' ) {
657
+ $ body .= $ this ->groupByTagType ($ tagsType );
658
+ }
659
+
618
660
$ body .= ' WHERE stat.type <> \'displaydown \'
619
661
AND stat.end > :fromDt
620
662
AND stat.start < :toDt
@@ -799,23 +841,29 @@ private function getProofOfPlayReportMySql(
799
841
$ body .= ' AND `media`.mediaId IN ( ' . trim ($ mediaSql , ', ' ) . ') ' ;
800
842
}
801
843
844
+ // We first implement default groupings
802
845
$ body .= '
803
846
GROUP BY stat.type,
804
847
stat.tag,
805
- display.Display,
806
848
stat.parentCampaignId,
807
- stat.displayId,
808
849
stat.campaignId,
809
850
layout.layout,
810
851
IFNULL(stat.mediaId, stat.widgetId),
811
852
IFNULL(`media`.name, IFNULL(`widgetoption`.value, `widget`.type)),
812
- stat.tag,
813
853
stat.layoutId,
814
854
stat.mediaId,
815
- stat.widgetId,
816
- stat.displayId
855
+ stat.widgetId
817
856
' ;
818
857
858
+ // Then add the optional groupings
859
+ if ($ groupBy === 'display ' ) {
860
+ $ body .= ', display.Display, stat.displayId ' ;
861
+ } else if ($ groupBy === 'displayGroup ' ) {
862
+ $ body .= ', displaydg.displayGroupId, displaydg.displayGroup ' ;
863
+ } else if ($ groupBy === 'tag ' ) {
864
+ $ body .= ', value, taglink.tagId ' ;
865
+ }
866
+
819
867
$ order = '' ;
820
868
if ($ columns != null ) {
821
869
$ order = 'ORDER BY ' . implode (', ' , $ columns );
@@ -829,8 +877,8 @@ private function getProofOfPlayReportMySql(
829
877
$ entry = [];
830
878
831
879
$ entry ['type ' ] = $ row ['type ' ];
832
- $ entry ['displayId ' ] = $ row ['displayId ' ];
833
- $ entry ['display ' ] = $ row ['Display ' ];
880
+ $ entry ['displayId ' ] = $ row ['displayId ' ] ?? '' ;
881
+ $ entry ['display ' ] = $ row ['Display ' ] ?? '' ;
834
882
$ entry ['layout ' ] = $ row ['Layout ' ];
835
883
$ entry ['parentCampaignId ' ] = $ row ['parentCampaignId ' ];
836
884
$ entry ['parentCampaign ' ] = $ row ['parentCampaign ' ];
@@ -843,7 +891,10 @@ private function getProofOfPlayReportMySql(
843
891
$ entry ['widgetId ' ] = $ row ['widgetId ' ];
844
892
$ entry ['mediaId ' ] = $ row ['mediaId ' ];
845
893
$ entry ['tag ' ] = $ row ['tag ' ];
846
-
894
+ $ entry ['displayGroupId ' ] = $ row ['displayGroupId ' ] ?? '' ;
895
+ $ entry ['displayGroup ' ] = $ row ['displayGroup ' ] ?? '' ;
896
+ $ entry ['tagId ' ] = $ row ['tagId ' ] ?? '' ;
897
+ $ entry ['tagName ' ] = $ row ['value ' ] ?? '' ;
847
898
$ rows [] = $ entry ;
848
899
}
849
900
@@ -1157,4 +1208,24 @@ private function getProofOfPlayReportMongoDb(
1157
1208
'count ' => count ($ rows )
1158
1209
];
1159
1210
}
1211
+
1212
+ /**
1213
+ * Add grouping by tag type
1214
+ * @param string $tagType
1215
+ * @return string
1216
+ */
1217
+ private function groupByTagType (string $ tagType ) : string
1218
+ {
1219
+ return match ($ tagType ) {
1220
+ 'media ' => 'INNER JOIN `lktagmedia` AS taglink ON taglink.mediaId = stat.mediaId ' ,
1221
+ 'layout ' => 'INNER JOIN `lktaglayout` ON `lktaglayout`.layoutId = stat.layoutId
1222
+ INNER JOIN `tag` AS taglink ON taglink.tagId = `lktaglayout`.tagId ' ,
1223
+ 'dg ' => 'INNER JOIN `lkdisplaydg` AS linkdg
1224
+ ON linkdg.DisplayID = display.displayid
1225
+ INNER JOIN `displaygroup` AS displaydg
1226
+ ON displaydg.displaygroupId = linkdg.displaygroupId
1227
+ AND `displaydg`.isDisplaySpecific = 1 INNER JOIN
1228
+ `lktagdisplaygroup` AS taglink ON taglink.displaygroupId = displaydg.displaygroupId ' ,
1229
+ };
1230
+ }
1160
1231
}
0 commit comments