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\AssetBundle\EventListener; |
||
13 | |||
14 | use Mautic\AssetBundle\Model\AssetModel; |
||
15 | use Mautic\DashboardBundle\Event\WidgetDetailEvent; |
||
16 | use Mautic\DashboardBundle\EventListener\DashboardSubscriber as MainDashboardSubscriber; |
||
17 | use Symfony\Component\Routing\RouterInterface; |
||
18 | |||
19 | class DashboardSubscriber extends MainDashboardSubscriber |
||
20 | { |
||
21 | /** |
||
22 | * Define the name of the bundle/category of the widget(s). |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $bundle = 'asset'; |
||
27 | |||
28 | /** |
||
29 | * Define the widget(s). |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $types = [ |
||
34 | 'asset.downloads.in.time' => [], |
||
35 | 'unique.vs.repetitive.downloads' => [], |
||
36 | 'popular.assets' => [], |
||
37 | 'created.assets' => [], |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * Define permissions to see those widgets. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $permissions = [ |
||
46 | 'asset:assets:viewown', |
||
47 | 'asset:assets:viewother', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @var AssetModel |
||
52 | */ |
||
53 | protected $assetModel; |
||
54 | |||
55 | /** |
||
56 | * @var RouterInterface |
||
57 | */ |
||
58 | protected $router; |
||
59 | |||
60 | public function __construct(AssetModel $assetModel, RouterInterface $router) |
||
61 | { |
||
62 | $this->assetModel = $assetModel; |
||
63 | $this->router = $router; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Set a widget detail when needed. |
||
68 | */ |
||
69 | public function onWidgetDetailGenerate(WidgetDetailEvent $event) |
||
70 | { |
||
71 | $this->checkPermissions($event); |
||
72 | $canViewOthers = $event->hasPermission('asset:assets:viewother'); |
||
73 | |||
74 | if ('asset.downloads.in.time' == $event->getType()) { |
||
75 | $widget = $event->getWidget(); |
||
76 | $params = $widget->getParams(); |
||
77 | |||
78 | if (!$event->isCached()) { |
||
79 | $event->setTemplateData([ |
||
80 | 'chartType' => 'line', |
||
81 | 'chartHeight' => $widget->getHeight() - 80, |
||
82 | 'chartData' => $this->assetModel->getDownloadsLineChartData( |
||
83 | $params['timeUnit'], |
||
84 | $params['dateFrom'], |
||
85 | $params['dateTo'], |
||
86 | $params['dateFormat'], |
||
87 | $canViewOthers |
||
88 | ), |
||
89 | ]); |
||
90 | } |
||
91 | |||
92 | $event->setTemplate('MauticCoreBundle:Helper:chart.html.php'); |
||
93 | $event->stopPropagation(); |
||
94 | } |
||
95 | |||
96 | if ('unique.vs.repetitive.downloads' == $event->getType()) { |
||
97 | if (!$event->isCached()) { |
||
98 | $params = $event->getWidget()->getParams(); |
||
99 | $event->setTemplateData([ |
||
100 | 'chartType' => 'pie', |
||
101 | 'chartHeight' => $event->getWidget()->getHeight() - 80, |
||
102 | 'chartData' => $this->assetModel->getUniqueVsRepetitivePieChartData($params['dateFrom'], $params['dateTo'], $canViewOthers), |
||
103 | ]); |
||
104 | } |
||
105 | |||
106 | $event->setTemplate('MauticCoreBundle:Helper:chart.html.php'); |
||
107 | $event->stopPropagation(); |
||
108 | } |
||
109 | |||
110 | if ('popular.assets' == $event->getType()) { |
||
111 | if (!$event->isCached()) { |
||
112 | $params = $event->getWidget()->getParams(); |
||
113 | |||
114 | if (empty($params['limit'])) { |
||
115 | // Count the pages limit from the widget height |
||
116 | $limit = round((($event->getWidget()->getHeight() - 80) / 35) - 1); |
||
117 | } else { |
||
118 | $limit = $params['limit']; |
||
119 | } |
||
120 | |||
121 | $assets = $this->assetModel->getPopularAssets($limit, $params['dateFrom'], $params['dateTo'], $canViewOthers); |
||
122 | $items = []; |
||
123 | |||
124 | // Build table rows with links |
||
125 | if ($assets) { |
||
0 ignored issues
–
show
|
|||
126 | foreach ($assets as &$asset) { |
||
127 | $assetUrl = $this->router->generate('mautic_asset_action', ['objectAction' => 'view', 'objectId' => $asset['id']]); |
||
128 | $row = [ |
||
129 | [ |
||
130 | 'value' => $asset['title'], |
||
131 | 'type' => 'link', |
||
132 | 'link' => $assetUrl, |
||
133 | ], |
||
134 | [ |
||
135 | 'value' => $asset['download_count'], |
||
136 | ], |
||
137 | ]; |
||
138 | $items[] = $row; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | $event->setTemplateData([ |
||
143 | 'headItems' => [ |
||
144 | 'mautic.dashboard.label.title', |
||
145 | 'mautic.dashboard.label.downloads', |
||
146 | ], |
||
147 | 'bodyItems' => $items, |
||
148 | 'raw' => $assets, |
||
149 | ]); |
||
150 | } |
||
151 | |||
152 | $event->setTemplate('MauticCoreBundle:Helper:table.html.php'); |
||
153 | $event->stopPropagation(); |
||
154 | } |
||
155 | |||
156 | if ('created.assets' == $event->getType()) { |
||
157 | if (!$event->isCached()) { |
||
158 | $params = $event->getWidget()->getParams(); |
||
159 | |||
160 | if (empty($params['limit'])) { |
||
161 | // Count the assets limit from the widget height |
||
162 | $limit = round((($event->getWidget()->getHeight() - 80) / 35) - 1); |
||
163 | } else { |
||
164 | $limit = $params['limit']; |
||
165 | } |
||
166 | |||
167 | $assets = $this->assetModel->getAssetList($limit, $params['dateFrom'], $params['dateTo'], [], ['canViewOthers' => $canViewOthers]); |
||
168 | $items = []; |
||
169 | |||
170 | // Build table rows with links |
||
171 | if ($assets) { |
||
0 ignored issues
–
show
The expression
$assets of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using
Loading history...
|
|||
172 | foreach ($assets as &$asset) { |
||
173 | $assetUrl = $this->router->generate('mautic_asset_action', ['objectAction' => 'view', 'objectId' => $asset['id']]); |
||
174 | $row = [ |
||
175 | [ |
||
176 | 'value' => $asset['name'], |
||
177 | 'type' => 'link', |
||
178 | 'link' => $assetUrl, |
||
179 | ], |
||
180 | ]; |
||
181 | $items[] = $row; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | $event->setTemplateData([ |
||
186 | 'headItems' => [ |
||
187 | 'mautic.dashboard.label.title', |
||
188 | ], |
||
189 | 'bodyItems' => $items, |
||
190 | 'raw' => $assets, |
||
191 | ]); |
||
192 | } |
||
193 | |||
194 | $event->setTemplate('MauticCoreBundle:Helper:table.html.php'); |
||
195 | $event->stopPropagation(); |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.