ResultViewHelper.php 39.8 KB
Newer Older
ssp's avatar
ssp committed
1
<?php
ssp's avatar
ssp committed
2
3
/*******************************************************************************
 * Copyright notice
4
 *
ssp's avatar
2012    
ssp committed
5
 * Copyright (C) 2011-2012 by Sven-S. Porst
6
 *
ssp's avatar
ssp committed
7
8
9
10
11
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
13
 *
ssp's avatar
ssp committed
14
15
16
17
18
19
20
21
22
23
24
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
25
26


27
28
29
30
31
32
33
34
// Maximum number of authors to display in the summary part of the record.
// (This could be more configurable and does not make particularly much sense
// to begin with, but is included to match the output given by the JavaScript
// generated markup.)
define(MAX_AUTHORS, 3);



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * ResultViewHelper.php
 *
 * View Helper for a single pazpar2 result as downloaded and parsed by
 * Tx_Pazpar2_Domain_Model_Query.
 *
 * @author Sven-S. Porst <porst@sub-uni-goettingen.de>
 */



/**
 * View Helper class.
 */
class Tx_Pazpar2_ViewHelpers_ResultViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
ssp's avatar
ssp committed
50

51
52
53
54
55
/**
 * DOMDocument used by all functions in this class to create DOMElements.
 *
 * @var DOMDocument
 */
ssp's avatar
ssp committed
56
57
58
private $doc;


ssp's avatar
ssp committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * Configuration array containing rendering options, in particular:
 * * preferSUBOpac (int)
 * * provideCOinSExport (int)
 * * exportFormats (array of strings)
 * 
 * @var array
 */
private $conf;



/**
 * Registers own arguments.
 */
public function initializeArguments() {
	parent::initializeArguments();
	$this->registerArgument('result', 'array', 'The pazpar2 result to render', true);
	$this->registerArgument('conf', 'array', 'Configuration array', true);
}


81

ssp's avatar
ssp committed
82
/**
ssp's avatar
ssp committed
83
84
 * Main function called by Fluid.
 * 
ssp's avatar
ssp committed
85
86
 * @return string
 */
ssp's avatar
ssp committed
87
88
89
90
public function render () {
	$result = $this->arguments['result'];
	$this->conf = $this->arguments['conf'];
	
ssp's avatar
ssp committed
91
	$this->doc = DOMImplementation::createDocument();
92
93
	$li = $this->doc->createElement('li');
	$this->doc->appendChild($li);
94
	$li->setAttribute('class', 'pz2-detailsVisible');
95

ssp's avatar
ssp committed
96
97
	$iconElement = $this->doc->createElement('span');
	$li->appendChild($iconElement);
98
	$mediaClass = 'other';
ssp's avatar
ssp committed
99
100
101
102
103
104
105
106
107
108
	if (count($result['md-medium']) == 1) {
		$mediaClass = $result['md-medium'][0]['values'][0];
	}
	elseif (count($result['md-medium']) > 1) {
		$mediaClass = 'multiple';
	}

	$iconElement->setAttribute('class', 'pz2-mediaIcon ' . $mediaClass);
	$iconElement->setAttribute('title', Tx_Extbase_Utility_Localization::translate('media-type-' . $mediaClass, 'Pazpar2'));

109
110
111
112
	// basic title/author information
	$this->appendInfoToContainer($this->titleInfo($result), $li);
	$authors = $this->authorInfo($result);
	$this->appendInfoToContainer($authors, $li);
ssp's avatar
ssp committed
113

114
	// year or journal + year information
ssp's avatar
ssp committed
115
	if($result['md-medium'][0]['values'][0] == 'article') {
ssp's avatar
ssp committed
116
		$this->appendJournalInfoToContainer($result, $li);
ssp's avatar
ssp committed
117
	}
118
119
120
121
122
123
124
125
	else {
		$spaceBefore = ' ';
		if ($authors) {
			$spaceBefore = ', ';
		}
		$this->appendMarkupForFieldToContainer('date', $result,  $li, $spaceBefore, '.');
	}

ssp's avatar
ssp committed
126
127
128
129
	if ($this->conf['provideCOinSExport'] == 1) {
		// Insert COinS information
		$this->appendCOinSSpansToContainer($result, $li);
	}
ssp's avatar
ssp committed
130
	
131
132
133
	// detailed information about the publication
	$this->appendInfoToContainer($this->renderDetails($result), $li);
	
ssp's avatar
ssp committed
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
	return $this->doc->saveHTML();
}



/**
 * Convenince method to append an item to another one, even if undefineds and arrays are involved.
 * @param $info - the DOM element(s) to insert
 * @param DOMElement $container - the DOM element to insert info to
 */
private function appendInfoToContainer ($info, $container) {
	if ($info && $container) {
		if (is_array($info) == False) {
			$container->appendChild($info);
		}
		else {
			foreach ($info as $infoItem) {
				$container->appendChild($infoItem);
			}
		}
	}
}



