Skip to content

Commit

Permalink
#21064
Browse files Browse the repository at this point in the history
  • Loading branch information
nczirjak-acdh committed Nov 30, 2022
1 parent 6f1ded0 commit b2d426a
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 24 deletions.
148 changes: 148 additions & 0 deletions src/aksearchExt/AcdhChHelper/Field880Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace aksearchExt\AcdhChHelper;

class Field880Helper {

private $data;
private static $titleEnabledFields = ['a', 'b', 'n', 'p', 'c'];
private $fields = [];
private $availableFields = [];

/**
*
*
*/
public function __construct(array $data) {
$this->data = $data;
}

/**
* Return the title
* @return string
*/
public function getTitle880(): string {

if (!$this->getField6Values()) {
return "";
}

if (!$this->fetchAvailableFields()) {
return "";
}

return $this->fetchTitle();
}

/**
* We have to fetch the title based on some rules. The order is coming from the $titleEnabledFields array.
*
* * a (no preceding character, it is always the first subfield)
* : b
* . n
* : p (if there is a preceding $n); . p (if there is no preceding $n)
* : p
* / c
*
* https://redmine.acdh.oeaw.ac.at/issues/21064#Examples
*
* @return string
*/
private function fetchTitle(): string {

$str = "";
$isN = false;
if (array_key_exists("n", $this->availableFields)) {
$isN = true;
}

foreach ($this->availableFields as $k => $v) {

if (strtolower($k) == "a") {
$str .= implode(' , ', $v);
}

$this->createStrByRules($str, $v, $k, "b", " : ");

$this->createStrByRules($str, $v, $k, "n", " . ");

if (strtolower($k) == "p" && $isN) {
if (!empty($str)) {
$str .= " : " . implode(' , ', $v);
} else {
$str .= implode(' , ', $v);
}
} else if (strtolower($k) == "p" && !$isN) {
if (!empty($str)) {
$str .= " . " . implode(' , ', $v);
} else {
$str .= implode(' , ', $v);
}
}

$this->createStrByRules($str, $v, $k, "c", " / ");

}
return $str;
}


/**
* Fetch the field by the rules
* @param string $str
* @param array $v
* @param string $k
* @param string $field
* @param string $separator
*/
private function createStrByRules(string &$str = "", array $v, string $k, string $field, string $separator) {
if (strtolower($k) == $field) {
if (!empty($str)) {
$str .= $separator . implode(' , ', $v);
} else {
$str .= implode(' , ', $v);
}
}
}

/**
* Fetch the available fields from the record
* @return bool
*/
private function fetchAvailableFields(): bool {
foreach ($this->fields as $f) {
foreach (self::$titleEnabledFields as $fc) {
if (isset($this->data[$f]->$fc)) {
$this->availableFields[$fc] = $this->data[$f]->$fc;
}
}
}

if (count($this->availableFields) > 0) {
return true;
}
return false;
}

/**
* Get the Field 6 values from the 880 values
* @return bool
*/
private function getField6Values(): bool {
foreach ($this->data as $k => $val) {
if (isset($val->{6})) {
foreach ($val->{6} as $v) {
if (strpos($v, '245-') !== false) {
$this->fields[] = $k;
}
}
}
}

if (count($this->fields) > 0) {
return true;
}
return false;
}
}
29 changes: 5 additions & 24 deletions src/aksearchExt/SolrMarc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use aksearchExt\container\IlsHoldingId;
use aksearchExt\container\HoldingData;
use aksearchExt\container\ItemData;
use aksearchExt\AcdhChHelper\Field880Helper;

class SolrMarc extends \AkSearch\RecordDriver\SolrMarc {

Expand Down Expand Up @@ -876,36 +877,16 @@ public function getTitle() {

/**
* https://redmine.acdh.oeaw.ac.at/issues/19490
* https://redmine.acdh.oeaw.ac.at/issues/21064
* If the 245 has field 6 with value 880- then we will fetch the 880 title first
* @return string
*/
private function getTitle880() {
//first check if the actual 245 has a field 6, if yes then we fetch the 880
$field880 = $this->getMarcFieldsAsObject($this->getMarcRecord(), 880, null, null, null);
$str = "";
$fchk = ['a', 'b', 'n', 'p', 'c'];
$fields = [];

foreach ($field880 as $k => $val) {
if (isset($val->{6})) {
foreach ($val->{6} as $v) {
if (strpos($v, '245-') !== false) {
$fields[] = $k;
}
}
}
}

foreach ($fields as $f) {
foreach ($fchk as $fc) {
if (isset($field880[$f]->$fc)) {
$text = implode(' / ', $field880[$f]->$fc);
//add the slash
$str .= $text.' / ';
}
}
}
return rtrim($str, '/ ');
$f880Helper = new \aksearchExt\AcdhChHelper\Field880Helper($field880);
$str = "";
return $f880Helper->getTitle880();
}

/**
Expand Down

0 comments on commit b2d426a

Please sign in to comment.