Source for file Abstract.php

Documentation is available at Abstract.php

  1. <?php
  2. abstract class Omeka_Output_Xml_Abstract extends Omeka_Output_Xml
  3. {
  4.     const XMLNS_XSI            = 'http://www.w3.org/2001/XMLSchema-instance';
  5.     const XMLNS                = 'http://omeka.org/schemas/omeka-xml/v2';
  6.     const XMLNS_SCHEMALOCATION = 'http://omeka.org/schemas/omeka-xml/v2/omeka-xml-2-2.xsd';
  7.     
  8.     /**
  9.      * This class' contextual record(s).
  10.      * @var array|Omeka_Record
  11.      */
  12.     protected $_record;
  13.  
  14.     /**
  15.      * The context of this DOMDocument. Determines how buildNode() builds the
  16.      * elements. Valid contexts include: item, file.
  17.      * 
  18.      * @var string 
  19.      */
  20.     protected $_context;
  21.     
  22.     /**
  23.      * The final document object.
  24.      * @var DOMDocument 
  25.      */
  26.     protected $_doc;
  27.     
  28.     /**
  29.      * The node built and set in child::_buildNode()
  30.      * @var DOMNode 
  31.      */
  32.     protected $_node;
  33.     
  34.     /**
  35.      * Abstract method. child::_buildNode() should set self::$_node.
  36.      */
  37.     abstract protected function _buildNode();
  38.     
  39.     /**
  40.      * @param Omeka_Record|array$record 
  41.      * @param string $context The context of this DOM document.
  42.      * @return void 
  43.      */
  44.     public function __construct($record$context)
  45.     {
  46.         $this->_record = $record;
  47.         $this->_context = $context;
  48.         $this->_doc = new DOMDocument('1.0''UTF-8');
  49.         $this->_doc->formatOutput true;
  50.         $this->_buildNode();
  51.     }
  52.     
  53.     /**
  54.      * Get the document object.
  55.      * 
  56.      * @return DOMDocument 
  57.      */
  58.     public function getDoc()
  59.     {
  60.         $this->_doc->appendChild($this->_setRootElement($this->_node));
  61.         return $this->_doc;
  62.     }
  63.     
  64.     /**
  65.      * Set an element as root.
  66.      * 
  67.      * @param DOMElement $rootElement 
  68.      * @return DOMElement The root element, including required attributes.
  69.      */
  70.     protected function _setRootElement($rootElement)
  71.     {
  72.         $rootElement->setAttribute('xmlns'self::XMLNS);
  73.         $rootElement->setAttribute('xmlns:xsi'self::XMLNS_XSI);
  74.         $rootElement->setAttribute('xsi:schemaLocation'self::XMLNS_SCHEMALOCATION);
  75.         $rootElement->setAttribute('uri'$this->_buildUrl());
  76.         $rootElement->setAttribute('accessDate'date('c'));
  77.         return $rootElement;
  78.     }
  79.     
  80.     /**
  81.      * Create a DOM element.
  82.      * 
  83.      * @param string $name The name of the element.
  84.      * @param null|stringThe value of the element.
  85.      * @param null|intThe id attribute of the element.
  86.      * @param null|DOMElementThe parent element.
  87.      * @return DOMElement 
  88.      */
  89.     protected function _createElement($name$value null$id null$parentElement null)
  90.     {
  91.         $element $this->_doc->createElement($name);
  92.         
  93.         // Append the value, if given.
  94.         if ($value{
  95.             $textNode $this->_doc->createTextNode($value);
  96.             $element->appendChild($textNode);
  97.         }
  98.         
  99.         // Set the @id attribute, if given.
  100.         if ($id{
  101.             $element->setAttribute("{$name}Id"$id);
  102.         }
  103.         
  104.         // Append to the parent element, if given.
  105.         if ($parentElement{
  106.             $parentElement->appendChild($element);
  107.         }
  108.         
  109.         return $element;
  110.     }
  111.     
  112.     /**
  113.      * Set the pagination node for container elements
  114.      *
  115.      * @param DOMElement The parent container element.
  116.      * @return void 
  117.      */
  118.     protected function _setContainerPagination(DOMElement $parentElement)
  119.     {
  120.         // Return if the pagination data is not registered.
  121.         if (!Zend_Registry::isRegistered('pagination')) {
  122.             return;
  123.         }
  124.         $pagination Zend_Registry::get('pagination');
  125.         $paginationElement $this->_createElement('pagination'nullnull$parentElement);
  126.         $this->_createElement('pageNumber',   $pagination['page'],          null$paginationElement);
  127.         $this->_createElement('perPage',      $pagination['per_page'],      null$paginationElement);
  128.         $this->_createElement('totalResults'$pagination['total_results']null$paginationElement);
  129.     }
  130.     
  131.     /**
  132.      * Get all element sets, elements, and element texts associated with the
  133.      * provided record.
  134.      * 
  135.      * @param Omeka_Record $record The record from which to extract metadata.
  136.      * @param bool $getItemType Whether to get the item type metadata.
  137.      * @return stdClass A list of element sets or an item type.
  138.      */
  139.     protected function _getElemetSetsByElementTexts(Omeka_Record $record$getItemType false)
  140.     {
  141.         $elementSets new stdClass;
  142.         $itemType    new stdClass;
  143.         
  144.         // Get all element texts associated with the provided record.
  145.         $elementTexts $record->getElementTextRecords();
  146.         foreach ($elementTexts as $elementText{
  147.             
  148.             // Get associated element and element set records.
  149.             $element    get_db()->getTable('Element')->find($elementText->element_id);
  150.             $elementSet get_db()->getTable('ElementSet')->find($element->element_set_id);
  151.             
  152.             // Differenciate between the element sets and the "Item Type 
  153.             // Metadata" pseudo element set.
  154.             if (ELEMENT_SET_ITEM_TYPE == $elementSet->name{
  155.                 $itemType->elements[$element->id]->name $element->name;
  156.                 $itemType->elements[$element->id]->description $element->description;
  157.                 $itemType->elements[$element->id]->elementTexts[$elementText->id]->text $elementText->text;
  158.             else {
  159.                 $elementSets->elementSets[$elementSet->id]->name $elementSet->name;
  160.                 $elementSets->elementSets[$elementSet->id]->description $elementSet->description;
  161.                 $elementSets->elementSets[$elementSet->id]->elements[$element->id]->name $element->name;
  162.                 $elementSets->elementSets[$elementSet->id]->elements[$element->id]->description $element->description;
  163.                 $elementSets->elementSets[$elementSet->id]->elements[$element->id]->elementTexts[$elementText->id]->text $elementText->text;
  164.             }
  165.         }
  166.         
  167.         // Return the item type metadata.
  168.         if ($getItemType{
  169.             $itemType->id          $record->Type->id;
  170.             $itemType->name        $record->Type->name;
  171.             $itemType->description $record->Type->description;
  172.             return $itemType;
  173.         }
  174.         
  175.         // Return the element sets metadata.
  176.         return $elementSets;
  177.     }
  178.     
  179.     /**
  180.      * Build an elementSetContainer element in a record (item or file) context.
  181.      * 
  182.      * @param Omeka_Record $record The record from which to build element sets.
  183.      * @param DOMElement $parentElement The element set container will append to
  184.      *  this element.
  185.      * @return void|null
  186.      */
  187.     protected function _buildElementSetContainerForRecord(Omeka_Record $recordDOMElement $parentElement)
  188.     {
  189.         $elementSets $this->_getElemetSetsByElementTexts($record);
  190.         
  191.         // Return if there are no element sets.
  192.         if (!count($elementSets->elementSets)) {
  193.             return null;
  194.         }
  195.         
  196.         // elementSetContainer
  197.         $elementSetContainerElement $this->_createElement('elementSetContainer');
  198.         foreach ($elementSets->elementSets as $elementSetId => $elementSet{
  199.              // elementSet
  200.             $elementSetElement $this->_createElement('elementSet'null$elementSetId);
  201.             $nameElement $this->_createElement('name'$elementSet->namenull$elementSetElement);
  202.             $descriptionElement $this->_createElement('description'$elementSet->descriptionnull$elementSetElement);
  203.             // elementContainer
  204.             $elementContainerElement $this->_createElement('elementContainer');
  205.             foreach ($elementSet->elements as $elementId => $element{
  206.                 // element
  207.                 $elementElement $this->_createElement('element'null$elementId);
  208.                 $nameElement $this->_createElement('name'$element->namenull$elementElement);
  209.                 $descriptionElement $this->_createElement('description'$element->descriptionnull$elementElement);
  210.                 // elementTextContainer
  211.                 $elementTextContainerElement $this->_createElement('elementTextContainer');
  212.                 foreach ($element->elementTexts as $elementTextId => $elementText{
  213.                     // elementText
  214.                     $elementTextElement $this->_createElement('elementText'null$elementTextId);
  215.                     $textElement $this->_createElement('text'$elementText->textnull$elementTextElement);
  216.                     $elementTextContainerElement->appendChild($elementTextElement);
  217.                 }
  218.                 $elementElement->appendChild($elementTextContainerElement);
  219.                 $elementContainerElement->appendChild($elementElement);
  220.             }
  221.             $elementSetElement->appendChild($elementContainerElement);
  222.             $elementSetContainerElement->appendChild($elementSetElement);
  223.         }
  224.         $parentElement->appendChild($elementSetContainerElement);
  225.     }
  226.     
  227.     /**
  228.      * Build an itemType element in an item context.
  229.      * 
  230.      * @param Item $item The item from which to build the item type.
  231.      * @param DOMElement $parentElement The item type will append to this element.
  232.      * @return void|null
  233.      */
  234.     protected function _buildItemTypeForItem(Item $itemDOMElement $parentElement)
  235.     {
  236.         // Return if the item does not have an item type.
  237.         if (!$item->Type{
  238.             return null;
  239.         }
  240.         
  241.         $itemType $this->_getElemetSetsByElementTexts($itemtrue);
  242.         
  243.         // itemType
  244.         $itemTypeElement $this->_createElement('itemType'null$itemType->id);
  245.         $nameElement $this->_createElement('name'$itemType->namenull$itemTypeElement);
  246.         $descriptionElement $this->_createElement('description'$itemType->descriptionnull$itemTypeElement);
  247.         
  248.         // Do not append elements if no element texts exist for this item type.
  249.         if (count($itemType->elements)) {
  250.             // elementContainer
  251.             $elementContainerElement $this->_createElement('elementContainer');
  252.             foreach ($itemType->elements as $elementId => $element{
  253.                 // element
  254.                 $elementElement $this->_createElement('element'null$elementId);
  255.                 $nameElement $this->_createElement('name'$element->namenull$elementElement);
  256.                 $descriptionElement $this->_createElement('description'$element->descriptionnull$elementElement);
  257.                 // elementTextContainer
  258.                 $elementTextContainerElement $this->_createElement('elementTextContainer');
  259.                 foreach ($element->elementTexts as $elementTextId => $elementText{
  260.                     // elementText
  261.                     $elementTextElement $this->_createElement('elementText'null$elementTextId);
  262.                     $textElement $this->_createElement('text'$elementText->textnull$elementTextElement);
  263.                     $elementTextContainerElement->appendChild($elementTextElement);
  264.                 }
  265.                 $elementElement->appendChild($elementTextContainerElement);
  266.                 $elementContainerElement->appendChild($elementElement);
  267.             }
  268.             $itemTypeElement->appendChild($elementContainerElement);
  269.         }
  270.         $parentElement->appendChild($itemTypeElement);
  271.     }
  272.     
  273.     /**
  274.      * Build a fileContainer element in an item context.
  275.      * 
  276.      * @param Item $item The item from which to build the file container.
  277.      * @param DOMElement $parentElement The file container will append to this
  278.      *  element.
  279.      * @return void|null
  280.      */
  281.     protected function _buildFileContainerForItem(Item $itemDOMElement $parentElement)
  282.     {
  283.         // Return if the item has no files.
  284.         if (!count($item->Files)) {
  285.             return null;
  286.         }
  287.         
  288.         // fileContainer
  289.         $fileContainerElement $this->_createElement('fileContainer');
  290.         foreach ($item->Files as $file{
  291.             $fileOmekaXml new Omeka_Output_Xml_File($file$this->_context);
  292.             $fileElement $this->_doc->importNode($fileOmekaXml->_nodetrue);
  293.             $fileContainerElement->appendChild($fileElement);
  294.         }
  295.         $parentElement->appendChild($fileContainerElement);
  296.     }
  297.     
  298.     /**
  299.      * Build a collection element in an item context.
  300.      * 
  301.      * @param Item $item The item from which to build the collection.
  302.      * @param DOMElement $parentElement The collection will append to this
  303.      *  element.
  304.      * @return void|null
  305.      */
  306.     protected function _buildCollectionForItem(Item $itemDOMElement $parentElement)
  307.     {
  308.         // Return if the item has no collection.
  309.         if (!$item->Collection{
  310.             return null;
  311.         }
  312.         
  313.         // collection
  314.         $collectionElement $this->_createElement('collection'null$item->Collection->id);
  315.         $nameElement $this->_createElement('name'$item->Collection->namenull$collectionElement);
  316.         $descriptionElement $this->_createElement('description'$item->Collection->descriptionnull$collectionElement);
  317.         $parentElement->appendChild($collectionElement);
  318.     }
  319.     
  320.     /**
  321.      * Build a tagContainer element in an item context.
  322.      * 
  323.      * @param Item $item The item from which to build the tag container.
  324.      * @param DOMElement $parentElement The tag container will append to this
  325.      *  element.
  326.      * @return void|null
  327.      */
  328.     protected function _buildTagContainerForItem(Item $itemDOMElement $parentElement)
  329.     {
  330.         // Return if the item has no tags.
  331.         if (!count($item->Tags)) {
  332.             return null;
  333.         }
  334.         
  335.         // tagContainer
  336.         $tagContainerElement $this->_createElement('tagContainer');
  337.         foreach ($item->Tags as $tag{
  338.             // tag
  339.             $tagElement $this->_createElement('tag'null$tag->id);
  340.             $name $this->_createElement('name'$tag->namenull$tagElement);
  341.             $tagContainerElement->appendChild($tagElement);
  342.         }
  343.         $parentElement->appendChild($tagContainerElement);
  344.    }
  345.    
  346.    protected function _buildTagUri()
  347.    {
  348.        $uri Zend_Uri::factory(abs_uri());
  349.        $tagUri 'tag:' $uri->getHost(',' date('Y-m-d'':' $uri->getPath();
  350.        return $tagUri;
  351.    }
  352.    
  353.    protected function _buildUrl()
  354.    {
  355.        $uri Zend_Uri::factory(abs_uri());
  356.        $uri->setQuery($_GET);
  357.        return $uri->getUri();
  358.    }
  359. }

Documentation generated on Thu, 15 Oct 2009 15:35:08 -0400 by phpDocumentor 1.4.2