ssp's avatar
ssp committed
159
160
/**
 * Add a target attribute to open in our target window and add a note
161
162
 * to the title as well as class about this fact.
 * The link’s title and class elements should be set before calling this method.
ssp's avatar
ssp committed
163
164
165
166
167
 * @param DOMElement $link
 */
private function turnIntoNewWindowLink ($link) {
	if ($link) {
		$link->setAttribute('target', 'pz2-linkTarget');
168
169
170
		$oldClass = $link->getAttribute('class');
		$newClass = trim($oldClass . ' pz2-newWindowLink');
		$link->setAttribute('class', $newClass);
ssp's avatar
ssp committed
171
172
173
174
175
176
177
178
179
180
181
182

		$newTitle = Tx_Extbase_Utility_Localization::translate('Erscheint in separatem Fenster.', 'Pazpar2');
		if ($link->hasAttribute('title')) {
			$oldTitle = $link->getAttribute('title');
			$newTitle = $oldTitle . ' (' . $newTitle . ')';
		}
		$link->setAttribute('title', $newTitle);
	}
}



ssp's avatar
ssp committed
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
/**
 * Creates span DOM element and content for a field name; Appends it to the given container.
 * @param string $fieldName
 * @param string $result result array to look the fieldName up in
 * @param DOMElement $container
 * @param string $prepend
 * @param string $append
 * @return DOMElement
 */
private function appendMarkupForFieldToContainer ($fieldName, $result, $container, $prepend='', $append='') {
	$span = Null;
	$fieldContent = $result['md-' . $fieldName][0]['values'][0];

	if ($fieldContent && $container) {
		$span = $this->doc->createElement('span');
		$span->setAttribute('class', 'pz2-' . $fieldName);
		$span->appendChild($this->doc->createTextNode($fieldContent));

		if ($prepend != '') {
			$container->appendChild($this->doc->createTextNode($prepend));
		}
		
		$container->appendChild($span);
		
		if ($append != '') {
			$container->appendChild($this->doc->createTextNode($append));
		}
	}

	return $span;
}



/**
 * Returns DOM SPAN element with markup for the current hit's title.
 * @param array $result
 * @return DOMElement
 */
private function titleInfo ($result) {
	$titleCompleteElement = $this->doc->createElement('span');
	$titleCompleteElement->setAttribute('class', 'pz2-title-complete');

	$titleMainElement = $this->doc->createElement('span');
	$titleCompleteElement->appendChild($titleMainElement);
	$titleMainElement->setAttribute('class', 'pz2-title-main');
	$this->appendMarkupForFieldToContainer('title', $result, $titleMainElement);
	$this->appendMarkupForFieldToContainer('multivolume-title', $result, $titleMainElement, ' ');

	$this->appendMarkupForFieldToContainer('title-remainder', $result, $titleCompleteElement, ' ');
	$this->appendMarkupForFieldToContainer('title-number-section', $result, $titleCompleteElement, ' ');

	$titleCompleteElement->appendChild($this->doc->createTextNode('. '));

	return $titleCompleteElement;
}



/**
 * Returns DOM SPAN element with markup for the current hit's author information.
 * The pre-formatted title-responsibility field is preferred and a list of author
 *	names is used as a fallback.
 * @param array $result
 * @return DOMElement
 */
private function authorInfo ($result) {
	$outputElement = Null;

	$outputText = $result['md-title-responsibility'][0]['values'][0];
	if (!$outputText && $result['md-author']) {
		$authors = Array();
255
256
257
258
259
260
261
262
263
		foreach ($result['md-author'] as $index => $author) {
			if ($index < MAX_AUTHORS) {
				$authorName = $author['values'][0];
				$authors[] = $authorName;
			}
			else {
				$authors[] = Tx_Extbase_Utility_Localization::translate('et al.', 'Pazpar2');
				break;
			}
ssp's avatar
ssp committed
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
		}

		$outputText = implode('; ', $authors);
	}

	if ($outputText) {
		$extraFullStop = '';
		if (strlen($outputText) > 1 && $outputText{strlen($outputText) - 1} != '.') {
			$extraFullStop = '.';
		}

		$outputElement = $this->doc->createElement('span');
		$outputElement->setAttribute('class', 'pz2-item-responsibility');
		$outputElement->appendChild($this->doc->createTextNode($outputText . $extraFullStop));
	}

	return $outputElement;
}



/**
 * Appends DOM SPAN element with the current hit's journal information to linkElement.
 * @param DOMElement $result
 * @param DOMElement $container to append the DOM element to
 */
