-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaicapp.module
3249 lines (3118 loc) · 130 KB
/
aicapp.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
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
* Manage objects and galleries that are used in the Mobile APP.
*
* Objects and galleries have some data stored in Drupal, and some coming from data hub queries to the
* Chicago Art Institute's existing DB. Objects physical location can be pinned on a Google Map of the Institute.
*
* This module creates a button that when clicked, pulls all the published content into a JSON file (appData.json) at
* the site root, which is then called by the mobile APP.
*
* Email notices are also fired when objects are pulled out of galleries or added back into galleries.
*/
/**
* Implements hook_init().
*/
function aicapp_init() {
global $language;
define('AICAPP_DEFAULT_LANG', $language->language);
define('AICAPP_ARTWORK_QUERY', 'artwork');
define('AICAPP_GALLERY_QUERY', 'gallery');
//internal use
define('AICAPP_IMAGE_SERVER_DEFAULT', rtrim(variable_get('aicapp_image_server_url', 'https://lakeimagesweb.artic.edu/iiif/2'), '/'));
//external use
define('AICAPP_IMAGE_SERVER_OVERRIDE', rtrim(variable_get('aicapp_image_server_override', 'https://lakeimagesweb.artic.edu/iiif/2'), '/'));
define('AICAPP_IMAGE_CROP_REGION', 'full');
define('AICAPP_IMAGE_CROP_ROTATION', 0);
define('AICAPP_IMAGE_CROP_QUALITY', 'default');
define('AICAPP_IMAGE_CROP_FORMAT', 'jpg');
define('AICAPP_TYPE_AUDIO', 'audio');
define('AICAPP_TYPE_OBJECT', 'object');
define('AICAPP_TYPE_GALLERY', 'gallery');
define('AICAPP_TYPE_PAGE', 'page');
define('AICAPP_TYPE_TOUR', 'tour');
define('AICAPP_TYPE_EXHIBITION', 'exhibition');
define('AICAPP_TYPE_MAP_ANNOTATION', 'map_annotation');
define('AICAPP_TYPE_MAP_FLOOR', 'map_floor');
define('AICAPP_TYPE_MESSAGE', 'message');
define('AICAPP_DATA_AGGREGATOR', 'aic_data_aggregrator');
define('AICAPP_DATA_API_HOST', rtrim(variable_get('aicapp_data_api_host', 'http://api.artic.edu/'), '/'));
define('AICAPP_DATA_API_HOST_INTERNAL', rtrim(variable_get('aicapp_data_api_host_internal', 'http://api.artic.edu/'), '/'));
define('AICAPP_DATA_API_ENDPOINT_EXHIBITION', variable_get('aicapp_data_endpoint_exhibitions', '/api/v1/exhibitions'));
define('AICAPP_DATA_API_ENDPOINT_ARTWORKS', variable_get('aicapp_data_endpoint_artworks', '/api/v1/artworks'));
define('AICAPP_DATA_API_ENDPOINT_GALLERIES', variable_get('aicapp_data_endpoint_galleries', '/api/v1/galleries'));
define('AICAPP_DATA_API_ENDPOINT_IMAGES', variable_get('aicapp_data_endpoint_images', '/api/v1/images'));
define('AICAPP_DATA_API_ENDPOINT_EVENTS', variable_get('aicapp_data_endpoint_events', '/api/v1/events'));
define('AICAPP_DATA_API_ENDPOINT_EVENTS_v2', variable_get('aicapp_data_endpoint_events_v2', '/api/v1/event-occurrences'));
define('AICAPP_DATA_API_ENDPOINT_TOURS', variable_get('aicapp_data_endpoint_tours', '/api/v1/tours'));
define('AICAPP_DATA_API_ENDPOINT_AUTOCOMPLETE', variable_get('aicapp_data_endpoint_autocomplete', '/api/v1/autocomplete'));
define('AICAPP_MULTISEARCH_ENDPOINT', variable_get('aicapp_data_endpoint_multisearch', '/api/v1/msearch'));
define('AICAPP_TICKETS_URL', variable_get('aicapp_tickets_url', 'https://sales.artic.edu/admissiondate?&utm_medium=mobile-app&utm_source=iphone&utm_campaign=buy-tickets'));
define('AICAPP_MEMBERSHIP_URL', variable_get('aicapp_membership_url', 'https://sales.artic.edu/admissiondate?&utm_medium=mobile-app&utm_source=iphone&utm_campaign=buy-tickets'));
define('AICAPP_WEBSITE_URL', variable_get('aicapp_website_url', 'http://www.artic.edu/?&utm_medium=mobile-app&utm_source=iphone&utm_campaign=homepage'));
define('AICAPP_JSON_FILENAME', variable_get('aicapp_json_filename', 'appData'));
define('AICAPP_JSON_V2', variable_get('aicapp_json_version_2', 'v2'));
define('AICAPP_JSON_V3', variable_get('aicapp_json_version_3', 'v3'));
define('AICAPP_TOUR_VOCABULARY_NAME', variable_get('aicapp_tour_vocabular_name', 'categories'));
define('AICAPP_CURRENT_SERVER_FQDM', rtrim(variable_get('aicapp_env_fqdm', 'http://aicweb10.artic.edu'), '/'));
}
/**
* Implements hook_menu().
*/
function aicapp_menu() {
$items['objects/search'] = array(
'title' => 'Add an Object',
'page callback' => 'aicapp_search_object',
'type' => MENU_CALLBACK,
'weight' => 20,
'access arguments' => array('add objects'),
);
// This is redundant, a placeholder for the next one.
$items['objects/object_add'] = array(
'title' => 'Add Object',
'page callback' => 'aicapp_search_object',
'type' => MENU_CALLBACK,
'weight' => 20,
'access arguments' => array('add objects'),
);
$items['objects/object_add/%'] = array(
'title' => 'Add Object',
'page callback' => 'aicapp_add_object',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'weight' => 20,
'access arguments' => array('add objects'),
);
$items['objects/noimg'] = array(
'title' => 'Objects without Images',
'page callback' => 'aicapp_object_noimg',
'type' => MENU_CALLBACK,
'weight' => 20,
'access arguments' => array('add objects'),
);
$items['objects/check-objects-gallery'] = array(
'title' => 'Check Objects Gallery Info',
'page callback' => 'aicapp_object_checks',
'type' => MENU_CALLBACK,
'weight' => 20,
'access arguments' => array('add objects'),
);
$items['app-search'] = array(
'title' => 'App Search Start Page',
'description' => 'Settings for the iOS app search start page.',
'page callback' => 'drupal_get_form',
'page arguments' => array('aicapp_app_search_admin'),
'access callback' => 'user_access',
'access arguments' => array('edit any object content'),
'file' => 'aicapp.app_search.inc',
'file path' => drupal_get_path('module', 'aicapp') . '/includes',
);
/* Queries to retrieve data from remote API */
$items['admin/settings/aic-api'] = array(
'title' => 'AIC API module settings',
'description' => 'Specify the URLs of your API',
'page callback' => 'aicapp_admin_redirect',
'access callback' => 'user_access',
'access arguments' => array('administer aic api settings'),
'type' => MENU_CALLBACK,
'file' => 'aicapp.admin.inc',
'file path' => drupal_get_path('module', 'aicapp') . '/includes',
);
$items['admin/config/system/aic'] = array(
'title' => 'AIC module settings',
'description' => 'Global Settings For AIC APP',
'page callback' => 'drupal_get_form',
'page arguments' => array('aicapp_admin'),
'access callback' => 'user_access',
'access arguments' => array('administer aic api settings'),
'file' => 'aicapp.admin.inc',
'file path' => drupal_get_path('module', 'aicapp') . '/includes',
);
$items['admin/config/system/aic/global'] = array(
'title' => 'AIC module settings',
'description' => 'Global Settings For AIC APP',
'page callback' => 'drupal_get_form',
'page arguments' => array('aicapp_admin'),
'access callback' => 'user_access',
'access arguments' => array('administer aic api settings'),
'file' => 'aicapp.admin.inc',
'file path' => drupal_get_path('module', 'aicapp') . '/includes',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
);
// API Configurations
$items['admin/config/system/aic/api'] = array(
'title' => 'AIC API settings',
'description' => 'Specify the URLs of your API',
'page callback' => 'drupal_get_form',
'page arguments' => array('aicapp_admin_api'),
'access callback' => 'user_access',
'access arguments' => array('administer aic api settings'),
'file' => 'aicapp.admin.inc',
'file path' => drupal_get_path('module', 'aicapp') . '/includes',
'type' => MENU_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/system/aic/operations'] = array(
'title' => 'AIC Operations',
'description' => 'Utilities to run common operations.',
'page callback' => 'drupal_get_form',
'page arguments' => array('aicapp_admin_operations'),
'access callback' => 'user_access',
'access arguments' => array('administer aic api settings'),
'file' => 'aicapp.admin.inc',
'file path' => drupal_get_path('module', 'aicapp') . '/includes',
'type' => MENU_LOCAL_TASK,
'weight' => -5,
);
return $items;
}
/**
* Implements hook_permission().
*/
function aicapp_permission() {
return array(
'add objects' => array(
'description' => t('Add or edit objects in the museum'),
'title' => t('Add or Edit Objects'),
'restrict access' => FALSE,
),
'administer aic api settings' => array(
'description' => t('Configure AIC Custom Module Settings'),
'title' => t('Administer AIC Settings'),
'restrict access' => FALSE,
),
);
}
/**
* Page callback function.
*/
function aicapp_search_object() {
$fm = drupal_get_form('aicapp_search_form');
return render($fm);
}
/**
* Page callback for adding an object.
*/
function aicapp_add_object($object_id = 0) {
$object_ids = explode(",", $object_id);
// Collecting information to present to the user.
$collect = array();
foreach ($object_ids as $object_id) {
// An object ID was passed and is not empty, query based on object ID.
$options = array(
'ids' => array($object_id),
'limit' => 1,
);
// Query the data aggregrator.
$results = _aicapp_fetch_data(AICAPP_DATA_API_HOST_INTERNAL . AICAPP_DATA_API_ENDPOINT_ARTWORKS, $options);
if (!empty($results[0])) {
// Creates the new object or updates existing if it finds it in our Db.
$nid = aicappSaveNode($results[0], AICAPP_TYPE_OBJECT);
$object = '<strong>Object ' . $object_id . ' saved.</strong><br />';
$object .= l(t('View object'), '/node/' . $nid) . '<br /><br />';
$collect[] = $object;
}
else {
$collect[] = t('Ran into a problem attempting data hub query with object id:') . $object_id . '<br />';
}
}
$output = implode(' ', $collect);
return $output;
}
/**
* Implements hook_block_info().
*/
function aicapp_block_info() {
$blocks['data_buttons'] = array(
'info' => t('Load and Generate Buttons'),
'status' => TRUE,
'weight' => 0,
'region' => 'sidebar_first',
'visibility' => 1,
);
$blocks['publish_butn'] = array(
'info' => t('Publish Button'),
'status' => TRUE,
'weight' => 0,
'region' => 'sidebar_first',
'visibility' => 1,
);
$blocks['data_search'] = array(
'info' => t('Search AI Data'),
'status' => TRUE,
'weight' => 0,
'region' => 'header',
'visibility' => 1,
);
$blocks['gallery_count'] = array(
'info' => t('Gallery Count'),
'status' => TRUE,
'weight' => 0,
'region' => 'content',
'visibility' => 1,
);
$blocks['updates_pending'] = array(
'info' => t('Updates Pending'),
'status' => TRUE,
'weight' => 0,
'region' => 'header',
'visibility' => 1,
);
$blocks['info_for_no-image_page'] = array(
'info' => t('Info for no-image page'),
'status' => TRUE,
'weight' => 0,
'region' => 'content',
'visibility' => 1,
);
$blocks['page_info'] = array(
'info' => t('Content and Language Status'),
'status' => TRUE,
'weight' => -99,
'region' => 'content',
'visibility' => 1,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function aicapp_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'data_buttons':
$block['subject'] = t('Load and Generate Buttons');
$block['content'] = aicapp_data_content();
break;
case 'publish_butn':
$block['subject'] = t('Publish Button');
$block['content'] = aicapp_pub_content();
break;
case 'data_search':
$search_form = aicapp_search_form();
$block['subject'] = t('Search AI Data');
$block['content'] = render($search_form);
break;
case 'gallery_count':
$block['subject'] = t('Gallery Count');
$block['content'] = aicapp_gallery_count();
break;
case 'updates_pending':
$block['subject'] = t('Updates Pending');
$block['content'] = aicapp_updates_pending();
break;
case 'info_for_no-image_page':
$block['subject'] = t('Info for no-image page');
$block['content'] = aicapp_info_for_no_image_page();
break;
case 'page_info':
$block['subject'] = '';
$block['content'] = aicapp_page_info_content();
break;
}
return $block;
}
/**
* Block content callback.
*/
function aicapp_updates_pending() {
$updates = variable_get('aicapp_pending');
if ($updates) {
return '<div class="pending">' . t('Updates Pending') . '</div>';
}
else {
return '<div class="done">' . t('Up to Date') . '</div>';
}
}
/**
* Block content callback.
*/
function aicapp_data_content() {
$fm1 = drupal_get_form('aicapp_loadgalleries_form');
$fm2 = drupal_get_form('aicapp_gendata_form');
return render($fm1) . render($fm2);
}
/**
* Block content callback.
*/
function aicapp_pub_content() {
$output = '';
$file_sync_form = drupal_get_form('aicapp_file_sync_form');
$output .= render($file_sync_form);
return $output;
}
/**
* Block content callback.
*/
function aicapp_info_for_no_image_page() {
return '<p>This page is conducting a live search of each published object from the Drupal CMS in the data hub data to see if there is a full-sized image. If there is not an image in data hub or Drupal, then it is flagged.</p>
';
}
/**
* Block content callback.
*/
function aicapp_page_info_content() {
// Get the object found at this path.
$object = menu_get_object();
if (!is_object($object) || !node_access('update', $object)) {
return;
}
// Get the path parts in an array.
$path_parts = arg();
// Node type info.
$node_type = node_type_get_type($object->type);
// Translation info.
$translations = $object->translations;
// Create an array of strings that will be imploded back into a single string.
$message_strings = array('This is @type item.');
// Create an array of replacement values, for use with the t().
$types = array(
'@type' => preg_match('/^[aeiou]/i', strtolower($object->type)) ? 'an ' . $node_type->name : 'a ' . $node_type->name,
);
// If this node has translation data.
if (!empty($translations->data)) {
global $language;
$types['@language'] = $language->name;
$message_strings[] = 'The current language is @language.';
// Get a list of enabled languages.
if (function_exists('locale_language_list')) {
$langauges = locale_language_list();
}
else {
$languages = language_list('enabled');
$languages = $languages[1];
$list = array();
foreach ($languages as $language) {
$list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field;
}
$langauges = $list;
}
// Add replacement values of the current language based on the path.
if (!empty($path_parts[3]) && $path_parts[3] === 'add' && !empty($path_parts[5]) && array_key_exists($path_parts[5], $langauges) ) {
$types['@language'] = $langauges[$path_parts[5]];
}
elseif (!empty($path_parts[2]) && $path_parts[2] === 'edit' && !empty($path_parts[3]) && array_key_exists($path_parts[3], $langauges) && empty($path_parts[4])) {
$types['@language'] = $langauges[$path_parts[3]];
}
}
if (!empty($path_parts[2]) && $path_parts[2] === 'edit') {
// Start the output that best put into the block.
$content = '<h5 style="border-bottom: 1px solid;background:#eee; padding:.5rem;text-align:center;">' . t(implode(' ', $message_strings), $types) . '</h5>';
return $content;
}
}
/**
* Hook_form() implementation.
*/
function aicapp_loadgalleries_form() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Refresh Gallery List'),
'#attributes' => array('class' => array('button')),
);
return $form;
};
/**
* Hook_form() implementation.
*/
function aicapp_gendata_form() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit for Publishing'),
'#attributes' => array('class' => array('button')),
);
return $form;
};
/**
* Hook_form() implementation.
*/
function aicapp_load_remote_form() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Refresh Events'),
'#attributes' => array('class' => array('button')),
);
return $form;
};
/**
* Hook_form() implementation.
*/
function aicapp_file_sync_form() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Publish'),
'#attributes' => array('class' => array('button')),
);
return $form;
}
/**
* Page callback wth email [email protected].
*/
function aicapp_object_checks() {
// Compare objects to data hub.
$gallery_status_form = drupal_get_form('aicapp_ObjectGalleryStatus');
$gallery_status_message = '<p>This button will cause the system to compare all objects with the current data hub data, and update any objects that
have been moved into galleries but are currently marked as "Not on display" in Drupal. It will send out an email to tell
you which objects have recently been put into a gallery, update their gallery number field, and set "in Gallery" to checked.
It will also set these objects to unpublished so you can review & enter lat / long values if needed.</p>
<p style="margin-bottom:50px;">
It will also check each object for any gallery location changes and update the object. An email will
be sent out to notify of any changes. <em>It will take a while to do all this - please be patient!</em></p>';
$output = render($gallery_status_form) . $gallery_status_message;
// Verify object gallery status.
$verify_obj_form = drupal_get_form('aicapp_verify_obj_gallery');
$verify_obj_message = '<p>This will check objects to make sure the data hub field "gallery_location" is a valid gallery (one listed here under Galleries).</p>';
$output .= render($verify_obj_form) . $verify_obj_message;
return $output;
}
/**
* Gallery status callback.
*/
function aicapp_ObjectGalleryStatus() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Update Objects' Gallery Status"),
'#attributes' => array('class' => array('button')),
);
return $form;
}
/**
* Verify Object Gallery submit.
*/
function aicapp_verify_obj_gallery() {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Verify Objects' Galleries"),
'#attributes' => array('class' => array('button')),
);
return $form;
}
/**
* Object Gallery Status submit.
*/
function aicapp_objectGalleryStatus_submit($form, &$form_state) {
$art_gone = array();
$art_in_gallery = array();
$locations = array();
$changed = array();
$object_ids = array();
$by_object_id = array();
$objects['#updated'] = array();
// Load all objects, including unpublished, because we compare if objects
// previously not in a gallery (and unpublished) are now displayed.
$artwork_objects = node_load_multiple(array(), array('type' => AICAPP_TYPE_OBJECT));
foreach ($artwork_objects as $object) {
if (!is_object($object)) {
continue;
}
$entity = entity_metadata_wrapper('node', $object);
$object_id = $entity->field_object_id->value();
if (!empty($object_id)) {
$object_ids[] = $object_id;
$by_object_id[$object_id] = $object;
}
else {
$objects['#updated'][$object->nid] = _aicapp_update_local_artwork_data($object, $entity);
}
}
// Query the data aggregrator for recent data using the object ids.
$options = array('ids' => $object_ids, 'limit' => count($object_ids));
$url = AICAPP_DATA_API_HOST_INTERNAL . AICAPP_DATA_API_ENDPOINT_ARTWORKS;
$results = _aicapp_fetch_data($url, $options);
if (!empty($results)) {
foreach ($results as $item) {
$id = isset($item['id']) ? $item['id'] : NULL;
if (empty($id) || !isset($by_object_id[$id])) {
continue;
}
$object = $by_object_id[$id];
$objects['#updated'][$object->nid] = _aicapp_remote_object_properties($object, $item);
}
}
foreach ($objects['#updated'] as $key => $object) {
// Check objects here to see if they recently went off display.
// We send email notice in that case - (they may need to update a tour?)
// First collect the ones marked not on display in current data hub data.
if (empty($object->in_gallery)) {
$art_gone[] = $object->nid;
}
else {
// Keep it simple for array comparison later.
$art_in_gallery[] = $object->nid;
if (!empty($object->gallery_title)) {
$locations[$object->nid] = $object->gallery_title;
}
}
// Make sure any objects that have changed galleries get updated.
if (isset($object->gallery_title)) {
$loc = $object->gallery_title;
if (empty($object->in_gallery)) {
$in_gallery = 0;
}
else {
// The value must be either 0 or 1, so test for truthiness and set to 1.
$in_gallery = $object->in_gallery ? 1 : 0;
}
}
else {
$loc = '';
$in_gallery = 0;
}
// Check if 'in gallery' statuses have changed and sync with data hub data.
if (isset($object->field_in_gallery[LANGUAGE_NONE][0]['value']) && $in_gallery != $object->field_in_gallery[LANGUAGE_NONE][0]['value']) {
db_query("UPDATE {field_data_field_in_gallery}
SET field_in_gallery_value = :ingal
WHERE entity_id = :nid", array(':nid' => $object->nid, ':ingal' => $in_gallery));
db_query("UPDATE {field_revision_field_in_gallery}
SET field_in_gallery_value = :ingal
WHERE entity_id = :nid", array(':nid' => $object->nid, ':ingal' => $in_gallery));
}
// Update gallery locations.
if (isset($object->field_gallery_location[LANGUAGE_NONE][0]['value']) && $loc != $object->field_gallery_location[LANGUAGE_NONE][0]['value']) {
// Track old gallery locations to compare with new locations.
$oldloc = $object->field_gallery_location[LANGUAGE_NONE][0]['value'];
db_query("UPDATE {field_data_field_gallery_location}
SET field_gallery_location_value = :gal
WHERE entity_id = :nid
AND entity_type = 'node'
AND bundle = :bundle", array(':bundle' => AICAPP_TYPE_OBJECT, ':nid' => $object->nid, ':gal' => $loc));
db_query("UPDATE {field_revision_field_gallery_location}
SET field_gallery_location_value = :gal
WHERE entity_id = :nid
AND entity_type = 'node'
AND bundle = :bundle", array(':bundle' => AICAPP_TYPE_OBJECT, ':nid' => $object->nid, ':gal' => $loc));
// Publish objects in gallery and unpublish objects not in gallery
if (!empty($loc)) {
db_query("UPDATE {node} SET status = 1 WHERE nid = :nid", array(':nid' => $object->nid));
db_query("UPDATE {node_revision} SET status = 1 WHERE nid = :nid", array(':nid' => $object->nid));
} else {
db_query("UPDATE {node} SET status = 0 WHERE nid = :nid", array(':nid' => $object->nid));
db_query("UPDATE {node_revision} SET status = 0 WHERE nid = :nid", array(':nid' => $object->nid));
}
$changed[] = array(
'object_id' => $object->id,
'title' => $object->title,
'location' => $object->gallery_title,
'old_location' => $oldloc,
);
}
}
// flush drupal cache.
drupal_flush_all_caches();
// notify of any objects no longer on display.
_email_art_removed($art_gone);
// notify of any objects that have changed gallery location.
_email_object_gallery_change($changed);
}
/**
* verify that the gallery assigned to a published object is one of the galleries we have in Drupal.
*/
function aicapp_verify_obj_gallery_submit($form, &$form_state) {
$object_query = "SELECT node.nid, fi.field_object_id_value AS object_id
FROM {node}, {field_data_field_object_id} fi
WHERE node.nid = fi.entity_id
AND fi.bundle = :bundle
AND node.status = 1";
$db_objects = db_query($object_query, array(':bundle' => AICAPP_TYPE_OBJECT));
$gallery_query = "SELECT nid, title
FROM {node}
WHERE node.type = :bundle
AND node.status = 1";
$galleries = db_query($gallery_query, array(':bundle' => AICAPP_TYPE_GALLERY));
// need a simple array
$gals = array();
foreach ($galleries as $gallery) {
$gals[] = $gallery->title;
}
$objects = array('#updated' => array());
$bad_obj_galleries = '';
$object_ids = array();
$by_object_id = array();
foreach ($db_objects as $object) {
if (empty($object->nid)) {
continue;
}
$object = node_load($object->nid);
$entity = entity_metadata_wrapper('node', $object);
$object_id = $entity->field_object_id->value();
if (!empty($object_id)) {
$object_ids[] = $object_id;
$by_object_id[$object_id] = $object;
}
else {
$objects['#updated'][$object->nid] = _aicapp_update_local_artwork_data($object, $entity);
}
}
// Query the data aggregrator for recent data using the object ids.
$options = array('ids' => $object_ids, 'limit' => count($object_ids));
$url = AICAPP_DATA_API_HOST_INTERNAL . AICAPP_DATA_API_ENDPOINT_ARTWORKS;
$results = _aicapp_fetch_data($url, $options);
if (!empty($results)) {
foreach ($results as $item) {
$id = isset($item['id']) ? $item['id'] : NULL;
if (empty($id) || !isset($by_object_id[$id])) {
continue;
}
$object = $by_object_id[$id];
$objects['#updated'][$object->nid] = _aicapp_remote_object_properties($object, $item);
}
}
foreach ($objects['#updated'] as $object) {
$match = FALSE;
if (isset($object->gallery_title)) {
$match = array_search($object->gallery_title, $gals);
}
if (($match === FALSE) && isset($object->gallery_title) && $object->gallery_title != NULL) {
$bad_obj_galleries .= '<li>Object ID: ' . $object->object_id . ' -- ' . $object->gallery_title . '</li>';
}
}
// now notify
if (!empty($bad_obj_galleries)) {
drupal_set_message('Invalid gallery names found on objects: <ul>' . $bad_obj_galleries . '</ul>', 'error');
}
else {
drupal_set_message('No invalid gallery names found in objects', 'status');
}
}
/**
* Helper function for aicapp_gendata_form_submit()
* Compare 2 arrays of art objects. The first comes from data hub & are marked as not in gallery.
* The 2nd is from the Drupal DB, and are marked as in gallery.
* For any that are in both, they have recently been moved out of their gallery.
* Shoot off email notice with a list - the admin may need to update tours.
*/
function _email_art_removed($art_gone) {
global $base_url;
$site_name = variable_get('site_name');
// Now collect all objects from our Drupal DB (field_in_gallery table) that are in gallery and are published
$result = db_query("SELECT f.entity_id, f.field_in_gallery_value
FROM {field_data_field_in_gallery} f, {node} n
WHERE f.bundle = :bundle
AND f.entity_type = 'node'
AND f.field_in_gallery_value = 1
AND f.entity_id = n.nid
AND n.status = 1", array(':bundle' => AICAPP_TYPE_OBJECT));
$was_in_gallery = array();
// grab the ones that have been on display
foreach ($result as $item) {
$was_in_gallery[] = $item->entity_id;
}
// now compare
$recent_pulled = array_intersect($art_gone, $was_in_gallery);
if (!empty($recent_pulled)) {
$from = 'admin@' . $base_url;
$to = variable_get('site_mail', '');
$msg = '<p>' . t('This automated email is to notify you that we\'ve found objects
that were recently REMOVED from galleries. You might need to update
the tours if any of these objects were shown:') . '</p><ul>';
$object_list = '';
foreach ($recent_pulled as $nid) {
$object = node_load($nid);
// was this object on a tour?
// v2 tour stops
$tours = array();
if ($tour_objects = _aicapp_get_tour_objects(array(), $nid)) {
foreach ($tour_objects as $tour_nid => $tour_values) {
$tours[] = node_load($tour_nid);
}
}
$object_list .= '<li>object_id = ' . $object->id . ' -- ' . $object->title;
if (!empty($tours[0])) {
foreach ($tours as $tour) {
$object_list .= '<br>-- On tour: ' . check_plain($tour->title);
}
}
$object_list .= '</li>';
// set the object's status to unpublished
db_query("UPDATE {node} SET status = 0 WHERE nid = :nid", array(':nid' => $object->nid));
}
$msg .= $object_list . '</ul><p>Do not reply to this email.</p>';
$headers = "From: <" . $from . ">\nMIME-Version: 1.0\nContent-type: text/html; charset=utf-8";
$subject = $site_name . ': Object Removal Notice';
if (mail($to, $subject, $msg, $headers)) {
drupal_set_message('Email notice was sent regarding objects that have been removed:<ul>' . $object_list . '</ul>', 'status');
}
else {
drupal_set_message('Email notice unable to send. (These objects were removed):<ul>' . $object_list . '</ul>', 'error');
}
}
}
/**
* Helper function that lists, given a list of tour ids, will return all objects
* that are currently in a published tour, grouped by tour. You can also pass an array of
* object ids to limit the items returned to only those objects. Passing an
* array with just one object id will return all the tours that object is in.
*
*/
function _aicapp_get_tour_objects($tour_nids = array(), $object_nids = array()) {
// Normalize arguments to make sure we have arrays.
if (!is_array($tour_nids)) {
$tour_nids = is_numeric($tour_nids) ? array($tour_nids) : array();
}
if (!is_array($object_nids)) {
$object_nids = is_numeric($object_nids) ? array($object_nids) : array();
}
// Load all tours filted by the given tour ids.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->propertyCondition('type', AICAPP_TYPE_TOUR, '=')
->propertyCondition('status', 1, '=');
if (count($tour_nids)) {
$query->entityCondition('entity_id', $tour_nids, 'IN');
}
$results = $query->execute();
$tours = isset($results['node']) ? entity_load('node', array_keys($results['node'])) : array();
$tour_objects = array();
foreach ($tours as $tour) {
$tour_objects[$tour->nid] = array();
$stops = _aicapp_get_tour_stops($tour);
foreach ($stops as $delta => $stop) {
if (!isset($stop['object']) || (count($object_nids) && !in_array($stop['object'], $object_nids))) {
continue;
}
$tour_objects[$tour->nid][$stop['object']] = $stop;
}
}
return array_filter($tour_objects);
}
function _aicapp_get_tour_stops($tour) {
$stops = array();
if (empty($tour->nid)) {
return $stops;
}
if ($fc_items = field_get_items('node', $tour, 'field_tour_stops')) {
$i = 0;
foreach ($fc_items as $fc_item) {
$item = field_collection_field_get_entity($fc_item);
$audio_commentary_id = !empty($item->field_tour_stop_audio_commentary) ? $item->field_tour_stop_audio_commentary[LANGUAGE_NONE][0]['target_id'] : NULL;
$audio_commentary = $audio_commentary_id ? entity_load_single('field_collection_item', $audio_commentary_id) : NULL;
$audio_node_id = !empty($audio_commentary->field_audio_commentary_audio) ? $audio_commentary->field_audio_commentary_audio[LANGUAGE_NONE][0]['target_id']: NULL;
$bumper_node_id = !empty($item->field_tour_stop_bumper) ? $item->field_tour_stop_bumper[LANGUAGE_NONE][0]['target_id'] : NULL;
$object_node_id = !empty($item->field_tour_stop_object) ? $item->field_tour_stop_object[LANGUAGE_NONE][0]['target_id'] : NULL;
$stops[] = array(
'object' => $object_node_id,
'audio_id' => $audio_node_id,
'audio_bumper' => $bumper_node_id,
'sort' => $i,
);
$i++;
}
}
return $stops;
}
/**
* Helper function for aicapp_gendata_form_submit()
* Similar to above, but the reverse. Notify of objects that are now on display in gallery.
*/
function _email_object_gallery_change($changed) {
if (!empty($changed)) {
global $base_url;
$site_name = variable_get('site_name');
$from = 'admin@' . $base_url;
$to = variable_get('site_mail', '');
$msg = '<p>This automated email is to notify you that we\'ve found object(s)
that have changed gallery locations. You might need to update the object\'s location (map LAT/LNG) values:</p><ul>';
$object_list = '';
foreach ($changed as $object) {
$object_list .= '<li>object_id - ' . $object['object_id'] . ' - ' . $object['title'];
$object_list .= '<br />Previous location: ' . $object['old_location'];
$object_list .= ', New location: ' . $object['location'] . '</li>';
}
$msg .= $object_list . '</ul><p>Do not reply to this email.</p>';
$headers = "From: <" . $from . ">\nMIME-Version: 1.0\nContent-type: text/html; charset=utf-8";
$subject = $site_name . ': Object Changed Location Notice';
if (mail($to, $subject, $msg, $headers)) {
drupal_set_message('Email notice was sent regarding objects that CHANGED galleries:<ul>' . $object_list . '</ul>', 'status');
}
else {
drupal_set_message('Email notice unable to send. (These objects CHANGED galleries):<ul>' . $object_list . '</ul>', 'error');
}
}
else {
drupal_set_message('No gallery changes were found.', 'status');
}
}
/*
* Update the gallery_location field, and into_gallery fields for newly displayed objects based on data hub
*/
function _update_object_galleries($recent_added, $locations) {
foreach ($recent_added as $nid) {
$node = node_load($nid);
$entity = entity_metadata_wrapper('node', $node);
$entity->field_gallery_location = (string) $locations[$nid];
$entity->field_in_gallery->set(1);
$entity->status->set(0); // status is 0 (unpublished) because they want to look at it before it gets published.
$entity->save();
}
}
/**
* Take all the data in the Drupal DB, adding data from matching records in
* the data hub, and write them to JSON file.
*/
function aicapp_gendata_form_submit($form, &$form_state) {
global $base_path;
// Load the general info by loading node number 1.
// @TODO instead of hard coding node 1, maybe add a configuration field in the
// admin settings page for this module?
$node_gen = node_load(1);
strip_drupal_from_node($node_gen);
// Load all published gallery nodes.
$galleries = node_load_multiple(array(), array('type' => AICAPP_TYPE_GALLERY, 'status' => 1));
// Load all published objects.
$objects = node_load_multiple(array(), array('type' => AICAPP_TYPE_OBJECT, 'status' => 1));
// Load audio nodes.
$audio_files = node_load_multiple(array(), array('type' => AICAPP_TYPE_AUDIO, 'status' => 1));
// Load tours nodes.
$tours = node_load_multiple(array(), array('type' => AICAPP_TYPE_TOUR));
// Load map annontations that are published.
$map_annotations = node_load_multiple(array(), array('type' => AICAPP_TYPE_MAP_ANNOTATION, 'status' => 1));
// Load map floor that are published. There should only be 1 node loaded.
$map_floor = node_load_multiple(array(), array('type' => AICAPP_TYPE_MAP_FLOOR, 'status' => 1));
// Load messages that are published.
$app_messages = node_load_multiple(array(), array('type' => AICAPP_TYPE_MESSAGE, 'status' => 1));
// Load exhibition from a view, which give the ordered and promoted exhibitions.
$exhibitions = views_get_view_result('exhibitions', 'page');
// Store a list of featured items.
$featured = array();
// Gallery nodes.
$gallery_ids = array();
$by_gallery_id = array();
foreach ($galleries as $gallery) {
// Strip out Drupal native stuff we don't need in the JSON output from galleries.
strip_drupal_from_node($gallery);
$gallery_ids[] = $gallery->gallery_id;
$by_gallery_id[$gallery->gallery_id] = $gallery;
}
$galleries['#updated'] = array();
// Query the data aggregrator for recent data using the gallery ids.
$options = array('ids' => $gallery_ids, 'limit' => count($gallery_ids));
$url = AICAPP_DATA_API_HOST_INTERNAL . AICAPP_DATA_API_ENDPOINT_GALLERIES;
$results = _aicapp_fetch_data($url, $options);
if (!empty($results)) {
// @TODO a better way would be to only request the fields we want.
$gallery_to_skip = array(
'id', 'lake_guid', 'category_ids', 'last_updated_fedora', 'latlon',
'last_updated_source', 'last_updated', 'last_updated_citi', 'api_model', 'api_link', 'timestamp',
);
foreach ($results as $item) {
$id = isset($item['id']) ? $item['id'] : NULL;
$galleryFloor = isset($item['floor']) ? $item['floor'] : NULL;
$galleryLatitude = isset($item['latitude']) ? $item['latitude'] : NULL;
if (empty($id) || !isset($by_gallery_id[$id]) || !isset($galleryFloor) || !isset($galleryLatitude)) {
continue;
}
$gallery = $by_gallery_id[$id];
foreach ($item as $key => $val) {
if ($key === 'is_closed') {
$key = 'closed';
unset($gallery->is_closed);
}
elseif (in_array($key, $gallery_to_skip)) {
continue;
}
$gallery->{$key} = $val;
//fields not needed in json
unset($gallery->status);
unset($gallery->type);
}
$galleries['#updated'][$gallery->nid] = $gallery;
}
}
// Use the updated galleries.
$galleries = $galleries['#updated'];
// Now we build up the objects array for JSON output.
$object_ids = array();
$by_object_id = array();
$objects['#updated'] = array();
foreach ($objects as $object) {
if (!is_object($object)) {
continue;
}
$entity = entity_metadata_wrapper('node', $object);
$object_id = $entity->field_object_id->value();
if (empty($object_id)) {
// If there is no object id, then we have a custom object.
$object->id = NULL;
$object->object_id = NULL;
// Add local values.
$objects['#updated'][$object->nid] = _aicapp_update_local_artwork_data($object, $entity);
}
else {
$object_ids[] = $object_id;