1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Controller; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Config\ImmutableConfig; |
6
|
|
|
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
7
|
|
|
use Drupal\Core\Template\Attribute; |
8
|
|
|
use Drupal\mongodb_watchdog\EventController; |
9
|
|
|
use Drupal\mongodb_watchdog\EventTemplate; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DetailController implements the controller for the event detail page. |
15
|
|
|
*/ |
16
|
|
|
class DetailController implements ContainerInjectionInterface { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The mongodb.watchdog_event_controller service. |
20
|
|
|
* |
21
|
|
|
* @var \Drupal\mongodb_watchdog\EventController |
22
|
|
|
*/ |
23
|
|
|
protected $eventController; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
29
|
|
|
* The logger service, to log intervening events. |
30
|
|
|
* @param \Drupal\mongodb_watchdog\EventController $event_controller |
31
|
|
|
* The event controller service. |
32
|
|
|
* @param \Drupal\Core\Config\ImmutableConfig $config |
33
|
|
|
* The module configuration. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(LoggerInterface $logger, EventController $event_controller, ImmutableConfig $config) { |
36
|
|
|
$this->config = $config; |
37
|
|
|
$this->eventController = $event_controller; |
38
|
|
|
$this->logger = $logger; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public static function create(ContainerInterface $container) { |
45
|
|
|
/** @var \Psr\Log\LoggerInterface $logger */ |
46
|
|
|
$logger = $container->get('logger.channel.mongodb_watchdog'); |
47
|
|
|
|
48
|
|
|
/** @var \Drupal\mongodb_watchdog\EventController $eventController */ |
49
|
|
|
$eventController = $container->get('mongodb.watchdog_event_controller'); |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** @var array $config */ |
52
|
|
|
$config = $container->get('config.factory')->get('mongodb_watchdog.settings'); |
53
|
|
|
|
54
|
|
|
return new static($logger, $eventController, $config); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Controller for mongodb_watchdog.detail. |
59
|
|
|
* |
60
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
61
|
|
|
* The event template. |
62
|
|
|
* |
63
|
|
|
* @return array |
|
|
|
|
64
|
|
|
* A render array. |
65
|
|
|
*/ |
66
|
|
|
public function detail(EventTemplate $event_template) { |
67
|
|
|
$template_rows = $this->detailHeader($event_template); |
68
|
|
|
$event_rows = $this->detailRows($event_template); |
69
|
|
|
|
70
|
|
|
$base = [ |
71
|
|
|
'#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']), |
72
|
|
|
'#type' => 'table', |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$event_header = [ |
76
|
|
|
t('Date'), |
77
|
|
|
t('User'), |
78
|
|
|
t('Location'), |
79
|
|
|
t('Referrer'), |
80
|
|
|
t('Hostname'), |
81
|
|
|
t('Message'), |
82
|
|
|
t('Operations'), |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$ret = [ |
86
|
|
|
"#attached" => [ |
|
|
|
|
87
|
|
|
'library' => 'mongodb_watchdog/styling', |
88
|
|
|
], |
89
|
|
|
'template' => $base + [ |
90
|
|
|
'#caption' => t('Event template'), |
91
|
|
|
'#rows' => $template_rows, |
92
|
|
|
], |
93
|
|
|
'events' => $base + [ |
94
|
|
|
'#caption' => t('Event occurrences'), |
95
|
|
|
'#header' => $event_header, |
96
|
|
|
'#rows' => $event_rows, |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
if ($footer = $this->detailFooter($event_template, $event_rows)) { |
101
|
|
|
$ret['extra'] = [ |
102
|
|
|
'#markup' => $footer, |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $ret; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Report on the extra event occurrences. |
111
|
|
|
* |
112
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
113
|
|
|
* The event template. |
114
|
|
|
* @param array $event_rows |
115
|
|
|
* The event rows array. |
116
|
|
|
* |
117
|
|
|
* @return \Drupal\Core\StringTranslation\TranslatableMarkup|null |
118
|
|
|
* A message about the remaining events not displayed on the page, or NULL |
119
|
|
|
* if there are no additional messages. |
120
|
|
|
* |
121
|
|
|
* @XXX Maybe replace by a pager later ? |
122
|
|
|
*/ |
123
|
|
|
protected function detailFooter(EventTemplate $event_template, array $event_rows) { |
124
|
|
|
$display_count = count($event_rows); |
125
|
|
|
$ret = NULL; |
126
|
|
|
if ($display_count >= $this->config->get('items_per_page')) { |
127
|
|
|
$count = $this->eventController->count($event_template); |
128
|
|
|
if ($count > $display_count) { |
129
|
|
|
$ret = t('There are also @count more older occurrences of this event', [ |
130
|
|
|
'@count' => $count - $display_count, |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
return $ret; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Build the heading rows on the event occurrences page. |
139
|
|
|
* |
140
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
141
|
|
|
* The event template. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
* A table render array. |
145
|
|
|
*/ |
146
|
|
|
protected function detailHeader(EventTemplate $template) { |
147
|
|
|
$rows = []; |
148
|
|
|
foreach (EventTemplate::keys() as $key => $info) { |
149
|
|
|
$value = $template->{$key}; |
150
|
|
|
$rows[] = [ |
151
|
|
|
[ |
152
|
|
|
'header' => TRUE, |
153
|
|
|
'data' => $info['label'], |
154
|
|
|
], |
155
|
|
|
isset($info['display_callback']) ? $info['display_callback']($value) : $value, |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
return $rows; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Build the occurrence rows on the event occurrences page. |
163
|
|
|
* |
164
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
165
|
|
|
* The event template. |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
* A table render array. |
169
|
|
|
*/ |
170
|
|
|
protected function detailRows(EventTemplate $template) { |
171
|
|
|
$rows = []; |
172
|
|
|
|
173
|
|
|
$events = $this->eventController->find($template, NULL, $this->config->get('items_per_page')); |
174
|
|
|
/** @var \Drupal\mongodb_watchdog\Event $event */ |
175
|
|
|
foreach ($events as $event) { |
176
|
|
|
$rows[] = $this->eventController->asTableRow($template, $event); |
177
|
|
|
} |
178
|
|
|
return $rows; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Title callback for mongodb_watchdog.detail. |
183
|
|
|
* |
184
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
185
|
|
|
* The event template for which the title is built. |
186
|
|
|
* |
187
|
|
|
* @return \Drupal\Core\StringTranslation\TranslatableMarkup |
188
|
|
|
* The page title. |
189
|
|
|
*/ |
190
|
|
|
public function detailTitle(EventTemplate $event_template) { |
191
|
|
|
return t('MongoDB events: "@template"', ['@template' => $event_template->message]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
|
|
|
|
195
|
|
|
|