ssp's avatar
ssp committed
290
private function appendJournalInfoToContainer ($result, $container) {
ssp's avatar
ssp committed
291
292
293
	$outputElement = $this->doc->createElement('span');
	$outputElement->setAttribute('class', 'pz2-journal');

ssp's avatar
ssp committed
294
	$journalTitle = $this->appendMarkupForFieldToContainer('journal-title', $result, $container, ' ' . Tx_Extbase_Utility_Localization::translate("In", "Pazpar2") . ": ");
ssp's avatar
ssp committed
295
296
297
298
299
300
301
302
	if ($journalTitle) {
		$this->appendMarkupForFieldToContainer('journal-subpart', $result, $journalTitle, ', ');
		$journalTitle->appendChild($this->doc->createTextNode('.'));
	}
}



ssp's avatar
ssp committed
303
304
305
306
307
308
309
310
311
312
313
/**
 * Turns the array $data, containing arrays of strings for its keys into a
 * string suitable for the title attribute of a COinS style element.
 * @param array $data
 * @return string
 */
private function COinSStringForObject ($data) {
	$infoList = Array();
	foreach ($data as $key => $info) {
		if ($info) {
			foreach ($info as $infoItem) {
314
				$infoList[] = $key . '=' . rawurlencode($infoItem[0]['values'][0]);
ssp's avatar
ssp committed
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
			}
		}
	}
	return implode('&', $infoList);
}



/**
 * Appends DOM SPAN elements with COinS information for $result to $container.
 * @param array $result
 * @param DOMElement $container
 */
private function appendCOinSSpansToContainer ($result, $container) {
	foreach ($result['location'] as $locationAll) {
		$location = $locationAll['ch'];
		$coinsData = Array('ctx_ver' => Array(Array('values' => Array('Z39.88-2004'))));
		
		// title
		$title = '';
		if ($location['md-title']) {
			$title .= $location['md-title'][0]['values'][0];
		}
		if ($location['md-multivolume-title']) {
			$title .= ' ' . $location['md-multivolume-title'][0]['values'][0];
		}
		if ($location['md-title-remainder']) {
			$title .= ' ' . $location['md-title-remainder'][0]['values'][0];
		}
		
		// format info
		if ($location['md-medium'][0]['values']['0'] == 'article') {
			$coinsData['rft_val_fmt'] = Array(Array('values' => Array('info:ofi/fmt:kev:mtx:journal')));
			$coinsData['rft.genre'] = Array(Array('values' => Array('article')));
			$coinsData['rft.atitle'] = Array(Array('values' => Array($title)));
			$coinsData['rft.jtitle'] = $location['md-journal-title'];
351
352
353
354
355
356
357
358
			if ($location['md-volume-number'] || $location['md-pages-number']) {
				// We have structured volume or pages information: use that instead of journal-subpart.
				$coinsData['rft.volume'] = $location['md-volume-number'];
				$coinsData['rft.issue'] = $location['md-issue-number'];
				if ($location['md-pages-number']) {
					$pageInfo = explode('-', $location['md-pages-number'][0]['values'][0]);
					$coinsData['rft.spage'] = Array(Array('values' => Array($pageInfo[0])));
					if (count($pageInfo) >= 2) {
359
						$coinsData['rft.epage'] = Array(Array('values' => Array($pageInfo[1])));
360
361
362
363
364
365
366
					}
				}
			}
			else {
				// We lack structured volume information: use the journal-subpart field.
				$coinsData['rft.volume'] = $location['md-journal-subpart'];
			}
ssp's avatar
ssp committed
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
		}
		else {
			$coinsData['rft_val_fmt'] = Array(Array('values' =>Array('info:ofi/fmt:kev:mtx:book')));
			$coinsData['rft.btitle'] = Array(Array('values' =>Array($title)));
			if ($location['md-medium'][0]['values']['0'] == 'book') {
				$coinsData['rft.genre'] = Array(Array('values' => Array('book')));
			}
			else {
				$coinsData['rft.genre'] = Array(Array('values' => Array('document')));
			}
		}
		
		// authors
		$coinsData['rft.au'] = Array();
		if ($location['md-author']) {
			$coinsData['rft.au'] = array_merge($coinsData['rft.au'], $location['md-author']);
		}
		if ($location['md-other-person']) {
			$coinsData['rft.au'] = array_merge($coinsData['rft.au'], $location['md-other-person']);
		}
		
		// further fields
		$coinsData['rft.date'] = $location['md-date'];
		$coinsData['rft.isbn'] = $location['md-isbn'];
		$coinsData['rft.issn'] = $location['md-issn'];
		$coinsData['rft.source'] = $location['md-catalogue-url'];
		$coinsData['rft.pub'] = $location['md-publication-name'];
		$coinsData['rft.place'] = $location['md-publication-place'];
		$coinsData['rft.series'] = $location['md-series-title'];
		$coinsData['rft.description'] = $location['md-description'];
		$URLs = Array();
		if ($location['md-doi']) {
			$URLs[] = Array(Array('values' => Array('info:doi/' . $location['md-doi'][0]['values'][0])));
		}
		if ($location['md-electronic-url']) {
			$URLs = array_merge($URLs, $location['md-electronic-url']);
		}
		$coinsData['rft_id'] = $URLs;
		
		$span = $this->doc->createElement('span');
		$span->setAttribute('class', 'Z3988');
		$coinsString = $this->COinSStringForObject($coinsData);
		$span->setAttribute('title', $coinsString);
		$container->appendChild($span);
	}
}



