-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathos2web_cp_service.module
970 lines (890 loc) · 33.3 KB
/
os2web_cp_service.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
<?php
/**
* @file
* Code for the OS2Web Case Publishing Service feature.
*/
include_once 'os2web_cp_service.features.inc';
/**
* Implements hook_init().
*/
function os2web_cp_service_init() {
// Menu Minipanels us using qTip. Only add qTip js if module doesnt exits.
if (!module_exists('menu_minipanels')) {
drupal_add_js(drupal_get_path('module', 'os2web_cp_service') . '/js/jquery.qtip-1.0.0-rc3.min.js');
}
drupal_add_js(drupal_get_path('module', 'os2web_cp_service') . '/js/os2web_cp_service.js');
drupal_add_css(drupal_get_path('module', 'os2web_cp_service') . '/css/os2web_cp_service.css');
}
/**
* Implements hook_menu().
*/
function os2web_cp_service_menu() {
$items = array();
$items['admin/config/os2web_cp_service'] = array(
'title' => 'OS2Web CP Service',
'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_cp_service/settings'] = array(
'title' => 'Settings',
'description' => 'Configure OS2Web CP Service',
'access arguments' => array('administer os2web'),
'page callback' => 'drupal_get_form',
'page arguments' => array('os2web_cp_service_settings'),
'type' => MENU_NORMAL_ITEM,
);
$items['os2web/service/cp/v1'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'os2web_cp_service_handler',
'access callback' => TRUE,
);
$items['os2web/service/gf/v1/%'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'os2web_gf_service_handler',
'page arguments' => array(4),
'access callback' => TRUE,
);
return $items;
}
/**
* Callback for Case Publishing service. Forwards handling to provided plugin.
*/
function os2web_cp_service_handler() {
if (!os2web_esdh_provider_has_api('cp')) {
drupal_access_denied();
}
$ip_string = variable_get('os2web_cp_service_cp_access_ip', '');
$valid_ips = array_map('trim', explode(',', $ip_string));
if (!in_array(ip_address(), $valid_ips)) {
drupal_access_denied();
}
$data = os2web_esdh_provider_invoke('cp', 'handle_request');
return $data;
}
/**
* Callback for the file provider service.
*/
function os2web_gf_service_handler($file_id) {
// Mime Types which are disallowed to be downloaded.
// People shouldn't be able to download special files.
$disallowed_mimes = array(
// Disallow .msg files.
'application/vnd.ms-outlook',
);
$disallowed_mimes = array_merge($disallowed_mimes, explode(',', variable_get('os2web_cp_service_disallow_mimes', '')));
$case_exceptions = explode(',', variable_get('os2web_cp_service_disallow_mimes_exceptions', ''));
if ($url = variable_get('os2web_cp_service_cp_document_fileurl')) {
$username = variable_get('os2web_cp_service_endpoint_user');
$password = variable_get('os2web_cp_service_endpoint_password');
if (!empty($username) && !empty($password)) {
// CURL for transfer:
$ch = curl_init($url . '?FileID=' . $file_id . '&FileFormat=Available');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
$data = curl_exec($ch);
$header = curl_getinfo($ch);
curl_close($ch);
if ($header['http_code'] === 200) {
// Load the title to use it as the filename, and for conditions.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'os2web_cp_service_cp_document')
->propertyCondition('status', 1)
->fieldCondition('field_os2web_cp_service_file_id', 'value', $file_id, '=')
->execute();
$nids = (isset($result['node']))?array_keys($result['node']) : NULL;
$node = node_load(array_pop($nids));
$case_id = NULL;
if ($node) {
$case_id = $node->field_os2web_cp_service_case_id[LANGUAGE_NONE][0]['value'];
}
if (!in_array($header['content_type'], $disallowed_mimes) || ($case_id && in_array($case_id, $case_exceptions))) {
drupal_add_http_header('Content-Type', $header['content_type']);
drupal_add_http_header('Content-Length', $header['download_content_length']);
drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
drupal_add_http_header('Cache-Control', 'private', FALSE);
drupal_add_http_header('Connection', 'close');
drupal_add_http_header('Expires', '0');
// Check for IE only headers.
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)) {
drupal_add_http_header('Pragma', 'public');
}
else {
drupal_add_http_header('Pragma', 'no-cache');
}
$filename = str_replace('/', '_', $file_id . '.' . os2web_cp_service_get_extension_from_mime($header['content_type']));
if ($node) {
// If we can find the doc in DB, give it a real name.
$filename = str_replace('/', '_', $node->field_os2web_cp_service_doc_id[LANGUAGE_NONE][0]['value'] . '.' . os2web_cp_service_get_extension_from_mime($header['content_type']));
}
drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $filename);
echo $data;
drupal_exit();
}
else {
// Show a polite message if the file isnt allowed for download.
// If the message isnt set in config, deliver a access denied page.
if ($error_message = variable_get('os2web_cp_service_access_denied_message')) {
$markup = '<div class="messages error"><ul><li>';
$markup .= $error_message;
$markup .= '</li></ul></div>';
$page['region'] = array(
'#type' => 'markup',
'#markup' => $markup,
);
return $page;
}
}
}
}
else {
error_log(basename(__FILE__) . ':' . __LINE__ . ' Settings are wrong.');
}
}
else {
error_log(basename(__FILE__) . ':' . __LINE__ . ' No URL found in variable DB');
}
// If we come to this line, show an error.
$markup = '<div class="messages error"><ul><li>';
$markup .= t('Det ønskede dokument kunne ikke hentes, hvilket kan skyldes opdatering af løsningen. Prøv venligst igen senere.');
$markup .= '</li></ul></div>';
$page['region'] = array(
'#type' => 'markup',
'#markup' => $markup,
);
return $page;
}
/**
* Function to control/alter any configurations before added to the variable table.
*
* @param string $key
* The key of the configuration.
* @param string $value
* The value of the conf, which has to be altered/stored.
*/
function os2web_cp_service_alter_configuration($key, $value) {
switch ($key) {
case 'FileServiceURL':
return variable_set('os2web_cp_service_cp_document_fileurl', $value);
break;
}
}
/**
* Service function for creating a case.
*
* @param array $data
* Structured array with data for the Case.
*
* @return bool
* TRUE on successful content creation.
*/
function os2web_cp_service_create_case(array $data) {
// Prepare data.
$data = array_replace_recursive(os2web_cp_service_default_case(), $data);
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'os2web_cp_service_cp_case')
->fieldCondition('field_os2web_cp_service_sysid', 'value', $data['fields']['SystemID'], '=')
->execute();
$nids = (isset($result['node']))?array_keys($result['node']) : NULL;
$node = new stdClass();
if (count($nids) > 1) {
if (is_array($nids)) {
node_delete_multiple($nids);
}
}
else {
$node = node_load(array_pop($nids));
}
// See if the triggered rules on the published node, is approved
// in the vocabulary. Otherwise the node will be unpublished.
$approved_vocabulary = taxonomy_vocabulary_machine_name_load('os2web_cp_service_tax_approved_rules');
$approved_rules = taxonomy_get_tree($approved_vocabulary->vid);
$approved = FALSE;
foreach ($data['rules'] as $trigger_rule) {
foreach ($approved_rules as $approved_rule) {
if ($approved_rule->name === $trigger_rule['tag']) {
$approved = TRUE;
break;
}
}
}
$node->type = 'os2web_cp_service_cp_case';
$node->uid = 0;
$node->status = $approved;
$node->comment = 0;
$node->promote = 0;
$node->moderate = 0;
$node->sticky = 0;
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
switch ($data['fields']['Sagstype - Sagstype']) {
case 'EJSAG':
// Old cases comes as: "Birkevej (8410) 11, 8410 Rønde"
// regex removes " (8410)"
$node->title = (string) preg_replace('/\s\(\d{4}\)/', '', $data['fields']['Sagstitel']) . ' - ' . $data['fields']['Sagsindhold'];
break;
default:
$node->title = (string) !empty($data['fields']['Sagstitel'])?$data['fields']['Sagstitel'] : $data['fields']['Officiel titel'];
break;
}
// Has all fields of a node type "case".
$node_fields = field_info_instances('node', 'os2web_cp_service_cp_case');
foreach ($node_fields as $field_name => $field) {
if (!empty($data['fields'][$field['label']]) && $field_name !== 'title') {
$field_info = field_info_field($field_name);
switch ($field_info['type']) {
case 'text':
if ($field['label'] === 'Sagstitel' && $data['fields']['Sagstype - Sagstype'] === 'EJSAG') {
$node->{$field_name}[LANGUAGE_NONE][0]['value'] = (string) preg_replace('/\s\(\d{4}\)/', '', $data['fields']['Sagstitel']);
}
else {
$node->{$field_name}[LANGUAGE_NONE][0]['value'] = (string) $data['fields'][$field['label']];
}
break;
case 'taxonomy_term_reference':
$choosen_term = NULL;
// Search of the term in KLE vocabulary.
if (strpos($field['label'], 'KL emnenummer') !== FALSE) {
// TODO: Use os2web_taxonomies_kle.
$vocabulary = taxonomy_vocabulary_machine_name_load('kle');
$terms = taxonomy_get_tree($vocabulary->vid);
foreach ($terms as $term) {
// Does the term start with the numbers xx.xx.xx.
if (strpos($term->name, substr($data['fields'][$field['label']], 0, strpos($data['fields'][$field['label']], ' '))) !== FALSE) {
$choosen_term = $term->name;
break;
}
}
}
// If non found, find in every vocabularies.
// Retrieve the first occurance of the term.
if (empty($choosen_term)) {
$choosen_term = array_pop(taxonomy_get_term_by_name((string) $data['fields'][$field['label']], NULL));
}
if (!empty($choosen_term)) {
// Note that if its an update of an existing node,
// it will ńot be resat.
$node->{$field_name}['und'][0]['tid'] = $term->tid;
}
break;
default:
$node->{$field_name}[LANGUAGE_NONE][0]['value'] = $data['fields'][$field['label']];
break;
}
}
}
unset($node->field_os2web_cp_service_rules[LANGUAGE_NONE]);
foreach ($data['rules'] as $rule) {
$term = taxonomy_get_term_by_name($rule['name'], 'os2web_cp_service_tax_rules');
if (empty($term)) {
$rules_vocabulary = taxonomy_vocabulary_machine_name_load('os2web_cp_service_tax_rules');
$term = new stdClass();
$term->vid = $rules_vocabulary->vid;
$term->name = $rule['name'];
taxonomy_term_save($term);
}
if (is_array($term)) {
$term = array_pop($term);
}
$node->field_os2web_cp_service_rules[LANGUAGE_NONE][]['tid'] = $term->tid;
}
try {
node_submit($node);
node_save($node);
return TRUE;
}
catch (Exception $e) {
error_log('Error on save: ' . print_r($e->getMessage(), 1));
return FALSE;
}
}
/**
* Default array structure for a case.
*/
function os2web_cp_service_default_case() {
return array(
'type' => 'case',
'rules' => array(),
'status' => NULL,
'fields' => array(
'SagsID' => NULL,
'SystemID' => NULL,
'Sagsnummer' => NULL,
// 'Sagsår' => NULL,
'Sagsløbenummer' => NULL,
// PERSAG, EJSAG, Akassesag, Børn- og ungesag.
'Sagstype - Sagstype' => NULL,
// Personsag, Ejendomssag, Akassesag, Børn- og ungesag.
'Sagstype - Sagstypebetegnelse' => NULL,
'Sagsundertype - Sagsundertype' => NULL,
'Sagstitel' => NULL,
'Officiel titel' => NULL,
'Sagsindhold' => NULL,
'CPR' => NULL,
'Matrikel' => NULL,
'Ejendomsnummer' => NULL,
'Sagsstatus - Sagstype' => NULL,
'Sagsstatus - Sagstypebetegnelse' => NULL,
'Sagsstatus - Afsluttet' => NULL,
'Sagsstatus - Passiv' => NULL,
'Sagsbehandler - BrugerID' => NULL,
'Sagsbehandler - Bruger logon' => NULL,
'Sagsbehandler - Brugernavn' => NULL,
'Sagsansvarlig enhed - ID' => NULL,
'Sagsansvarlig enhed - Administrativ enhed forkortelse' => NULL,
'Sagsansvarlig enhed - Administrativ enhed' => NULL,
'Sagsansvarlig enhed - Administrativ enhed beskrivelse' => NULL,
'Sagsdato' => NULL,
'Oprettelsesdato' => NULL,
'Redigeringsdato' => NULL,
'Sidste dokument' => NULL,
'Afslutningsdato' => NULL,
'Kassationsdato' => NULL,
'Kassationskode - Kassationskode' => NULL,
'Kassationskode - Kassationskodebetegnelse' => NULL,
'Slettekode' => NULL,
'Adgangskode - Adgangskode' => NULL,
'Adgangskode - Adgangskodebetegnelse' => NULL,
'Adgangsgruppe - AdgangsgruppeID' => NULL,
'Adgangsgruppe - Adgangsgruppe' => NULL,
'Aktindsigt - AktindsigtsID' => NULL,
'Aktindsigt - Aktindsigt' => NULL,
'Arkivdel - Arkivdel' => NULL,
'Arkivdel - Arkivdelbetegnelse' => NULL,
'Journalenhed - Journalenhed' => NULL,
'Journalenhed - Journalenhedsbetegnelse' => NULL,
'Papirsag' => NULL,
'KL emnenummer - KL emnenummer' => NULL,
'KL emnenummer - KL emnenbeskrivelse' => NULL,
'KL emnenummer - KL emnenummer og beskrivelse' => NULL,
'KL handlingsfacet - KL handlingsfacet' => NULL,
'KL handlingsfacet - KL handlingsfacetbeskrivelse' => NULL,
'KL handlingsfacet - KL handlingsfacet og beskrivelse' => NULL,
'OBS-dato' => NULL,
'OBS-note' => NULL,
'Sagsfrist' => NULL,
'Sagsfrist note' => NULL,
'Projekt - Projekt' => NULL,
'Sagsgruppe - SagsgruppeID' => NULL,
'Sagsgruppe - Sagsgruppe' => NULL,
'Afgørelse - AfgørelsesID' => NULL,
'Afgørelse - Afgørelse' => NULL,
'Prioritet - ID' => NULL,
'Prioritet - Prioritet' => NULL,
'Sagsfase - SagsfaseID' => NULL,
'Sagsfase - Sagsfase' => NULL,
'Sagsfasefrist' => NULL,
'Afventer - AfventerID' => NULL,
'Afventer - Afventer' => NULL,
'Klient køn - ID' => NULL,
'Klient køn - Klient køn' => NULL,
'Klient fødselsdato' => NULL,
'Klient alder ved registrering' => NULL,
'Klient faggruppe - ID' => NULL,
'Klient faggruppe - Klient faggruppe' => NULL,
'Klient henvendelse - ID' => NULL,
'Klient henvendelse - Klient henvendelse' => NULL,
'Klient faglig vurdering - ID' => NULL,
'Klient faglig vurdering - Klient faglig vurdering' => NULL,
'Klient foranstaltningsforslag - ID' => NULL,
'Klient foranstaltningsforslag - Klient foranstaltningsforslag' => NULL,
'Klient afgørelse - ID' => NULL,
'Klient afgørelse - Klient afgørelse' => NULL,
'Klient kategori - ID' => NULL,
'Klient kategori - Klient kategori' => NULL,
'Klient klassetrin - ID' => NULL,
'Klient klassetrin - Klient klassetrin' => NULL,
'Klient forældremyndighed - ID' => NULL,
'Klient forældremyndighed - Klient forældremyndighed' => NULL,
'Klient note' => NULL,
),
);
}
/**
* Service function for creating a document.
*
* @param array $data
* Structured array with data for the Case.
*
* @return bool
* TRUE on successful content creation.
*/
function os2web_cp_service_create_document(array $data) {
$data = array_replace_recursive(os2web_cp_service_default_document(), $data);
// Prepare data.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'os2web_cp_service_cp_document')
->fieldCondition('field_os2web_cp_service_sysid', 'value', $data['fields']['SystemID'], '=')
->execute();
$node = new stdClass();
$nids = (isset($result['node']))?array_keys($result['node']) : array();
if (count($nids) > 1) {
if (is_array($nids)) {
node_delete_multiple($nids);
}
}
else {
$node = node_load(array_pop($nids));
}
// See if the triggered rules on the published node, is approved
// in the vocabulary. Otherwise the node will be unpublished.
$vocabulary = taxonomy_vocabulary_machine_name_load('os2web_cp_service_tax_approved_rules');
$approved_rules = taxonomy_get_tree($vocabulary->vid);
$approved = FALSE;
foreach ($data['rules'] as $trigger_rule) {
foreach ($approved_rules as $approved_rule) {
if ($approved_rule->name === $trigger_rule['tag']) {
$approved = TRUE;
break;
}
}
}
$node->type = 'os2web_cp_service_cp_document';
$node->uid = 0;
$node->status = $approved;
$node->comment = 0;
$node->promote = 0;
$node->moderate = 0;
$node->sticky = 0;
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
$node->title = (string) !empty($data['fields']['Dokumenttitel'])?$data['fields']['Dokumenttitel'] : $data['fields']['Officiel titel'];
// Has all fields of a node type "document".
$node_fields = field_info_instances('node', 'os2web_cp_service_cp_document');
foreach ($node_fields as $field_name => $field) {
if (!empty($data['fields'][$field['label']]) && $field_name !== 'title') {
$field_info = field_info_field($field_name);
switch ($field_info['type']) {
case 'text':
$node->{$field_name}[LANGUAGE_NONE][0]['value'] = (string) $data['fields'][$field['label']];
break;
default:
$node->{$field_name}[LANGUAGE_NONE][0]['value'] = $data['fields'][$field['label']];
break;
}
}
}
unset($node->field_os2web_cp_service_rules[LANGUAGE_NONE]);
foreach ($data['rules'] as $rule) {
$term = taxonomy_get_term_by_name($rule['name'], 'os2web_cp_service_tax_rules');
if (empty($term)) {
$rules_vocabulary = taxonomy_vocabulary_machine_name_load('os2web_cp_service_tax_rules');
$term = new stdClass();
$term->vid = $rules_vocabulary->vid;
$term->name = $rule['name'];
taxonomy_term_save($term);
}
if (is_array($term)) {
$term = array_pop($term);
}
$node->field_os2web_cp_service_rules[LANGUAGE_NONE][]['tid'] = $term->tid;
}
try {
node_submit($node);
node_save($node);
// Trigger a cache reload by calling the URL for the node.
$cache_trigger = curl_init(url('dok/' . $node->field_os2web_cp_service_doc_id[LANGUAGE_NONE][0]['value'], array('absolute' => TRUE)));
// Stop the call after 1 sec. Dont care about the response.
curl_setopt($cache_trigger, CURLOPT_TIMEOUT, 1);
curl_exec($cache_trigger);
curl_close($cache_trigger);
}
catch (Exception $e) {
error_log('Error on save: ' . print_r($e->getMessage(), 1));
return FALSE;
}
// Update reference on cases.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'os2web_cp_service_cp_case')
->fieldCondition('field_os2web_cp_service_sysid', 'value', $data['fields']['Sag SystemID'], '=')
->execute();
if (!empty($result['node'])) {
$nids = array_keys($result['node']);
foreach ($nids as $nid) {
$cnode = node_load($nid);
if (!empty($cnode)) {
$node->field_os2web_cp_service_case_id[LANGUAGE_NONE][]['value'] = $cnode->field_os2web_cp_service_case_id[LANGUAGE_NONE][0]['value'];
node_save($node);
$is_missing = TRUE;
foreach ($cnode->field_os2web_cp_service_doc_ref[LANGUAGE_NONE] as $value) {
if ($value['target_id'] == $node->nid) {
$is_missing = FALSE;
}
}
if ($is_missing) {
$cnode->field_os2web_cp_service_doc_ref[LANGUAGE_NONE][]['target_id'] = $node->nid;
node_save($cnode);
}
}
}
}
return TRUE;
}
/**
* Default array structure for a case.
*/
function os2web_cp_service_default_document() {
return array(
'type' => 'document',
'rules' => array(),
'status' => NULL,
'fields' => array(
'Opfølgning påkrævet' => NULL,
'Journaldato' => NULL,
'Dokumentdato' => NULL,
'Oprettelsesdato' => NULL,
'Redigeringsdato' => NULL,
'Dokumentfrist' => NULL,
'FilRedigeringsdato' => NULL,
'SystemID' => NULL,
'Sag SystemID' => NULL,
'Dokumentnummer' => NULL,
'Dokumentløbenummer' => NULL,
'Journalår' => NULL,
'Lagringsform - ID' => NULL,
'Sagsbehandler - BrugerID' => NULL,
'Aktindsigt - AktindsigtsID' => NULL,
'Adgangsgruppe - AdgangsgruppeID' => NULL,
'FilID' => NULL,
'Filversion' => NULL,
'SagsID' => NULL,
'Indhold_FileID' => NULL,
'Journalnummer' => NULL,
'Dokumenttitel' => NULL,
'Officiel titel' => NULL,
'Indholdsbeskrivelse' => NULL,
'Lagringsform - Lagringsform' => NULL,
'Dokumenttype - Dokumenttype' => NULL,
'Dokumenttype - Dokumenttypebetegnelse' => NULL,
'Kategori - Kategori' => NULL,
'Kategori - Kategoribetegnelse' => NULL,
'Sagsbehandler - Bruger logon' => NULL,
'Sagsbehandler - Brugernavn' => NULL,
'Aktindsigt - Aktindsigt' => NULL,
'Adgangskode - Adgangskode' => NULL,
'Adgangskode - Adgangskodebetegnelse' => NULL,
'Adgangsgruppe - Adgangsgruppe' => NULL,
'Journalstatus - Journalstatus' => NULL,
'Journalstatus - Journalstatusbetegnelse' => NULL,
'Dokumentstatus - Dokumentstatus' => NULL,
'Dokumentstatus - Dokumentstatusbetegnelse' => NULL,
'Dokumentfrist note' => NULL,
'Arkivdel - Arkivdel' => NULL,
'Arkivdel - Arkivdelbetegnelse' => NULL,
'Filformat' => NULL,
'Låst' => NULL,
),
);
}
/**
* Service function to delete an Case or Document.
*
* @param string $guid
* The key of the node stored in field_os2web_cp_service_key
*
* @return bool
* Success
*/
function os2web_cp_service_delete($guid) {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->fieldCondition('field_os2web_cp_service_key', 'value', $guid, '=')
->execute();
$nids = (isset($result['node']))?array_keys($result['node']) : array();
try {
if (count($nids) > 1) {
if (is_array($nids)) {
node_delete_multiple($nids);
}
}
else {
node_delete(array_pop($nids));
}
return TRUE;
}
catch (Exception $e) {
error_log('Error on delete: ' . print_r($e->getMessage(), 1));
return FALSE;
}
}
/**
* Implements hook_node_delete().
*/
function os2web_cp_service_node_delete($node) {
if ($node->type == 'os2web_cp_service_cp_document') {
// Prepare data.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', 'os2web_cp_service_cp_case')
->fieldCondition('field_os2web_cp_service_doc_ref', 'target_id', $node->nid, '=')
->execute();
if (!empty($result['node'])) {
$nids = array_keys($result['node']);
$new_nids = array();
foreach ($nids as $nid) {
$cnode = node_load($nid);
foreach ($cnode->field_os2web_cp_service_doc_ref[LANGUAGE_NONE] as $value) {
if ($value['target_id'] != $node->nid) {
$new_nids[] = $value;
}
}
$cnode->field_os2web_cp_service_doc_ref[LANGUAGE_NONE] = $new_nids;
node_save($cnode);
}
}
}
}
/**
* Implements hook_field_formatter_info().
*/
function os2web_cp_service_field_formatter_info() {
return array(
'os2web_cp_service_file_formatter' => array(
'label' => t('OS2Web Filelink'),
'field types' => array('text'),
'settings' => array(
// Base url up which we'll insert the file id.
'base_url' => 'os2web/service/gf/v1/!id',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function os2web_cp_service_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$element = array();
$element['base_url'] = array(
'#type' => 'textfield',
'#title' => t('Base URL'),
'#description' => t('Base URL where to request the file. !id will be substituded for the actual ID.'),
'#default_value' => $display['settings']['base_url'],
);
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function os2web_cp_service_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = t('Make a link to a file with a base url of @url', array(
'@url' => $settings['base_url'],
));
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function os2web_cp_service_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
if ($display['type'] != 'os2web_cp_service_file_formatter') {
return;
}
$base_url = $display['settings']['base_url'];
$element = array();
foreach ($items as $delta => $item) {
$file_id = $item['value'];
$url = format_string($base_url, array('!id' => $file_id));
$element[$delta]['#markup'] = l(t('Download'), $url);
}
return $element;
}
/**
* Implements hook_foorm_alter().
*/
function os2web_cp_service_form_alter(&$form, &$form_state, $form_id) {
switch ($form['#id']) {
case 'views-exposed-form-os2web-cp-service-cp-case-search-page':
case 'views-exposed-form-os2web-cp-service-cp-case-search-panel-pane-1':
case 'views-exposed-form-os2web-cp-service-cp-case-search-default':
foreach ($form['#info'] as &$widget) {
// Adds placeholder to all textfields with a description [email protected].
if (!empty($widget['description']) && isset($form[$widget['value']]['#type']) && $form[$widget['value']]['#type'] === 'textfield') {
$form[$widget['value']]['#attributes']['placeholder'] = $widget['description'];
}
if (isset($form[$widget['value']]['value']) && $form[$widget['value']]['value']['#type'] === 'date_popup') {
$form[$widget['value']]['value']['#date_format'] = 'd-m-Y';
}
}
break;
case 'search-form':
$form['#suffix'] .= '<div class="link-aaben-indsigt">Søger du efter sager i vores sagssystem, kan du bruge vores <a href="/aaben-indsigt">Åben Indsigt</a>.</div>';
break;
}
}
/**
* Implements theme_preprocess_views_exposed_form().
*/
function os2web_cp_service_preprocess_views_exposed_form(&$variables) {
// Wrap certain elements in search form. Used for styling etc.
if ($variables['form']['#id'] == 'views-exposed-form-os2web-cp-service-cp-case-search-page' ||
$variables['form']['#id'] == 'views-exposed-form-os2web-cp-service-cp-case-search-panel-pane-1' ||
$variables['form']['#id'] == 'views-exposed-form-os2web-cp-service-cp-case-search-default') {
foreach ($variables['widgets'] as $id => &$widget) {
switch ($id) {
case 'filter-field_os2web_cp_service_date_value_1':
$widget->prefix = '<div class="filter-widgets-wrapper clearfix">';
break;
case 'filter-field_os2web_cp_service_date_value_2':
$widget->suffix = '<input type="submit" name="" value="Søg" class="form-submit"><div class="os2web_cp_service-ajax-loader ajax-loader"></div></div>';
break;
case 'filter-field_os2web_cp_service_case_id_value':
$widget->prefix = '<div class="filter-widgets-wrapper clearfix">';
$widget->suffix = '<input type="submit" name="" value="Find sag" class="form-submit"></div>';
break;
case 'filter-field_os2web_cp_service_doc_id_value':
$widget->prefix = '<div class="filter-widgets-wrapper clearfix">';
$widget->suffix = '<input type="submit" name="" value="Find dokument" class="form-submit"></div>';
break;
}
}
$cal_icon = drupal_get_path('module', 'os2web_cp_service') . "/images/cal.png";
$datepicker = 'jQuery(document).ready(function($) {
$( "#edit-field-os2web-cp-service-date-from-value-datepicker-popup-0" ).datepicker({
showOn: "both",
buttonImage: "' . $cal_icon . '",
buttonImageOnly: true,
dateFormat: "dd-mm-yy"
});
$( "#edit-field-os2web-cp-service-date-to-value-datepicker-popup-0" ).datepicker({
showOn: "both",
buttonImage: "' . $cal_icon . '",
buttonImageOnly: true,
dateFormat: "dd-mm-yy"
});
});';
drupal_add_js($datepicker,
array(
'type' => 'inline',
'scope' => 'footer',
'weight' => 5,
)
);
}
}
/**
* Implements hook_theme_registry_alter().
*/
function os2web_cp_service_theme_registry_alter(&$theme_registry) {
// Add the tpl for exposed form to the theme registry
// Somehow this gets overridden by os2web_core_theme.
if (!empty($theme_registry['views_exposed_form__os2web_cp_service_cp_case_search']['path'])) {
$theme_registry['views_exposed_form__os2web_cp_service_cp_case_search']['path'] = drupal_get_path('module', 'os2web_cp_service') . '/theme/';
}
}
/**
* Implements hook_form_alter().
*
* Add configuration to the settings form.
*/
function os2web_cp_service_settings() {
$form = array();
// CP Configuration.
$form['os2web_cp_service_config_group'] = array(
'#type' => 'fieldset',
'#title' => t('CP File Endpoint configuration'),
);
$form['os2web_cp_service_config_group']['os2web_cp_service_endpoint_user'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('os2web_cp_service_endpoint_user'),
'#title' => t('CP File endpoint user'),
'#description' => t('CP File endpoint HTTP authentification user.'),
);
$form['os2web_cp_service_config_group']['os2web_cp_service_endpoint_password'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('os2web_cp_service_endpoint_password'),
'#title' => t('CP File endpoint password'),
'#description' => t('CP File endpoint HTTP authentification password.'),
);
$form['os2web_cp_service_config_group']['os2web_cp_service_cp_access_ip'] = array(
'#type' => 'textfield',
'#title' => 'IP addresser der kan tilgå publiserings webservicen',
'#description' => 'Komma separeret liste af ip-addresser der kan tilgå <em>webservicen</em>.',
'#default_value' => variable_get('os2web_cp_service_cp_access_ip', ip_address()),
);
$form['os2web_cp_service_config_group']['os2web_cp_service_access_denied_message'] = array(
'#type' => 'textfield',
'#title' => 'Besked til brugeren, hvis fil ikke er tilgængelig.',
'#description' => 'Vises når den modtagede filtype ikke er godkendt til Download.',
'#default_value' => variable_get('os2web_cp_service_access_denied_message'),
);
$form['os2web_cp_service_config_group']['os2web_cp_service_disallow_mimes'] = array(
'#type' => 'textfield',
'#title' => 'Filers MIME type, som ikke må downloades af brugere.',
'#description' => 'Tillad kun filer som ikke har disse mimes. Seperer mimes med komma. Standard:.msg',
'#default_value' => variable_get('os2web_cp_service_disallow_mimes'),
);
$form['os2web_cp_service_config_group']['os2web_cp_service_disallow_mimes_exceptions'] = array(
'#type' => 'textfield',
'#title' => 'Sager hvor ALLE mimes er tilladt.',
'#description' => 'Tillad brugere at hente ALLE mime typer på angivende sager. Seperer sager med komma.',
'#default_value' => variable_get('os2web_cp_service_disallow_mimes_exceptions'),
);
return system_settings_form($form);
}
/**
* Implements hook_date_formats().
*/
function os2web_cp_service_date_formats() {
return array(
array(
'type' => 'os2web_cp_service_date_only',
'format' => 'Y',
'locales' => array(),
),
);
}
/**
* Implements hook_date_format_types().
*/
function os2web_cp_service_date_format_types() {
// Define the core date format types.
return array(
'os2web_cp_service_date_only' => t('Date Only'),
);
}
/**
* Turns a mime type to its corresponding file ext.
*
* @param string $mime
* The mime-type.
*
* @return string
* The file ext without the dot.
*/
function os2web_cp_service_get_extension_from_mime($mime) {
// Todo: use file_mimetype_mapping().
$map = array(
'application/pdf' => 'pdf',
'application/zip' => 'zip',
'image/gif' => 'gif',
'image/jpeg' => 'jpg',
'image/png' => 'png',
'text/css' => 'css',
'text/html' => 'html',
'text/javascript' => 'js',
'text/plain' => 'txt',
'text/xml' => 'xml',
);
if (isset($map[$mime])) {
return $map[$mime];
}
$pieces = explode('/', $mime);
return array_pop($pieces);
}