-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelper_base.php
3968 lines (3822 loc) · 151 KB
/
helper_base.php
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
* Base class for client helper classes.
*
* Indicia, the OPAL Online Recording Toolkit.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/gpl.html.
*
* @author Indicia Team
* @license http://www.gnu.org/licenses/gpl.html GPL 3.0
* @link https://github.com/Indicia-Team/client_helpers
*/
if (file_exists(dirname(__FILE__) . '/helper_config.php')) {
require_once 'helper_config.php';
}
require_once 'lang.php';
global $indicia_templates;
/**
* Provides control templates to define the output of the data entry helper class.
*/
$indicia_templates = [
'blank' => '',
'prefix' => '',
'formControlClass' => 'form-control',
'controlWrap' => "<div id=\"ctrl-wrap-{id}\" class=\"form-row ctrl-wrap{wrapClasses}\">{control}</div>\n",
'controlWrapErrorClass' => '',
// Template for control with associated buttons/icons to appear to the side.
'controlAddonsWrap' => "{control}{addons}",
'justControl' => "{control}\n",
'label' => '<label for="{id}"{labelClass}>{label}:</label>',
// Use if label ends with another punctuation mark.
'labelNoColon' => '<label for="{id}"{labelClass}>{label}</label>',
'labelAfter' => '<label for="{id}"{labelClass}>{label}</label>',
'toplabel' => '<label data-for="{id}"{labelClass}>{label}:</label>',
'toplabelNoColon' => '<label data-for="{id}"{labelClass}>{label}</label>',
'suffix' => "\n",
'requiredsuffix' => "<span class=\"deh-required\">*</span>",
'button' => '<button id="{id}" type="button" title="{title}"{class}>{caption}</button>',
// Button classes. If changing these, keep the indicia-button class to ensure functionality works.
'buttonDefaultClass' => 'indicia-button',
'buttonHighlightedClass' => 'indicia-button',
'buttonWarningClass' => 'indicia-button',
'buttonSmallClass' => 'btn-xs',
// Classes applied to <a> when styled like a button.
'anchorButtonClass' => 'indicia-button',
'submitButton' => '<input id="{id}" type="submit"{class} name="{name}" value="{caption}" />',
// Floats.
'floatLeftClass' => 'left',
'floatRightClass' => 'right',
// Message boxes.
'messageBox' => '<div class="page-notice ui-state-default ui-corner-all">{message}</div>',
'warningBox' => '<div class="page-notice ui-state-highlight ui-corner-all"><span class="fas fa-exclamation-triangle"></span>{message}</div>',
// Lock icons.
'lock_icon' => '<span id="{id}_lock" class="unset-lock"> </span>',
'lock_javascript' => "indicia.locks.initControls (
\"" . lang::get('locked tool-tip') . "\",
\"" . lang::get('unlocked tool-tip') . "\",
\"{lock_form_mode}\"
);\n",
'validation_message' => "<p class=\"{class}\">{error}</p>\n",
'validation_icon' => '<span class="ui-state-error ui-corner-all validation-icon"><span class="ui-icon ui-icon-alert"></span></span>',
'error_class' => 'inline-error',
'invalid_handler_javascript' => "function(form, validator) {
var tabselected=false;
jQuery.each(validator.errorMap, function(ctrlId, error) {
// select the tab containing the first error control
var ctrl = jQuery('[name=' + ctrlId.replace(/:/g, '\\\\:').replace(/\[/g, '\\\\[').replace(/\\]/g, '\\\\]') + ']');
if (!tabselected) {
var tp=ctrl.filter('input,select,textarea').closest('.ui-tabs-panel');
if (tp.length===1) {
indiciaFns.activeTab($(tp).parent(), tp.id);
}
tabselected = true;
}
ctrl.parents('fieldset').removeClass('collapsed');
ctrl.parents('.fieldset-wrapper').show();
});
}",
'image_upload' => "<input type=\"file\" id=\"{id}\" name=\"{fieldname}\" accept=\"png|jpg|gif|jpeg|mp3|wav\" {title}/>\n" .
"<input type=\"hidden\" id=\"{pathFieldName}\" name=\"{pathFieldName}\" value=\"{pathFieldValue}\"/>\n",
'text_input' => '<input {attribute_list} id="{id}" name="{fieldname}"{class} {disabled} {readonly} value="{default|escape}" {title} {maxlength} />'."\n",
'hidden_text' => '<input type="hidden" id="{id}" name="{fieldname}" {disabled} value="{default}" />',
'password_input' => '<input type="password" id="{id}" name="{fieldname}"{class} {disabled} value="{default}" {title} />'."\n",
'textarea' => '<textarea id="{id}" name="{fieldname}"{class} {disabled} cols="{cols}" rows="{rows}" {title}>{default}</textarea>'."\n",
'checkbox' => '<input type="hidden" name="{fieldname}" value="0"/><input type="checkbox" id="{id}" name="{fieldname}" value="1"{class}{checked}{disabled} {title} />'."\n",
'training' => '<input type="hidden" name="{fieldname}" value="{hiddenValue}"/><input type="checkbox" id="{id}" name="{fieldname}" value="1"{class}{checked}{disabled} {title} />'."\n",
'date_picker' => '<input type="text" {attribute_list} {class} id="{id}" name="{fieldname}" value="{default}" style="display: none" {title}/>
<input type="date" {attribute_list_date} class="{datePickerClass}" id="{id}:date">' . "\n",
'date_picker_mode_toggle' => '<span>{vagueLabel}:</span> <label class="switch">
<input type="checkbox" class="date-mode-toggle" id="{id}:toggle">
<span class="slider round"></span>
</label>' . "\n",
'select' => '<select {attribute_list} id="{id}" name="{fieldname}"{class} {disabled} {title}>{items}</select>',
'select_item' => '<option value="{value}"{selected}{attribute_list}>{caption}</option>',
'select_species' => '<option value="{value}" {selected} >{caption} - {common}</option>',
'listbox' => '<select id="{id}" name="{fieldname}"{class} {disabled} size="{size}" multiple="{multiple}" {title}>{items}</select>',
'listbox_item' => '<option value="{value}"{selected}{attribute_list}>{caption}</option>',
'list_in_template' => '<ul{class} {title}>{items}</ul>',
'check_or_radio_group' => '<ul {class} id="{id}">{items}</ul>',
'check_or_radio_group_item' => '<li>{sortHandle}<input type="{type}" name="{fieldname}" id="{itemId}" value="{value}"{class}{checked}{title} {disabled}/><label for="{itemId}">{caption}</label></li>',
'map_panel' => '<div id="map-container" style="width: {width};"><div id="map-loading" class="loading-spinner" style="display: none"><div>Loading...</div></div><div id="{divId}" style="width: {width}; height: {height};"{class}></div></div>',
'georeference_lookup' => '<input type="text" id="imp-georef-search"{class} />{searchButton}' .
'<div id="imp-georef-div" class="ui-corner-all ui-widget-content ui-helper-hidden">' .
'<div id="imp-georef-output-div"></div> ' .
'{closeButton}' .
'</div>',
'tab_header' => "<ul class=\"tab-header\">{tabs}</ul>\n",
'taxon_label' => '<div class="biota"><span class="nobreak sci binomial"><em class="taxon-name">{taxon}</em></span> {authority} '.
'<span class="nobreak vernacular">{default_common_name}</span></div>',
'single_species_taxon_label' => '{taxon}',
'treeview_node' => '<span>{caption}</span>',
'tree_browser' => '<div{outerClass} id="{divId}"></div><input type="hidden" name="{fieldname}" id="{id}" value="{default}"{class}/>',
'tree_browser_node' => '<span>{caption}</span>',
'autocomplete' => '<input type="hidden" class="hidden" id="{id}" name="{fieldname}" value="{default}" />' .
'<input id="{inputId}" name="{inputId}" type="text" value="{defaultCaption}" {class} {disabled} {title} {attribute_list}/>' . "\n",
'autocomplete_javascript' => "
$('input#{escaped_input_id}').change(function() {
if ($('input#{escaped_id}').data('set-for') !== $('input#{escaped_input_id}').val()) {
$('input#{escaped_id}').val('');
}
});
$('input#{escaped_input_id}').autocomplete('{url}',
{
extraParams : {
orderby : '{captionField}',
mode : 'json',
qfield : '{captionField}',
{sParams}
},
simplify: {simplify},
selectMode: {selectMode},
warnIfNoMatch: {warnIfNoMatch},
continueOnBlur: {continueOnBlur},
matchContains: {matchContains},
parse: function(data)
{
// Clear the current selected key as the user has changed the search text
$('input#{escaped_id}').val('');
var results = [], done = [];
$.each(data, function(i, item) {
if ({duplicateCheck}) {
results.push({
'data' : item,
'result' : item.{captionField},
'value' : item.{valueField}
});
{storeDuplicates}
}
});
return results;
},
formatItem: {formatFunction}
{max}
});
$('input#{escaped_input_id}').result(function(event, data) {
$('input#{escaped_id}').attr('value', data.{valueField});
// Remember what text string this value was for.
$('input#{escaped_id}').data('set-for', $('input#{escaped_input_id}').val());
$('.item-icon').remove();
if (typeof data.icon!=='undefined') {
$('input#{escaped_input_id}').after(data.icon).next().hover(indiciaFns.hoverIdDiffIcon);
}
$('input#{escaped_id}').trigger('change', data);
});
",
'autocomplete_new_taxon_form' => '
<div style="display: none">
<fieldset class="popup-form" id="new-taxon-form">
<legend>{title}</legend>
<p>{helpText}</p>
<label for="new-taxon-name">Taxon name:</label>
<input type="text" id="new-taxon-name" class="{required:true}"/><span class="deh-required">*</span><br />
<label for="new-taxon-group">Taxon group:</label>
<select id="new-taxon-group" class="{required:true}"><span class="deh-required">*</span>
{taxonGroupOpts}
</select><br/>
<button type="button" class="indicia-button" id="do-add-new-taxon">Add taxon</button>
</fieldset>
</div>
',
'sub_list' => '<div id="{id}:box" class="control-box wide"><div>'."\n".
'<div>'."\n".
"{panel_control}\n".
'</div>'."\n".
'<ul id="{id}:sublist" class="ind-sub-list">{items}</ul>{subListAdd}'."\n".
'</div></div>'."\n",
'sub_list_item' => '<li class="ui-widget-content ui-corner-all"><span class="ind-delete-icon"> </span>{caption}'.
'<input type="hidden" name="{fieldname}" value="{value}" /></li>',
'postcode_textbox' => '<input type="text" name="{fieldname}" id="{id}"{class} value="{default}" '.
'onblur="javascript:indiciaFns.decodePostcode(\'{linkedAddressBoxId}\');" />'."\n",
'sref_textbox' => '<input type="text" id="{id}" name="{fieldname}" {class} {disabled} value="{default}" />' .
'<input type="hidden" id="{geomid}" name="{geomFieldname}" value="{defaultGeom}" />'."\n",
'sref_textbox_latlong' => '<label for="{idLat}">{labelLat}:</label>'.
'<input type="text" id="{idLat}" name="{fieldnameLat}" {class} {disabled} value="{defaultLat}" /><br />' .
'<label for="{idLong}">{labelLong}:</label>'.
'<input type="text" id="{idLong}" name="{fieldnameLong}" {class} {disabled} value="{defaultLong}" />' .
'<input type="hidden" id="{geomid}" name="geomFieldname" value="{defaultGeom}" />'.
'<input type="hidden" id="{id}" name="{fieldname}" value="{default}" />',
'attribute_cell' => "\n<td class=\"scOccAttrCell ui-widget-content {class}\" headers=\"{headers}\">{content}</td>",
'taxon_label_cell' => "\n<td class=\"scTaxonCell{editClass}\" headers=\"{tableId}-species-{idx}\" {colspan}>{content}</td>",
'helpText' => "\n<p class=\"{helpTextClass}\">{helpText}</p>",
'file_box' => '', // the JQuery plugin default will apply, this is just a placeholder for template overrides.
'file_box_initial_file_info' => '', // the JQuery plugin default will apply, this is just a placeholder for template overrides.
'file_box_uploaded_image' => '', // the JQuery plugin default will apply, this is just a placeholder for template overrides.
'paging_container' => "<div class=\"pager ui-helper-clearfix\">\n{paging}\n</div>\n",
'paging' => '<div class="left">{first} {prev} {pagelist} {next} {last}</div><div class="right">{showing}</div>',
'jsonwidget' => '<div id="{id}" {class}></div>',
'report_picker' => '<div id="{id}" {class}>{reports}<div class="report-metadata"></div><button type="button" id="picker-more">{moreinfo}</button><div class="ui-helper-clearfix"></div></div>',
'report_download_link' => '<div class="report-download-link"><a href="{link}"{class}>{caption}</a></div>',
'verification_panel' => '<div id="verification-panel">{button}<div class="messages" style="display: none"></div></div>',
'two-col-50' => '<div class="two columns"{attrs}><div class="column">{col-1}</div><div class="column">{col-2}</div></div>',
'loading_overlay' => '<div class="loading-spinner" style="display: none"><div>Loading...</div></div>',
'report-table' => '<table{class}>{content}</table>',
'report-thead' => '<thead{class}>{content}</thead>',
'report-thead-tr' => '<tr{class}{title}>{content}</tr>',
'report-thead-th' => '<th>{content}</th>',
'report-tbody' => '<tbody>{content}</tbody>',
'report-tbody-tr' => '<tr{class}{rowId}{rowTitle}>{content}</tr>',
'report-tbody-td' => '<td{class}>{content}</td>',
'report-action-button' => '<a{class}{href}{onclick}>{content}</a>',
'data-input-table' => '<table{class}{id}>{content}</table>',
'review_input' => '<div{class}{id}><div{headerClass}{headerId}>{caption}</div>
<div id="review-map-container"></div>
<div{contentClass}{contentId}></div>
</div>',
// Rows in a list of key-value pairs.
'dataValueList' => '<div class="detail-panel" id="{id}"><h3>{title}</h3><dl class="dl-horizontal">{content}</dl></div>',
'dataValue' => '<dt>{caption}</dt><dd{class}>{value}</dd>',
'speciesDetailsThumbnail' => '<div class="gallery-item"><a data-fancybox="gallery" href="{imageFolder}{the_text}"><img src="{imageFolder}{imageSize}-{the_text}" title="{caption}" alt="{caption}"/><br/>{caption}</a></div>',
];
/**
* Base class for the report and data entry helpers. Provides several generally useful methods and also includes
* resource management.
*/
class helper_base {
/*
* Variables that can be specified in helper_config.php, or should be set by
* the host system.
*/
/**
* Base URL of the warehouse we are linked to.
*
* @var string
*/
public static $base_url = '';
/**
* Path to proxy script for calls to the warehouse.
*
* Allows the warehouse to sit behind a firewall only accessible from the
* server.
*
* @var string
*/
public static $warehouse_proxy = NULL;
/**
* Base URL of the GeoServer we are linked to if GeoServer is used.
*
* @var string
*/
public static $geoserver_url = '';
/**
* A temporary location for uploaded images.
*
* Images are stored here when uploaded by a recording form but before they
* are sent to the warehouse.
*
* @var string
*/
public static $interim_image_folder;
/**
* Google API key for place searches.
*
* @var string
*/
public static $google_api_key = '';
/**
* Google Maps JavaScript API key.
*
* @var string
*/
public static $google_maps_api_key = '';
/**
* Bing Maps API key.
*
* @var string
*/
public static $bing_api_key = '';
/**
* Ordnance Survey Maps API key.
*
* @var string
*/
public static $os_api_key = '';
/**
* Breadcrumb info.
*
* @var array
*/
public static $breadcrumb = NULL;
/**
* Force breadcrumb to display even on non-node based pages.
*
* @var array
*/
public static $force_breadcrumb = FALSE;
/**
* Setting which allows the host site (e.g. Drupal) handle translation.
*
* For example, when TRUE, a call to lang::get() is delegated to Drupal's t()
* function.
*
* @var bool
*/
public static $delegate_translation_to_hostsite = FALSE;
/**
* Setting which allows the host site (e.g. Drupal) handle caching.
*
* Defaults to true but only delegates if there are hostsite_cache_get() and
* hostsite_cache_get() functions available.
*
* @var bool
*/
public static $delegate_caching_to_hostsite = TRUE;
/**
* Check on maximum file size for image uploads.
*
* @var string
*/
public static $upload_max_filesize = '4M';
/*
* End of variables that can be specified in helper_config.php.
*/
/**
* @var boolean Flag set to true if returning content for an AJAX request. This allows the javascript to be returned
* direct rather than embedding in document.ready and window.onload handlers.
*/
public static $is_ajax = FALSE;
/**
* @var integer Website ID, stored here to assist with caching.
*/
public static $website_id = NULL;
/**
* @var Array List of resources that have been identified as required by the
* controls used. This defines the JavaScript and stylesheets that must be
* added to the page. Each entry is an array containing stylesheets and
* javascript sub-arrays. This has public access so the Drupal module can
* perform Drupal specific resource output.
*/
public static $required_resources = [];
/**
* @var Array List of all available resources known. Each resource is named, and contains a sub array of
* deps (dependencies), stylesheets and javascripts.
*/
public static $resource_list = NULL;
/**
* Any control that wants to access the read authorisation tokens from JavaScript can set them here. They will then
* be available from indiciaData.auth.read.
* @var Array
*/
public static $js_read_tokens = NULL;
/**
* @var string Path to Indicia JavaScript folder. If not specified, then it is
* calculated from the Warehouse $base_url.
* This path should be a full path on the server (starting with '/' exluding
* the domain and ending with '/').
*/
public static $js_path = NULL;
/**
* @var string Path to Indicia CSS folder. If not specified, then it is calculated from the Warehouse $base_url.
* This path should be a full path on the server (starting with '/' exluding the domain).
*/
public static $css_path = NULL;
/**
* Path to Indicia Images folder.
*
* @var string
*/
public static $images_path = NULL;
/**
* Path to Indicia cache folder. Defaults to client_helpers/cache.
*
* @var string
*/
public static $cache_folder = FALSE;
/**
* List of resources that have already been dumped out.
*
* Avoids duplication. For example, if the site template includes JQuery set
* $dumped_resources[]='jquery'.
*
* @var array
*/
public static $dumped_resources = [];
/**
* Data to be added to the indiciaData JavaScript variable.
*
* @var array
*/
public static $indiciaData = [
'lang' => [],
'templates' => [],
];
/**
* Inline JavaScript to be added to the page.
*
* Each control that needs custom JavaScript can append the script to this
* variable. Will be enclosed in a document ready event hander.
*
* @var string
*/
public static $javascript = '';
/**
* JavaScript text to be emitted after all other JavaScript.
*
* @var string
*/
public static $late_javascript = '';
/**
* JavaScript text to be emitted during window.onload.
*
* @var string
*/
public static $onload_javascript = '';
/**
* Setting to completely disable loading from the cache.
*
* @var bool
*/
public static $nocache = FALSE;
/**
* Age of image files in seconds before they will be considered for purging.
*
* @var int
*/
public static $interim_image_expiry = 14400;
/**
* File types and extensions allowed for upload.
*
* Contains elements for each media type that can be uploaded. Each element
* is an array of allowed file extensions for that media type. Used for
* filtering files to upload on client side. File extensions must be in lower
* case. Each entry should have its mime type included in $upload_mime_types.
*
* @var array
*/
public static $upload_file_types = [
'image' => ['jpg', 'gif', 'png', 'jpeg'],
'pdf' => ['pdf'],
'audio' => ['mp3', 'wav'],
'zerocrossing' => ['zc'],
];
/**
* Mime types allowed for upload.
*
* Contains elements for each media type that can be uploaded. Each element
* is an array of the allowed mime subtypes for that media type. Used for
* testing uploaded files. Each entry in $upload_file_types should have its
* mime type in this list.
*
* @var array
*/
public static $upload_mime_types = [
'image' => ['jpeg', 'gif', 'png'],
'application' => ['pdf', 'octet-stream'],
'audio' => ['mpeg', 'x-wav'],
];
/**
* List of methods used to report a validation failure. Options are message, message, hint, icon, colour, inline.
* The inline option specifies that the message should appear on the same line as the control.
* Otherwise it goes on the next line, indented by the label width. Because in many cases, controls
* on an Indicia form occupy the full available width, it is often more appropriate to place error
* messages on the next line so this is the default behaviour.
* @var array
*/
public static $validation_mode = ['message', 'colour'];
/**
* Name of the form which has been set up for jQuery validation, if any.
*
* @var array
*/
public static $validated_form_id = NULL;
/**
* jQuery Validation should only initialise once.
*
* @var bool
*/
private static $validationInitialised = FALSE;
/**
* @var string Helptext positioning. Determines where the information is displayed when helpText is defined for a control.
* Options are before, after.
*/
public static $helpTextPos = 'after';
/**
* Form Mode. Initially unset indicating new input, but can be set to ERRORS or RELOAD.
*
* @var string
*/
public static $form_mode = NULL;
/**
* List of all error messages returned from an attempt to save.
*
* @var array
*/
public static $validation_errors = NULL;
/**
* Default validation rules to apply to the controls on the form.
*
* Used if the built in client side validation is used (with the jQuery
* validation plugin). This array can be replaced if required.
*
* @var array
*
* @todo This array could be auto-populated with validation rules for a
* survey's fields from the Warehouse.
*/
public static $default_validation_rules = [
'sample:date' => ['required', 'date'],
'sample:entered_sref' => ['required'],
'occurrence:taxa_taxon_list_id' => ['required'],
'location:name' => ['required'],
'location:centroid_sref' => ['required'],
];
/**
* List of messages defined to pass to the validation plugin.
*
* @var array
*/
public static $validation_messages = [];
/**
* Length of time in seconds after which cached Warehouse responses will start to expire.
*
* @var int
*/
public static $cache_timeout = 3600;
/**
* Chance of a cached file being refreshed after expiry.
*
* On average, every 1 in $cache_chance_expire times the Warehouse is called
* for data which is cached but older than the cache timeout, the cached data
* will be refreshed. This introduces a random element to cache refreshes so
* that no single form load event is responsible for refreshing all cached
* content.
*
* @var int
*/
public static $cache_chance_refresh_file = 10;
/**
* Chance of a cache purge evemt.
*
* On average, every 1 in $cache_chance_purge times the Warehouse is called
* for data, all files older than 5 times the cache_timeout will be purged,
* apart from the most recent $cache_allowed_file_count files.
*
* @var int
*/
public static $cache_chance_purge = 500;
/**
* Number of recent files allowed in the cache.
*
* Number of recent files allowed in the cache which the cache will not
* bother clearing during a deletion operation. They will be refreshed
* occasionally when requested anyway.
*
* @var int
*/
public static $cache_allowed_file_count = 50;
/**
* A place to keep data and settings for Indicia code, to avoid using globals.
*
* @var array
*/
public static $data = [];
/*
* Global format for display of dates such as sample date, date attributes in Drupal.
* Note this only affects the loading of the date itself when a form in edit mode loads, the format displayed as soon as the
* date picker is selected is determined by Drupal's settings. So make sure Drupal's date format and this option match up.
* @todo Need to create a proper config option for this.
* @todo Need to ensure this setting is utilised every where it should be.
*
*/
public static $date_format = 'd/m/Y';
/**
* Indicates if any form controls have specified the lockable option.
*
* If so, we will need to output some javascript.
*
* @var bool
*/
protected static $using_locking = FALSE;
/**
* Are we linking in the default stylesheet?
*
* Handled sligtly different to the others so it can be added to the end of
* the list, allowing our CSS to override other stuff.
*
* @var bool
*/
protected static $default_styles = FALSE;
/**
* Array of html attributes. When replacing items in a template, these get automatically wrapped. E.g.
* a template replacement for the class will be converted to class="value". The key is the parameter name,
* and the value is the html attribute it will be wrapped into.
*/
protected static $html_attributes = [
'class' => 'class',
'outerClass' => 'class',
'selected' => 'selected',
];
/**
* List of error messages that have been displayed.
*
* So we don't duplicate them when dumping any remaining ones at the end.
*
* @var array
*/
protected static $displayed_errors = [];
/**
* Track if we have already output the indiciaFunctions.
*
* @var bool
*/
protected static $indiciaFnsDone = FALSE;
/**
* Returns the URL to access the warehouse by, respecting proxy settings.
*
* @return string
* URL.
*/
public static function getProxiedBaseUrl() {
return empty(self::$warehouse_proxy) ? self::$base_url : self::$warehouse_proxy;
}
/**
* Returns the folder to store uploaded images in before submission.
*
* When an image has been uploaded on a form but not submitted to the
* warehouse, it is stored in this folder location temporarily.
*
* @param string $mode
* Set to one of these options:
* * fullpath - full path from root of the server disk.
* * domain - path from the root of the domain.
* * relative - path relative to the current script location (default)
*
* @return string
* The folder location.
*/
public static function getInterimImageFolder($mode = 'relative') {
$folder = isset(self::$interim_image_folder)
? self::$interim_image_folder
: self::client_helper_path() . 'upload/';
switch ($mode) {
case 'fullpath':
return getcwd() . '/' . $folder;
case 'domain':
return self::getRootFolder() . $folder;
default:
return $folder;
}
}
/**
* Utility function to insert a list of translated text items for use in JavaScript.
*
* @param string $group
* Name of the group of strings.
* @param array $strings
* Associative array of keys and texts to translate.
*/
public static function addLanguageStringsToJs($group, array $strings) {
$translations = [];
foreach ($strings as $key => $text) {
$translations[$key] = lang::get($text);
}
self::$indiciaData['lang'][$group] = $translations;
}
/**
* Utility function to convert a list of strings into translated strings.
*
* @param array $strings
* Associative array of keys and texts to translate.
*
* @return array
* Associative array of translated strings keyed by untranslated string.
*/
public static function getTranslations(array $strings) {
$r = [];
foreach ($strings as $string) {
$r[$string] = lang::get($string);
}
return $r;
}
/**
* Adds a resource to the page (e.g. a set of CSS and/or JS files).
*
* Method to link up the external css or js files associated with a set of code.
* This is normally called internally by the control methods to ensure the required
* files are linked into the page so does not need to be called directly. However
* it can be useful when writing custom code that uses one of these standard
* libraries such as jQuery.
* Ensures each file is only linked once and that dependencies are included
* first and in the order given.
*
* @param string $resource
* Name of resource to link. The following options are available:
* * indiciaFns
* * jquery
* * datepicker
* * sortable
* * openlayers
* * graticule
* * clearLayer
* * addrowtogrid
* * speciesFilterPopup
* * import
* * indiciaMapPanel
* * indiciaMapEdit
* * postcode_search
* * locationFinder
* * createPersonalSites
* * autocomplete
* * addNewTaxon
* * indicia_locks
* * jquery_cookie
* * jquery_ui
* * jquery_form
* * json
* * reportPicker
* * treeview
* * treeview_async
* * googlemaps
* * multimap
* * virtualearth
* * fancybox
* * treeBrowser
* * defaultStylesheet
* * validation
* * plupload
* * dmUploader
* * uploader
* * jqplot
* * jqplot_bar
* * jqplot_pie
* * jqplot_category_axis_renderer
* * jqplot_canvas_axis_label_renderer
* * jqplot_trendline
* * reportgrid
* * freeformReport
* * tabs
* * wizardprogress
* * spatialReports
* * jsonwidget
* * timeentry
* * verification
* * complexAttrGrid
* * footable
* * indiciaFootableReport
* * indiciaFootableChecklist
* * html2pdf
* * review_input
* * sub_list
* * georeference_default_geoportal_lu
* * georeference_default_nominatim
* * georeference_default_google_places
* * georeference_default_indicia_locations
* * sref_handlers_4326
* * sref_handlers_osgb
* * sref_handlers_osie
* * font_awesome
* * leaflet
* * leaflet_google
* * file_classifier
* * brc_atlas
* * brc_charts
* * bigr
* * d3
* * html2canvas
*/
public static function add_resource($resource) {
// Ensure indiciaFns is always the first resource added.
if (!self::$indiciaFnsDone) {
self::$indiciaFnsDone = TRUE;
self::add_resource('indiciaFns');
}
$resourceList = self::get_resources();
// If this is an available resource and we have not already included it,
// then add it to the list.
if (array_key_exists($resource, $resourceList) && !in_array($resource, self::$required_resources)) {
if (isset($resourceList[$resource]['deps'])) {
foreach ($resourceList[$resource]['deps'] as $dep) {
self::add_resource($dep);
}
}
self::$required_resources[] = $resource;
}
}
/**
* List of external resources including stylesheets and js files used by the data entry helper class.
*/
public static function get_resources() {
if (self::$resource_list === NULL) {
$base = self::$base_url;
if (!self::$js_path) {
self::$js_path = $base . 'media/js/';
}
elseif (substr(self::$js_path, -1) != "/") {
// Ensure a trailing slash.
self::$js_path .= "/";
}
if (!self::$css_path) {
self::$css_path = $base . 'media/css/';
}
elseif (substr(self::$css_path, -1) != '/') {
// Ensure a trailing slash.
self::$css_path .= "/";
}
global $indicia_theme, $indicia_theme_path;
if (!isset($indicia_theme)) {
// Use default theme if page does not specify it's own.
$indicia_theme = 'default';
}
if (!isset($indicia_theme_path)) {
// Use default theme path if page does not specify it's own.
$indicia_theme_path = preg_replace('/css\/$/', 'themes/', self::$css_path);
}
// Ensure a trailing slash.
if (substr($indicia_theme_path, -1) !== '/') {
$indicia_theme_path .= '/';
}
self::$resource_list = [
'indiciaFns' => [
'deps' => ['jquery'],
'javascript' => [self::$js_path . "indicia.functions.js"],
],
'jquery' => [
'javascript' => [
self::$js_path . 'jquery.js',
],
],
'datepicker' => [
'deps' => ['jquery_cookie'],
'javascript' => [
self::$js_path . 'indicia.datepicker.js',
self::$js_path . 'date.polyfill/better-dom/dist/better-dom.min.js',
self::$js_path . 'date.polyfill/better-dateinput-polyfill/dist/better-dateinput-polyfill.min.js',
]
],
'sortable' => [
'javascript' => ['https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.js'],
],
'proj4-lib' => [
'javascript' => [
self::$js_path . 'proj4js.js',
],
],
'proj4' => [
'javascript' => [
self::$js_path . 'proj4defs.js',
],
'deps' => ['proj4-lib'],
],
'openlayers' => [
'javascript' => [
self::$js_path . (function_exists('iform_openlayers_get_file') ? iform_openlayers_get_file() : 'OpenLayers.js'),
self::$js_path . 'lang/en.js',
],
'deps' => ['proj4'],
],
'graticule' => [
'deps' => ['openlayers'],
'javascript' => [self::$js_path . 'indiciaGraticule.js'],
],
'clearLayer' => [
'deps' => ['openlayers'],
'javascript' => [self::$js_path . 'clearLayer.js'],
],
'hoverControl' => [
'deps' => ['openlayers'],
'javascript' => [self::$js_path . 'hoverControl.js'],
],
'addrowtogrid' => [
'deps' => ['validation'],
'javascript' => [self::$js_path . "addRowToGrid.js"],
],
'speciesFilterPopup' => [
'deps' => ['addrowtogrid'],
'javascript' => [self::$js_path . "speciesFilterPopup.js"],
],
'indiciaMapPanel' => [
'deps' => ['jquery', 'openlayers', 'jquery_ui', 'jquery_cookie', 'hoverControl'],
'javascript' => [self::$js_path . "jquery.indiciaMapPanel.js"],
],
'indiciaMapEdit' => [
'deps' => ['indiciaMapPanel'],
'javascript' => [self::$js_path . "jquery.indiciaMap.edit.js"],
],
'postcode_search' => [
'deps' => ['indiciaMapPanel'],
'javascript' => [self::$js_path . "postcode_search.js"],
],
'locationFinder' => [
'deps' => ['indiciaMapEdit'],
'javascript' => [self::$js_path . "jquery.indiciaMap.edit.locationFinder.js"],
],
'createPersonalSites' => [
'deps' => ['jquery'],
'javascript' => [self::$js_path . "createPersonalSites.js"],
],
'autocomplete' => [
'deps' => ['jquery'],
'stylesheets' => [self::$css_path . "jquery.autocomplete.css"],
'javascript' => [self::$js_path . "jquery.autocomplete.js"],
],
'addNewTaxon' => [
'javascript' => [self::$js_path . "addNewTaxon.js"],
],
'import' => [
'javascript' => [self::$js_path . "import.js"],
],
'indicia_locks' => [
'deps' => ['jquery_cookie', 'json'],
'javascript' => [self::$js_path . "indicia.locks.js"],
],
'jquery_cookie' => [
'deps' => ['jquery'],
'javascript' => [self::$js_path . "jquery.cookie.js"],