ssp's avatar
ssp committed
416
417
418
419
420
421
/**
 *
 * @param array $result
 * @return DOMElement
 */
private function renderDetails ($result) {
422
423
	$detailsDiv = $this->doc->createElement('div');
	$detailsDiv->setAttribute('class', 'pz2-details');
ssp's avatar
ssp committed
424
425
	
	$detailsList = $this->doc->createElement('dl');
426
	$detailsDiv->appendChild($detailsList);
427
	$clearSpan = $this->doc->createElement('span');
428
	$detailsDiv->appendChild($clearSpan);
429
	$clearSpan->setAttribute('class', 'pz2-clear');
ssp's avatar
ssp committed
430

431
432
433
434
435
436
437
438
	/*	A somewhat sloppy heuristic to create cleaned up author and other-person
		lists to avoid duplicating names listed in title-responsiblity already:
		* Do _not_ separately display authors and other-persons whose apparent
			surname appears in the title-reponsibility field to avoid duplication.
		* Completely omit the author list if no title-responsibility field is present
			as author fields are used in its place then.
	*/
	$allResponsibility = '';
ssp's avatar
ssp committed
439
	
440
	if ($result['md-title-responsibility']) {
ssp's avatar
ssp committed
441
442
443
		foreach ($result['md-title-responsibility'] as $responsibility) {
			$allResponsibility .= $responsibility['values'][0] . '; ';
		}
444
445
446
447
448
449
450
451
452
		$authors = $result['md-author'];
		if ($authors) {
			$result['md-author-clean'] = Array();
			foreach ($authors as $author) {
				$nameParts = explode(",", $author['values'][0]);
				$authorSurname = trim($nameParts[0]);
				if (strpos($allResponsibility, $authorSurname) === False) {
					$result['md-author-clean'][] = $author;
				}
ssp's avatar
ssp committed
453
454
			}
		}
455
	}
456
457
458
459
	else if (array_key_exists('md-author', $result) && count($result['md-author']) > MAX_AUTHORS) {
		$result['md-author-clean'] = array_slice($result['md-author'], MAX_AUTHORS);
	}

460
461
462
463
464
465
466
467
	$otherPeople = $result['md-other-person'];
	if ($otherPeople) {
		$result['md-other-person-clean'] = Array();
		foreach ($otherPeople as $otherPerson) {
			$nameParts = explode(",", $otherPerson['values'][0]);
			$personSurname = trim($nameParts[0]);
			if (strpos($allResponsibility, $personSurname) === False) {
				$result['md-other-person-clean'][] = $otherPerson;
ssp's avatar
ssp committed
468
469
470
471
472
473
			}
		}
	}

	$this->appendInfoToContainer( $this->detailLineAuto('author-clean', $result), $detailsList);
	$this->appendInfoToContainer( $this->detailLineAuto('other-person-clean', $result), $detailsList);
ssp's avatar
ssp committed
474
	$this->appendInfoToContainer( $this->detailLineAuto('abstract', $result), $detailsList);
ssp's avatar
ssp committed
475
476
	$this->appendInfoToContainer( $this->detailLineAuto('description', $result), $detailsList);
	$this->appendInfoToContainer( $this->detailLineAuto('series-title', $result), $detailsList);
477
	$this->appendInfoToContainer( $this->ISSNsDetailLine($result), $detailsList);
ssp's avatar
ssp committed
478
	$this->appendInfoToContainer( $this->detailLineAuto('doi', $result), $detailsList);
479
	$this->appendInfoToContainer( $this->detailLineAuto('creator', $result), $detailsList);
ssp's avatar
ssp committed
480
	$this->appendInfoToContainer( $this->detailLineAuto('mapscale', $result), $detailsList);
481
	$this->appendInfoToContainer( $this->MSCDetailLine($result), $detailsList);
ssp's avatar
ssp committed
482
	$this->appendInfoToContainer( $this->keywordsDetailLine($result), $detailsList);
ssp's avatar
ssp committed
483
484

	$this->appendInfoToContainer( $this->locationDetails($result), $detailsList);
ssp's avatar
ssp committed
485
486
	$this->appendGoogleBooksElementTo( $detailsList, $result );
	$this->appendInfoToContainer( $this->mapDetailLine($result), $detailsList);
ssp's avatar
ssp committed
487
	$this->addZDBInfoIntoElement( $detailsList, $result );
ssp's avatar
ssp committed
488
	$this->appendInfoToContainer( $this->exportLinks($result), $detailsDiv);
ssp's avatar
ssp committed
489
	
490
	return $detailsDiv;
ssp's avatar
ssp committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
}



