Esempio n. 1
0
 /**
  *  Take a Xerxes data object representing a database, output
  *  a DOMDocument nodeset representing that database, for including
  *  in an XML response. Used by some Databases controllers. 
  *
  *  To actually include the returned value, you will need to import it into
  *  your DOMDocument of choice first. Example:
  *        $objDatabase = self::databaseToNodeset($objDatabaseData, $objRequest);
  *        $objDatabase = $objXml->importNode( $objDatabase, true );
  *        $objXml->documentElement->appendChild($objDatabase);
  *
  *
  * @param Xerxes_Data_Database $objDatabaseData
  * @param Xerxes_Framework_Request $objRequest  need the Xerxes request object to create urls for us. 
  * @param Xerxes_Framework_Registry $objRegistry need a registry object too, sorry. 
  * @param &$index = null  sometimes we want to append a count index to the xml. Pass in a counter variable, and it will be included AND incremented (passed by reference).
  * @return DOMNode
  */
 public static function databaseToNodeset(Xerxes_Data_Database $objDatabaseData, Xerxes_Framework_Request $objRequest, Xerxes_Framework_Registry $objRegistry, &$index = null)
 {
     $xml = $objDatabaseData->xml;
     //PHP 5.1.6 simplexml bug, 'for' iteration over ->searchable will create
     //it already if it doesn't exist, which we don't want, so
     //we have to wrap 'for' in 'if'.
     if (count($xml->searchable)) {
         foreach ($xml->searchable as $searchable) {
             // sometimes we're asked to track and record index.
             if ((string) $searchable == "1") {
                 $searchable["count"] = $index;
                 $index++;
             }
         }
     }
     // display name for group restrictions
     // bug in PGP 5.1.6 SimpleXML, if we do the foreach WITHOUT wrapping
     // it in this if testing count first,
     // it'll add on an empty <group_restriction/> to the xml
     // graph even if none were there previously. That's bad.
     if (count($xml->group_restriction) > 0) {
         foreach ($xml->group_restriction as $group_restriction) {
             $group_restriction->addAttribute("display_name", $objRegistry->getGroupDisplayName((string) $group_restriction));
         }
     }
     $multilingual = $objRegistry->getConfig("db_description_multilingual", false, "");
     // XML object
     $lang = $objRequest->getProperty("lang");
     if ($lang == "") {
         $lang = "eng";
     }
     // build a list of configured description languages
     if ($multilingual != "") {
         $order = 0;
         foreach ($multilingual->language as $language) {
             $order++;
             $code = NULL;
             foreach ($language->attributes() as $name => $val) {
                 if ($name == "code") {
                     $code = (string) $val;
                 }
             }
             $db_languages_order[$code] = $order;
             $db_languages_code[$order] = $code;
         }
     }
     $notes = array("description", "search_hints");
     foreach ($notes as $note_field_name) {
         $node_queue = array();
         // nodes to add when done looping to prevent looping over nodes added inside the loop
         foreach ($xml->{$note_field_name} as $note_field_xml) {
             $note_field = (string) $note_field_xml;
             // strip out "##" chars, not just singular "#" to allow # in markup
             // or other places.
             $pos = strpos($note_field, '######');
             if ($multilingual == false or $pos === false) {
                 $note_field = str_replace('######', '\\n\\n\\n', $note_field);
             } else {
                 $descriptions = explode('######', $note_field);
                 $i = 1;
                 foreach ($descriptions as $description) {
                     $description = self::embedNoteField($description);
                     $node_queue[] = array('note_field_name' => $note_field_name, 'description' => $description, 'code' => $db_languages_code[$i++]);
                 }
             }
             $note_field = self::embedNoteField($note_field);
             $xml->{$note_field_name} = $note_field;
             $xml->{$note_field_name}->addAttribute('lang', 'ALL');
         }
         foreach ($node_queue as $node) {
             $descNode = $xml->addChild($node['note_field_name'], $node['description']);
             $descNode->addAttribute('lang', $node['code']);
         }
     }
     $objDom = new DOMDocument();
     $objDom->loadXML($xml->asXML());
     $objDatabase = $objDom->documentElement;
     $objDatabase->setAttribute("metalib_id", $objDatabaseData->metalib_id);
     // is the particular user allowed to search this?
     $objElement = $objDom->createElement("searchable_by_user", self::dbSearchableForUser($objDatabaseData, $objRequest, $objRegistry));
     $objDatabase->appendChild($objElement);
     //add an element for url to xerxes detail page for this db
     $objElement = $objDom->createElement("url", $objRequest->url_for(array("base" => "databases", "action" => "database", "id" => htmlentities($objDatabaseData->metalib_id))));
     $objDatabase->appendChild($objElement);
     // The 'add to personal collection' url for logged in user--if no
     // logged in user, generate link anyway, but it's going to have
     // an empty user. User should be required to log in before continuing
     // with action.
     $url = $objRequest->url_for(array("base" => "collections", "action" => "save_choose_collection", "id" => $objDatabaseData->metalib_id, "username" => $objRequest->getSession("username"), "return" => $objRequest->getServer('REQUEST_URI')));
     $objElement = $objDom->createElement("add_to_collection_url", $url);
     $objDatabase->appendChild($objElement);
     //add an element for url to xerxes-mediated direct link to db.
     $objElement = $objDom->createElement("xerxes_native_link_url", $objRequest->url_for(array("base" => "databases", "action" => "proxy", "database" => htmlentities($objDatabaseData->metalib_id))));
     $objDatabase->appendChild($objElement);
     return $objDatabase;
 }