forked from OS2web/os2web_esdh_provider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
os2web_esdh_provider.module
596 lines (558 loc) · 18 KB
/
os2web_esdh_provider.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
/**
* @file
* Implements pluginable interface for accessing ESDH systems.
* All plugins should be implemented in a seperate provider module
* which implements the ctools api.
* Look to acadre_esdh for example.
*/
/**
* Implements hook_menu().
*/
function os2web_esdh_provider_menu() {
$items['admin/config/os2web_esdh_provider'] = array(
'title' => 'OS2Web ESDH Provider',
'position' => 'right',
'weight' => -15,
'access arguments' => array('administer os2web'),
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/os2web_esdh_provider/importconfirm'] = array(
'title' => 'Confirm reimport',
'access arguments' => array('administer os2web'),
'page callback' => 'drupal_get_form',
'page arguments' => array('_os2web_esdh_provider_status_confirm'),
'type' => MENU_CALLBACK,
'file' => 'os2web_esdh_provider.admin.inc',
);
$items['admin/config/os2web_esdh_provider/status'] = array(
'title' => 'Status',
'description' => 'Overview page for OS2Web ESDH Provider',
'access arguments' => array('administer os2web'),
'page callback' => '_os2web_esdh_provider_status',
'weight' => -10,
'type' => MENU_NORMAL_ITEM,
'file' => 'os2web_esdh_provider.admin.inc',
);
$items['admin/config/os2web_esdh_provider/settings'] = array(
'title' => 'Settings',
'description' => 'Configure OS2Web ESDH Provider',
'access arguments' => array('administer os2web'),
'page callback' => 'drupal_get_form',
'page arguments' => array('_os2web_esdh_provider_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => 'os2web_esdh_provider.admin.inc',
);
$items['node/%/info'] = array(
'page callback' => '_os2web_esdh_provider_node_tab_page',
'page arguments' => array(1, 3),
'access callback' => '_os2web_esdh_provider_node_tab_access',
'access arguments' => array(1),
'title' => 'Meeting control',
'type' => MENU_LOCAL_TASK,
'file' => 'os2web_esdh_provider.mmapi.inc',
);
$items['admin/os2web/import/%'] = array(
'page callback' => '_os2web_esdh_provider_import',
'page arguments' => array(3),
'access callback' => '_os2web_esdh_provider_import_access',
'type' => MENU_CALLBACK,
'file' => 'os2web_esdh_provider.mmapi.inc',
);
$items['os2web/esdh/v1'] = array(
'type' => MENU_CALLBACK,
'page callback' => '_os2web_esdh_provider_handler',
'access callback' => TRUE,
'file' => 'os2web_esdh_provider.cpapi.inc',
);
return $items;
}
/**
* Implements hook_ctools_plugin_type().
*/
function os2web_esdh_provider_ctools_plugin_type() {
$plugins['cm'] = array(
'cache' => TRUE,
'required methods' => array(
'get_document',
'get_document_file',
'get_case_docs',
'get_case_data',
'lookup_doc',
'lookup_case',
),
'optional methods' => array(
'available',
),
);
$plugins['mm'] = array(
'cache' => TRUE,
'required methods' => array(
'get_import_list',
'import_meeting',
),
'optional methods' => array(
'post_import_process',
'available',
),
);
$plugins['cp'] = array(
'cache' => TRUE,
'required methods' => array(
'handle_request',
),
'optional methods' => array(
'available',
),
);
return $plugins;
}
/**
* Access callback. Show tab only on meeting pages, and if user has access.
*/
function _os2web_esdh_provider_node_tab_access($nid) {
$node = node_load($nid);
if ($node->type != 'meeting') {
return FALSE;
}
return user_access('administer os2web');
}
/**
* Access callback for the import service call.
*/
function _os2web_esdh_provider_import_access() {
$ip_string = variable_get('os2web_meetings_import_service_ip', '');
$valid_ips = array_map('trim', explode(',', $ip_string));
return in_array(ip_address(), $valid_ips);
}
/**
* Implements hook_node_delete().
*/
function os2web_esdh_provider_node_delete($node) {
if ($node->type === 'os2web_meetings_meeting') {
// Remove any addenums relating to this node.
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'os2web_meetings_meeting')
->fieldCondition('field_os2web_meetings_addendum', 'nid', $node->nid, '=')
->execute();
$result = array_shift($result);
if (is_array($result)) {
$result = array_keys($result);
foreach ($result as $add_node) {
node_delete($add_node->nid);
}
}
// Delete bullets from meeting.
$items = array_shift($node->field_os2web_meetings_bullets);
if (is_array($items)) {
foreach ($items as $nids) {
node_delete($nids['target_id']);
}
}
}
if ($node->type === 'os2web_meetings_bullet') {
// Delete bullet attachments from meeting.
$bullets = array_shift($node->field_os2web_meetings_attach);
if (is_array($bullets)) {
foreach ($bullets as $nids) {
node_delete($nids['target_id']);
}
}
}
}
/**
* Implemets hook_cron().
*
* Will run import on every cron.
*/
function os2web_esdh_provider_cron() {
if (variable_get('os2web_esdh_provider_queue_enabled', FALSE)) {
return;
}
if (os2web_esdh_provider_has_api('mm')) {
$queue = DrupalQueue::get('acadre_mm_import');
if ($queue->numberOfItems() == 0) {
$meetings = os2web_esdh_provider_invoke('mm', 'get_import_list');
$current_timestamp = time();
$last_mm_import_time = variable_get('os2web_esdh_provider_last_mm_import', FALSE);
foreach ($meetings as $meeting) {
// Only import files newer than last cron job.
if (!$last_mm_import_time || filemtime($meeting['uri']) > $last_mm_import_time) {
$queue->createItem(array('meeting' => $meeting, 'force' => FALSE));
}
}
$queue->createItem(array('post_import_process' => TRUE));
variable_set('os2web_esdh_provider_last_mm_import', $current_timestamp);
}
}
}
/**
* Worker function that queues meetings for external import.
*/
function os2web_esdh_provider_queue_meetings() {
if (os2web_esdh_provider_has_api('mm')) {
$meetings = os2web_esdh_provider_invoke('mm', 'get_import_list');
$last_mm_import_time = variable_get('os2web_esdh_provider_last_mm_import', FALSE);
$queue = DrupalQueue::get('acadre_mm_import');
foreach ($meetings as $meeting) {
// Only import files newer than last cron job.
if (!$last_mm_import_time || filemtime($meeting['uri']) > $last_mm_import_time) {
$queue->createItem(array('meeting' => $meeting, 'force' => FALSE));
}
}
$queue->createItem(array('post_import_process' => TRUE));
variable_set('os2web_esdh_provider_last_mm_import', $current_timestamp);
variable_set('os2web_esdh_provider_last_mm_import', time());
}
}
/**
* Implements hook_cron_queue_info().
*/
function os2web_esdh_provider_cron_queue_info() {
// If the external queue handler is enabled, don't let
// drupals cron impor the meetings.
if (variable_get('os2web_esdh_provider_queue_enabled', FALSE)) {
return array();
}
$queues['acadre_mm_import'] = array(
'worker callback' => '_os2web_esdh_provider_cron_queue_worker',
'time' => 30,
);
return $queues;
}
/**
* Worker function for a single import.
*
* @param array $args
* Meeting data for the import.
*/
function _os2web_esdh_provider_cron_queue_worker($args) {
if (isset($args['meeting']) && isset($args['force'])) {
module_load_include('inc', 'os2web_esdh_provider', 'os2web_esdh_provider.mmapi');
_os2web_esdh_provider_import_meeting($args['meeting'], $args['force']);
}
elseif (isset($args['post_import_process']) &&
$args['post_import_process'] === TRUE &&
os2web_esdh_provider_supports('mm', 'post_import_process')) {
os2web_esdh_provider_invoke('mm', 'post_import_process');
}
}
/**
* Validate an API implementation for use.
*
* This function validates all required methods are available,
* and that only 1 implementation exists.
*
* @param string $api
* API Id
*
* @return bool
* True is API is implmemented and available.
*/
function os2web_esdh_provider_has_api($api) {
ctools_include('plugins');
$info = ctools_plugin_get_info('os2web_esdh_provider', $api);
if ($info) {
$plugins = ctools_get_plugins('os2web_esdh_provider', $api);
if (count($plugins) > 1) {
watchdog('esdh provider', 'API %api has multiple implementations active.', array('%api' => $api), WATCHDOG_ERROR);
return FALSE;
}
if (isset($info['required methods'])) {
foreach ($info['required methods'] as $method) {
if (!os2web_esdh_provider_supports($api, $method)) {
watchdog('esdh provider', 'Incomplete API found during check: %api', array('%api' => $api), WATCHDOG_ERROR);
return FALSE;
}
}
}
if (os2web_esdh_provider_supports($api, 'available')) {
if (!os2web_esdh_provider_invoke($api, 'available')) {
return FALSE;
}
}
}
else {
watchdog('esdh provider', 'Unimplemented API requested: %api', array('%api' => $api), WATCHDOG_ERROR);
return FALSE;
}
return TRUE;
}
/**
* Check if the api supports a given method.
*
* @param string $api
* API identifier
* @param string $method
* Method to invoke
*
* @return bool
* True if method is available
*/
function os2web_esdh_provider_supports($api, $method) {
ctools_include('plugins');
$plugin = ctools_get_plugins('os2web_esdh_provider', $api);
$plugin = array_shift($plugin);
if ($plugin) {
return function_exists(ctools_plugin_get_function($plugin, $method));
}
else {
return FALSE;
}
}
/**
* Invokes a method from an api.
*
* @return any
* Fowarded return value from method
*/
function os2web_esdh_provider_invoke() {
/* Expects:
* $arg[0] $api
* $arg[1] $method
* $ars[2:] additional arguments passed to the method
*/
$args = func_get_args();
$api = array_shift($args);
$method = array_shift($args);
ctools_include('plugins');
$plugin = ctools_get_plugins('os2web_esdh_provider', $api);
$plugin = array_shift($plugin);
if ($plugin) {
$function = ctools_plugin_get_function($plugin, $method);
if ($function) {
return call_user_func_array($function, $args);
}
watchdog('esdh_provider', 'Incomplete api implementation: method %method in api: %api called, but does not exists.', array(
'%method' => $method,
'%api' => $api,
), WATCHDOG_ERROR);
}
}
/* * *************** API METHODS BELOW HERE ************************************ */
/**
* Builds an array with required content for a case structure.
*/
function os2web_esdh_provider_default_case() {
return array(
// Int - CaseId.
'id' => NULL,
// String.
'access_code' => NULL,
// Int.
'restricted_from_public' => NULL,
);
}
/**
* Builds an array with required content for a document structure.
*/
function os2web_esdh_provider_default_document() {
return array(
// Int - DocId.
'id' => NULL,
// Int - CaseId.
'case_reference' => NULL,
// String.
'title' => NULL,
// String.
'last_update' => NULL,
// String.
'access_code' => NULL,
// Int.
'publication_indicator' => NULL,
// Bool.
'lock_status' => NULL,
);
}
/**
* Builds an array with required content for a meeting structure.
*/
function os2web_esdh_provider_default_meeting() {
return array(
// String.
'title' => NULL,
// Int.
'meeting_id' => NULL,
// Int.
'system_id' => NULL,
// Int.
'meeting_sub_id' => NULL,
// Boolean.
'publish' => NULL,
// Int - optional.
'publication_id' => NULL,
// Date.
'creation_date' => NULL,
// String.
'description' => NULL,
// Datetime.
'meeting_date_start' => NULL,
// Datetime.
'meeting_date_finish' => NULL,
// String.
'type' => NULL,
// String.
'committee' => NULL,
// String.
'location' => NULL,
// File.
'full_doc' => NULL,
// item_struct.
'items' => NULL,
);
}
/**
* Builds an array with required content for a meeting-item structure.
*/
function os2web_esdh_provider_default_item() {
return array(
// Int.
'id' => NULL,
// String.
'title' => NULL,
// String.
'access' => NULL,
// File[].
'enclosures' => NULL,
// bullet_struct.
'bullets' => NULL,
);
}
/**
* Builds an array with required content for a meeting-bullet structure.
*/
function os2web_esdh_provider_default_bullet() {
return array(
// String.
'title' => NULL,
// String.
'body' => NULL,
);
}
function os2web_esdh_provider_prod_check_alter(&$checks) {
$checks['os2web'] = array(
'title' => 'OS2Web',
'description' => 'Check various integration systems used in OS2Web, aswell as sanity check on settings.',
'functions' => array(
'_os2web_esdh_provider_import_check' => 'ESDH Meeting import status',
'_os2web_esdh_provider_pws_check' => 'ESDH case/document backend check',
'_os2web_esdh_provider_adlib_check' => 'AdLib PDF Conversion',
),
);
}
function _os2web_esdh_provider_import_check($caller = 'internal') {
$last_import = variable_get('os2web_esdh_provider_last_import', 0);
$last_import_hr = format_date($last_import);
$time_since = time() - $last_import;
if ($time_since < 3600) {
$error = 0;
$severity = ($caller == 'nagios') ? NAGIOS_STATUS_OK : PROD_CHECK_REQUIREMENT_OK;
}
elseif ($time_since < 3600 * 6) {
$error = 1;
$severity = ($caller == 'nagios') ? NAGIOS_STATUS_WARNING : PROD_CHECK_REQUIREMENT_WARNING;
}
else {
$error = 2;
$severity = ($caller == 'nagios') ? NAGIOS_STATUS_CRITICAL : PROD_CHECK_REQUIREMENT_ERROR;
}
$check['esdh_import_check'] = array(
'#title' => t('ESDH Import'),
'#state' => $error == 0,
'#severity' => $severity,
'#value_ok' => t('Working'),
'#value_nok' => $error == 1 ? t('Warning') : t('Error'),
'#description_ok' => t('Import was run within the last hour, at %time, which indicates that it is run correctly during cron.', array('%time' => $last_import_hr)),
'#description_nok' => t('Import has not been run recently. Lastly at %time This could be due to a faulty cron setup, you can run it manually Verify endpoint is properly set up in the <a href="/admin/config/os2web/esdh_provider">here</a>.', array('%time' => $last_import_hr)),
'#nagios_key' => 'OS2',
'#nagios_type' => 'state',
);
return prod_check_execute_check($check, $caller);
}
function _os2web_esdh_provider_pws_check($caller = 'internal') {
$status = os2web_esdh_provider_has_api('cm');
if ($status) {
$severity = ($caller == 'nagios') ? NAGIOS_STATUS_OK : PROD_CHECK_REQUIREMENT_OK;
}
else {
$severity = ($caller == 'nagios') ? NAGIOS_STATUS_CRITICAL : PROD_CHECK_REQUIREMENT_ERROR;
}
$check['esdh_pws_check'] = array(
'#title' => t('ESDH Webservices'),
'#state' => $status,
'#severity' => $severity,
'#value_ok' => t('Connected'),
'#value_nok' => t('Disconnected'),
'#description_ok' => t('The case/document service is correctly configured and connected.'),
'#description_nok' => t('Could not connect to the case/document serivice. Verify endpoint is properly set up in the <a href="/admin/config/os2web/settings">settings</a>.'),
'#nagios_key' => 'OS2',
'#nagios_type' => 'state',
);
return prod_check_execute_check($check, $caller);
}
function _os2web_esdh_provider_adlib_check($caller = 'internal') {
$status = FALSE;
$severity = ($caller == 'nagios') ? NAGIOS_STATUS_CRITICAL : PROD_CHECK_REQUIREMENT_ERROR;
$msg = 'AdLib service seems to be connected and running with %jobs active jobs.';
$jobs = 0;
if (drupal_load('module', 'os2web_adlib_api')) {
$adlib = _os2web_adlib_api_get_instance();
$statuslist = $adlib->getConnectorStatusList();
if (is_array($statuslist)) {
foreach ($statuslist as $cstatus) {
if ($cstatus->ServerDetail['Enabled'] == 'True') {
// A Server is available.
$status = TRUE;
$severity = ($caller == 'nagios') ? NAGIOS_STATUS_OK : PROD_CHECK_REQUIREMENT_OK;
$jobs += $cstatus->ServerInfo['JobCount'];
}
}
}
else {
// No available connectors.
$msg = $statuslist . ' Check endpoint setup in <a href="/admin/config/os2web/settings">settings</a>.';
}
}
else {
// Module not available.
$msg = 'AdLib module not available. Check <a href="/admin/modules">module list</a>';
}
$check['esdh_adlib_check'] = array(
'#title' => t('AdLib webservice'),
'#state' => $status,
'#severity' => $severity,
'#value_ok' => t('Connected'),
'#value_nok' => t('Disconnected'),
'#description_ok' => t($msg, array('%jobs' => $jobs)),
'#description_nok' => t($msg),
'#nagios_key' => 'OS2',
'#nagios_type' => 'state',
);
return prod_check_execute_check($check, $caller);
}
/**
* Implements hook_help().
*/
function o2web_esdh_provider_help($path, $arg) {
switch ($path) {
case 'admin/help#o2web_esdh_provider':
// TODO.
return t('Missing documentation.');
}
}
/**
* Implements hook_os2web_help().
*/
function o2web_esdh_provider_os2web_help($sections) {
// Module specific.
$sections['list_of_content'] .= t('<a href="#os2web_esdh_provider">OS2web ESDH Provider</a><br />');
$sections['o2web_esdh_provider'] = t('<h2 id="os2web_esdh_provider">The ESDH Provider:</h2>');
$sections['o2web_esdh_provider'] .= t('<b>Description:</b> The ESDH provider links your page with your esdh system. To see if your esdh link is up running, see the <a href="@url" target="_blank">status page</a>.', array('@url' => url('admin/config/os2web/esdh_provider')));
$sections['configuration'] .= t('<b>Configuration:</b> Set up who is allowed to import new meetings from the <a href="@url" target="_blank">OS2web Settings Page (Referat Import)</a>.', array('@url' => url('admin/config/os2web/settings')));
// Import.
$sections['import'] = t('<b>Meeting Import:</b> Import meetings trough the esdh provider. Press the [Run Import] at the <a href="@url" target="_blank">ESDH page</a>.', array('@url' => url('admin/config/os2web/esdh_provider')));
return $sections;
}