/**
 * @param string $title
 * @param array $result
 * @return Null|array of DT/DD DOMElements
 */
private function detailLineAuto ($title, $result) {
	$line = Null;
	$element = $this->DOMElementForTitle($title, $result);

	if ($element) {
		$line = $this->detailLine($title, $element);
	}

	return $line;
}



/**
514
515
 * @param string $title
 * @param array $informationElements (DOM Elements)
ssp's avatar
ssp committed
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
 * @return Null|array of DT/DD DOMElements
 */
private function detailLine ($title, $informationElements) {
	$line = Null;

	if ($informationElements && $title) {
		$headingText = Null;

		if (count($informationElements) == 1) {
			$headingText = Tx_Extbase_Utility_Localization::translate('detail-label-' . $title, 'Pazpar2');
		}
		else {
			$labelKey = 'detail-label-' . $title . '-plural';
			$labelLocalisation = Tx_Extbase_Utility_Localization::translate($labelKey, 'Pazpar2');
			if ($labelLocalisation == '') {
				$labelKey = 'detail-label-' . $title;
				$labelLocalisation = Tx_Extbase_Utility_Localization::translate($labelKey, 'Pazpar2');
			}
			$headingText = $labelLocalisation;
		}

		$infoItems = $this->markupInfoItems($informationElements);


		if ($infoItems) {
			$line = Array();

			$rowTitle = $this->doc->createElement('dt');
			$line[] = $rowTitle;
			$labelNode = $this->doc->createTextNode($headingText . ":");
			$acronym = Tx_Extbase_Utility_Localization::translate('detail-label-acronym-' . $title, 'Pazpar2');
547
			if ($acronym) {
548
				$acronymElement = $this->doc->createElement('abbr');
ssp's avatar
ssp committed
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
				$acronymElement->setAttribute('title', $acronym);
				$acronymElement->appendChild($labelNode);
				$labelNode = $acronymElement;
			}
			$rowTitle->appendChild($labelNode);

			$rowData = $this->doc->createElement('dd');
			$line[] = $rowData;
			$rowData->appendChild($infoItems);
		}
	}

	return $line;
}



/**
 * Returns marked up version of the DOM items passed, putting them into a list if necessary.
 * @param array $elements (DOM Elements)
 * @return array
 */
private function markupInfoItems ($infoItems) {
	$result = Null;

	if (count($infoItems) == 1) {
		$result = $infoItems[0];
	}
	else {
		$result = $this->doc->createElement('ul');
		foreach ($infoItems as $item) {
			$li = $this->doc->createElement('li');
			$result->appendChild($li);
582
			$li->appendChild($item);
ssp's avatar
ssp committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
		}
	}

	return $result;
}



/**
 * Returns markup for each location of the item found from the current data.
 * @param array $result
 * @return array of DOM elements
 */
private function locationDetails ($result) {
	$locationDetails = Array();

	foreach ($result['location'] as $locationAll) {
		$location = $locationAll['ch'];

		$detailsData = $this->doc->createElement('dd');
603
604
605
606
607
608
609
		if ($location['md-medium'][0]['values'][0] != 'article') {
			$this->appendInfoToContainer( $this->detailInfoItem('edition', $location), $detailsData);
			$this->appendInfoToContainer( $this->detailInfoItem('publication-name', $location), $detailsData);
			$this->appendInfoToContainer( $this->detailInfoItem('publication-place', $location), $detailsData);
			$this->appendInfoToContainer( $this->detailInfoItem('date', $location), $detailsData);
			$this->appendInfoToContainer( $this->detailInfoItem('physical-extent', $location), $detailsData);
		}
ssp's avatar
ssp committed
610
611
		// $this->cleanISBNs(); not implemented in PHP version
		$this->appendInfoToContainer( $this->detailInfoItem('isbn', $location), $detailsData);
612
		$this->appendInfoToContainer( $this->electronicURLs($location, $result), $detailsData);
613
		$this->appendInfoToContainer( $this->parentLink($locationAll, $result), $detailsData);
ssp's avatar
ssp committed
614
615
		$this->appendInfoToContainer( $this->catalogueLink($locationAll), $detailsData);

616
617
618
619
620
621
		// Only append location information if additional details exist
		if ($detailsData->hasChildNodes()) {
			$detailsHeading = $this->doc->createElement('dt');
			$locationDetails[] = $detailsHeading;
			$detailsHeading->appendChild($this->doc->createTextNode(Tx_Extbase_Utility_Localization::translate('Ausgabe', 'Pazpar2') . ':'));
			$locationDetails[] = $detailsData;
ssp's avatar
ssp committed
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
		}
	}
	
	return $locationDetails;
}



/**
 *
 * @param string $fieldName
 * @param array $location
 * @return NULL|DOMElement
 */
