diff --git a/includes/blocks.inc b/includes/blocks.inc index 70cdbf7..fa00835 100644 --- a/includes/blocks.inc +++ b/includes/blocks.inc @@ -49,6 +49,10 @@ function islandora_manuscript_container_list($object) { ); module_load_include('inc', 'islandora_manuscript', 'includes/jstreebuilder'); $builder = new \Islandora\Manuscript\ContainerListJSTreeBuilder($object); + $structure = $builder->getTreeStructure(); + if (empty($structure)) { + return; + } $form['tree']['#attached']['js'][] = array( 'type' => 'setting', 'data' => array( @@ -58,7 +62,7 @@ function islandora_manuscript_container_list($object) { $id => array( 'core' => array( 'multiple' => FALSE, - 'data' => $builder->getTreeStructure(), + 'data' => $structure, ), 'plugins' => array('types'), 'types' => array( diff --git a/includes/jstreebuilder.inc b/includes/jstreebuilder.inc index fa60e80..23192b9 100644 --- a/includes/jstreebuilder.inc +++ b/includes/jstreebuilder.inc @@ -62,7 +62,10 @@ class JSTreeBuilder { $list = $this->xpath->query($query, $node); $roots = array(); foreach ($list as $component) { - $roots[] = $this->getComponentTree($component); + $root = $this->getComponentTree($component); + if ($root) { + $roots[] = $root; + } } return $roots; } @@ -84,20 +87,28 @@ class JSTreeBuilder { * Never actually got used? */ protected function getComponentTree(DOMElement $element) { - $title = $this->xpath->evaluate('normalize-space(string(ead:did/ead:unittitle/text()))', $element); - $date = $this->xpath->evaluate('normalize-space(string(ead:did/ead:unitdate/text()))', $element); + $level = $this->translateLevelCode($this->xpath->evaluate('string(./@level)', $element)); + $unitid = $this->stringifyNode('ead:did/ead:unitid', $element); + $title = $this->stringifyNode('ead:did/ead:unittitle', $element); + $date = $this->stringifyNode('ead:did/ead:unitdate', $element); $text = empty($date) ? $title : format_string('@title (@date)', array( '@title' => $title, '@date' => $date, )); + if ($unitid) { + $text = $unitid.' '.$text; + } + if ($level) { + $text = $level.' '.$text; + } $info = array( 'id' => $element->getAttribute('id'), 'text' => $text, 'type' => $element->getAttribute('level'), 'children' => array_merge( - $this->getLevel('ead:c01 | ead:c02 | ead:c03 | ead:c04 | ead:c05 | ead:c', $element), + $this->getLevel('ead:c01 | ead:c02 | ead:c03 | ead:c04 | ead:c05 | ead:c06 | ead:c07 | ead:c08 | ead:c09 | ead:c', $element), $this->getContainers($element) ), 'path' => $element->getNodePath(), @@ -105,6 +116,40 @@ class JSTreeBuilder { return $info; } + /** + * Given the string of a component's level + * return a human-readable representation + * @param string $level the code from an EAD component's level attribute + * @return string A human-readable representation + */ + protected function translateLevelCode($level) { + $value = ''; + switch ($level) { + case 'recordgrp': + $value = t('Record Group'); + break; + case 'subgrp': + $value = t('Subgroup'); + break; + default: + $value = t(ucfirst($level)); + } + return $value; + } + + + /** + * Given the xpath to a single node in a DOMElement, + * return the text content of the node and its children + * @param string $xpath Path to a single node + * @param DOMElement $element Element which may contain the xpath + * @return string The textContent of the node, or an empty string if no node is found + */ + protected function stringifyNode($xpath, DOMElement $element) { + $node = $this->xpath->query($xpath, $element)->item(0); + return $node ? $node->textContent : ''; + } + /** * Get all containers of this component. */