forked from UB-Mannheim/theme-maobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
627 lines (527 loc) · 20 KB
/
functions.php
File metadata and controls
627 lines (527 loc) · 20 KB
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
<?php
function centerrow_display_featured_exhibit() {
$html = '';
$featuredExhibit = exhibit_builder_random_featured_exhibit();
if ($featuredExhibit) {
$html .= get_view()->partial('exhibit-builder/exhibits/single.php', array('exhibit' => $featuredExhibit));
}
$html = apply_filters('exhibit_builder_display_random_featured_exhibit', $html);
return $html;
}
function centerrow_get_square_thumbnail_url($file, $view) {
if ($file->hasThumbnail()) {
$squareThumbnail = file_display_url($file, 'square_thumbnail');
} else {
$mimeType = $file->mime_type;
$fileType = (strpos($mimeType, 'image')) ? 'image' : 'video';
$squareThumbnail = $view->baseUrl() . '/application/views/scripts/images/fallback-' . $fileType . '.png';
}
return $squareThumbnail;
}
function centerrow_public_nav_main() {
$view = get_view();
$nav = new Omeka_Navigation;
$nav->loadAsOption(Omeka_Navigation::PUBLIC_NAVIGATION_MAIN_OPTION_NAME);
$nav->addPagesFromFilter(Omeka_Navigation::PUBLIC_NAVIGATION_MAIN_FILTER_NAME);
$html = $view->navigation()->menu($nav)->setPartial('common/accessible-megamenu.php')->render();
$view->navigation()->menu($nav)->setPartial(null);
return $html;
}
/**
* Queues the necessary CSS and JS assets for lightGallery, if enabled in the theme options.
*
* @return void
*/
function maobjects_queue_lightgallery_assets() {
queue_css_file('lightgallery');
queue_css_file('lightgallery-bundle.min', 'all', false, 'javascripts/vendor/lightgallery/css');
queue_js_file(array(
'vendor/lightgallery/lightgallery.min',
'vendor/lightgallery/plugins/thumbnail/lg-thumbnail.min',
'vendor/lightgallery/plugins/video/lg-video.min',
'vendor/lightgallery/plugins/rotate/lg-rotate.min',
'vendor/lightgallery/plugins/hash/lg-hash.min',
'vendor/lightgallery/plugins/zoom/lg-zoom.min',
));
queue_js_file('lg-itemfiles-config', 'js');
}
/**
* Return the public URL for a theme-uploaded file option.
*
* @param string $optionName
* @return string
*/
function maobjects_theme_upload_url($optionName) {
$filename = trim((string) get_theme_option($optionName));
if ($filename === '') {
return '';
}
try {
$storage = Zend_Registry::get('storage');
$path = $storage->getPathByType($filename, 'theme_uploads');
return (string) $storage->getUri($path);
} catch (Exception $e) {
return '';
}
}
/**
* Render Omeka browse sort links as select dropdowns.
*
* The helper reuses browse_sort_links() as its source for sort fields, then
* builds a second dropdown for sort direction while preserving the current
* browse context. The original link list is kept in a <noscript> fallback.
*
* Disclaimer: This functions was mainly generated by AI (2026-03-23)
*
* @param array $sortLinks Label-to-field mapping passed to browse_sort_links().
* @param string $selectId HTML id attribute used for the generated field select.
* @param string|null $ariaLabel Accessible label for the generated field select.
* @return string
*/
function maobjects_browse_sort_select(array $sortLinks, $selectId = 'browse-sort-select', $ariaLabel = null) {
$sortLinksHtml = browse_sort_links($sortLinks);
$options = array();
$parseSortQuery = function ($href) {
$decodedHref = html_entity_decode($href, ENT_QUOTES, 'UTF-8');
$query = array();
$queryString = parse_url($decodedHref, PHP_URL_QUERY);
if ($queryString !== null) {
parse_str($queryString, $query);
}
return $query;
};
$replaceSortQuery = function ($href, array $replacements) use ($parseSortQuery) {
$decodedHref = html_entity_decode($href, ENT_QUOTES, 'UTF-8');
$parts = parse_url($decodedHref);
$query = $parseSortQuery($decodedHref);
foreach ($replacements as $key => $value) {
if ($value === null || $value === '') {
unset($query[$key]);
} else {
$query[$key] = $value;
}
}
$rebuiltHref = '';
if (isset($parts['scheme'])) {
$rebuiltHref .= $parts['scheme'] . '://';
if (isset($parts['user'])) {
$rebuiltHref .= $parts['user'];
if (isset($parts['pass'])) {
$rebuiltHref .= ':' . $parts['pass'];
}
$rebuiltHref .= '@';
}
if (isset($parts['host'])) {
$rebuiltHref .= $parts['host'];
}
if (isset($parts['port'])) {
$rebuiltHref .= ':' . $parts['port'];
}
}
if (isset($parts['path'])) {
$rebuiltHref .= $parts['path'];
}
$rebuiltQuery = http_build_query($query);
if ($rebuiltQuery !== '') {
$rebuiltHref .= '?' . $rebuiltQuery;
}
if (isset($parts['fragment'])) {
$rebuiltHref .= '#' . $parts['fragment'];
}
return $rebuiltHref;
};
// Try to parse the HTML with DOMDocument if available:
if (class_exists('DOMDocument')) {
$dom = new DOMDocument();
$previousUseInternalErrors = libxml_use_internal_errors(true);
$loaded = $dom->loadHTML('<?xml encoding="utf-8" ?><div>' . $sortLinksHtml . '</div>');
libxml_clear_errors();
libxml_use_internal_errors($previousUseInternalErrors);
if ($loaded) {
foreach ($dom->getElementsByTagName('a') as $link) {
$label = trim($link->textContent);
$href = trim($link->getAttribute('href'));
$parentClass = '';
if ($link->parentNode instanceof DOMElement) {
$parentClass = $link->parentNode->getAttribute('class');
}
if ($label === '' || $href === '') {
continue;
}
$isCurrent = strpos(' ' . $parentClass . ' ', ' sorting ') !== false
|| strpos(' ' . $link->getAttribute('class') . ' ', ' sorting ') !== false;
$sortQuery = $parseSortQuery($href);
$options[] = array(
'href' => $href,
'label' => $label,
'selected' => $isCurrent,
'sort_field' => isset($sortQuery['sort_field']) ? $sortQuery['sort_field'] : null,
);
}
}
}
// If DOMDocument parsing failed or is unavailable, try regex parsing:
if (!$options && preg_match_all('/<li\b([^>]*)>\s*<a\b([^>]*)href=(["\'])(.*?)\3([^>]*)>(.*?)<\/a>\s*<\/li>/is', $sortLinksHtml, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$label = trim(html_entity_decode(strip_tags($match[6]), ENT_QUOTES, 'UTF-8'));
$href = trim(html_entity_decode($match[4], ENT_QUOTES, 'UTF-8'));
$isCurrent = strpos(' ' . $match[1] . ' ' . $match[2] . ' ' . $match[5] . ' ', ' sorting ') !== false;
$sortQuery = $parseSortQuery($href);
if ($label === '' || $href === '') {
continue;
}
$options[] = array(
'href' => $href,
'label' => $label,
'selected' => $isCurrent,
'sort_field' => isset($sortQuery['sort_field']) ? $sortQuery['sort_field'] : null,
);
}
}
// If regex parsing with list items failed, try a more lenient regex parsing without list items:
if (!$options && preg_match_all('/<a\b([^>]*)href=(["\'])(.*?)\2([^>]*)>(.*?)<\/a>/is', $sortLinksHtml, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$label = trim(html_entity_decode(strip_tags($match[5]), ENT_QUOTES, 'UTF-8'));
$href = trim(html_entity_decode($match[3], ENT_QUOTES, 'UTF-8'));
$isCurrent = strpos(' ' . $match[1] . ' ' . $match[4] . ' ', ' sorting ') !== false;
$sortQuery = $parseSortQuery($href);
if ($label === '' || $href === '') {
continue;
}
$options[] = array(
'href' => $href,
'label' => $label,
'selected' => $isCurrent,
'sort_field' => isset($sortQuery['sort_field']) ? $sortQuery['sort_field'] : null,
);
}
}
// If no options were parsed, return the original HTML:
if (!$options) {
return $sortLinksHtml;
}
// Determine the selected option index (default to 0 if none selected):
$selectedIndex = null;
foreach ($options as $index => $option) {
if ($option['selected']) {
$selectedIndex = $index;
break;
}
}
// If no option is marked as selected, default to the first option:
if ($selectedIndex === null) {
$selectedIndex = 0;
}
$currentSortField = isset($_GET['sort_field']) && $_GET['sort_field'] !== ''
? $_GET['sort_field']
: $options[$selectedIndex]['sort_field'];
$currentSortDir = isset($_GET['sort_dir']) && $_GET['sort_dir'] === 'd' ? 'd' : 'a';
$currentFieldHref = $options[$selectedIndex]['href'];
foreach ($options as $index => $option) {
if (!empty($option['sort_field'])) {
$options[$index]['href'] = $replaceSortQuery($option['href'], array(
'sort_field' => $option['sort_field'],
'sort_dir' => $currentSortDir,
));
}
if ($option['sort_field'] === $currentSortField) {
$currentFieldHref = $options[$index]['href'];
}
}
$orderOptions = array(
array(
'href' => $replaceSortQuery($currentFieldHref, array(
'sort_field' => $currentSortField,
'sort_dir' => 'a',
)),
'label' => __('Ascending'),
'selected' => $currentSortDir !== 'd',
),
array(
'href' => $replaceSortQuery($currentFieldHref, array(
'sort_field' => $currentSortField,
'sort_dir' => 'd',
)),
'label' => __('Descending'),
'selected' => $currentSortDir === 'd',
),
);
// Build the select HTML:
if ($ariaLabel === null) {
$ariaLabel = __('Sort records');
}
$html = '<label class="sort-label" for="' . html_escape($selectId) . '">' . __('Sort by: ') . '</label>';
$html .= '<select id="' . html_escape($selectId) . '" class="sort-select sort-field-select" aria-label="' . html_escape($ariaLabel) . '" onchange="if (this.value) { window.location.href = this.value; }">';
foreach ($options as $index => $option) {
$html .= '<option value="' . html_escape($option['href']) . '"';
if ($index === $selectedIndex) {
$html .= ' selected="selected"';
}
$html .= '>' . html_escape($option['label']) . '</option>';
}
$html .= '</select>';
$html .= '<select id="' . html_escape($selectId . '-direction') . '" class="sort-select sort-order-select" aria-label="' . html_escape(__('Sort direction')) . '" onchange="if (this.value) { window.location.href = this.value; }">';
foreach ($orderOptions as $option) {
$html .= '<option value="' . html_escape($option['href']) . '"';
if ($option['selected']) {
$html .= ' selected="selected"';
}
$html .= '>' . html_escape($option['label']) . '</option>';
}
$html .= '</select>';
$html .= '<noscript>' . $sortLinksHtml . '</noscript>';
return $html;
}
/**
* Render the Facets plugin block only when it contains actual facet fields.
*
* The Facets plugin can emit its outer container even when no configured facet
* has any values in the current browse context. Capture the plugin output first
* so the theme can suppress that empty shell.
*
* @param array $args Hook arguments passed to the Facets plugin.
* @return string
*/
function maobjects_public_facets_if_available(array $args = array()) {
if (!function_exists('get_specific_plugin_hook_output')) {
return '';
}
$html = get_specific_plugin_hook_output('Facets', 'public_facets', $args);
if (!is_string($html) || trim($html) === '') {
return '';
}
if (strpos($html, 'id="facets-field-') === false && strpos($html, "id='facets-field-") === false) {
return '';
}
return $html;
}
/**
* Determine whether the current public request resolves to the homepage.
*
* @param bool $isSimplePagesHome Whether Simple Pages marked the current page as home. $is_home_page is set by the Simple Pages plugin when the page is set as the homepage in the navigation.
* @return bool
*/
function maobjects_is_home_page($isSimplePagesHome = false) {
if ($isSimplePagesHome) {
return true;
}
// Check the current route name for a direct match with the homepage route:
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$router = $front->getRouter();
$currentRouteName = $router ? $router->getCurrentRouteName() : null;
// The homepage route is typically named 'home' in Omeka, but this can be customized:
if ($currentRouteName === Omeka_Application_Resource_Router::HOMEPAGE_ROUTE_NAME) {
return true;
}
return $request
&& in_array((string) $request->getModuleName(), array('', 'default'), true)
&& $request->getControllerName() === 'index'
&& $request->getActionName() === 'index';
}
/**
* Return configured footer branding logos.
*
* @return array
*/
function maobjects_footer_branding_logos() {
$logoSlots = array(
'primary' => array(
'file_option' => 'footer_logo_primary',
'label_option' => 'footer_logo_primary_label',
),
'secondary' => array(
'file_option' => 'footer_logo_secondary',
'label_option' => 'footer_logo_secondary_label',
),
);
$logos = array();
foreach ($logoSlots as $slot => $settings) {
$src = maobjects_theme_upload_url($settings['file_option']);
if ($src === '') {
continue;
}
$logos[] = array(
'slot' => $slot,
'src' => $src,
'label' => trim((string) get_theme_option($settings['label_option'])),
);
}
return $logos;
}
/**
* Normalize html-input textarea content into plain text lines.
*
* @param string $value
* @return string
*/
function maobjects_footer_textarea_lines($value) {
if (!is_string($value) || trim($value) === '') {
return '';
}
$normalizedValue = preg_replace('/ ?/i', ' ', $value); // Replace literal nbsp entities before decoding
$normalizedValue = html_entity_decode($normalizedValue, ENT_QUOTES, 'UTF-8'); // Decode HTML entities
$normalizedValue = preg_replace('/[\x{00A0}\x{2007}\x{202F}]/u', ' ', $normalizedValue); // Normalize non-breaking spaces to regular spaces
$normalizedValue = preg_replace('/<br\s*\/?>/i', "\n", $normalizedValue); // Replace <br> tags with newlines
$normalizedValue = preg_replace('/<\/p>|<\/div>|<\/li>/i', "\n", $normalizedValue); // Replace paragraph and list tags with newlines
$normalizedValue = strip_tags($normalizedValue); // Remove any remaining HTML tags
$normalizedValue = preg_replace("/\r\n?/", "\n", $normalizedValue); // Normalize line endings
$normalizedValue = preg_replace("/[ \t]+\n/", "\n", $normalizedValue); // Drop trailing spaces before line breaks
return trim($normalizedValue);
}
/**
* Determine whether a footer link href is allowed.
*
* @param string $href
* @return bool
*/
function maobjects_footer_link_is_allowed($href) {
if ($href === '') {
return false;
}
if (filter_var($href, FILTER_VALIDATE_URL)) {
return true;
}
return (bool) preg_match('/^(\/|#|\?|mailto:|tel:)/i', $href);
}
/**
* Parse footer links from a textarea using the format "url":"title".
*
* @param string $value
* @return array
*/
function maobjects_footer_links_from_textarea($value) {
$normalizedValue = maobjects_footer_textarea_lines($value);
if ($normalizedValue === '') {
return array();
}
$links = array();
$lines = preg_split('/\n+/', $normalizedValue); // Split the normalized value into lines
// Process each line:
foreach ($lines as $line) {
$line = trim($line);
if ($line === '') {
continue;
}
if (!preg_match('/^\s*"([^"]+)"\s*:\s*"([^"]+)"\s*$/', $line, $matches)) {
continue;
}
$href = trim($matches[1]);
$label = trim($matches[2]);
if ($href === '' || $label === '' || !maobjects_footer_link_is_allowed($href)) {
continue;
}
$links[] = array(
'href' => (strpos($href, '/') === 0) ? url(ltrim($href, '/')) : $href,
'label' => $label,
);
}
return $links;
}
/**
* Return configured footer info blocks.
*
* @return array
*/
function maobjects_footer_sections() {
$sections = array();
// Iterate through the footer sections:
for ($index = 1; $index <= 3; $index++) {
$title = trim((string) get_theme_option('footer_section_' . $index . '_title'));
$links = maobjects_footer_links_from_textarea((string) get_theme_option('footer_section_' . $index . '_links'));
if ($title === '' && !$links) {
continue;
}
$sections[] = array(
'id' => 'info-block-' . $index,
'title' => $title,
'links' => $links,
);
}
return $sections;
}
/**
* Return configured footer legal links.
*
* @return array
*/
function maobjects_footer_legal_links() {
return maobjects_footer_links_from_textarea((string) get_theme_option('footer_legal_links'));
}
/**
* Return footer social links from individual theme settings.
*
* @return array
*/
function maobjects_footer_social_links() {
$supportedPlatforms = maobjects_footer_social_icon_map();
$socialLinks = array();
// Retrieve social links from theme options:
foreach ($supportedPlatforms as $platform => $label) {
$href = trim((string) get_theme_option('footer_social_' .$platform));
// Check if the URL is valid:
if ($href === '' || !filter_var($href, FILTER_VALIDATE_URL)) {
continue;
}
$socialLinks[] = array(
'platform' => $platform,
'href' => $href,
'label' => $label,
'rel' => 'noreferrer me nofollow noopener',
);
}
return $socialLinks;
}
/**
* Render one supported footer social icon.
*
* @param string $platform
* @return string
*/
function maobjects_footer_social_icon($platform) {
$supportedPlatforms = maobjects_footer_social_icon_map();
static $svgCache = array();
if (!isset($supportedPlatforms[$platform])) {
return ''; // return empty string if platform is not supported
}
$svgPath = dirname(__FILE__) . '/img/footer/social/' . $platform . '.svg';
// Check if the SVG file exists and is readable. If so, cache its markup.
if (!isset($svgCache[$platform])) {
if (!is_readable($svgPath)) {
$svgCache[$platform] = '';
} else {
$svgMarkup = trim((string) file_get_contents($svgPath));
$svgMarkup = preg_replace(
'/<svg\b/i',
'<svg class="svg-icon-social-' . html_escape($platform) . '-dims" aria-hidden="true" focusable="false"',
$svgMarkup,
1
);
$svgCache[$platform] = $svgMarkup;
}
}
if ($svgCache[$platform] === '') {
return '';
}
return '<div class="icon-' . html_escape($platform) . '">'
. '<span class="svg-icon">'
. $svgCache[$platform]
. '</span>'
. '</div>';
}
/**
* Return the supported footer social media platforms and their labels.
*
* @return array
*/
function maobjects_footer_social_icon_map() {
return array(
'instagram' => 'Instagram',
'facebook' => 'Facebook',
'linkedin' => 'LinkedIn',
'tiktok' => 'TikTok',
'youtube' => 'YouTube',
'mastodon' => 'Mastodon',
);
}
?>