forked from ec-europa/nexteuropa-newsroom-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexteuropa_newsroom.admin.inc
1054 lines (954 loc) · 37.2 KB
/
nexteuropa_newsroom.admin.inc
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Nexteuropa Newsroom administration page.
*/
/**
* Nexteuropa newsroom admin form.
*
* @param array $form
* Form array.
* @param array $form_state
* Form state array.
*
* @return array
* Form array.
*/
function nexteuropa_newsroom_admin_settings(array $form, array &$form_state) {
// Be aware about the selected content types in the fields fieldset.
$top_ct = NexteuropaNewsroomHelper::getFieldInstances('topic');
$newsroom_universe_id = NexteuropaNewsroomHelper::getUniverseId();
// Get some information basing on the activation of the universe id.
if (!empty($newsroom_universe_id)) {
$universe_id_info['disabled'] = TRUE;
$universe_id_info['description'] = t('To change the Newsroom Universe ID contact the site administrator.');
$universe_id_info['class'] = 'selected';
}
else {
$universe_id_info['disabled'] = FALSE;
$universe_id_info['description'] = t('After setting the Newsroom Universe ID for the first time content will be imported from the Newsroom service. This might take a few minutes.');
$universe_id_info['class'] = 'not-selected';
}
// This is special, made just to hold the universe_id field.
$form['universe_id'] = [
'#type' => 'container',
'#title' => t('Newsroom Universe ID'),
'#attributes' => ['class' => [$universe_id_info['class']]],
];
$form['universe_id']['nexteuropa_newsroom_universe_id'] = [
'#type' => 'textfield',
'#title' => t('Universe ID value'),
'#required' => TRUE,
'#disabled' => $universe_id_info['disabled'],
'#description' => $universe_id_info['description'],
'#default_value' => $newsroom_universe_id,
'#element_validate' => ['_nexteuropa_newsroom_admin_universe_id_validate'],
];
// Other settings for the newsroom.
$form['newsroom_others'] = [
'#type' => 'fieldset',
'#title' => t('Newsroom client settings'),
'#description' => t("These settings will determine newsroom behavior in your website.
In particular you can choose here whether to import multilingual content from the newsroom or not.
Mind the fact that we rely on the enabled languages in your website, so you will not be able to import items in a language that doesn't exist yet."),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
];
$form['newsroom_others']['importers_link'] = [
'#type' => 'item',
'#markup' => l(t('Feed importers'), 'import'),
];
$form['newsroom_others']['nexteuropa_newsroom_subsite'] = [
'#type' => 'textfield',
'#title' => t('Name of the subsite, if any.'),
'#description' => t('The value you enter here will be used to filter the items belonging to this website.'),
'#default_value' => variable_get('nexteuropa_newsroom_subsite'),
'#element_validate' => ['_nexteuropa_newsroom_admin_subsite_validate'],
];
$form['newsroom_others']['nexteuropa_newsroom_use_css'] = [
'#type' => 'checkbox',
'#title' => t('Use the stylesheets provided by this feature'),
'#default_value' => variable_get('nexteuropa_newsroom_use_css', 1),
];
$form['newsroom_others']['nexteuropa_newsroom_legacy'] = [
'#type' => 'checkbox',
'#title' => t('If you have custom css code related to the newsroom maintain the legacy html code.'),
'#default_value' => variable_get('nexteuropa_newsroom_legacy', 1),
];
$types = NexteuropaNewsroomVocabularyHelper::getNewsroomTypes();
if (!empty($types)) {
$form['newsroom_paths'] = [
'#type' => 'fieldset',
'#title' => t('Newsroom content path settings'),
'#prefix' => '<div id="newsroom_reset">',
'#suffix' => '</div>',
'#description' => t("You can define here a root path for your items basing on the item type. The default behavior is to have sections correponding to the types, so if you have two types like
news and events you will get pattern like news/[node:title], events/[node:title], you can override the root path for your elements here. Do not use leading or trailing slashes"),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
];
$form['newsroom_paths']['nexteuropa_newsroom_patterns'] = [
'#type' => 'checkbox',
'#title' => t('Do you want to take over the configuration of pattern for node aliases?'),
'#description' => t('if this is unchecked the newsroom will not alter the aliases for its nodes.'),
'#default_value' => variable_get('nexteuropa_newsroom_patterns', FALSE),
];
foreach ($types as $type) {
$type_name = pathauto_cleanstring($type->name);
$parents = taxonomy_get_parents($type->tid);
$parent = '';
if (!empty($parents)) {
$parent = reset($parents);
$parent = pathauto_cleanstring($parent->name) . '/';
}
$path_var_name = 'nexteuropa_newsroom_' . $type_name . '_root';
$form['newsroom_paths'][$path_var_name] = [
'#type' => 'textfield',
'#title' => t('Pattern for %type', ['%type' => $type_name]),
'#required' => FALSE,
'#description' => t('Aliases pattern for the %type_name topic.', ['%type_name' => $type_name]),
'#default_value' => variable_get($path_var_name, $parent . $type_name),
'#element_validate' => ['_nexteuropa_newsroom_admin_type_root_validate'],
];
}
$form['newsroom_paths']['reset'] = [
'#type' => 'submit',
'#value' => 'Reset to the defaults',
'#ajax' => [
'callback' => '_nexteuropa_newsroom_admin_reset_paths',
'event' => 'click',
'wrapper' => 'newsroom_reset',
'effect' => 'fade',
'method' => 'replace',
],
];
}
// Settings about the newsroom fields.
$form['newsroom_fields'] = [
'#type' => 'fieldset',
'#title' => t('Newsroom fields settings'),
'#description' => t('Here you can easily manage the addition or the removal of the newsroom fields from your content types. Refer to the <a href="@documentation">documentation</a> for having detailed information about the usage of the newsroom fields. They are meant to give you the chance to associate topics to you contents so that matching news coming from the newsroom can be shown or mentioned contextually.', ['@documentation' => 'https://webgate.ec.europa.eu/fpfis/wikis/pages/viewpage.action?spaceKey=MULTISITE&title=%5BFPFIS%5D+Nexteuropa+Newsroom+-+Documentation']),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
];
$form['newsroom_fields']['nexteuropa_newsroom_content_types_topic'] = [
'#type' => 'checkboxes',
'#title' => t('Direct topic relation field in your content types'),
'#description' => t('Choose the content types in which you want to activate a relationship with a newsroom topic.'),
'#options' => _nexteuropa_newsroom_prepare_options('ct'),
'#multiple' => TRUE,
'#default_value' => $top_ct,
];
$form['newsroom_fields']['nexteuropa_newsroom_content_types_delete_topic'] = [
'#type' => 'checkbox',
'#title' => t('Delete all the topic relation fields in your content types.'),
'#description' => t('If you want to delete all the instance of the topic field together check this.'),
'#default_value' => 0,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
// Settings about the universe.
$form['newsroom_universe'] = [
'#type' => 'fieldset',
'#title' => t('Newsroom universe settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['newsroom_universe']['nexteuropa_newsroom_app'] = [
'#type' => 'textfield',
'#title' => t('Newsroom app'),
'#default_value' => variable_get('nexteuropa_newsroom_app'),
];
$form['newsroom_universe']['nexteuropa_newsroom_app_key'] = [
'#type' => 'textfield',
'#title' => t('Newsroom key'),
'#default_value' => variable_get('nexteuropa_newsroom_app_key'),
];
$form['newsroom_universe']['nexteuropa_newsroom_subscription_url'] = [
'#type' => 'textfield',
'#title' => t('Newsroom subscription URL'),
'#default_value' => variable_get('nexteuropa_newsroom_subscription_url'),
];
$form['newsroom_universe']['nexteuropa_newsroom_base_url'] = [
'#type' => 'textfield',
'#title' => t('Newsroom universe base URL'),
'#default_value' => variable_get('nexteuropa_newsroom_base_url', NEXTEUROPA_NEWSROOM_BASE_URL),
'#description' => t('The base url of newsroom feeds.'),
];
$form['newsroom_universe']['nexteuropa_newsroom_allowed_ips'] = [
'#type' => 'textfield',
'#title' => t('IP addresses allowed for import'),
'#default_value' => variable_get('nexteuropa_newsroom_allowed_ips', '127.0.0.1,158.167.165.192,158.167.165.191,158.167.209.222,158.167.240.147,158.167.209.223,158.167.210.159,158.167.210.158'),
'#description' => t('Comma separated list of IP addresses where the importer can be launched from.'),
'#maxlength' => 512,
];
$form['newsroom_universe']['nexteuropa_newsroom_item_import_script'] = [
'#type' => 'textfield',
'#title' => t('Item import script name'),
'#default_value' => variable_get('nexteuropa_newsroom_item_import_script', NEXTEUROPA_NEWSROOM_ITEM_SCRIPT),
];
$form['newsroom_universe']['nexteuropa_newsroom_single_item_import_segment'] = [
'#type' => 'textfield',
'#title' => t('URL chunk for single item import'),
'#default_value' => variable_get('nexteuropa_newsroom_single_item_import_segment', NEXTEUROPA_NEWSROOM_ITEM_SEGMENT),
];
$form['newsroom_universe']['nexteuropa_newsroom_topic_import_script'] = [
'#type' => 'textfield',
'#title' => t('Topic import script name'),
'#default_value' => variable_get('nexteuropa_newsroom_topic_import_script', NEXTEUROPA_NEWSROOM_TOPIC_SCRIPT),
];
$form['newsroom_universe']['nexteuropa_newsroom_single_topic_import_segment'] = [
'#type' => 'textfield',
'#title' => t('URL chunk for single topic import'),
'#default_value' => variable_get('nexteuropa_newsroom_single_topic_import_segment', NEXTEUROPA_NEWSROOM_TOPIC_SEGMENT),
];
$form['newsroom_universe']['nexteuropa_newsroom_type_import_script'] = [
'#type' => 'textfield',
'#title' => t('Type import script name'),
'#default_value' => variable_get('nexteuropa_newsroom_type_import_script', NEXTEUROPA_NEWSROOM_TYPE_SCRIPT),
];
$form['newsroom_universe']['nexteuropa_newsroom_single_type_import_segment'] = [
'#type' => 'textfield',
'#title' => t('URL chunk for single type import'),
'#default_value' => variable_get('nexteuropa_newsroom_single_type_import_segment', NEXTEUROPA_NEWSROOM_TYPE_SEGMENT),
];
// For topics and services we use the same script name.
$form['newsroom_universe']['nexteuropa_newsroom_service_import_script'] = [
'#type' => 'textfield',
'#title' => t('Service import script name'),
'#default_value' => variable_get('nexteuropa_newsroom_service_import_script', NEXTEUROPA_NEWSROOM_TOPIC_SCRIPT),
];
$form['newsroom_universe']['nexteuropa_newsroom_single_service_import_segment'] = [
'#type' => 'textfield',
'#title' => t('URL chunk for single service import'),
'#default_value' => variable_get('nexteuropa_newsroom_single_service_import_segment', NEXTEUROPA_NEWSROOM_SERVICE_SEGMENT),
];
$form['newsroom_universe']['nexteuropa_newsroom_proposal_script'] = [
'#type' => 'textfield',
'#title' => t('Newsroom proposal script'),
'#default_value' => variable_get('nexteuropa_newsroom_proposal_script', NEXTEUROPA_NEWSROOM_PROPOSAL_SCRIPT),
];
$form['newsroom_universe']['nexteuropa_newsroom_docsroom_url'] = [
'#type' => 'textfield',
'#title' => t('Newsroom Docsroom URL'),
'#default_value' => variable_get('nexteuropa_newsroom_docsroom_url'),
];
// Page settings.
$form['newsroom_pages'] = [
'#type' => 'fieldset',
'#title' => t('Newsroom page settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['newsroom_pages']['nexteuropa_newsroom_newsletter_privacy'] = [
'#type' => 'textarea',
'#title' => t('Privacy statement'),
'#default_value' => variable_get('nexteuropa_newsroom_newsletter_privacy'),
];
$form['newsroom_pages']['nexteuropa_newsroom_featured_type'] = [
'#type' => 'select',
'#title' => t('Featured newsroom type'),
'#options' => NexteuropaNewsroomHelper::getNewsroomTypeOptions(FALSE),
'#default_value' => variable_get('nexteuropa_newsroom_featured_type'),
];
// Settings about the newsroom blocks.
$form['newsroom_blocks'] = [
'#type' => 'fieldset',
'#title' => t('Newsroom blocks settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['newsroom_blocks']['nexteuropa_newsroom_order_by_last_update'] = [
'#type' => 'checkbox',
'#title' => t('Sort newsroom boxes chronologically in the summary blocks.'),
'#default_value' => variable_get('nexteuropa_newsroom_order_by_last_update', 0),
];
$form['newsroom_blocks']['nexteuropa_newsroom_display_highlights_begin'] = [
'#type' => 'checkbox',
'#title' => t('Highlighted topics are displayed at the begining of the list.'),
'#default_value' => variable_get('nexteuropa_newsroom_display_highlights_begin', 1),
];
$form['newsroom_blocks']['nexteuropa_newsroom_agenda_after_highlights'] = [
'#type' => 'checkbox',
'#title' => t('Display Agenda after the highlights.'),
'#default_value' => variable_get('nexteuropa_newsroom_agenda_after_highlights', 1),
];
$form['newsroom_blocks']['nexteuropa_newsroom_summary_block_num_items'] = [
'#type' => 'select',
'#title' => t('Number of items in boxes in the summary blocks'),
'#options' => _nexteuropa_newsroom_prepare_options(5),
'#default_value' => variable_get('nexteuropa_newsroom_summary_block_num_items', 3),
];
$form['newsroom_blocks']['nexteuropa_newsroom_summary_block_num_highlighted_items'] = [
'#type' => 'select',
'#title' => t('Number of items in boxes of highlighted types in the summary blocks'),
'#options' => _nexteuropa_newsroom_prepare_options(6),
'#default_value' => variable_get('nexteuropa_newsroom_summary_block_num_highlighted_items', 3),
];
$form['newsroom_blocks']['home_blocks'] = [
'#type' => 'fieldset',
'#title' => t('Newsroom home blocks settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
];
// Settings about newsroom blocks in the home page.
$form['newsroom_blocks']['home_blocks']['nexteuropa_newsroom_summary_home_block_num_items'] = [
'#type' => 'select',
'#title' => t('Number of items in boxes in the summary block on the homepage'),
'#options' => _nexteuropa_newsroom_prepare_options(5),
'#default_value' => variable_get('nexteuropa_newsroom_summary_home_block_num_items', 3),
];
$form['newsroom_blocks']['home_blocks']['nexteuropa_newsroom_summary_home_block_num_highlighted_items'] = [
'#type' => 'select',
'#title' => t('Number of items in boxes of highlighted types in the summary block on the homepage'),
'#options' => _nexteuropa_newsroom_prepare_options(6),
'#default_value' => variable_get('nexteuropa_newsroom_summary_home_block_num_highlighted_items', 3),
];
$form['newsroom_blocks']['home_blocks']['nexteuropa_newsroom_days_number_for_new_user'] = [
'#type' => 'textfield',
'#title' => t('Number of days from now where we check new newsroom items'),
'#default_value' => variable_get('nexteuropa_newsroom_days_number_for_new_user', 14),
];
$form['viewmodes'] = [
'#type' => 'fieldset',
'#title' => 'Newsroom View Modes',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['viewmodes']['nexteuropa_newsroom_events_type_id'] = [
'#type' => 'textfield',
'#title' => t('Event type ID'),
'#description' => t('Newsroom event type ID'),
'#default_value' => variable_get('nexteuropa_newsroom_events_type_id'),
'#size' => 10,
];
$form['viewmodes']['nexteuropa_newsroom_fundings_type_id'] = [
'#type' => 'textfield',
'#title' => t('Fundings type ID'),
'#description' => t('Newsroom fundings type ID'),
'#default_value' => variable_get('nexteuropa_newsroom_fundings_type_id'),
'#size' => 10,
];
$form['viewmodes']['nexteuropa_newsroom_consultations_type_id'] = [
'#type' => 'textfield',
'#title' => t('Consultations type ID'),
'#description' => t('Newsroom consultations type ID'),
'#default_value' => variable_get('nexteuropa_newsroom_consultations_type_id'),
'#size' => 10,
];
$form['viewmodes']['nexteuropa_newsroom_use_default_view_mode'] = [
'#type' => 'checkbox',
'#title' => t('Use the newsroom default view mode'),
'#default_value' => variable_get('nexteuropa_newsroom_use_default_view_mode', 1),
];
$form['viewmodes']['nexteuropa_newsroom_redirect_short_items'] = [
'#type' => 'checkbox',
'#title' => t('Redirect short items'),
'#default_value' => variable_get('nexteuropa_newsroom_redirect_short_items', 0),
];
// Attach a css to the admin settings form.
$form['#attached']['css'] = [
drupal_get_path('module', 'nexteuropa_newsroom') . '/styles/nexteuropa_newsroom_admin_setting.css',
];
// Validate function.
$form['#validate'][] = '_nexteuropa_newsroom_admin_settings_form_validate';
// Submit handler.
$form['#submit'][] = '_nexteuropa_newsroom_admin_settings_form_submit';
if ($newsroom_universe_id) {
$form['actions']['run'] = [
'#type' => 'submit',
'#value' => t('Run importers'),
'#submit' => ['_nexteuropa_newsroom_import_importers'],
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
'#weight' => 9,
];
$form['actions']['delete_items'] = [
'#type' => 'submit',
'#value' => t('Delete existing items'),
'#submit' => ['_nexteuropa_newsroom_delete_items'],
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
'#weight' => 10,
];
$form['actions']['rebuild'] = [
'#type' => 'submit',
'#value' => t('Rebuild importers'),
'#submit' => ['_nexteuropa_newsroom_admin_rebuild_importers'],
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
'#weight' => 11,
];
$form['actions']['clean_files'] = [
'#type' => 'submit',
'#value' => 'Clean files',
'#submit' => ['_nexteuropa_newsroom_admin_clean_files'],
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
'#weight' => 12,
];
$form['actions']['clean_cache'] = [
'#type' => 'submit',
'#value' => 'Clean cache',
'#submit' => ['_nexteuropa_newsroom_admin_clean_cache'],
'#access' => user_access(NEXTEUROPA_NEWSROOM_ADMIN_ACCESS),
'#weight' => 12,
];
}
// Define the fields_form array.
$form_state['fields_form'] = [];
$form_state['fields_form']['topic'] = $top_ct;
// Confirmation step_needed.
if (!empty($form_state['storage']['confirm']) && empty($form_state['confirm']['processed'])) {
return _nexteuropa_newsroom_admin_delete_confirm($form, $form_state);
}
// Normal form.
else {
return system_settings_form($form);
}
}
/**
* Deletes files duplication.
*/
function _nexteuropa_newsroom_admin_clean_files() {
$operations = [];
$result = db_select('field_data_field_newsroom_illustrative_img')
->fields(NULL, ['entity_id', 'field_newsroom_illustrative_img_fid'])
->condition('bundle', NEXTEUROPA_NEWSROOM_CONTENT_TYPE)
->orderBy('entity_id', 'DEC')
->execute()->fetchAll();
if (!empty($result)) {
$cache_key = 'get_agenda:clean_files';
$items = [];
if ($cache = cache_get($cache_key, NEXTEUROPA_NEWSROOM_CACHE_TABLE)) {
$items = $cache->data;
}
foreach ($result as $nid) {
// We cleaned it already. Skip it.
if (in_array($nid->entity_id, $items)) {
continue;
}
$operations[] = [
'_nexteuropa_newsroom_file_cleanup_batch_process',
[
$nid->entity_id,
],
];
}
}
$query = db_select('file_managed', 'fm');
$query->fields('fm', ['fid']);
$query->join('file_usage', 'fu', 'fm.fid = fu.fid');
$query->leftJoin('node', 'n', "n.nid = fu.id AND n.type = '" . NEXTEUROPA_NEWSROOM_CONTENT_TYPE . "'");
$query->condition('fu.type', 'node');
$query->condition('fm.type', 'image');
$query->condition('fm.uri', '%newsroom%', 'LIKE');
$query->isNull('n.nid');
$result = $query->execute()->fetchAll();
if (!empty($result)) {
foreach ($result as $fid) {
$operations[] = [
'_nexteuropa_newsroom_remove_file_batch_process',
[
$fid->fid,
],
];
}
}
if (!empty($operations)) {
$batch = [
'operations' => $operations,
'title' => t('Clean up files'),
'init_message' => t('Starting...'),
'error_message' => t('An error occurred'),
'finished' => '_nexteuropa_newsroom_file_cleanup_batch_finish',
];
// Start the batch job.
batch_set($batch);
}
else {
drupal_set_message(t('Nothing to do'), 'status');
}
}
/**
* Validation step for the the newsroom admin form.
*
* @param array $form
* Form array.
* @param array $form_state
* Form state array.
*/
function _nexteuropa_newsroom_admin_settings_form_validate(array $form, array &$form_state) {
// If we are not during a confirm or this has happened right before.
if (empty($form_state['storage']['confirm']) && empty($form_state['confirm']['processed'])) {
$check_values = _nexteuropa_newsroom_admin_prepare_check($form_state['fields_form'], $form_state['values']);
// Check every topic instance if requested.
foreach (['topic'] as $field_item) {
if ($check_values['delete'][$field_item] == 1) {
$test = _nexteuropa_newsroom_admin_check_deletion($field_item, NEXTEUROPA_NEWSROOM_ALL);
}
// If there are less values than before, check instances.
elseif (!empty($check_values['check'][$field_item][0])) {
$test = _nexteuropa_newsroom_admin_check_deletion($field_item, $check_values['check'][$field_item][0]);
}
}
if (!empty($test)) {
$form_state['storage']['confirm'] = TRUE;
$form_state['confirm']['values'] = $test;
}
}
}
/**
* Submit handler for the newsroom admin form.
*
* @param array $form
* Form array.
* @param array $form_state
* Form state array.
*/
function _nexteuropa_newsroom_admin_settings_form_submit(array $form, array &$form_state) {
$universe_id = NexteuropaNewsroomHelper::getUniverseId();
if (empty($universe_id) && $form_state['values']['nexteuropa_newsroom_universe_id']) {
variable_set('nexteuropa_newsroom_universe_id', $form_state['values']['nexteuropa_newsroom_universe_id']);
_nexteuropa_newsroom_admin_rebuild_importers();
}
$patterns = !empty($form_state['values']['patterns']) && $form_state['values']['patterns'] == 1 ? TRUE : FALSE;
$keys = array_keys($form_state['values']);
$rootpaths = preg_grep('/rootpath/', $keys);
// Shouldn't be needed, it is a setting form.
if (!empty($rootpaths)) {
foreach ($rootpaths as $type) {
$typename = str_replace('_rootpath', '', $type);
variable_set('newsroom_' . $typename . '_root', $form_state['values'][$type]);
}
}
$patterns ? variable_set('nexteuropa_newsroom_patterns', 1) : variable_set('nexteuropa_newsroom_patterns', 0);
// If the validation returns the confirm request.
if (!empty($form_state['storage']['confirm']) && empty($form_state['confirm']['processed'])) {
$form_state['rebuild'] = TRUE;
}
else {
$check_values = _nexteuropa_newsroom_admin_prepare_check($form_state['fields_form'], $form_state['values']);
// Remove every topic instance if requested.
foreach (['topic'] as $field_item) {
if ($check_values['delete'][$field_item] == 1) {
_nexteuropa_newsroom_admin_remove_fields($field_item, NEXTEUROPA_NEWSROOM_ALL);
}
elseif (!empty($check_values['selected'][$field_item]) && !empty($check_values['check'][$field_item][1])) {
// Add new instances.
_nexteuropa_newsroom_admin_add_fields($field_item, $check_values['check'][$field_item][1]);
}
elseif (!empty($check_values['check'][$field_item][0])) {
// Remove instances.
_nexteuropa_newsroom_admin_remove_fields($field_item, $check_values['check'][$field_item][0]);
}
}
cache_clear_all('newsroom:', 'cache', TRUE);
}
}
/**
* Add a confirmation step if needed.
*
* @param array $form
* Form array.
* @param array $form_state
* Form state array.
*
* @return array
* Confirmation form array.
*/
function _nexteuropa_newsroom_admin_delete_confirm(array $form, array &$form_state) {
$form_state['confirm']['processed'] = TRUE;
$values = $form_state['confirm']['values'];
$desc = '<div class="messages error"><h3>' . t('Part of the form has not been submitted yet. You have:') . '</h3></br><i>';
foreach ($values as $field => $nodes) {
for ($i = 0; $i < count($values[$field]); $i++) {
$desc .= t('@num nodes containing values for the field @field in the content type @ct.',
[
'@num' => $values[$field][$i]['nodes'],
'@field' => $field,
'@ct' => $values[$field][$i]['ct'],
]
) . '<br/>';
}
}
$desc .= '</i>' . t('If you continue these information could be lost.') . '</p>';
$form['process'] = ['#type' => 'hidden', '#value' => 'true'];
return confirm_form($form, 'Confirmation needed for removing field instances with values in the current database', 'admin/config/content/newsroom', check_markup($desc, 'full_html'), 'Continue', 'Restart');
}
/**
* Checks universe ID for valid value.
*
* @param array $element
* Form element.
* @param array $form_state
* Form state.
*/
function _nexteuropa_newsroom_admin_universe_id_validate(array $element, array &$form_state) {
if (!NexteuropaNewsroomHelper::validateUniverse($form_state['values']['nexteuropa_newsroom_universe_id'])) {
form_error($element, t('You must enter a valid Newsroom Universe ID.'));
}
}
/**
* Checks subsite for valid value.
*
* @param array $element
* Form element.
* @param array $form_state
* Form state.
*/
function _nexteuropa_newsroom_admin_subsite_validate(array $element, array &$form_state) {
if (!NexteuropaNewsroomHelper::validateSubsite($form_state['values']['nexteuropa_newsroom_subsite'])) {
form_error($element, t("This subsite doesn't seem to exist, please check the value you entered."));
}
}
/**
* Validates newsroom URL patterns.
*
* @param array $element
* Form element.
* @param array $form_state
* Form state.
*/
function _nexteuropa_newsroom_admin_type_root_validate(array $element, array &$form_state) {
$value = preg_replace('@^/|/$@', '', $element['#value']);
$value_array = explode('/', $value);
$pattern = '';
for ($i = 1; $i <= count($value_array); $i++) {
$segment = pathauto_cleanstring($value_array[$i - 1]);
$pattern .= $segment;
if ($i < count($value_array) && $segment != '') {
$pattern .= '/';
}
}
if ($form_state['values'][$element['#name']] != $pattern) {
drupal_set_message(
t('The value for the %el has been modified. BEFORE: %or ::: AFTER: %mod',
[
'%el' => $element['#name'],
'%or' => $form_state['values'][$element['#name']],
'%mod' => $pattern,
]),
'warning'
);
$form_state['values'][$element['#name']] = $pattern;
}
}
/**
* Callback: Resets values in the path patterns field for the newsroom items.
*
* @param array $form
* Form array.
* @param array $form_state
* Form state.
*
* @return array
* Pathes.
*/
function _nexteuropa_newsroom_admin_reset_paths(array $form, array &$form_state) {
foreach (NexteuropaNewsroomVocabularyHelper::getNewsroomTypes() as $type) {
$type_name = pathauto_cleanstring($type->name);
$parents = taxonomy_get_parents($type->tid);
$parent = '';
if (!empty($parents)) {
$parent = reset($parents);
$parent = pathauto_cleanstring($parent->name) . '/';
}
$form['newsroom_paths'][$type_name . '_rootpath']['#value'] = $parent . $type_name;
}
return $form['newsroom_paths'];
}
/**
* Cleans newsroom cache - Ajax callback.
*/
function _nexteuropa_newsroom_admin_clean_cache() {
cache_clear_all('*', NEXTEUROPA_NEWSROOM_CACHE_TABLE, TRUE);
drupal_set_message(t('Cache has been cleaned.'));
}
/**
* Adds fields to selected content types.
*
* @param string $type
* Select or topic fo the moment.
* @param array $sel_ct
* Array of content types machine names.
*/
function _nexteuropa_newsroom_admin_add_fields($type = 'topic', array $sel_ct = []) {
$field_name = 'field_newsroom_associated_' . $type;
if (!empty($sel_ct)) {
foreach ($sel_ct as $machine_name => $content_type) {
$exist = field_info_instance('node', $field_name, $machine_name);
// Check for existing instances.
if ($exist === NULL) {
$instance = [
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => $machine_name,
'label' => 'Newsroom ' . $type,
'description' => '',
'required' => 0,
];
field_create_instance($instance);
// Call the field_group helper function, we surely have to update it.
_nexteuropa_newsroom_admin_field_group($machine_name);
// Set a message to inform the user about the field instance creation.
drupal_set_message(t('Created instance of @field in the @bundle content type.', ['@field' => $field_name, '@bundle' => $machine_name]));
}
}
}
}
/**
* Creates a field_group to hold the newsroom fields in a vertical tab.
*
* @param string $machine_name
* Content type machine name.
*/
function _nexteuropa_newsroom_admin_field_group($machine_name) {
// Get info about the group, it's likely to be already in the database.
$groups = field_group_info_groups('node', $machine_name, 'form', TRUE);
$fields = ['topic'];
$group_name = 'group_newsroom_fields';
$instances = FALSE;
// Check for instances of the newsroom fields inside the given content type.
foreach ($fields as $field) {
$field_name = 'field_newsroom_associated_' . $field;
if (field_info_instance('node', $field_name, $machine_name) != NULL) {
$instances[] = field_info_instance('node', $field_name, $machine_name);
}
}
// If the group is already there, remove it.
if (!empty($groups[$group_name])) {
db_delete('field_group')
->condition('bundle', $machine_name, '=')
->condition('group_name', $group_name, '=')
->execute();
}
// Create the group with the right children.
if ($instances) {
$field_group = (object) [
'identifier' => $group_name . '|node|' . $machine_name . '|form',
'group_name' => $group_name,
'entity_type' => 'node',
'bundle' => $machine_name,
'mode' => 'form',
'children' => [],
'parent_name' => '',
'weight' => 5,
'label' => 'Newsroom fields',
'format_type' => 'tab',
'disabled' => FALSE,
'format_settings' => [
'instance_settings' => [
'required_fields' => 0,
'classes' => 'group-newsroom field-group-tab',
'description' => '',
],
'formatter' => 'closed',
],
];
foreach ($instances as $instance) {
$field_group->children[] = $instance['field_name'];
}
field_group_group_save($field_group);
}
else {
drupal_set_message(t('Removed the fieldgroup @name from the @content_type content type.', ['@name' => $group_name, '@content_type' => $machine_name]), 'status');
}
}
/**
* Removes all the instances of a fields.
*
* @param string $type
* Select or topic fo the moment.
* @param array $sel_ct
* Array of content types machine names.
*/
function _nexteuropa_newsroom_admin_remove_fields($type = 'topic', array $sel_ct = []) {
// This function runs after checking for existing field values in the databas.
$field_name = 'field_newsroom_associated_' . $type;
// All is the parameter we get when an user want to delete all the instances.
if ($sel_ct == NEXTEUROPA_NEWSROOM_ALL) {
$instances = NexteuropaNewsroomHelper::getFieldInstances($type);
}
// Limit the search to the selected content types.
elseif (!empty($sel_ct)) {
foreach ($sel_ct as $content_type) {
$instances[] = $content_type;
}
}
if (!empty($instances)) {
// Loop through the instances to delete them.
foreach ($instances as $instance) {
$instance = field_info_instance('node', $field_name, $instance);
field_delete_instance($instance, FALSE);
// Call the field_group helper function, we could have to delete it.
_nexteuropa_newsroom_admin_field_group($instance['bundle'], $field_name);
// Add the message to queue.
drupal_set_message(t('Deleted instance of @field in the @bundle content type.', ['@field' => $field_name, '@bundle' => $instance['bundle']]));
}
}
}
/**
* Checks if it safe to delete field instances.
*
* @param string $type
* Select or topic fo the moment.
* @param array $checks
* Array of items to check.
*
* @return array
* Information about the field name and nodes found.
*/
function _nexteuropa_newsroom_admin_check_deletion($type, array $checks = []) {
$nodes = [];
// "All" is the parameter we get to delete all the instances.
if ($checks == NEXTEUROPA_NEWSROOM_ALL) {
$instances = NexteuropaNewsroomHelper::getFieldInstances($type);
}
elseif (!empty($checks)) {
// Get the instances to check for.
foreach ($checks as $check) {
$instances[] = $check;
}
}
if (!empty($instances)) {
foreach ($instances as $instance) {
// Check in the database for values of the selected field.
$query = db_select('field_data_field_newsroom_associated_' . $type, 'ch');
$query->condition('bundle', $instance, '=');
$num_rows = $query->countQuery()->execute()->fetchField();
// There are values, we store the number of nodes to show it to the user.
if ($num_rows > 0) {
$nodes['field_data_field_newsroom_associated_' . $type][] = [
'ct' => $instance,
'nodes' => $num_rows,
];
}
}
}
return $nodes;
}
/**
* Prepares check for instances deletion.
*
* @param array $previous
* Old values.
* @param array $input
* New values.
*
* @return array
* Variables to pass.
*/
function _nexteuropa_newsroom_admin_prepare_check(array $previous, array $input) {
// Set some values.
$selected['topic'] = array_filter($input['nexteuropa_newsroom_content_types_topic']);
$check_topic = [];
// Check the two array for differences. we need to catch also a deselection.
if ($selected != $previous) {
$check_topic[] = array_diff($previous['topic'], $selected['topic']);
$check_topic[] = array_diff($selected['topic'], $previous['topic']);
}
// Prepare all the relevant data formatted into an [).
$infos = [
'selected' => [
'topic' => $selected['topic'],
],
'delete' => [
'topic' => $input['nexteuropa_newsroom_content_types_delete_topic'],
],
'check' => [
'topic' => $check_topic,
],
];
return $infos;
}
/**
* Rebuilds multilingual importers.
*/
function _nexteuropa_newsroom_admin_rebuild_importers() {
NexteuropaNewsroomImporterHelper::rebuildImporters();
drupal_set_message(t('Newsroom importers have been successfully generated.'));
}
/**
* Runs all importers.
*/
function _nexteuropa_newsroom_import_importers() {
// Run the multilingual imports if required.
$importers = NexteuropaNewsroomImporterHelper::returnImporters();
foreach ($importers as $importer) {
NexteuropaNewsroomImporterHelper::runFeedImporter($importer);
}
}
/**
* Submit handler.
*
* @param array $form
* Form array.
* @param array $form_state
* Form state array.
*/
function _nexteuropa_newsroom_delete_items_confirm_submit(array $form, array &$form_state) {
if ($form_state['values']['confirm']) {
// Delete the imported items.
$importers = NexteuropaNewsroomImporterHelper::returnImporters();
foreach ($importers as $importer) {
NexteuropaNewsroomImporterHelper::deleteItems($importer);
}
drupal_set_message(t('Newsroom data has been removed.'));
}
$form_state['redirect'] = 'admin/config/content/newsroom';
}
/**
* Confirmation form page callback.
*