-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.php
135 lines (117 loc) · 5.04 KB
/
custom.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
<?php
function custom_paging()
{
//Starts a conditional statement that determines a search has been run
if (isset($_SERVER['QUERY_STRING'])) {
// Sets the current item ID to the variable $current
$current = metadata('item', 'id');
//Break the query into an array
parse_str($_SERVER['QUERY_STRING'], $queryarray);
//Items don't need the page level
unset($queryarray['page']);
$itemIds = array();
$list = array();
if (isset($queryarray['query'])) {
//We only want to browse previous and next for Items
$queryarray['record_types'] = array('Item');
//Get an array of the texts from the query.
$textlist = get_db()->getTable('SearchText')->findBy($queryarray);
//Loop through the texts ans populate the ids and records.
foreach ($textlist as $value) {
$itemIds[] = $value->record_id;
$record = get_record_by_id($value['record_type'], $value['record_id']);
$list[] = $record;
}
} elseif (isset($queryarray['advanced'])) {
if (!array_key_exists('sort_field', $queryarray)) {
$queryarray['sort_field'] = 'Dublin Core,Identifier';
$queryarray['sort_dir'] = 'd';
}
//Get an array of the items from the query.
$list = get_db()->getTable('Item')->findBy($queryarray);
foreach ($list as $value) {
$itemIds[] = $value->id;
$list[] = $value;
}
} //Browsing all items in general
else {
if (!array_key_exists('sort_field', $queryarray)) {
$queryarray['sort_field'] = 'Dublin Core,Identifier';
$queryarray['sort_dir'] = 'a';
}
$list = get_db()->getTable('Item')->findBy($queryarray);
foreach ($list as $value) {
$itemIds[] = $value->id;
}
}
//Update the query string without the page and with the sort_fields
$updatedquery = http_build_query($queryarray);
$updatedquery = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $updatedquery);
// Find where we currently are in the result set
$key = array_search($current, $itemIds);
// If we aren't at the beginning, print a Previous link
if ($key > 0) {
$previousItem = $list[$key - 1];
$previousUrl = record_url($previousItem, 'show') . '?' . $updatedquery;
$text = __('← Previous Item');
echo '<li id="previous-item" class="previous"><a href="' . html_escape(
$previousUrl
) . '">' . $text . '</a></li>';
}
// If we aren't at the end, print a Next link
if ($key >= 0 && $key < (count($list) - 1)) {
$nextItem = $list[$key + 1];
$nextUrl = record_url($nextItem, 'show') . '?' . $updatedquery;
$text = __("Next Item →");
echo '<li id="next-item" class="next"><a href="' . html_escape($nextUrl) . '">' . $text . '</a></li>';
}
} else {
// If a search was not run, then the normal next/previous navigation is displayed.
echo '<li id="previous-item" class="previous">' . link_to_previous_item_show() . '</li>';
echo '<li id="next-item" class="next">' . link_to_next_item_show() . '</li>';
}
}
function connolly_format_citations($citation_string)
{
$citation_string = strip_viaf($citation_string);
$citation_string = reverse_articles($citation_string);
return $citation_string;
}
function reverse_articles($citation_string)
{
$citation_string = preg_replace('/“(.*), (The|A|An)(,| \/)/', '“$2 $1$3', $citation_string);
return $citation_string;
}
// Remove VIAF IDs
function strip_viaf($citation_string)
{
return preg_replace('/ \d{8,12}/', '', $citation_string);
}
function bcl_link_to_browse_collection(Collection $collection)
{
$name = metadata($collection, array('Dublin Core', 'Title'));
return link_to_items_browse($name, ['collection' => $collection->id]);
}
// The Solr search handles snippets poorly. This function fixes some of that.
function bcl_fix_snippet_text($snippet_text)
{
// HTML entities at the end of snippets aren't completed.
$snippet_text = preg_replace('/&lsquo$/u','‘',$snippet_text);
$snippet_text = preg_replace('/&rsquo$/u','’',$snippet_text);
// Punctuation at the start of a snippet looks dumb.
$snippet_text = ltrim($snippet_text,'.,; ');
// Only add ellipses if they really should be there.
if (! preg_match('/^[A-Z]/u',$snippet_text)) {
$snippet_text = "...$snippet_text";
}
if (! preg_match('/[\.]$/u', $snippet_text)) {
$snippet_text = "$snippet_text...";
}
return $snippet_text;
}
// Adds VIAF links around VIAF IDs.
function bcl_add_viaf_links($text)
{
$link_to_viaf = '<a href="http://viaf.org/viaf/\2/" target="_blank">\2</a>';
return preg_replace('/( |.\x{00A0})(\d\d\d\d\d+)<\/a>/',"</a> $link_to_viaf" , $text);
}