private function detailInfoItem ($fieldName, $location) {
	$infoItem = Null;
	$value = $location['md-' . $fieldName];

	if ($value) {
		$label = Null;
		$labelID = 'detail-label-' + $fieldName;
		$localisedLabelString = Tx_Extbase_Utility_Localization::translate($labelID, 'Pazpar2');

		if ($localisedLabelString != '') {
			$label = $localisedLabelString;
		}

		$content = '';
		foreach ($value as $index => $item) {
			if ($index > 0) { $content .= ', '; }
			$content .= $item['values'][0];
		}
		$content = preg_replace('/^[ ]*/', '', $content);
		$content = preg_replace('/[ ;.,]*$/', '', $content);

		$infoItem = $this->detailInfoItemWithLabel($content, $label);
	}

	return $infoItem;
}



/**
 * @param string $fieldContent
 * @param string $labelName
 * @param boolean $dontTerminate
 * @return Null|DOMElement
 */
private function detailInfoItemWithLabel($fieldContent, $labelName, $dontTerminate = False) {
	$infoSpan = Null;
	if ($fieldContent) {
		$infoSpan = $this->doc->createElement('span');
		$infoSpan->setAttribute('class', 'pz2-info');
		if ($labelName) {
			$infoLabel = $this->doc->createElement('span');
			$infoSpan->appendChild($infoLabel);
			$infoLabel->setAttribute('class', 'pz2-label');
			$infoLabel->appendChild($this->doc->createTextNode($labelName));
			$infoSpan->appendChild($this->doc->createTextNode(' '));
		}
		$infoSpan->appendChild($this->doc->createTextNode($fieldContent));

		if (!$dontTerminate) {
			$infoSpan->appendChild($this->doc->createTextNode('; '));
		}
	}

	return $infoSpan;
}



/**
696
697
698
699
 * Comparison function for sorting URLs. Put URLs with attributes to the front.
 * @param string|Array $a
 * @param string|Array $b
 * @return int
ssp's avatar
ssp committed
700
 */
701
702
703
704
705
706
707
private function URLSort ($a, $b) {
	if ($a['attrs'] && !$b['attrs']) {
		return -1;
	}
	else if (!$a['attrs'] && $b['attrs']) {
		return 1;
	}
ssp's avatar
ssp committed
708
	else {
ssp's avatar
ssp committed
709
710
		return $a['attrs']['originalPosition'] - $b['attrs']['originalPosition'];
	}
711
712
713
714
715
716
717
718
719
720
721
}



/**
 * Returns a cleaned and sorted list of the URLs in the md-electronic-url fields.
 * @param Array $location
 * @param Array $result the result containing $location
 * @return type Array subarray of $location without duplicates and sorted
 */
private function cleanURLList ($location, $result) {
ssp's avatar
ssp committed
722
	$URLs = $location['md-electronic-url'];
723
	
ssp's avatar
ssp committed
724
725
726
	if ($URLs) {
		// Figure out which URLs are duplicates and collect indexes of those to remove.
		$indexesToRemove = Array();
ssp's avatar
ssp committed
727
728
		foreach ($URLs as $URLIndex => &$URLInfo) {
			$URLInfo['attrs']['originalPosition'] = $URLIndex;
ssp's avatar
ssp committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
			$URL = $URLInfo['values'][0];

			// Check for duplicates in the electronic-urls field.
			for ($remainingURLIndex = $URLIndex + 1; $remainingURLIndex < count($URLs); $remainingURLIndex++) {
				$remainingURLInfo = $URLs[$remainingURLIndex];
				$remainingURL = $remainingURLInfo['values'][0];
				if ($URL == $remainingURL) {
					// Two of the URLs are identical.
					// Keep the one with the title if only one of them has one,
					// keep the first one otherwise.
					$URLIndexToRemove = $URLIndex + $remainingURLIndex;
					if (!$URLInfo['attrs'] && $remainingURLInfo['attrs']) {
						$URLIndexToRemove = $URLIndex;
					}
					$indexesToRemove[$URLIndexToRemove] = true;
ssp's avatar
ssp committed
744
				}
745
			}
ssp's avatar
ssp committed
746
747
748
749
750
751
752
753
754
			// Check for duplicates among the DOIs.
			$DOIs = $result['md-doi'];
			if ($DOIs) {
				foreach ($DOIs as $DOI) {
					if (strpos($DOI['values'][0], $URL) !== False) {
						$indexesToRemove[$URLIndexToRemove] = true;
						break;
					}
				}
755
756
			}
		}
757

ssp's avatar
ssp committed
758
759
760
761
762
		// Remove the duplicate URLs.
		foreach (array_keys($indexesToRemove) as $index) {
			$URLs[$index] = FALSE;
		}
		$URLs = array_filter($URLs);
763

ssp's avatar
ssp committed
764
765
766
767
		// Re-order URLs so those with explicit labels appear at the beginning.
		usort($URLs, Array($this, "URLSort"));
	}
	
768
769
770
771
772
773
774
775
776
777
778
779
	return $URLs;
}


