1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\PiwikBundle\Block\Service; |
13
|
|
|
|
14
|
|
|
use Core23\PiwikBundle\Client\ClientFactoryInterface; |
15
|
|
|
use Core23\PiwikBundle\Exception\PiwikException; |
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
17
|
|
|
use Psr\Log\LoggerAwareTrait; |
18
|
|
|
use Psr\Log\NullLogger; |
19
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
20
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
21
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService; |
22
|
|
|
use Sonata\BlockBundle\Model\BlockInterface; |
23
|
|
|
use Sonata\CoreBundle\Form\Type\ImmutableArrayType; |
24
|
|
|
use Sonata\CoreBundle\Model\Metadata; |
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
26
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
27
|
|
|
use Symfony\Component\Form\Extension\Core\Type\NumberType; |
28
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
31
|
|
|
|
32
|
|
|
final class PiwikStatisticBlockService extends AbstractAdminBlockService implements LoggerAwareInterface |
33
|
|
|
{ |
34
|
|
|
use LoggerAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ClientFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $factory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* PiwikStatisticBlockService constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $name |
45
|
|
|
* @param EngineInterface $templating |
46
|
|
|
* @param ClientFactoryInterface $factory |
47
|
|
|
*/ |
48
|
|
|
public function __construct(string $name, EngineInterface $templating, ClientFactoryInterface $factory) |
49
|
|
|
{ |
50
|
|
|
parent::__construct($name, $templating); |
51
|
|
|
|
52
|
|
|
$this->factory = $factory; |
53
|
|
|
$this->logger = new NullLogger(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function execute(BlockContextInterface $blockContext, Response $response = null) |
60
|
|
|
{ |
61
|
|
|
return $this->renderResponse($blockContext->getTemplate(), [ |
62
|
|
|
'context' => $blockContext, |
63
|
|
|
'settings' => $blockContext->getSettings(), |
64
|
|
|
'block' => $blockContext->getBlock(), |
65
|
|
|
'data' => $this->getData($blockContext->getSettings()), |
66
|
|
|
], $response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void |
73
|
|
|
{ |
74
|
|
|
$formMapper->add('settings', ImmutableArrayType::class, [ |
75
|
|
|
'keys' => [ |
76
|
|
|
['title', TextType::class, [ |
77
|
|
|
'required' => false, |
78
|
|
|
'label' => 'form.label_title', |
79
|
|
|
]], |
80
|
|
|
['translation_domain', TextType::class, [ |
81
|
|
|
'label' => 'form.label_translation_domain', |
82
|
|
|
'required' => false, |
83
|
|
|
]], |
84
|
|
|
['icon', TextType::class, [ |
85
|
|
|
'label' => 'form.label_icon', |
86
|
|
|
'required' => false, |
87
|
|
|
]], |
88
|
|
|
['class', TextType::class, [ |
89
|
|
|
'label' => 'form.label_class', |
90
|
|
|
'required' => false, |
91
|
|
|
]], |
92
|
|
|
['host', TextType::class, [ |
93
|
|
|
'required' => false, |
94
|
|
|
'label' => 'form.label_host', |
95
|
|
|
]], |
96
|
|
|
['token', TextType::class, [ |
97
|
|
|
'required' => false, |
98
|
|
|
'label' => 'form.label_token', |
99
|
|
|
]], |
100
|
|
|
['site', NumberType::class, [ |
101
|
|
|
'label' => 'form.label_site', |
102
|
|
|
]], |
103
|
|
|
['method', ChoiceType::class, [ |
104
|
|
|
'choices' => [ |
105
|
|
|
'form.choice_visitors' => 'VisitsSummary.getVisits', |
106
|
|
|
'form.choice_unique_visitors' => 'VisitsSummary.getUniqueVisitors', |
107
|
|
|
'form.choice_hits' => 'VisitsSummary.getActions ', |
108
|
|
|
], |
109
|
|
|
'label' => 'form.label_method', |
110
|
|
|
]], |
111
|
|
|
['period', ChoiceType::class, [ |
112
|
|
|
'choices' => [ |
113
|
|
|
'form.choice_day' => 'day', |
114
|
|
|
'form.choice_week' => 'week', |
115
|
|
|
'form.choice_month' => 'month', |
116
|
|
|
'form.choice_year' => 'year', |
117
|
|
|
], |
118
|
|
|
'label' => 'form.label_period', |
119
|
|
|
]], |
120
|
|
|
['date', ChoiceType::class, [ |
121
|
|
|
'choices' => [ |
122
|
|
|
'form.choice_today' => 'last1', |
123
|
|
|
'form.choice_1_week' => 'last7', |
124
|
|
|
'form.choice_2_weeks' => 'last14', |
125
|
|
|
'form.choice_1_month' => 'last30', |
126
|
|
|
'form.choice_3_months' => 'last90', |
127
|
|
|
'form.choice_6_months' => 'last180', |
128
|
|
|
'form.choice_1_year' => 'last360', |
129
|
|
|
], |
130
|
|
|
'label' => 'form.label_date', |
131
|
|
|
]], |
132
|
|
|
], |
133
|
|
|
'translation_domain' => 'Core23PiwikBundle', |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
141
|
|
|
{ |
142
|
|
|
$resolver->setDefaults([ |
143
|
|
|
'title' => null, |
144
|
|
|
'translation_domain' => null, |
145
|
|
|
'icon' => 'fa fa-bar-chart-o', |
146
|
|
|
'class' => null, |
147
|
|
|
'site' => null, |
148
|
|
|
'method' => 'VisitsSummary.getVisits', |
149
|
|
|
'host' => null, |
150
|
|
|
'token' => null, |
151
|
|
|
'period' => 'day', |
152
|
|
|
'date' => 'last30', |
153
|
|
|
'template' => '@Core23Piwik/Block/block_piwik_statistic.html.twig', |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
$resolver->setRequired(['site', 'host', 'token']); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
|
|
public function getJavascripts($media) |
163
|
|
|
{ |
164
|
|
|
return [ |
165
|
|
|
'/assets/javascript/chartist.js', |
166
|
|
|
'/assets/javascript/jquery.piwikTable.js', |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function getStylesheets($media) |
174
|
|
|
{ |
175
|
|
|
return [ |
176
|
|
|
'/assets/stylesheet/chartist.css', |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function getBlockMetadata($code = null) |
184
|
|
|
{ |
185
|
|
|
return new Metadata($this->getName(), $code ?? $this->getName(), false, 'Core23PiwikBundle', [ |
186
|
|
|
'class' => 'fa fa-area-chart', |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param array $settings |
192
|
|
|
* |
193
|
|
|
* @return array|null |
194
|
|
|
*/ |
195
|
|
|
protected function getData(array $settings = []): ?array |
196
|
|
|
{ |
197
|
|
|
try { |
198
|
|
|
$client = $this->factory->createPiwikClient($settings['host'], $settings['token']); |
199
|
|
|
|
200
|
|
|
$response = $client->call($settings['method'], [ |
201
|
|
|
'idSite' => $settings['site'], |
202
|
|
|
'period' => $settings['period'], |
203
|
|
|
'date' => $settings['date'], |
204
|
|
|
]); |
205
|
|
|
|
206
|
|
|
return $response; |
207
|
|
|
} catch (PiwikException $ce) { |
208
|
|
|
$this->logger->warning('Error retrieving Piwik url: '.$settings['host'], [ |
209
|
|
|
'exception' => $ce, |
210
|
|
|
]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return null; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|