Grima 2018-08
Whispering into Alma's ear
Public Member Functions | Public Attributes | List of all members
AlmaObjectWithMARC Class Reference

Methods for fields and subfields in Alma's MARCXML. More...

Inheritance diagram for AlmaObjectWithMARC:
AlmaObject Bib Holding

Public Member Functions

  useNormac ()
 
  addControlField ($tag, $data)
  add a data field to the MARC record using Normac More...
 
  addDataField ($tag, $indicators, $subfields)
  add a data field to the MARC record using Normac More...
 
  normacToXML ()
  convert normac fields back to XML More...
 
  appendField ($tag, $ind1, $ind2, $subfields) ## XXX
  add a field to the MARC record More...
 
  getFields ($tag)
  get fields for the given MARC tag More...
 
  getMarcFields ($tag)
  get fields for the given MARC tag More...
 
  getSubfieldValues ($tag, $code)
  get subfield value More...
 
  deleteField ($tag)
  delete all $tag fields from the MARC record More...
 
  deleteSubfieldMatching ($tag, $code, $regex)
  delete subfields matching a regex More...
 
  deleteFieldMatching ($tag, $code, $regex)
  delete fields with subfields matching a regex More...
 
  replaceOrAddSubfield ($tag, $code, $value) ## XXX
  replace or add subfield value in marc More...
 
- Public Member Functions inherited from AlmaObject
  __construct ()
  create new blank Alma Object More...
 
  offsetExists ($offset)
 
  offsetGet ($offset)
 
  offsetSet ($offset, $value)
 
  offsetUnset ($offset)
 

Public Attributes

  $marc
 
- Public Attributes inherited from AlmaObject
  $el_access = array()
 
  $xml
 
  $templateDir = __DIR__ . "/templates"
 

Detailed Description

Methods for fields and subfields in Alma's MARCXML.

Definition at line 2239 of file grima-lib.php.

Member Function Documentation

◆ addControlField()

AlmaObjectWithMARC::addControlField (   $tag,
  $data 
)

add a data field to the MARC record using Normac

Parameters
string $tag a three character MARC tag
string $data control field data

Definition at line 2260 of file grima-lib.php.

2261 {
2262 $this->useNormac(); // this function uses normac
2263 $subarray = array();
2264 $field = new ISO2709Field($tag, $data);
2265 $this->marc->AppendField($field, true);
2266 }

References useNormac().

◆ addDataField()

AlmaObjectWithMARC::addDataField (   $tag,
  $indicators,
  $subfields 
)

add a data field to the MARC record using Normac

Parameters
string $tag a three character MARC tag
string $indicators both indicators in one string
Array $subfields each entry of the form $code => $value

Definition at line 2277 of file grima-lib.php.

2278 {
2279 $this->useNormac(); // this function uses normac
2280 $subarray = array();
2281 foreach ($subfields as $k => $v) {
2282 $subarray[] = new ISO2709Subfield($k, $v);
2283 }
2284 $field = new ISO2709Field($tag, null, $indicators, $subarray);
2285 $fieldstr = $field->AsMnemonicString();
2286
2287 $fields = $this->marc->getFields($tag);
2288 $found = false;
2289 foreach($fields as $f) {
2290 $fstr = $f->AsMnemonicString();
2291 if ($fstr == $fieldstr) {
2292 $found = true;
2293 } else {
2294 #error_log($fstr . " vs " . $fieldstr);
2295 }
2296 }
2297 if (! $found) {
2298 $this->marc->AppendField($field, true);
2299 }
2300 }

References useNormac().

◆ appendField()

AlmaObjectWithMARC::appendField (   $tag,
  $ind1,
  $ind2,
  $subfields 
)

add a field to the MARC record

Parameters
string $tag a three character MARC tag
Int $ind1 one digit, first indicator
Int $ind2 second digit, second indicator
Array $subfields each entry of the form $code => $value

Definition at line 2336 of file grima-lib.php.