/**
 * Create markup for URLs in current location data.
 * @param array $location
 * @param array $result the result containing $location
 * @return DOMElement
 */
private function electronicURLs ($location, $result) {
	$electronicURLs = $this->cleanURLList($location, $result);
ssp's avatar
ssp committed
780
781
782
783
784
785
	$URLsContainer = Null;

	if ($electronicURLs && count($electronicURLs) != 0) {
		$URLsContainer = $this->doc->createElement('span');

		foreach ($electronicURLs as $URLInfo) {
ssp's avatar
ssp committed
786
			$linkTexts = Array();
ssp's avatar
ssp committed
787
788
789
			$linkURL = $URLInfo['values'][0];

			if ($URLInfo['attrs']['name']) {
ssp's avatar
ssp committed
790
791
792
793
				$linkTexts[] = $URLInfo['attrs']['name'];
				if ($URLInfo['attrs']['note']) {
					$linkTexts[] = $URLInfo['attrs']['note'];
				}
ssp's avatar
ssp committed
794
			}
ssp's avatar
ssp committed
795
			else if ($URLInfo['attrs']['note']) {
ssp's avatar
ssp committed
796
				$linkTexts[] = $URLInfo['attrs']['note'];
ssp's avatar
ssp committed
797
798
			}
			else if ($URLInfo['attrs']['fulltextfile']) {
ssp's avatar
ssp committed
799
800
801
802
				$linkTexts[] = 'Document';
			}
			else {
				$linkTexts[] = 'Link';
ssp's avatar
ssp committed
803
804
			}
			
ssp's avatar
ssp committed
805
806
807
808
809
810
811
			$localisedLinkTexts = Array();
			foreach ($linkTexts as $linkText) {
				$localisedLinkText = Tx_Extbase_Utility_Localization::translate('link-description-' . $linkText, 'Pazpar2');
				if (!$localisedLinkText) {
					$localisedLinkText = $linkText;
				}
				$localisedLinkTexts[] = $localisedLinkText; 
ssp's avatar
ssp committed
812
			}
ssp's avatar
ssp committed
813
			$linkText = '[' . implode(', ', $localisedLinkTexts) . ']';
ssp's avatar
ssp committed
814
			
ssp's avatar
ssp committed
815
816
817
818
819
820
821
			if ($URLsContainer->hasChildNodes()) {
				$URLsContainer->appendChild($this->doc->createTextNode(', '));
			}

			$link = $this->doc->createElement('a');
			$URLsContainer->appendChild($link);
			$link->setAttribute('href', $linkURL);
ssp's avatar
ssp committed
822
			$this->turnIntoNewWindowLink($link);
ssp's avatar
ssp committed
823
824
825
826
827
828
829
830
831
832
			$link->appendChild($this->doc->createTextNode($linkText));
		}
		$URLsContainer->appendChild($this->doc->createTextNode('; '));
	}

	return $URLsContainer;
}



833
/**
834
 * Returns DOM nodes linking to the catalogue page of the current
835
 * record’s parent record, plus spacing.
836
837
838
 * @param Array $locationAll
 * @param Array $result
 * @return NULL|Array of DOMNodes
839
 */
840
841
private function parentLink ($locationAll, $result) {
	$nodes = NULL;
842
	$URL = $locationAll['ch']['md-parent-catalogue-url'][0]['values'][0];
843
844
	if ($URL && $result['md-medium'][0]['values'][0] !== 'article') {
debugster($result);
845

ssp's avatar
ssp committed
846
		$linkElement = $this->doc->createElement('a');
847
848
849
		$linkElement->setAttribute('href', $URL);
		$linkTitle = Tx_Extbase_Utility_Localization::translate('enthaltendes Werk im Katalog ansehen', 'Pazpar2');
		$linkElement->setAttribute('title', $linkTitle);
850
		$this->turnIntoNewWindowLink($linkElement);
851
852
853
854
855
		$linkElement->setAttribute('class', 'pz2-detail-parent-catalogueLink');

		$linkText = Tx_Extbase_Utility_Localization::translate('enthaltendes Werk', 'Pazpar2');
		$linkElement->appendChild($this->doc->createTextNode($linkText));

856
		$nodes = array($linkElement, $this->doc->createTextNode(' '));
857
858
	}

859
	return $nodes;
860
861
862
863
864
865
866
867
868
869
870
}



/**
 * Returns a link for the current record that points to the catalogue page for that item.
 * @param array $locationAll
 * @return DOMElement
 */
