/
BrowseEngine.php
6874 lines (5921 loc) · 311 KB
/
BrowseEngine.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
/** ---------------------------------------------------------------------
* app/lib/Browse/BrowseEngine.php : Base class for browse interfaces
* ----------------------------------------------------------------------
* CollectiveAccess
* Open-source collections management software
* ----------------------------------------------------------------------
*
* Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
* Copyright 2009-2018 Whirl-i-Gig
*
* For more information visit http://www.CollectiveAccess.org
*
* This program is free software; you may redistribute it and/or modify it under
* the terms of the provided license as published by Whirl-i-Gig
*
* CollectiveAccess is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTIES whatsoever, including any implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This source code is free and modifiable under the terms of
* GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See
* the "license.txt" file for details, or visit the CollectiveAccess web site at
* http://www.CollectiveAccess.org
*
* @package CollectiveAccess
* @subpackage Browse
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License version 3
*
* ----------------------------------------------------------------------
*/
/**
*
*/
require_once(__CA_LIB_DIR__.'/BaseFindEngine.php');
require_once(__CA_LIB_DIR__.'/Datamodel.php');
require_once(__CA_LIB_DIR__.'/Db.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/AuthorityAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/CollectionsAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/EntitiesAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/LoansAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/MovementsAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/ObjectLotsAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/ObjectRepresentationsAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/ObjectsAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/OccurrencesAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/PlacesAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/StorageLocationsAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Attributes/Values/ListAttributeValue.php');
require_once(__CA_LIB_DIR__.'/Browse/BrowseResult.php');
require_once(__CA_LIB_DIR__.'/Browse/BrowseCache.php');
require_once(__CA_LIB_DIR__.'/Parsers/TimeExpressionParser.php');
require_once(__CA_APP_DIR__.'/helpers/searchHelpers.php');
require_once(__CA_APP_DIR__.'/helpers/accessHelpers.php');
require_once(__CA_MODELS_DIR__.'/ca_metadata_elements.php');
require_once(__CA_MODELS_DIR__.'/ca_lists.php');
require_once(__CA_MODELS_DIR__.'/ca_acl.php');
require_once(__CA_MODELS_DIR__.'/ca_relationship_types.php');
class BrowseEngine extends BaseFindEngine {
# ------------------------------------------------------
# Properties
# ------------------------------------------------------
/**
* @var Table number of browse subject (ie. the kind of record we're browsing for)
*/
private $opn_browse_table_num;
/**
* @var Table name of browse subject
*/
private $ops_browse_table_name;
/**
* @var Instance of BrowseCache class
*/
private $opo_ca_browse_cache;
/**
* @var subject type_id to limit browsing to (eg. only browse ca_objects with type_id = 10)
*/
private $opa_browse_type_ids = null;
/**
* @var option to expand type restrictions hierarchically
*/
private $opb_dont_expand_type_restrictions = false;
private $opb_dont_expand_source_restrictions = false;
/**
* @var Instance of Datamodel class
*/
protected $opo_datamodel;
/**
* @var Instance of Db database client
*/
protected $opo_db;
/**
* @var Instance of Configuration class loaded with application configuration (app.conf)
*/
private $opo_config;
/**
* @var Instance of Configuration class loaded with browse configuration (browse.conf)
*/
private $opo_ca_browse_config;
/**
* @var Array of browse settings loaded from browse.conf
*/
private $opa_browse_settings;
/**
* @var Array of filters to apply to browse results
*/
private $opa_result_filters;
/**
* @var
*/
private $ops_facet_group = null;
/**
* @var Flag indicating if browse criteria have changed since the last execute()
*/
private $opb_criteria_have_changed = false;
# ------------------------------------------------------
/**
* @var Type_id cache
*/
static $s_type_id_cache = array();
static $s_source_id_cache = array();
# ------------------------------------------------------
/**
*
*/
public function __construct($pm_subject_table_name_or_num, $pn_browse_id=null, $ps_browse_context='') {
$this->opo_db = new Db();
$this->opa_result_filters = array();
if (is_numeric($pm_subject_table_name_or_num)) {
$this->opn_browse_table_num = intval($pm_subject_table_name_or_num);
$this->ops_browse_table_name = Datamodel::getTableName($this->opn_browse_table_num);
} else {
$this->opn_browse_table_num = Datamodel::getTableNum($pm_subject_table_name_or_num);
$this->ops_browse_table_name = $pm_subject_table_name_or_num;
}
$this->opo_config = Configuration::load();
$this->opo_ca_browse_config = Configuration::load(__CA_CONF_DIR__.'/browse.conf');
$this->opa_browse_settings = $this->opo_ca_browse_config->getAssoc($this->ops_browse_table_name);
// Add "virtual" search facet - allows seeding of a browse with a search
$this->opa_browse_settings['facets']['_search'] = array(
'label_singular' => _t('Search'),
'label_plural' => _t('Searches')
);
// Add "virtual" relationship types facet - allows filtering of a browse by relationship types in a specific relationships (Eg. only return records that have at least one relationship with one of the specified types)
$this->opa_browse_settings['facets']['_reltypes'] = array(
'label_singular' => _t('Relationship type'),
'label_plural' => _t('Relationship types')
);
$this->_processBrowseSettings();
$this->opo_ca_browse_cache = new BrowseCache();
if ($pn_browse_id) {
$this->opo_ca_browse_cache->setParameter('table_num', $this->opn_browse_table_num);
$this->opo_ca_browse_cache->load($pn_browse_id);
} else {
$this->opo_ca_browse_cache->setParameter('table_num', $this->opn_browse_table_num);
$this->setContext($ps_browse_context);
}
}
# ------------------------------------------------------
/**
* Dynamically insert facet configuration
*
* @param string $ps_facet_name The name of the facet to insert
* @return bool true if add succeeded
*/
public function addFacetConfiguration($ps_facet_name, $pa_options) {
$this->opa_browse_settings['facets'][$ps_facet_name] = $pa_options;
$this->_processBrowseSettings();
return true;
}
# ------------------------------------------------------
/**
* Forces reload of the browse instance (ie. a cached browse) from the database
*
* @param int $pn_browse_id The id of the browse to reload
* @return bool true if reload succeeded, false if browse_id was invalid
*/
public function reload($pn_browse_id) {
$this->opo_ca_browse_cache = new BrowseCache();
$this->opo_ca_browse_cache->setParameter('table_num', $this->opn_browse_table_num);
return (bool)$this->opo_ca_browse_cache->load($pn_browse_id);
}
# ------------------------------------------------------
/**
* Rewrite browse config settings as needed before starting actual processing of browse
*/
private function _processBrowseSettings() {
$va_revised_facets = array();
foreach($this->opa_browse_settings['facets'] as $vs_facet_name => $va_facet_info) {
global $g_ui_locale;
if(is_array($va_facet_info['label_singular'])) {
if(isset($va_facet_info['label_singular'][$g_ui_locale])) {
$va_facet_info['label_singular'] = $va_facet_info['label_singular'][$g_ui_locale];
}
}
if(is_array($va_facet_info['label_plural'])) {
if(isset($va_facet_info['label_plural'][$g_ui_locale])) {
$va_facet_info['label_plural'] = $va_facet_info['label_plural'][$g_ui_locale];
}
}
// group_mode = hierarchical is only supported for location facets when current location criteria is storage locations-only
if (($va_facet_info['type'] === 'location') && (caGetOption('group_mode', $va_facet_info, null) == 'hierarchical')) {
if((is_array($current_location_criteria = $this->opo_config->get('current_location_criteria'))) && (sizeof($current_location_criteria) == 1) && isset($current_location_criteria['ca_storage_locations'])) {
$va_facet_info['table'] = 'ca_storage_locations';
} else {
$va_facet_info['group_mode'] == 'none';
}
}
// generate_facets_for_types config directive triggers auto-generation of facet config for each type of an authority item
// it's typically employed to provide browsing of occurrences where the various types are unrelated
// you can also use this on other authorities to provide a finer-grained browse without having to know the type hierarchy ahead of time
if (($va_facet_info['type'] === 'authority') && isset($va_facet_info['generate_facets_for_types']) && $va_facet_info['generate_facets_for_types']) {
// get types for authority
$t_table = Datamodel::getInstanceByTableName($va_facet_info['table'], true);
$va_type_list = $t_table->getTypeList();
// auto-generate facets
foreach($va_type_list as $vn_type_id => $va_type_info) {
if ($va_type_info['is_enabled']) {
$va_facet_info = array_merge($va_facet_info, array(
'label_singular' => $va_type_info['name_singular'],
'label_singular_with_indefinite_article' => _t('a').' '.$va_type_info['name_singular'],
'label_plural' => $va_type_info['name_plural'],
'restrict_to_types' => array($va_type_info['item_id'])
));
$va_revised_facets[$vs_facet_name.'_'.$vn_type_id] = $va_facet_info;
}
}
} else {
$va_revised_facets[$vs_facet_name] = $va_facet_info;
}
}
// rewrite single_value settings for attribute and fieldList facets
foreach($va_revised_facets as $vs_facet => $va_facet_info) {
if (!((isset($va_facet_info['single_value'])) && strlen($va_facet_info['single_value']))) { continue; }
switch($va_facet_info['type']) {
case 'attribute':
$t_element = new ca_metadata_elements();
if ($t_element->load(array('element_code' => array_pop(explode(".", $va_facet_info['element_code']))))) {
if (($t_element->get('datatype') == __CA_ATTRIBUTE_VALUE_LIST__) && ($vn_list_id = $t_element->get('list_id'))) {
if ($vn_item_id = caGetListItemID($vn_list_id, $va_facet_info['single_value'])) {
$va_revised_facets[$vs_facet]['single_value'] = $vn_item_id;
}
}
}
break;
case 'fieldList':
$t_instance = Datamodel::getInstanceByTableName($this->ops_browse_table_name, true);
if ($vn_item_id = caGetListItemID($t_instance->getFieldInfo($va_facet_info['field'], 'LIST_CODE'), $va_facet_info['single_value'])) {
$va_revised_facets[$vs_facet]['single_value'] = $vn_item_id;
}
break;
}
}
$this->opa_browse_settings['facets'] = $va_revised_facets;
}
# ------------------------------------------------------
/**
* Returns unique id for current browse. The key is calculated based upon the browse subject,
* various options and the current browse criteria.
*
* @return string
*/
public function getBrowseID() {
return $this->opo_ca_browse_cache->getCacheKey();
}
# ------------------------------------------------------
/**
* Returns the table number of the browse subject
*
* @return int
*/
public function getSubject() {
return $this->opn_browse_table_num;
}
# ------------------------------------------------------
/**
* Returns an instance of the current browse subject
*
* @return BaseModel
*/
public function getSubjectInstance() {
return Datamodel::getInstanceByTableNum($this->opn_browse_table_num, true);
}
# ------------------------------------------------------
/**
* Sets the current browse context.
* Separate cache namespaces are maintained for each browse context; this means that
* if you do the same browse in different contexts each will be cached separately. This
* is handy when you have multiple interfaces (say the cataloguing back-end and a public front-end)
* using the same browse engine and underlying cache tables.
*
* @param string $ps_browse_context
* @return bool True on success
*/
public function setContext($ps_browse_context) {
$va_params = $this->opo_ca_browse_cache->setParameter('context', $ps_browse_context);
return true;
}
# ------------------------------------------------------
/**
* Returns current browse context
*
* @return string
*/
public function getContext() {
return ($vs_context = $this->opo_ca_browse_cache->getParameter('context')) ? $vs_context : '';
}
# ------------------------------------------------------
# Add/remove browse criteria
# ------------------------------------------------------
/**
* Add criteria to the current browse
*
* @param string $ps_facet_name Name of facet for which to add criteria
* @param array $pa_row_ids One or more facet values to browse on
*
* @return boolean - true on success, false on error
*/
public function addCriteria($ps_facet_name, $pa_row_ids, $pa_display_strings=null) {
if (is_null($pa_row_ids)) { return null;}
if (!is_array($pa_row_ids)) { $pa_row_ids = array($pa_row_ids); }
if (!in_array($ps_facet_name, ['_search', '_reltypes'])) {
if (!($va_facet_info = $this->getInfoForFacet($ps_facet_name))) { return false; }
if (!$this->isValidFacetName($ps_facet_name)) { return false; }
}
$va_criteria = $this->opo_ca_browse_cache->getParameter('criteria');
$va_criteria_display_strings = $this->opo_ca_browse_cache->getParameter('criteria_display_strings');
if (!is_array($pa_row_ids)) { $pa_row_ids = array($pa_row_ids); }
$o_purifier = caGetHTMLPurifier();
foreach($pa_row_ids as $vn_i => $vn_row_id) {
$vn_row_id = $o_purifier->purify(urldecode($vn_row_id)); // sanitize facet values
$va_criteria[$ps_facet_name][urldecode($vn_row_id)] = true;
if (isset($pa_display_strings[$vn_i])) { $va_criteria_display_strings[$ps_facet_name][urldecode($vn_row_id)] = $pa_display_strings[$vn_i]; }
}
$this->opo_ca_browse_cache->setParameter('criteria', $va_criteria);
$this->opo_ca_browse_cache->setParameter('criteria_display_strings', $va_criteria_display_strings);
$this->opo_ca_browse_cache->setParameter('sort', null);
$this->opo_ca_browse_cache->setParameter('facet_html', null);
$this->opb_criteria_have_changed = true;
return true;
}
# ------------------------------------------------------
/**
* Remove criteria from the current browse
*
* @param string $ps_facet_name Name of facet for which to remove criteria
* @param array $pa_row_ids One or more facet values applied as criteria. Values that are not current criteria are ignored.
*
* @return boolean - true on success, false on error
*/
public function removeCriteria($ps_facet_name, $pa_row_ids) {
if (is_null($pa_row_ids)) { return false;}
if (!($va_facet_info = $this->getInfoForFacet($ps_facet_name))) { return false; }
if (!$this->isValidFacetName($ps_facet_name)) { return false; }
$va_criteria = $this->opo_ca_browse_cache->getParameter('criteria');
$va_criteria_display_strings = $this->opo_ca_browse_cache->getParameter('criteria_display_strings');
if (!is_array($pa_row_ids)) { $pa_row_ids = array($pa_row_ids); }
foreach($pa_row_ids as $vn_row_id) {
unset($va_criteria[$ps_facet_name][urldecode($vn_row_id)]);
unset($va_criteria_display_strings[$ps_facet_name][urldecode($vn_row_id)]);
if(is_array($va_criteria[$ps_facet_name]) && !sizeof($va_criteria[$ps_facet_name])) {
unset($va_criteria[$ps_facet_name]);
unset($va_criteria_display_strings[$ps_facet_name]);
}
}
$this->opo_ca_browse_cache->setParameter('criteria', $va_criteria);
$this->opo_ca_browse_cache->setParameter('criteria_display_strings', $va_criteria_display_strings);
$this->opo_ca_browse_cache->setParameter('sort', null);
$this->opo_ca_browse_cache->setParameter('facet_html', null);
$this->opb_criteria_have_changed = true;
return true;
}
# ------------------------------------------------------
/**
* Indicates if the browse criteria have changed since the last execute()
*
* @return bool
*/
public function criteriaHaveChanged() {
return $this->opb_criteria_have_changed;
}
# ------------------------------------------------------
/**
* Returns the number of criteria on the current browse
*
* @return int
*/
public function numCriteria() {
$va_criteria = $this->opo_ca_browse_cache->getParameter('criteria');
if (isset($va_criteria) && is_array($va_criteria)) {
$vn_c = 0;
foreach($va_criteria as $vn_table_num => $va_criteria_list) {
$vn_c += sizeof($va_criteria_list);
}
return $vn_c;
}
return 0;
}
# ------------------------------------------------------
/**
* Removes all criteria from the current browse. If the $ps_facet_name parameter is set
* then all criteria for that facet are removed, otherwise all criteria for all facets is removed.
*
* @param string $ps_facet_name Optional name of facet for which to remove criteria
* @return bool True on success, false on failure
*
*/
public function removeAllCriteria($ps_facet_name=null) {
if ($ps_facet_name && !$this->isValidFacetName($ps_facet_name)) { return false; }
$va_criteria = $this->opo_ca_browse_cache->getParameter('criteria');
$va_criteria_display_strings = $this->opo_ca_browse_cache->getParameter('criteria_display_strings');
if($ps_facet_name) {
$va_criteria[$ps_facet_name] = array();
$va_criteria_display_strings[$ps_facet_name] = array();
} else {
$va_criteria = array();
$va_criteria_display_strings = array();
}
$this->opo_ca_browse_cache->setParameter('criteria', $va_criteria);
$this->opo_ca_browse_cache->setParameter('criteria_display_strings', $va_criteria_display_strings);
$this->opo_ca_browse_cache->setParameter('facet_html', null);
$this->opb_criteria_have_changed = true;
return true;
}
# ------------------------------------------------------
/**
* Returns a list of criteria on the current browse. If the $ps_facet_name parameter is set
* then only criteria for the facet are returned, otherwise all criteria for all facets are returned.
* The returned array contains only facet codes and values.
*
* @param string $ps_facet_name Optional name of facet for which to list criteria
* @return array
*/
public function getCriteria($ps_facet_name=null) {
if ($ps_facet_name && (!$this->isValidFacetName($ps_facet_name))) { return null; }
$va_criteria = $this->opo_ca_browse_cache->getParameter('criteria');
if($ps_facet_name) {
return isset($va_criteria[$ps_facet_name]) ? $va_criteria[$ps_facet_name] : null;
}
return isset($va_criteria) ? $va_criteria : null;
}
# ------------------------------------------------------
/**
* Returns a list of criteria on the current browse. If the $ps_facet_name parameter is set
* then only criteria for the facet are returned, otherwise all criteria for all facets are returned.
* The returned array contains facet codes, values and display labels for criterion. Use this method
* only if you need display labels. If you only need basic criteria data use BrowseEngine::getCriteria()
* which is faster.
*
* @param string $ps_facet_name Optional name of facet for which to list criteria
* @return array
*
* @see BrowseEngine::getCriteria
*/
public function getCriteriaWithLabels($ps_facet_name=null) {
if ($ps_facet_name && (!$this->isValidFacetName($ps_facet_name))) { return null; }
$va_criteria = $this->opo_ca_browse_cache->getParameter('criteria');
$va_criteria_display_strings = $this->opo_ca_browse_cache->getParameter('criteria_display_strings');
$va_criteria_with_labels = array();
if($ps_facet_name) {
if (is_array($va_criteria_display_strings[$ps_facet_name])) {
foreach($va_criteria_display_strings[$ps_facet_name] as $vm_criterion => $vs_display_criterion) {
$va_criteria_with_labels[$vm_criterion] = $this->getCriterionLabel($ps_facet_name, $vs_display_criterion);
}
} elseif(is_array($va_criteria[$ps_facet_name])) {
foreach($va_criteria[$ps_facet_name] as $vm_criterion => $vn_tmp) {
$va_criteria_with_labels[$vm_criterion] = $this->getCriterionLabel($ps_facet_name, $vm_criterion);
}
}
} else {
if (is_array($va_criteria)) {
foreach($va_criteria as $vs_facet_name => $va_criteria_by_facet) {
if (is_array($va_criteria_display_strings[$vs_facet_name])) {
foreach($va_criteria_display_strings[$vs_facet_name] as $vm_criterion => $vs_display_criterion) {
$va_criteria_with_labels[$vs_facet_name][$vm_criterion] = $this->getCriterionLabel($vs_facet_name, $vs_display_criterion);
}
} else {
foreach($va_criteria_by_facet as $vm_criterion => $vn_tmp) {
$va_criteria_with_labels[$vs_facet_name][$vm_criterion] = $this->getCriterionLabel($vs_facet_name, $vm_criterion);
}
}
}
}
}
return $va_criteria_with_labels;
}
# ------------------------------------------------------
/**
* Returns a display label for a given criterion and facet.
*
* @param string $ps_facet_name Name of facet
* @param mixed $pm_criterion
* @return string
*/
public function getCriterionLabel($ps_facet_name, $pn_row_id) {
if (!($va_facet_info = $this->getInfoForFacet($ps_facet_name))) { return null; }
switch($va_facet_info['type']) {
# -----------------------------------------------------
case 'has':
$vs_yes_text = (isset($va_facet_info['label_yes']) && $va_facet_info['label_yes']) ? $va_facet_info['label_yes'] : _t('Yes');
$vs_no_text = (isset($va_facet_info['label_no']) && $va_facet_info['label_no']) ? $va_facet_info['label_no'] : _t('No');
return ((bool)$pn_row_id) ? $vs_yes_text : $vs_no_text;
break;
# -----------------------------------------------------
case 'label':
if (!($t_table = Datamodel::getInstanceByTableName((isset($va_facet_info['relative_to']) && $va_facet_info['relative_to']) ? $va_facet_info['relative_to'] : $this->ops_browse_table_name, true))) { break; }
if (!$t_table->load($pn_row_id)) { return '???'; }
return $t_table->getLabelForDisplay();
break;
# -----------------------------------------------------
case 'authority':
if (!($t_table = Datamodel::getInstanceByTableName($va_facet_info['table'], true))) { break; }
if (!$t_table->load($pn_row_id)) { return '???'; }
return $t_table->getLabelForDisplay();
break;
# -----------------------------------------------------
case 'relationship_types':
if (!($t_rel = Datamodel::getInstanceByTableName($va_facet_info['relationship_table'], true))) { break; }
$t_rel_type = new ca_relationship_types();
$info = $t_rel_type->getRelationshipInfo($va_facet_info['relationship_table']);
return isset($info[(int)$pn_row_id]) ? $info[(int)$pn_row_id]['typename'] : "???";
break;
# -----------------------------------------------------
case 'attribute':
$t_element = new ca_metadata_elements();
if (!$t_element->load(array('element_code' => array_pop(explode(".", $va_facet_info['element_code']))))) {
return urldecode($pn_row_id);
}
$vn_element_id = $t_element->getPrimaryKey();
switch($vn_element_type = $t_element->get('datatype')) {
case __CA_ATTRIBUTE_VALUE_LIST__:
$vs_label = caProcessTemplateForIDs("^ca_list_items.hierarchy.preferred_labels.name_plural", "ca_list_items", array($pn_row_id), array("delimiter" => " ➜ "));
if(is_array($va_facet_info['relabel']) && isset($va_facet_info['relabel'][$vs_label])) {
$vs_label = $va_facet_info['relabel'][$vs_label];
}
return $vs_label;
break;
case __CA_ATTRIBUTE_VALUE_OBJECTS__:
case __CA_ATTRIBUTE_VALUE_ENTITIES__:
case __CA_ATTRIBUTE_VALUE_PLACES__:
case __CA_ATTRIBUTE_VALUE_OCCURRENCES__:
case __CA_ATTRIBUTE_VALUE_COLLECTIONS__:
case __CA_ATTRIBUTE_VALUE_LOANS__:
case __CA_ATTRIBUTE_VALUE_MOVEMENTS__:
case __CA_ATTRIBUTE_VALUE_STORAGELOCATIONS__:
case __CA_ATTRIBUTE_VALUE_OBJECTLOTS__:
if ($t_rel_item = AuthorityAttributeValue::elementTypeToInstance($vn_element_type)) {
return ($t_rel_item->load($pn_row_id)) ? $t_rel_item->getLabelForDisplay() : "???";
}
break;
case __CA_ATTRIBUTE_VALUE_GEONAMES__:
$value = ca_attribute_values::getValuesFor($pn_row_id);
return preg_replace('![ ]*\[[^\]]*\]!', '', $value['value_longtext1']);
case __CA_ATTRIBUTE_VALUE_CURRENCY__:
$value = ca_attribute_values::getValuesFor($pn_row_id);
return $value['value_longtext1'].' '.sprintf("%4.2f", $value['value_decimal1']);
break;
default:
$value = ca_attribute_values::getValuesFor($pn_row_id);
return $value['value_longtext1'];
break;
}
break;
# -----------------------------------------------------
case 'field':
if (!($t_item = Datamodel::getInstanceByTableName($this->ops_browse_table_name, true))) { break; }
if($vb_is_bit = ($t_item->getFieldInfo($va_facet_info['field'], 'FIELD_TYPE') == FT_BIT)) {
return ((bool)$pn_row_id) ? caGetOption('label_yes', $va_facet_info, _t('Yes')) : caGetOption('label_no', $va_facet_info, _t('No'));
}
return urldecode($pn_row_id);
break;
# -----------------------------------------------------
case 'violations':
if (!($t_rule = Datamodel::getInstanceByTableName('ca_metadata_dictionary_rules', true))) { break; }
if ($t_rule->load(array('rule_code' => $pn_row_id))) {
return $t_rule->getSetting('label');
}
return urldecode($pn_row_id);
break;
# -----------------------------------------------------
case 'tags':
if ($tag = ca_item_tags::find(['tag_id' => $pn_row_id], ['returnAs' => 'firstModelInstance'])) {
return $tag->get('ca_item_tags.tag');
}
return urldecode($pn_row_id);
break;
# -----------------------------------------------------
case 'checkouts':
$vs_status_text = null;
$vs_status_code = (isset($va_facet_info['status']) && $va_facet_info['status']) ? $va_facet_info['status'] : $pn_row_id;
switch($vs_status_code) {
case 'overdue':
$vs_status_text = _t('Overdue');
break;
case 'reserved':
$vs_status_text = _t('Reserved');
break;
case 'available':
$vs_status_text = _t('Available');
break;
default:
case 'out':
$vs_status_text = _t('Out');
break;
}
$va_params = array();
switch($va_facet_info['mode']) {
case 'user':
$vs_name = null;
$t_user = new ca_users($pn_row_id);
if ($t_user->getPrimaryKey()) {
$vs_name = $t_user->get('fname').' '.$t_user->get('lname').(($vs_email = $t_user->get('email')) ? " ({$vs_email})" : "");
return _t('%1 for %2', $vs_status_text, $vs_name);
}
break;
default:
case 'all':
return $vs_status_text;
break;
}
return urldecode($pn_row_id);
break;
# -----------------------------------------------------
case 'location':
$va_row_tmp = explode(":", urldecode($pn_row_id));
if (
(sizeof($va_row_tmp) < 3)
&&
($object_location_tracking_relationship_type = $this->opo_config->get('object_storage_location_tracking_relationship_type'))
&&
($object_location_tracking_relationship_type_id = array_shift(caMakeRelationshipTypeIDList('ca_objects_x_storage_locations', [$object_location_tracking_relationship_type])))
) {
//
// Hierarchical display of current location facets is only available when pure storage location tracking (ie. only
// locations, not loans, occurrences etc.) is configured. The value of location criteria is typically in the
// form <table num>:<type id>:<row id> but is shortened to just the row_id (which is the storage location location_id)
// by the hierarchy browser. In this case we can assume the table number is 119 (ca_objects_x_storage_locations) and
// the type_id is whatever is configured in "object_storage_location_tracking_relationship_type" in app.conf.
//
// We prepend those values below, allowing the criterion value to behave as a standard location value/
//
array_unshift($va_row_tmp, $object_location_tracking_relationship_type_id);
if (sizeof($va_row_tmp) < 3) { array_unshift($va_row_tmp, 119); } // assume ca_objects_x_storage_locations
}
$vs_loc_table_name = Datamodel::getTableName($va_row_tmp[0]);
$va_collapse_map = $this->getCollapseMapForLocationFacet($va_facet_info);
$t_instance = Datamodel::getInstanceByTableName($vs_loc_table_name, true);
if (($vs_table_name = $vs_loc_table_name) == 'ca_objects_x_storage_locations') {
$vs_table_name = 'ca_storage_locations';
}
if (isset($va_collapse_map[$vs_table_name][$va_row_tmp[1]])) {
// Class/subclass is collapsable
return $va_collapse_map[$vs_table_name][$va_row_tmp[1]];
} elseif(isset($va_collapse_map[$vs_table_name]['*'])) {
// Class is collapsable
return $va_collapse_map[$vs_table_name]['*'];
} elseif($va_row_tmp[2] && ($qr_res = caMakeSearchResult($vs_table_name, [$va_row_tmp[2]])) && $qr_res->nextHit()) {
// Return label for id
$va_config = ca_objects::getConfigurationForCurrentLocationType($vs_table_name, $va_row_tmp[1]);
$vs_template = isset($va_config['template']) ? $va_config['template'] : "^{$vs_table_name}.preferred_labels";
return caTruncateStringWithEllipsis($qr_res->getWithTemplate($vs_template), 30, 'end');
}
return '???';
break;
# -----------------------------------------------------
case 'normalizedLength':
$vn_start = urldecode($pn_row_id);
if (!($vs_output_units = caGetLengthUnitType($vs_units=caGetOption('units', $va_facet_info, 'm')))) {
$vs_output_units = Zend_Measure_Length::METER;
}
$vs_increment = caGetOption('increment', $va_facet_info, '1 m');
$vo_increment = caParseLengthDimension($vs_increment);
$vn_increment_in_current_units = (float)$vo_increment->convertTo($vs_output_units, 6, 'en_US');
$vn_end = $vn_start + $vn_increment_in_current_units;
return "{$vn_start} {$vs_units} - {$vn_end} {$vs_units}";
break;
# -----------------------------------------------------
case 'normalizedDates':
return ($pn_row_id === 'null') ? _t('Date unknown') : urldecode($pn_row_id);
break;
# -----------------------------------------------------
case 'fieldList':
if (!($t_item = Datamodel::getInstanceByTableName($this->ops_browse_table_name, true))) { break; }
$vs_field_name = $va_facet_info['field'];
$va_field_info = $t_item->getFieldInfo($vs_field_name);
$t_list = new ca_lists();
if ($vs_list_name = $va_field_info['LIST_CODE']) {
$t_list_item = new ca_list_items($pn_row_id);
if ($vs_tmp = $t_list_item->getLabelForDisplay()) {
return $vs_tmp;
}
return '???';
} else {
if ($vs_list_name = $va_field_info['LIST']) {
if (is_array($va_list_items = $t_list->getItemsForList($vs_list_name))) {
$va_list_items = caExtractValuesByUserLocale($va_list_items);
foreach($va_list_items as $vn_id => $va_list_item) {
if ($va_list_item['item_value'] == $pn_row_id) {
return $va_list_item['name_plural'];
}
}
}
}
}
if(isset($va_field_info['BOUNDS_CHOICE_LIST'])) {
$va_choice_list = $va_field_info['BOUNDS_CHOICE_LIST'];
if (is_array($va_choice_list)) {
foreach($va_choice_list as $vs_val => $vn_id) {
if ($vn_id == $pn_row_id) {
return $vs_val;
}
}
}
}
if($va_facet_info['table'] && ($t_browse_table = Datamodel::getInstanceByTableName($vs_facet_table = $va_facet_info['table'], true))) {
if (!($app = AppController::getInstance())) { return '???'; }
if ($t_browse_table->load($pn_row_id) && $t_browse_table->isReadable($app->getRequest(), 'preferred_labels')) {
return $t_browse_table->getWithTemplate(isset($va_facet_info['display']) ? $va_facet_info['display'] : "^".$t_browse_table->tableName().".preferred_labels");
}
}
return '???';
break;
# -----------------------------------------------------
case 'dupeidno':
return _t("%1 repeats", $pn_row_id);
break;
# -----------------------------------------------------
default:
if (in_array($ps_facet_name, ['_search', '_reltypes'])) {
return $pn_row_id;
}
return 'Invalid type';
break;
# -----------------------------------------------------
}
}
# ------------------------------------------------------
# Facets
# ------------------------------------------------------
/**
* Returns list of all facets configured for this for browse subject
*
* @return array
*/
public function getInfoForFacets() {
return $this->opa_browse_settings['facets'];
}
# ------------------------------------------------------
/**
* Return info for specified facet, or null if facet is not valid
*
* @param string $ps_facet_name
* @return array
*/
public function getInfoForFacet($ps_facet_name) {
if (!$this->isValidFacetName($ps_facet_name)) { return null; }
$va_facets = $this->opa_browse_settings['facets'];
return $va_facets[$ps_facet_name];
}
# ------------------------------------------------------
/**
* Returns true if facet exists, false if not
*
* @param string $ps_facet_name
* @return bool
*/
public function isValidFacetName($ps_facet_name) {
$va_facets = $this->getInfoForFacets();
return (isset($va_facets[$ps_facet_name])) ? true : false;
}
# ------------------------------------------------------
/**
* Returns list of all valid facet names
*
* @return array()
*/
public function getFacetList() {
if (!is_array($this->opa_browse_settings)) { return null; }
// Facets can be restricted such that they are applicable only to certain types when browse type restrictions are in effect.
// These restrictions are distinct from per-facet 'restrict_to_type' and 'restrict_to_relationship_types' settings, which affect
// what items and *included* in the browse. 'restrict_to_type' restricts a browse to specific types of items (eg. only entities of type "individual" are returned in the facet);
// 'restrict_to_relationship_types' restricts authority facets to items related to the browse subject by specific relationship types. By contrast, the
// 'type_restrictions' setting indicates that a facet is only valid for specific types when a browse is limited to specific types (eg. there is a browse type
// restriction in effect. Browse type restrictions apply to the browse result, not the facet content (eg. on an object browse, a type restriction of "documents" would limit
// the browse to only consider and return object of that type).
$va_type_restrictions = $this->getTypeRestrictionList();
$t_list = new ca_lists();
$t_subject = Datamodel::getInstanceByTableNum($this->opn_browse_table_num, true);
$vs_type_list_code = $t_subject->getTypeListCode();
$va_criteria_facets = is_array($va_tmp = $this->getCriteria()) ? array_keys($this->getCriteria()) : array();
//
if (is_array($va_type_restrictions) && sizeof($va_type_restrictions)) {
$va_facets = array();
foreach($this->opa_browse_settings['facets'] as $vs_facet_name => $va_facet_info) {
if (in_array($vs_facet_name, $va_criteria_facets) && (caGetOption('type', $va_facet_info, null) == 'field')) { continue; } // fields can only appear once
if (isset($va_facet_info['requires']) && !is_array($va_facet_info['requires']) && $va_facet_info['requires']) { $va_facet_info['requires'] = array($va_facet_info['requires']); }
//
// enforce "requires" setting, which allows one to specify that a given facet should only appear if any one
// of the specified "required" facets is present in the criteria
//
$vb_facet_is_meets_requirements = true;
if (isset($va_facet_info['requires']) && is_array($va_facet_info['requires'])) {
$vb_facet_is_meets_requirements = false;
foreach($va_facet_info['requires'] as $vs_req_facet) {
if (in_array($vs_req_facet, $va_criteria_facets)) {
$vb_facet_is_meets_requirements = true;
break;
}
}
}
if ($vb_facet_is_meets_requirements) {
if (isset($va_facet_info['type_restrictions']) && is_array($va_facet_restrictions = $va_facet_info['type_restrictions']) && sizeof($va_facet_restrictions)) {
foreach($va_facet_restrictions as $vs_code) {
if ($va_item = $t_list->getItemFromList($vs_type_list_code, $vs_code)) {
if (in_array($va_item['item_id'], $va_type_restrictions)) {
$va_facets[] = $vs_facet_name;
break;
}
}
}
} else {
$va_facets[] = $vs_facet_name;
}
}
}
return $va_facets;
} else {
//
// enforce "requires" setting, which allows one to specify that a given facet should only appear if any one
// of the specified "required" facets is present in the criteria
//
$va_facets = array();
foreach($this->opa_browse_settings['facets'] as $vs_facet_name => $va_facet_info) {
if (isset($va_facet_info['requires']) && !is_array($va_facet_info['requires']) && $va_facet_info['requires']) { $va_facet_info['requires'] = array($va_facet_info['requires']); }
if (isset($va_facet_info['requires']) && is_array($va_facet_info['requires'])) {
foreach($va_facet_info['requires'] as $vs_req_facet) {
if (in_array($vs_req_facet, $va_criteria_facets)) {
$va_facets[] = $vs_facet_name;
continue;
}
}
} else {
$va_facets[] = $vs_facet_name;
}
}
return $va_facets;
}
}
# ------------------------------------------------------
/**
* Sets the current facet group. Facet groups let you restrict what facets are displayed to the
* end-user. In the browse configuration file you tag facets with one or more group names; only
* facets tagged with the current facet group will be returnd by getInfoForAvailableFacets()
* If the facet group is set to null then all available facets will be displayed.
*
* @param string $ps_group Group name to restrict facets to
* @return bool
*/
public function setFacetGroup($ps_group) {
$this->ops_facet_group = $ps_group;
return true;
}
# ------------------------------------------------------
/**
* Returns the current facet group.
*
* @return string The current facet group or null if no group is set
*/
public function getFacetGroup() {
return $this->ops_facet_group ? $this->ops_facet_group : null;
}
# ------------------------------------------------------
/**
* Returns list of all facets that currently have content (ie. that can refine the browse further)
* with full facet info included
*
* @return array
*/
public function getInfoForAvailableFacets() {
if (!is_array($this->opa_browse_settings)) { return null; }
$va_facets = $this->opa_browse_settings['facets'];
$va_facet_with_content = $this->opo_ca_browse_cache->getFacets();
$vs_facet_group = $this->getFacetGroup();
foreach($va_facets as $vs_facet_name => $va_facet_info) {
if ($vs_facet_group) {
if (!(isset($va_facet_info['facet_groups']) && is_array($va_facet_info['facet_groups']) && in_array($vs_facet_group, $va_facet_info['facet_groups']))) {
unset($va_facets[$vs_facet_name]);
continue;
}
}
if (!isset($va_facet_with_content[$vs_facet_name]) || !$va_facet_with_content[$vs_facet_name]) {
unset($va_facets[$vs_facet_name]);
}
}
return $va_facets;
}
# ------------------------------------------------------
/**
* Returns list of facets that will return content for the current browse table assuming no criteria
* It is the list of facets returned as "available" when no criteria are specific, in other words.
*
* Note that this method does NOT take into account type restrictions
*
* @return array List of facet codes
*/
public function getFacetsWithContentList() {
$t_browse = new BrowseEngine($this->opn_browse_table_num, null, $this->getContext());
return $t_browse->getFacetList();
}
# ------------------------------------------------------
/**
* Returns list of all facets that will return content for the current browse table assuming no criteria
* with full facet information is included. If all you need is the list of facets with content, and not
* full facet information consider using BrowseEngine::getFacetsWithContentList() which is faster.
*
* @return array
*/
public function getInfoForFacetsWithContent() {
if (!($va_facets_with_content = $this->opo_ca_browse_cache->getGlobalParameter('facets_with_content'))) {
$t_browse = new BrowseEngine($this->opn_browse_table_num, null, $this->getContext());