forked from SU-SWS/stanford_capx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstanford_capx.forms.inc
1987 lines (1680 loc) · 70.1 KB
/
stanford_capx.forms.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
* @author [author] <[email]>
*
* A collection of forms
*
*/
use CAPx\Drupal\Util\CAPx;
use CAPx\Drupal\Util\CAPxConnection;
use CAPx\Drupal\Util\CAPxMapper;
use CAPx\Drupal\Util\CAPxImporter;
use \Peekmo\JsonPath\JsonStore as JsonParser;
// /////////////////////////////////////////////////////////////////////////////
// CONNECT FORM
// /////////////////////////////////////////////////////////////////////////////
/**
* Connection details form builder.
*/
function stanford_capx_forms_connect_form($form, &$form_state) {
$username = CAPx::getAuthUsername();
$connection = CAPxConnection::testConnection();
$form['status'] = array(
'#type' => 'fieldset',
'#title' => t('Connection Status'),
'#collapsible' => TRUE,
);
$form['status']['status'] = array(
'#markup' => stanford_capx_connection_status_block(),
);
$form['auth'] = array(
'#type' => 'fieldset',
'#title' => t('Authorization'),
'#collapsible' => TRUE,
'#collapsed' => $connection->status,
);
$form['auth']['description'] = array(
'#markup' => t('Please enter your authentication information for the CAP API.'),
);
$form['auth']['stanford_capx_username'] = array(
'#type' => 'textfield',
'#title' => t('Client ID:'),
'#default_value' => $username,
'#required' => TRUE,
);
$form['auth']['stanford_capx_password'] = array(
'#type' => 'password',
'#title' => t('Password:'),
);
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Advanced setting for CAP API and authentication URIs'),
);
$form['advanced']['stanford_capx_api_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Endpoint'),
'#description' => t('CAP API endpoint URI, only useful when switching between development/production environment.'),
'#default_value' => CAPx::getAPIEndpoint(),
'#required' => TRUE,
);
$form['advanced']['stanford_capx_api_auth_uri'] = array(
'#type' => 'textfield',
'#title' => t('Authentication URI'),
'#description' => t('CAP API authentication URI.'),
'#default_value' => CAPx::getAuthEndpoint(),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save connection settings'),
);
return $form;
}
/**
* [stanford_capx_forms_connect_form_validate description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_forms_connect_form_validate($form, &$form_state) {
$username = $form_state['values']['stanford_capx_username'];
$password = $form_state['values']['stanford_capx_password'];
$endpoint = $form_state['values']['stanford_capx_api_base_url'];
$authpoint = $form_state['values']['stanford_capx_api_auth_uri'];
// If password changed validate auth.
if (!empty($password)) {
$auth_test = CAPxConnection::testAuthConnection($username, $password, $authpoint);
if (!$auth_test->status) {
form_set_error('stanford_capx_username', t('Could not authenticate with server. Invalid credentials. Please check your username and password.'));
form_set_error('stanford_capx_password');
}
}
// In case the end points change we need to get pass from db.
$password = !empty($password) ? $password : CAPx::getAuthPassword();
// If authpoint changed.
if ($authpoint !== "https://authz.stanford.edu/oauth/token") {
if (!isset($auth_test)) {
$auth_test = CAPxConnection::testAuthConnection($username, $password, $authpoint);
}
if (!$auth_test->status) {
form_set_error("stanford_capx_api_auth_uri", t("Please check your authorization url."));
}
}
// If endpoint changed.
if ($endpoint !== "https://api.stanford.edu") {
if (!isset($auth_test)) {
$auth_test = CAPxConnection::testAuthConnection($username, $password, $authpoint);
}
if (isset($auth_test->token)) {
$token = $auth_test->token;
$end_test = CAPxConnection::testApiConnection($token, $endpoint);
}
}
}
/**
* [stanford_capx_forms_connect_form_submit description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_forms_connect_form_submit($form, &$form_state) {
drupal_set_message(t('Configuration options were saved.'));
// Username and Pass.
if (!empty($form_state['values']['stanford_capx_username'])
&& !empty($form_state['values']['stanford_capx_password'])) {
$username = encrypt($form_state['values']['stanford_capx_username']);
$password = encrypt($form_state['values']['stanford_capx_password']);
variable_set('stanford_capx_username', $username);
variable_set('stanford_capx_password', $password);
}
// Endpoints.
variable_set('stanford_capx_api_base_url', $form_state['values']['stanford_capx_api_base_url']);
variable_set('stanford_capx_api_auth_uri', $form_state['values']['stanford_capx_api_auth_uri']);
// In order to get here we have to have a valid connection. Lets get the org codes
// and schema.
stanford_capx_schema_refresh();
stanford_capx_organizations_sync();
}
// /////////////////////////////////////////////////////////////////////////////
// END CONNECT FORM
// /////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////
// MAPPER FORMS
// /////////////////////////////////////////////////////////////////////////////
/**
* [stanford_capx_mapper_form description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_mapper_form($form, &$form_state, $mapper = NULL) {
$form = array();
$form += stanford_capx_mapper_form_get_machine_name_form($form, $form_state, $mapper);
$form += stanford_capx_mapper_form_get_entity_mapping_form($form, $form_state, $mapper);
$form += stanford_capx_mapper_form_get_field_mapping_form($form, $form_state, $mapper);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Save mapping',
);
if (isset($mapper)) {
$form['actions']['delete'] = array(
'#markup' => l(t('Delete'), 'admin/config/capx/mapper/delete/' . $mapper->getMachineName(), array("attributes" => array("class" => array("button")))),
);
}
$form['actions']['cancel'] = array(
'#markup' => l(t('Cancel'), 'admin/config/capx/mapper'),
);
return $form;
}
/**
* [stanford_capx_mapper_form_get_machine_name_form description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_mapper_form_get_machine_name_form($form, &$form_state, $mapper = NULL) {
$form['naming'] = array(
'#title' => t('Mapping title'),
'#type' => 'fieldset',
'#collapsed' => FALSE,
'#collapsible' => FALSE,
'#description' => t('Provide a human-readable name and machine name for this set of mapping configuration.'),
);
$form['naming']['title'] = array(
'#type' => 'textfield',
'#description' => isset($mapper->machine_name) ? t("This field has been disabled and cannot change once it has been set.") : t('Please enter a unique name for this mapper'),
'#default_value' => isset($mapper->title) ? $mapper->title : '',
'#disabled' => isset($mapper->machine_name),
);
$form['naming']['machine-name'] = array(
'#type' => 'machine_name',
'#title' => t('Machine name'),
'#default_value' => isset($mapper->machine_name) ? $mapper->machine_name : '',
'#size' => 64,
'#maxlength' => 64,
'#description' => isset($mapper->machine_name) ? t("This field has been disabled and cannot change once it has been set.") : t('A unique name for the mapping. It must only contain lowercase letters, numbers and hyphens.'),
'#machine_name' => array(
'exists' => 'stanford_capx_mapper_machine_name_exits',
'source' => array('naming','title'),
),
'#disabled' => isset($mapper->machine_name),
);
return $form;
}
/**
* [stanford_capx_mapper_form_get_entity_mapping_form description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_mapper_form_get_entity_mapping_form($form, &$form_state, $mapper = NULL) {
$entity_types = capx_entity_get_info();
$entity_options = array();
$bundle_options = array();
foreach ($entity_types as $entity_name => $values) {
$entity_options[$entity_name] = $values['label'];
$bundle_options[$entity_name] = array();
foreach($values['bundles'] as $bundle_machine_name => $bundle_info) {
$bundle_options[$entity_name][$bundle_machine_name] = $bundle_info['label'];
}
}
$form['entity-mapping'] = array(
'#type' => 'fieldset',
'#title' => t('Entity Mapping'),
'#description' => t('Select which entity type and bundle you would like to map CAP data into. Please select the entity type first and the bundle type will appear. The entity and bundle has to exist before creating a mapping to it.'),
'#collapsed' => FALSE,
'#collapsible' => FALSE,
);
$form['entity-mapping']['entity-type'] = array(
'#type' => 'select',
'#description' => isset($mapper->machine_name) ? t("This field has been disabled and cannot change once it has been set.") : t(''),
'#options' => $entity_options,
'#default_value' => isset($mapper->entity_type) ? $mapper->entity_type : 'node',
'#disabled' => isset($mapper->machine_name),
);
// Build out the bundles options.
foreach ($bundle_options as $entity_name => $bundle_opts) {
$form['entity-mapping']['bundle-'.$entity_name] = array(
'#type' => 'select',
'#title' => t('Select bundle'),
'#description' => isset($mapper->machine_name) ? t("This field has been disabled and cannot change once it has been set.") : t(''),
'#options' => $bundle_opts,
'#default_value' => isset($mapper->bundle_type) ? $mapper->bundle_type : 'stanford_person',
'#states' => array(
'visible' => array('select[name="entity-type"]' => array('value' => $entity_name)),
),
'#disabled' => isset($mapper->machine_name),
);
}
return $form;
}
/**
* [stanford_capx_mapper_form_get_field_mapping_form description]
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_mapper_form_get_field_mapping_form($form, &$form_state, $mapper = NULL) {
$entity_types = capx_entity_get_info();
$field_collections = array();
global $CAP_RESTRICTED_FIELD_PROPERTIES;
$form['field-mapping'] = array(
'#type' => 'fieldset',
'#title' => t('Field Mapping'),
'#description' => t(''),
'#collapsed' => FALSE,
'#collapsible' => FALSE,
'#tree' => TRUE,
);
$form['field-mapping']['help']['#markup'] = "<p>" . t("Using the fields below you are able to pull in data from the CAP API into your site. You do not have to input a setting for every field. Only use the fields that you want. The fields that you are importing data from the CAP API into will be locked and can only be changed by chaning the data on the CAP website.") . "</p>";
$form['field-mapping']['help']['#markup'] .= "<p>" . t('Use the data browser to find the information you would like to map into the various different properties and fields below. You will need to use JSONPath syntax. For more information on JSONPath please read the documentation. ') . l('JSONPath Documentation →', 'http://goessner.net/articles/JsonPath/', array('attributes' => array('target' => '_blank'))) . "</p>";
// ******************************************************
// **** WARNING ***** BIG HEAVY FORM ***** WARNING ******
// ******************************************************
// PROPERTIES
// ---------------------------------------------------------------------
foreach ($entity_types as $entity_machine_name => $entity_info_values) {
// If this is an edit mapper form (mapper exists) do not render the other
// entities or bundles.
if (isset($mapper->settings['entity_type']) && $mapper->settings['entity_type'] !== $entity_machine_name) {
continue;
}
// Get all the properties of the entity type.
$entity_info = entity_get_property_info($entity_machine_name);
$property_container = $entity_machine_name . "-properties";
// Generate property forms for each entity + bundle.
$form['field-mapping'][$property_container] = array(
'#type' => 'fieldset',
'#title' => t('@name Properties', array("@name" => $entity_machine_name)),
'#description' => t(''),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
'#states' => array(
'visible' => array(
'select[name="entity-type"]' => array('value' => $entity_machine_name),
),
),
);
$properties = $entity_info['properties'];
$properties = stanford_capx_filter_properties($entity_machine_name, $properties);
foreach ($properties as $property_machine_name => $prop_info) {
$form['field-mapping'][$property_container][$property_machine_name] = array(
'#type' => 'container',
'#title' => check_plain($prop_info['label']),
'#field_info' => $prop_info,
'#theme_wrappers' => array('stanford_capx_columns_to_the_right'),
'#tree' => FALSE,
);
$form['field-mapping'][$property_container][$property_machine_name][$property_machine_name] = array(
'#type' => 'textfield',
'#title' => t('value'),
'#description' => check_plain($prop_info['description']),
'#default_value' => isset($mapper->properties[$property_machine_name]) ? $mapper->properties[$property_machine_name] : '',
'#field_info' => $prop_info,
'#parents' => array('field-mapping', $property_container, $property_machine_name),
);
if (isset($prop_info['required'])) {
$form['field-mapping'][$property_container][$property_machine_name]['#attributes']['class'][] = "required";
}
}
}
// FIELDS
// ---------------------------------------------------------------------
foreach ($entity_types as $entity_machine_name => $entity_info_values) {
// If this is an edit mapper form (mapper exists) do not render the other
// entities or bundles.
if (isset($mapper->settings['entity_type']) && $mapper->settings['entity_type'] !== $entity_machine_name) {
continue;
}
// Get all the properties of the entity type.
$entity_info = entity_get_property_info($entity_machine_name);
// Skip those entities with no bundles.
if (!isset($entity_info['bundles'])) {
continue;
}
foreach ($entity_info['bundles'] as $bundle_machine_name => $bundle_info) {
// Skip bundles that dont match a loaded mapper.
if (isset($mapper->settings['bundle_type']) && $bundle_machine_name !== $mapper->settings['bundle_type']) {
continue;
}
$fields = $entity_info['bundles'][$bundle_machine_name]['properties'];
$field_container = $entity_machine_name . "-" . $bundle_machine_name . "-fields";
// Generate property forms for each entity + bundle.
$form['field-mapping'][$field_container] = array(
'#type' => 'fieldset',
'#title' => t("@var Fields", array("@var" => $bundle_machine_name)),
'#description' => t(''),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
'#states' => array(
'visible' => array(
'select[name="entity-type"]' => array('value' => $entity_machine_name),
'select[name="bundle-' . $entity_machine_name . '"]' => array('value' => $bundle_machine_name),
),
),
);
foreach ($fields as $field_machine_name => $field_info) {
// Field collections need their own fieldset and fields.
if (strpos($field_info['type'], 'field_collection_item') !== FALSE) {
$field_collections[$entity_machine_name][$bundle_machine_name][$field_machine_name] = $field_info;
continue;
}
$field_info_field = field_info_field($field_machine_name);
$field_instance_info = field_info_instance($entity_machine_name, $field_machine_name, $bundle_machine_name);
$form['field-mapping'][$field_container][$field_machine_name] = array(
'#type' => 'container',
'#title' => t("@var", array("@var" => $field_instance_info['label'])),
'#description' => t("@var", array("@var" => $field_instance_info['description'])),
'#field_info' => $field_info_field,
'#field_instance_info' => $field_instance_info,
'#theme_wrappers' => array('stanford_capx_columns_to_the_right'),
);
// Do not give all options for the file field. File field processor
// will take care of most.
if ($field_info_field['type'] == "file" || $field_info_field['type'] == "image") {
$form['field-mapping'][$field_container][$field_machine_name][0] = array(
'#type' => 'textfield',
'#title' => t('File Information'),
'#description' => t('In your JSON Path selector please pass in the whole array of image information. eg: $.profilePhotos.bigger'),
'#default_value' => @$mapper->fields[$field_machine_name][0],
);
if ($field_instance_info['required']) {
$form['field-mapping'][$field_container][$field_machine_name]['#attributes']['class'][] = "required";
}
continue;
}
// Relation module doesn't have this column key set.
if (!isset($field_info_field['columns'])) {
continue;
}
foreach ($field_info_field['columns'] as $column_key => $column_info) {
if (!in_array($column_key, $CAP_RESTRICTED_FIELD_PROPERTIES)) {
$form['field-mapping'][$field_container][$field_machine_name][$column_key] = array(
'#type' => 'textfield',
'#title' => t("@var", array("@var" => $column_key)),
'#description' => !empty($column_info['description']) ? t("@var", array("@var" => $column_info['description'])) : "",
'#default_value' => isset($mapper->fields[$field_machine_name][$column_key]) ? $mapper->fields[$field_machine_name][$column_key] : "",
);
if ($field_instance_info['required']) {
$form['field-mapping'][$field_container][$field_machine_name]['#attributes']['class'][] = "required";
}
}
}
}
}
} // end field looop
// FIELD COLLECTIONS
// ----------------------------------------------------------------------
foreach ($field_collections as $entity_machine_name => $bundles) {
foreach ($bundles as $bundle_machine_name => $fields) {
foreach ($fields as $field_machine_name => $field_info) {
$instances = field_info_instances('field_collection_item', $field_machine_name);
$collection_container = $entity_machine_name . "-" . $bundle_machine_name . "-collections";
// Fieldset for all fields on this entity -> bundle -> collection field
$form['field-mapping'][$collection_container] = array(
'#type' => 'fieldset',
'#title' => t('@name Field Collection', array("@name" => $field_machine_name)),
'#description' => t(''),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
'#states' => array(
'visible' => array(
'select[name="entity-type"]' => array('value' => $entity_machine_name),
'select[name="bundle-' . $entity_machine_name . '"]' => array('value' => $bundle_machine_name),
),
),
);
foreach ($instances as $fc_field_name => $instance_info) {
$fc_field_info = field_info_field($fc_field_name);
$form['field-mapping'][$collection_container][$field_machine_name][$fc_field_name] = array(
'#type' => 'container',
'#title' => t("@var", array("@var" => $instance_info['label'])),
'#description' => t("@var", array("@var" => $instance_info['description'])),
'#field_info' => $fc_field_info,
'#field_instance_info' => $instance_info,
'#theme_wrappers' => array('stanford_capx_columns_to_the_right'),
);
// Do not give all options for the file field. File field processor
// will take care of most.
if ($fc_field_info['type'] == "file" || $fc_field_info['type'] == "image") {
$form['field-mapping'][$collection_container][$field_machine_name][$fc_field_name][0] = array(
'#type' => 'textfield',
'#title' => t('File Info'),
'#description' => t('In your JSON Path selector please pass in the whole array of image information. eg: $.profilePhotos.bigger'),
'#default_value' => @$mapper->collections[$field_machine_name][$fc_field_name][0],
);
continue;
}
foreach ($fc_field_info['columns'] as $column_key => $column_info) {
if (!in_array($column_key, $CAP_RESTRICTED_FIELD_PROPERTIES)) {
$form['field-mapping'][$collection_container][$field_machine_name][$fc_field_name][$column_key] = array(
'#type' => 'textfield',
'#title' => t("@var", array("@var" => $column_key)),
'#description' => t(@$column_info['description']),
'#default_value' => @$mapper->collections[$field_machine_name][$fc_field_name][$column_key],
);
if ($instance_info['required']) {
$form['field-mapping'][$collection_container][$field_machine_name][$fc_field_name][$column_key]["#attributes"]["class"] = "required";
}
}
}
}
}
}
}
return $form;
}
/**
* [stanford_capx_mapper_machine_name_exits description]
* @param [type] $name [description]
* @return [type] [description]
*/
function stanford_capx_mapper_machine_name_exits($name) {
$result = db_select('capx_cfe', 'cfe')
->fields('cfe', array('machine_name'))
->condition('machine_name', $name)
->condition('type', 'mapper')
->execute();
$count = $result->rowCount();
// We found a match!
if ($count > 0) {
return TRUE;
}
// Nothing by the passed in name.
return FALSE;
}
/**
* Takes in an array of properties and filters out the ones that users should
* never map to. Things like nid, vid, comments, etc.
* @param array $properties [description]
* @return [type] [description]
*/
function stanford_capx_filter_properties($entity_type, $properties = array()) {
$blacklist = array();
$property_keys = array_keys($properties);
// Blocks / Beans.
$blacklist['bean'][] = "bid";
$blacklist['bean'][] = "vid";
$blacklist['bean'][] = "delta";
$blacklist['bean'][] = "view_mode";
$blacklist['bean'][] = "type";
$blacklist['bean'][] = "url";
$blacklist['bean'][] = "uid";
$blacklist['bean'][] = "created";
$blacklist['bean'][] = "changed";
$blacklist['bean'][] = "data";
$blacklist['bean'][] = "body"; // body is not a property!
// Field collection item.
$blacklist['field_collection_item'][] = "item_id";
$blacklist['field_collection_item'][] = "revision_id";
$blacklist['field_collection_item'][] = "field_name";
$blacklist['field_collection_item'][] = "archived";
$blacklist['field_collection_item'][] = "url";
$blacklist['field_collection_item'][] = "host_entity";
// Nodes.
$blacklist['node'][] = "nid";
$blacklist['node'][] = "vid";
$blacklist['node'][] = "is_new";
$blacklist['node'][] = "changed";
$blacklist['node'][] = "created";
$blacklist['node'][] = "type";
$blacklist['node'][] = "language";
$blacklist['node'][] = "url";
$blacklist['node'][] = "edit_url";
$blacklist['node'][] = "status";
$blacklist['node'][] = "promote";
$blacklist['node'][] = "sticky";
$blacklist['node'][] = "author";
$blacklist['node'][] = "source";
$blacklist['node'][] = "revision";
$blacklist['node'][] = "comment";
$blacklist['node'][] = "comments";
$blacklist['node'][] = "comment_count";
$blacklist['node'][] = "comment_count_new";
$blacklist['node'][] = "feed_nid";
$blacklist['node'][] = "uuid";
$blacklist['node'][] = "vuuid";
$blacklist['node'][] = "log";
$blacklist['node'][] = "body"; // body is not a property!
// Terms.
$blacklist['taxonomy_term'][] = "tid";
$blacklist['taxonomy_term'][] = "weight";
$blacklist['taxonomy_term'][] = "node_count";
$blacklist['taxonomy_term'][] = "url";
$blacklist['taxonomy_term'][] = "vocabulary";
$blacklist['taxonomy_term'][] = "parent";
$blacklist['taxonomy_term'][] = "parents_all";
$blacklist['taxonomy_term'][] = "created";
$blacklist['taxonomy_term'][] = "changed";
$blacklist['taxonomy_term'][] = "body"; // body is not a property!
// User.
$blacklist['user'][] = "uid";
$blacklist['user'][] = "url";
$blacklist['user'][] = "edit_url";
$blacklist['user'][] = "last_access";
$blacklist['user'][] = "last_login";
$blacklist['user'][] = "roles";
$blacklist['user'][] = "status";
$blacklist['user'][] = "theme";
$blacklist['user'][] = "created";
$blacklist['user'][] = "changed";
$blacklist['user'][] = "body"; // body is not a property!
// Anything we don't know about.
$blacklist['unknown'][] = "id";
$blacklist['unknown'][] = "vid";
$blacklist['unknown'][] = "url";
$blacklist['unknown'][] = "edit_url";
$blacklist['unknown'][] = "uid";
$blacklist['unknown'][] = "changed";
$blacklist['unknown'][] = "created";
$blacklist['unknown'][] = "body"; // body is not a property!
if (!isset($blacklist[$entity_type])) {
$entity_type = "unknown";
}
// Rip out the blacklisted items.
$allowed = array_diff($property_keys, $blacklist[$entity_type]);
$allowed = array_flip($allowed);
$properties = array_intersect_key($properties, $allowed);
return $properties;
}
/**
* Validation function for the mapper form.
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_mapper_form_validate($form, $form_state) {
// Recursively validate json selectors for mapped fields and properties.
// Only validate the entity/bundle that was selected...
$entity = $form_state['values']['entity-type'];
$bundle = $form_state['values']['bundle-' . $entity];
// Entities have fields and properties.
stanford_capx_mapper_form_validate_element($form_state['values']['field-mapping'][$entity . "-properties"], array('field-mapping'));
if (isset($form_state['values']['field-mapping'][$entity . "-" . $bundle . "-fields"])) {
stanford_capx_mapper_form_validate_element($form_state['values']['field-mapping'][$entity . "-" . $bundle . "-fields"], array('field-mapping'));
}
// Only some have field collections.
if (isset($form_state['values']['field-mapping'][$entity . "-" . $bundle . "-collections"])) {
stanford_capx_mapper_form_validate_element($form_state['values']['field-mapping'][$entity . "-" . $bundle . "-collections"], array('field-mapping'));
}
// Set the error messages after everything has been looped through.
stanford_capx_mapper_form_validate_element_set_messages();
// Ensure all required fields were posted. Because of the way the form works
// we cannot place a required on to each of the form element fields as there
// are a number of fields that are hidden and we do not want them. They still
// need to be filled out.
stanford_capx_mapper_form_validate_required_fields($form, $form_state, $entity, $bundle);
}
/**
* Ensure all required fields were posted.
*
* Because of the way the form works we cannot place a required on to each of
* the form element fields as there are a number of fields that are hidden and
* we do not want them. They still need to be filled out.
*
* @param array $form
* Form array
* @param array $form_state
* Form state array
* @param string $entity
* The machine name of the entity type to be validated.
* @param string $bundle
* The machine name of the bundle type to be validated.
*/
function stanford_capx_mapper_form_validate_required_fields(&$form, &$form_state, $entity, $bundle) {
// Get all the properties of the entity type.
$entity_info = entity_get_property_info($entity);
$properties = $entity_info['properties'];
$fields = isset($entity_info['bundles']) ? $entity_info['bundles'][$bundle]['properties'] : array();
// Properties validation.
foreach ($form_state['values']['field-mapping'][$entity . "-properties"] as $k => $v) {
if (empty($v) && !empty($properties[$k]['required'])) {
$element = 'field-mapping][' . $entity . "-properties][" . $k;
form_set_error($element, t('@name property is required.', array("@name" => $properties[$k]['label'])));
}
}
// Field validation.
foreach ($fields as $field_name => $value) {
if (isset($value['required']) && $value['required']) {
if (!isset($form_state['values']['field-mapping'][$entity . "-" . $bundle . "-fields"][$field_name])) {
continue;
}
$columns = $form_state['values']['field-mapping'][$entity . "-" . $bundle . "-fields"][$field_name];
// Loop through the columns and check for values.
foreach ($columns as $column_name => $value) {
if (empty($value) && $column_name != 'summary') {
$element = "field-mapping][" . $entity . "-" . $bundle . "-" . "fields][" . $field_name . "][" . $column_name;
form_set_error($element, t("@name field is required", array("@name" => $field_name)));
break;
}
}
}
}
}
/**
* Validate the user input.
*
* Validate the user input from the mapper form by checking to see if the
* selector finds something in the API schema.
*
* @param [type] $element [description]
* @param array $parents [description]
* @return [type] [description]
*/
function stanford_capx_mapper_form_validate_element($element, $parents = array()) {
static $json_parser;
$form_error = &drupal_static(__FUNCTION__ . "_error", FALSE);
$form_field_warnings = &drupal_static(__FUNCTION__ . "_warnings", FALSE);
// Load and cache the parser.
if (!isset($json_parser)) {
$schema = stanford_capx_schema_format_validation();
$json_parser = new JsonParser($schema);
}
// Recursively iterate over child elements.
foreach ($element as $name => $child_element) {
array_push($parents, $name);
if (is_array($child_element)) {
stanford_capx_mapper_form_validate_element($child_element, $parents);
}
elseif (is_string($child_element) && strlen(trim($child_element))) {
try {
// Data types: 'string', 'boolean', 'integer', 'array' or 'object'.
$data_type = $json_parser->get($child_element);
// Empty array is returned for an invalid selector.
if (empty($data_type) && !_stanford_capx_mapper_is_valid_path($child_element, $json_parser)) {
throw new \Exception('String indicates a valid element.');
}
}
catch (\Exception $e) {
// Catch any Exception from JsonParser or the condition above.
// @TODO: Enable validation when the schema is consistent with the data.
// form_set_error(implode('][', $parents));
if ($name == "title") {
$field_name = "title";
}
else {
$field_name = $parents[(count($parents) - 2)];
}
// Record the problematic selector for display in a warning message.
$form_field_warnings[] = "<b>" . $field_name . "</b> has a selector of <b>" . check_plain($child_element) . "</b> that does not match any element in the API.";
$form_error = TRUE;
}
}
array_pop($parents);
}
}
/**
* Sets the message for the element validation.
*/
function stanford_capx_mapper_form_validate_element_set_messages() {
$form_error = &drupal_static("stanford_capx_mapper_form_validate_element_error", FALSE);
$form_field_warnings = &drupal_static("stanford_capx_mapper_form_validate_element_warnings", FALSE);
// Set the error message once when the recursive calls have finished.
if ($form_error) {
$field_warnings_list = theme('item_list', array(
'title' => '',
'items' => $form_field_warnings,
'type' => 'ul',
'attributes' => array()
));
$browser_link = l(t('Data Browser'),
'admin/config/capx/data-browser', array(
'attributes' => array('target' => '_blank')
));
drupal_set_message(
t('The following selectors did not match an element in the profile schema. Try using the !databrowser to find a valid selector.!warnings',
array(
'!warnings' => $field_warnings_list,
'!databrowser' => $browser_link,
)
), 'warning');
}
}
/**
* Submit handler for the mapper form. Create and save the mapper entity.
* @param [type] $form [description]
* @param [type] $form_state [description]
* @return [type] [description]
*/
function stanford_capx_mapper_form_submit($form, $form_state) {
$values = $form_state['values'];
$mapper = capx_cfe_load_by_machine_name($values['machine-name'], 'mapper');
if (!$mapper) {
$mapper = entity_create('capx_cfe', array());
}
$mapper->title = $values['title'];
$mapper->machine_name = $values['machine-name'];
$mapper->type = "mapper";
$settings = array();
$settings['fields'] = array();
$settings['properties'] = array();
$settings['collections'] = array();
$entity = $values['entity-type'];
$bundle = $values['bundle-' . $entity];
$settings['entity_type'] = $entity;
$settings['bundle_type'] = $bundle;
// If there are properties...
$properties = @$values['field-mapping'][$entity . "-properties"];
if (!empty($properties)) {
$settings['properties'] = $properties;
}
// If there are fields...
$fields = @$values['field-mapping'][$entity . "-" . $bundle . "-fields"];
if (!empty($fields)) {
$settings['fields'] = $fields;
}
// If there are collections...
$collections = @$values['field-mapping'][$entity . "-" . $bundle . "-collections"];
// If there are field collections check to see if they are any values in them.
// if (!empty($collections)) {
// foreach ($collections as $collection => $fields) {
// foreach ($fields as $field_name => $values) {
// $value = array_pop($values);
// if (empty($value)) {
// unset($collections[$collection][$field_name]);
// }
// }
// if (empty($collections[$collection])) {
// unset($collections[$collection]);
// }
// }
// }
// We have values!
if (!empty($collections)) {
$settings['collections'] = $collections;
}
$mapper->settings = $settings;
// New vs Edit.
if (!empty($mapper->is_new)) {
drupal_set_message(t('Congratulations, Your mapping has been created.'), 'status', FALSE);
}
else {
CAPx::invalidateEtags("mapper", $mapper);
drupal_set_message(t("Your mapping has been updated."), 'status', FALSE);
}
// Do the save.
capx_mapper_save($mapper);
// Lets check the error messages that may be related to this mapper.
$messages = variable_get('stanford_capx_admin_messages', array());
foreach ($messages as $key => &$message) {
$message_meta = explode(':', $key);
// When the field is missing from some bundle in
// \CAPx\Drupal\Mapper\EntityMapper::mapFields we creating key from 4
// components:
// - mapper machine name
// - entity type
// - entity bundle
// - entity field name
// In this exact order.
if (count($message_meta) === 4) {
if (
$message_meta[0] == $values['machine-name']
&& $message_meta[1] == $entity
&& $message_meta[2] == $bundle
) {
unset($messages[$key]);
}
}
elseif (!empty($message['mappers']) && array_key_exists($values['machine-name'], $message['mappers'])) {
unset($message['mappers'][$values['machine-name']]);
if (empty($message['mappers'])) {
// This was the last mapper that were using deleted field.
unset($messages[$key]);
}
else {
$message['message_vars']['!mappers'] = implode(', ', $message['mappers']);