private function catalogueLink ($locationAll) {
	$linkElement = NULL;
871
	$URL = $locationAll['ch']['md-catalogue-url'][0]['values'][0];
872
873
874
875
876
	$targetName = $locationAll['attrs']['name'];

	if ($URL && $targetName) {
		$linkElement = $this->doc->createElement('a');
		$linkElement->setAttribute('href', $URL);
ssp's avatar
ssp committed
877
		$linkTitle = Tx_Extbase_Utility_Localization::translate('Im Katalog ansehen', 'Pazpar2');
878
		$linkElement->setAttribute('title', $linkTitle);
879
880
881
		$this->turnIntoNewWindowLink($linkElement);
		$linkElement->setAttribute('class', 'pz2-detail-catalogueLink');

882
883
884
885
886
887
888
		/* Try to localise catalogue name, fall back to original target name
			if no localisation is available */
		$linkText = Tx_Extbase_Utility_Localization::translate($targetName, 'Pazpar2');
		if ($linkText === Null) {
			$linkText = $targetName;
		}
		$linkElement->appendChild($this->doc->createTextNode($linkText));
ssp's avatar
ssp committed
889
890
891
892
893
894
895
	}

	return $linkElement;
}



896
897
898
899
900
/**
 * @param array $result
 * @return array of DOM Elements
 */
private function ISSNsDetailLine ($result) {
901
	$ISSNTypes = Array('issn' => '', 'pissn' => 'gedruckt', 'eissn' => 'elektronisch');
902
	$ISSNList = Array();
903
904
905
906
907
908
	foreach ($ISSNTypes as $ISSNTypeIndex => $ISSNType) {
		$fieldName = 'md-' . $ISSNTypeIndex;
		if ($result[$fieldName]) {
			foreach($result[$fieldName][0]['values'] as $ISSNString) {
				$ISSN = substr($ISSNString, 0, 9);
				if (!in_array($ISSN, $ISSNList)) {
909
					if ($ISSNType != '') {
910
911
912
913
						$ISSN .= ' (' . Tx_Extbase_Utility_Localization::translate($ISSNType, 'Pazpar2') . ')';
					}
					$ISSNList[] = $ISSN;
				}
914
915
916
			}
		}
	}
917
	
918
919
920
921
	$infoElements = Null;
	if (count($ISSNList) > 0) {
		$infoElements = Array( $this->doc->createTextNode(implode(', ', $ISSNList)) );
	}
922
923
924
925
926
927
	
	return $this->detailLine('issn', $infoElements);
}



ssp's avatar
ssp committed
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
/**
 * @param array $result
 * @return array of DOM Elements
 */
private function keywordsDetailLine ($result) {
	$infoElements = Null;
	$labelString = 'keyword';

	if ($result['md-subject'] && $this->conf['useKeywords']) {
		$infoElement = $this->doc->createElement('span');
		$infoElements = Array($infoElement);
		foreach ($result['md-subject'] as $keywordIndex => $keywordArray) {
			$keyword = $keywordArray['values'][0];
			$linkElement = $this->doc->createElement('a');
			$URIBuilder = $this->controllerContext->getUriBuilder();

			$linkURI = $URIBuilder->uriFor('index', Array('queryStringKeyword' => '"' . $keyword . '"', 'extended' => 1, 'useJS' => 'no'), 'Pazpar2', 'Pazpar2');
			$linkElement->setAttribute('href', $linkURI);
			$titleString = Tx_Extbase_Utility_Localization::translate('nach Schlagwort "#" suchen', 'Pazpar2');
			$titleString = str_replace('#', $keyword, $titleString);
			$linkElement->setAttribute('title', $titleString);
			$linkElement->appendChild($this->doc->createTextNode($keyword));
			$infoElement->appendChild($linkElement);

			if ($keywordIndex + 1 < count($result['md-subject'])) {
				$infoElement->appendChild($this->doc->createTextNode('; '));
			}
		}

		if (count($result['md-subject']) > 1) {
			$labelString .= '-plural';
		}
	}

	return $this->detailLine($labelString, $infoElements);
}


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
/**
 *
 * @param array $result
 * @return array of DOM Elements
 */
private function MSCDetailLine ($result) {
	$infoElements = Null;
	$MSCInfo = Array();
	$notes = Array();

	foreach($result['location'] as $locationAll) {
		$location = $locationAll['ch'];
		if ($location['md-classification-msc']) {
			foreach($location['md-classification-msc'] as $MSCAll) {
				$MSCInfo[$MSCAll['values'][0]] = TRUE;
				if (array_key_exists('attrs', $MSCAll) && array_key_exists('accordingto', $MSCAll['attrs'])) {
					$notes[$MSCAll['attrs']['accordingto']] = TRUE;
				}
			}
		}
	}

	$MSCStrings = array_keys($MSCInfo);
	if (count($MSCStrings) > 0 ) {
		$MSCString = implode(', ', $MSCStrings);

		$MSCNotes = array_keys($notes);
		if (count($MSCNotes) > 0) {
			$MSCString .= ' (' .  Tx_Extbase_Utility_Localization::translate('gemäß', 'Pazpar2') . ' ' . implode(', ', $MSCNotes) . ')';
		}

		$infoElements = Array($this->doc->createTextNode($MSCString));
	}

For faster browsing, not all history is shown. View entire blame