Grima  2018-08
Whispering into Alma's ear
Public Member Functions | 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

 appendField ($tag, $ind1, $ind2, $subfields)
 add a field to the MARC record More...
 
 getFields ($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)
 

Additional Inherited Members

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

Member Function Documentation

◆ appendField()

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

add a field to the MARC record

Parameters
string$taga three character MARC tag
Int$ind1one digit, first indicator
Int$ind2second digit, second indicator
Array$subfieldseach entry of the form $code => $value

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

References appendInnerXML().

Referenced by replaceOrAddSubfield().

1638  {
1639  $frag = "<datafield ind1=\"$ind1\" ind2=\"$ind2\" tag=\"$tag\">";
1640  foreach ($subfields as $k => $v) {
1641  $frag .= "<subfield code=\"$k\">$v</subfield>";
1642  }
1643  $frag .= "</datafield>";
1644  $xpath = new DomXpath($this->xml);
1645  $record = $xpath->query("//record");
1646  appendInnerXML($record[0],$frag);
1647  }
appendInnerXML( $elt, $xmlString)
Definition: grima-util.php:20

◆ deleteField()

AlmaObjectWithMARC::deleteField (   $tag)

delete all $tag fields from the MARC record

Parameters
string$taga three character MARC tag

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

1702  {
1703  $xpath = new DomXpath($this->xml);
1704  $fields = $xpath->query("//record/datafield[@tag='$tag']");
1705  foreach( $fields as $field ) {
1706  $field->parentNode->removeChild( $field );
1707  }
1708  }

◆ deleteSubfieldMatching()

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

delete subfields matching a regex

Parameters
Array$subfieldseach entry of the form $code => $value

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

1717  {
1718  $xpath = new DomXPath($this->xml);
1719  $subfs = $xpath->query("//datafield[@tag='$tag']/subfield[@code='$code']");
1720  foreach ($subfs as $subf) {
1721  if (preg_match($regex,$subf->nodeValue)) {
1722  $subf->parentNode->removeChild($subf);
1723  }
1724  }
1725  }

◆ getFields()

AlmaObjectWithMARC::getFields (   $tag)

get fields for the given MARC tag

Parameters
String$tagfield
Returns
Array of MARC fields -> object?

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

1657  {
1658  $xpath = new DomXpath($this->xml);
1659  $tag = preg_replace('/X*$/','',$tag);
1660  $tag = preg_replace('/\.*$/','',$tag);
1661  $fields = $xpath->query("//record/datafield[starts-with(@tag,'$tag')]");
1662  $fieldarr = array();
1663  foreach ($fields as $field) {
1664  $subfieldarr = array();
1665  foreach ($field->childNodes as $child) {
1666  $subfieldarr[] = array(
1667  $child->attributes[0]->value,
1668  $child->nodeValue
1669  );
1670  }
1671  $fieldarr[] = $subfieldarr;
1672  }
1673  return $fieldarr;
1674  }

◆ getSubfieldValues()

AlmaObjectWithMARC::getSubfieldValues (   $tag,
  $code 
)

get subfield value

Parameters
String$tagfield
String$codesubfield
Returns
Array array containing all values of subfield

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

Referenced by Holding\offsetGet().

1685  {
1686  $xpath = new DomXpath($this->xml);
1687  $subfields = $xpath->query("//record/datafield[@tag='$tag']/subfield[@code='$code']");
1688  $arr = array();
1689  foreach ($subfields as $subfield) {
1690  $arr[] = $subfield->nodeValue;
1691  }
1692  return $arr;
1693  }

◆ replaceOrAddSubfield()

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

replace or add subfield value in marc

Parameters
string$taga three character MARC tag
string$codea one letter subfield code
string$valuewhat to set the value to

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

References appendField().

Referenced by Bib\offsetSet().

1736  {
1737  # very shady but sometimes needed
1738  $xpath = new DomXpath($this->xml);
1739  $fields = $xpath->query("//record/datafield[@tag='$tag']");
1740  if (sizeof($fields) == 0) {
1741  $this->appendField($tag,' ',' ',array($code => $value));
1742  } else {
1743  $done = false;
1744  foreach ($fields[0]->childNodes as $subfield) {
1745  if($subfield->nodeType !== 1) {
1746  continue;
1747  }
1748  if ($subfield->getAttribute("code") == $code) {
1749  $subfield->nodeValue = $value;
1750  $done = true;
1751  break;
1752  }
1753  }
1754  if (!$done) {
1755  $subfield = $this->xml->createElement("subfield");
1756  $subfield->setAttribute("code",$code);
1757  $subfield->appendChild($this->xml->createTextNode($value));
1758  $fields[0]->appendChild($subfield);
1759  }
1760  }
1761  }
appendField($tag, $ind1, $ind2, $subfields)
add a field to the MARC record
Definition: grima-lib.php:1638

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