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)
  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...
 
  replaceOrAddSubfield ($tag, $code, $value)
  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 1935 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 1954 of file grima-lib.php.

1954  {
1955  $this->useNormac(); # this function uses normac
1956  $subarray = array();
1957  $field = new ISO2709Field($tag, data);
1958  $this->marc->AppendField($field,true);
1959  }

◆ 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 1970 of file grima-lib.php.

1970  {
1971  $this->useNormac(); # this function uses normac
1972  $subarray = array();
1973  foreach ($subfields as $k => $v) {
1974  $subarray[] = new ISO2709Subfield($k,$v);
1975  }
1976  $field = new ISO2709Field($tag, null, $indicators, $subarray);
1977  $this->marc->AppendField($field,true);
1978  }

◆ 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 2011 of file grima-lib.php.

References appendInnerXML().

2011  {
2012  $frag = "<datafield ind1=\"$ind1\" ind2=\"$ind2\" tag=\"$tag\">";
2013  foreach ($subfields as $k => $v) {
2014  $frag .= "<subfield code=\"$k\">$v</subfield>";
2015  }
2016  $frag .= "</datafield>";
2017  $xpath = new DomXpath($this->xml);
2018  $record = $xpath->query("//record");
2019  appendInnerXML($record[0],$frag);
2020  }
appendInnerXML( $elt, $xmlString)
Definition: grima-util.php:20

◆ deleteField()

AlmaObjectWithMARC::deleteField (   $tag )

delete all $tag fields from the MARC record

Parameters
string $tag a three character MARC tag

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

2090  {
2091  $xpath = new DomXpath($this->xml);
2092  $fields = $xpath->query("//record/datafield[@tag='$tag']");
2093  foreach( $fields as $field ) {
2094  $field->parentNode->removeChild( $field );
2095  }
2096  }

◆ deleteSubfieldMatching()

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

delete subfields matching a regex

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

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

2105  {
2106  $xpath = new DomXPath($this->xml);
2107  $subfs = $xpath->query("//datafield[@tag='$tag']/subfield[@code='$code']");
2108  foreach ($subfs as $subf) {
2109  if (preg_match($regex,$subf->nodeValue)) {
2110  $subf->parentNode->removeChild($subf);
2111  }
2112  }
2113  }

◆ getFields()

AlmaObjectWithMARC::getFields (   $tag )

get fields for the given MARC tag

Parameters
String $tag field
Returns
Array of ISO2709Field objects

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

2030  {
2031  $this->useNormac(); # this function uses normac
2032  return $this->marc->getFields($tag);
2033  }

◆ getMarcFields()

AlmaObjectWithMARC::getMarcFields (   $tag )

get fields for the given MARC tag

Parameters
String $tag field
Returns
Array of MARCField Objects

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

2043  {
2044  $xpath = new DomXpath($this->xml);
2045  $tag = preg_replace('/X*$/','',$tag);
2046  $tag = preg_replace('/\.*$/','',$tag);
2047  $fields = $xpath->query("//record/datafield[starts-with(@tag,'$tag')]");
2048  $fieldarr = array();
2049  foreach ($fields as $field) {
2050  $marcfield = new MARCField();
2051  # indicators?
2052  $subfieldarr = array();
2053  foreach ($field->childNodes as $child) {
2054  $subfieldarr[] = array(
2055  $child->attributes[0]->value,
2056  $child->nodeValue
2057  );
2058  }
2059  $fieldarr[] = $subfieldarr;
2060  }
2061  return $fieldarr;
2062  }

◆ 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 2073 of file grima-lib.php.

2073  {
2074  $xpath = new DomXpath($this->xml);
2075  $subfields = $xpath->query("//record/datafield[@tag='$tag']/subfield[@code='$code']");
2076  $arr = array();
2077  foreach ($subfields as $subfield) {
2078  $arr[] = $subfield->nodeValue;
2079  }
2080  return $arr;
2081  }

◆ normacToXML()

AlmaObjectWithMARC::normacToXML ( )

convert normac fields back to XML

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

1985  {
1986  if (isset($this->marc)) {
1987  $newRecord = new DOMDocument;
1988  $newRecord->loadXML($this->marc->asXMLString());
1989 
1990  $xpath = new DOMXpath($this->xml);
1991  $record = $xpath->query('//record');
1992  $node = $record[0];
1993  $node->parentNode->removeChild($node);
1994 
1995  $node = $this->xml->importNode($newRecord->documentElement, true);
1996  $this->xml->documentElement->appendChild($node);
1997  }
1998  }

◆ 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 2124 of file grima-lib.php.

2124  {
2125  # very shady but sometimes needed
2126  $xpath = new DomXpath($this->xml);
2127  $fields = $xpath->query("//record/datafield[@tag='$tag']");
2128  if (sizeof($fields) == 0) {
2129  $this->appendField($tag,' ',' ',array($code => $value));
2130  } else {
2131  $done = false;
2132  foreach ($fields[0]->childNodes as $subfield) {
2133  if($subfield->nodeType !== 1) {
2134  continue;
2135  }
2136  if ($subfield->getAttribute("code") == $code) {
2137  $subfield->nodeValue = $value;
2138  $done = true;
2139  break;
2140  }
2141  }
2142  if (!$done) {
2143  $subfield = $this->xml->createElement("subfield");
2144  $subfield->setAttribute("code",$code);
2145  $subfield->appendChild($this->xml->createTextNode($value));
2146  $fields[0]->appendChild($subfield);
2147  }
2148  }
2149  }
appendField($tag, $ind1, $ind2, $subfields)
add a field to the MARC record
Definition: grima-lib.php:2011

◆ useNormac()

AlmaObjectWithMARC::useNormac ( )

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

1939  {
1940  if (isset($this->marc)) { return; }
1941  $this->marc = new MARC21Record();
1942  #error_log("LOADING:");
1943  #error_log($this->xml->saveXML());
1944  $this->marc->loadFromString($this->xml->saveXML());
1945  }

Member Data Documentation

◆ $marc

AlmaObjectWithMARC::$marc

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


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