2337 : replace with normac
2338 {
2339 $this->useNormac(); // this function uses normac
2340
2341 $subs = array();
2342 foreach ($subfields as $k => $v) {
2343 $subfa = new ISO2709Subfield($k, $v);
2344 $subs[] = $subfa;
2345 }
2346 $field = new ISO2709Field($tag, null, $ind1.$ind2, $subs);
2347 $this->marc->AppendField($field,true);
2348
2349 /* XXX: sheesh
2350 $frag = "<datafield ind1=\"$ind1\" ind2=\"$ind2\" tag=\"$tag\">";
2351 foreach ($subfields as $k => $v) {
2352 $frag .= "<subfield code=\"$k\">$v</subfield>";
2353 }
2354 $frag .= "</datafield>";
2355 $xpath = new DomXpath($this->xml);
2356 $record = $xpath->query("//record");
2357 appendInnerXML($record[0], $frag);
2358 */
2359 }

References useNormac().

Referenced by replaceOrAddSubfield().

◆ deleteField()

AlmaObjectWithMARC::deleteField (   $tag )

delete all $tag fields from the MARC record

Parameters
string $tag a three character MARC tag

Definition at line 2432 of file grima-lib.php.

2433 {
2434 $xpath = new DomXpath($this->xml);
2435 $fields = $xpath->query("//record/datafield[@tag='$tag']");
2436 foreach ($fields as $field) {
2437 $field->parentNode->removeChild($field);
2438 }
2439 }

◆ deleteFieldMatching()

AlmaObjectWithMARC::deleteFieldMatching (   $tag,
  $code,
  $regex 
)

delete fields with subfields matching a regex

For example, delete all 856 fields with $u that contain "ezproxy" case insensitively, like: $this->deleteFieldMatching("856","u","/ezproxy/i");

Parameters
string $tag a three character MARC tag
string $code a subfield code
string $regex a regular expression

Reimplemented in Holding.

Definition at line 2472 of file grima-lib.php.

2473 {
2474 $this->useNormac();
2475 foreach ($this->getFields($tag) as $field) {
2476 foreach ($field->getSubfields($code) as $subfield) {
2477 if (preg_match($regex,$subfield->data)) {
2478 $field->delete();
2479 break;
2480 }
2481 }
2482 }
2483 }
getFields($tag)
get fields for the given MARC tag
Definition: grima-lib.php:2369

References getFields(), and useNormac().

◆ deleteSubfieldMatching()

AlmaObjectWithMARC::deleteSubfieldMatching (   $tag,
  $code,
  $regex 
)

delete subfields matching a regex

Parameters
Array $subfields each entry of the form $code => $value

Reimplemented in Holding.

Definition at line 2448 of file grima-lib.php.

2449 {
2450 $xpath = new DomXPath($this->xml);
2451 $subfs = $xpath->query("//datafield[@tag='$tag']/subfield[@code='$code']");
2452 foreach ($subfs as $subf) {
2453 if (preg_match($regex, $subf->nodeValue)) {
2454 $subf->parentNode->removeChild($subf);
2455 }
2456 }
2457 }

◆ getFields()

AlmaObjectWithMARC::getFields (   $tag )

get fields for the given MARC tag

Parameters
String $tag field
Returns
Array of ISO2709Field objects

Definition at line 2369 of file grima-lib.php.

2370 {
2371 $this->useNormac(); // this function uses normac
2372 return $this->marc->getFields($tag);
2373 }

References useNormac().

Referenced by deleteFieldMatching(), Holding\deleteFieldMatching(), Holding\deleteSubfieldMatching(), Holding\getInstitutionCode(), Holding\getLibraryCode(), Holding\getLocationCode(), Holding\getMethodOfAcquisition(), Bib\offsetGet(), Holding\setCallNumberType(), Holding\setInstitutionCode(), Holding\setLibraryCode(), Holding\setLocationCode(), and Holding\setMethodOfAcquisition().

◆ getMarcFields()

AlmaObjectWithMARC::getMarcFields (   $tag )

get fields for the given MARC tag

Parameters
String $tag field
Returns
Array of MARCField Objects

Definition at line 2383 of file grima-lib.php.

2384 {
2385 $xpath = new DomXpath($this->xml);
2386 $tag = preg_replace('/X*$/', '', $tag);
2387 $tag = preg_replace('/\.*$/', '', $tag);
2388 $fields = $xpath->query("//record/datafield[starts-with(@tag,'$tag')]");
2389 $fieldarr = array();
2390 foreach ($fields as $field) {
2391 $marcfield = new MARCField();
2392 // indicators?
2393 $subfieldarr = array();
2394 foreach ($field->childNodes as $child) {
2395 $subfieldarr[] = array(
2396 $child->attributes[0]->value,
2397 $child->nodeValue
2398 );
2399 }
2400 $fieldarr[] = $subfieldarr;
2401 }
2402 return $fieldarr;
2403 }

◆ getSubfieldValues()

AlmaObjectWithMARC::getSubfieldValues (   $tag,
  $code 
)

get subfield value

Parameters
String $tag field
String $code subfield
Returns
Array array containing all values of subfield

Definition at line 2414 of file grima-lib.php.

2415 {
2416 $xpath = new DomXpath($this->xml);
2417 $subfields = $xpath->query("//record/datafield[@tag='$tag']/subfield[@code='$code']");
2418 $arr = array();
2419 foreach ($subfields as $subfield) {
2420 $arr[] = $subfield->nodeValue;
2421 }
2422 return $arr;
2423 }

Referenced by Holding\offsetGet().

◆ normacToXML()

AlmaObjectWithMARC::normacToXML ( )

convert normac fields back to XML

Definition at line 2307 of file grima-lib.php.

2308 {
2309 if (isset($this->marc)) {
2310 $newRecord = new DOMDocument;
2311 $marc = preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;',
2312 $this->marc->asXMLString());
2313 $newRecord->loadXML($marc);
2314
2315 $xpath = new DOMXpath($this->xml);
2316 $record = $xpath->query('//record');
2317 $node = $record[0];
2318 $node->parentNode->removeChild($node);
2319
2320 $node = $this->xml->importNode($newRecord->documentElement, true);
2321 $this->xml->documentElement->appendChild($node);
2322 }
2323 }

References $marc.

Referenced by Bib\addToAlma(), Holding\addToAlmaBib(), Holding\setCallNumber(), Bib\updateAlma(), and Holding\updateAlma().

◆ replaceOrAddSubfield()

AlmaObjectWithMARC::replaceOrAddSubfield (   $tag,
  $code,
  $value 
)

replace or add subfield value in marc

Parameters
string $tag a three character MARC tag
string $code a one letter subfield code
string $value what to set the value to

Definition at line 2494 of file grima-lib.php.

2495 : replace with normac
2496 {
2497 // very shady but sometimes needed
2498 $xpath = new DomXpath($this->xml);
2499 $fields = $xpath->query("//record/datafield[@tag='$tag']");
2500 if (sizeof($fields) == 0) {
2501 $this->appendField($tag, ' ', ' ', array($code => $value));
2502 } else {
2503 $done = false;
2504 foreach ($fields[0]->childNodes as $subfield) {
2505 if ($subfield->nodeType !== 1) {
2506 continue;
2507 }
2508 if ($subfield->getAttribute("code") == $code) {
2509 $subfield->nodeValue = $value;
2510 $done = true;
2511 break;
2512 }
2513 }
2514 if (!$done) {
2515 $subfield = $this->xml->createElement("subfield");
2516 $subfield->setAttribute("code", $code);
2517 $subfield->appendChild($this->xml->createTextNode($value));
2518 $fields[0]->appendChild($subfield);
2519 }
2520 }
2521 }
appendField($tag, $ind1, $ind2, $subfields) ## XXX
add a field to the MARC record
Definition: grima-lib.php:2336

References appendField().

Referenced by Bib\offsetSet().

◆ useNormac()

AlmaObjectWithMARC::useNormac ( )

Member Data Documentation

◆ $marc

AlmaObjectWithMARC::$marc

Definition at line 2242 of file grima-lib.php.

Referenced by normacToXML().


The documentation for this class was generated from the following file: