08: COMPOUND OBJECTS — II

Modifying dmBrowse.php (code back in Searching Your Collections), I can link to a program that actually displays each page as a thumbnail if the object is a compound object. First, though, I am going to build a simple HTML page that will let me feed a search term to my new program.

search.html

<html>
        <head>
                <title>Search for a term</title>
        </head>
        <body>
                <h1 style="text-align: center;">Search for a Title</h1>
                <h2 style="text-align: center;">Browsing the Illinois Digital Archives</h2>
                <form method="post" action="dmEnhancedBrowse.php">
                        <center>
                                <strong>Search for:</strong>   <input type="text" name="searchterm" size="50">
                                <p />
                                <input type="submit" name="submit" value="Search across IDA...">
                        </center>
                </form>
        </body>
</html>

dmEnhancedBrowse.php

<?php

$searchTerm = $_POST["searchterm"];

$xmlData = file_get_contents("https://server16614.contentdm.oclc.org/dmwebservices/index.php?q=dmQuery/all/title^$searchTerm^all^and/title!subjec!descri/title/50/1/0/0/0/0/xml");

// Create the document object

$xml = simplexml_load_string($xmlData);

$pager = array();

// How many hits did the search yield

foreach ($xml->xpath('//pager') as $hits) {
        $pager[] = array(
                'start' => (string) $hits->start,
                'total' => (string) $hits->total
        );
}

$result = array();

// Get the nodes and loop them

foreach ($xml->xpath('//record') as $record) {
        $result[] = array(
                'collection' => (string) $record->collection,
                'title' => (string) $record->title,
                'subject' => (string) $record->subjec,
                'descri' => (string) $record->descri,
                'thumb' => (string) $record->pointer
        );
}

$numberOfHits = $pager[0]["total"];
$resultCount = count($result) - 1;

?>
<html>
        <head>
                <title>My Search Results</title>
        </head>
        <body>
                <div id="header">
                        <h1 style="text-align: center;">My Library</h1>
                        <h2 style="text-align: center;">Browsing our collections, with <?php echo $numberOfHits ?> results</h2>
                </div>
                <div id="list">
                        <ol>
                        <?php
for ($i=0;$i<=$resultCount;$i++) {
        $title = $result[$i]["title"];
        $subject = $result[$i]["subject"];
        $description = $result[$i]["descri"];
        $thumb = $result[$i]["thumb"];
        $collection = $result[$i]["collection"];
        $collection = str_ireplace("/", "", "$collection");
        $urlStr = "getCompoundObjectsWebPage.php?collection=$collection&pointer=$thumb";
        $imgStr = "http://www.idaillinois.org/utils/getthumbnail/collection/$collection/id/" . $thumb;
        echo "<li><a href=\"$urlStr\"><img src=\"$imgStr\"></a> <strong>$title</strong><br /><em>$description</em><br />\n$subject\n<p /></li>\n";
}
?>
                        </ol>
                </div>
                <div id="footer">
                        <p />
                        <strong>Our Library</strong>
                </div>
        </body>
</html>

Not very attractive, but getCompoundObjectsWebPage.php takes all of the pages in a compound object and displays them as cells in a table:

getCompoundObjectsWebPage.php

<?php

// Sent from the browse record when user selects an image for "full record"

$collection = $_GET["collection"];
$pointer = $_GET["pointer"];

$xmlData = file_get_contents("https://server16614.contentdm.oclc.org/dmwebservices/index.php?q=dmGetItemInfo/$collection/$pointer/xml");

// $xmData now holds the contents of the metadata for the record pointed at by $pointer

$fieldData = file_get_contents("https://server16614.contentdm.oclc.org/dmwebservices/index.php?q=dmGetCollectionFieldInfo/$collection/xml");

// $fieldData now holds the nicknames/proper field names of the metadata

$sxe = new SimpleXMLElement($xmlData);

$xml = simplexml_load_string($xmlData);

// load the metadata into a data structure, $xml

$fieldData = simplexml_load_string($fieldData);

// load the nicknames into a data structure, $fieldData

foreach ($fieldData->xpath('//field') as $record) {

        // build a key/value structure from the raw structure

        $result[] = array(
                'nick' => (string) $record->nick,
                'name' => (string) $record->name
        );
}

// I have found the quickest way to determine if a record points to a compound object is to simply check for the file extension .cpd in the <find> element

$typeOfRecord = $xml[0]->{"find"};
$pos = strpos($typeOfRecord, ".cpd");

// This bit should be expanded; it just separates Compound objects from single images. You might want to match this per file ending for more sensitive treatment...

if ($pos === false) { $typeOfRecord = "Image"; }
else {
        $typeOfRecord = "Text";
        $tableStr = getCompoundObjectData($collection,$pointer);
}

function searchForId($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['nick'] === $id) {
           return $key;
       }
   }
   return null;
}
function getCompoundObjectData($collection, $pointer) {

        // gets the compound object pages, then places them in the cells of a table

        $tableStr = "<table width=\"80%\" border=\"1\">\n";

        $coData = file_get_contents("https://server16614.contentdm.oclc.org/dmwebservices/index.php?q=dmGetCompoundObjectInfo/$collection/$pointer/xml");
        $coXML = simplexml_load_string($coData);
        $cpd = $coXML->{"type"};
        if ($cpd == "Monograph") {
                foreach ($coXML->xpath('//node') as $coRecord) {
                        foreach ($coRecord->xpath('//page') as $page) {
                                        $pagePtr = (string) $page->pageptr;
                                        $tableStr = $tableStr . "<tr>\n<td width=\"100%\">\n";
                                        $urlStr = "dmGetItemInfoWebPage.php?collection=$collection&pointer=$pagePtr";
                                        $tableStr = $tableStr . "<a href=\"$urlStr\"><img src=\"http://www.idaillinois.org/utils/getthumbnail/collection/$collection/id/$pagePtr\"></a>\n</td>\n</tr>\n";
                        }
                }
        }
        else {
                foreach ($coXML->xpath('//page') as $coRecord) {
                        $pagePtr = (string) $coRecord->pageptr;
                        $tableStr = $tableStr . "<tr>\n<td width=\"100%\">\n";
                        $urlStr = "dmGetItemInfoWebPage.php?collection=$collection&pointer=$pagePtr";
                        $tableStr = $tableStr . "<a href=\"$urlStr\"><img src=\"http://www.idaillinois.org/utils/getthumbnail/collection/$collection/id/$pagePtr\"></a>\n</td>\n</tr>\n";
                }
        }
        $pageFileStr = $pageFileStr . "</table>\n";
        return ($tableStr);
}

?>
<html>
        <head>
                <title>My Collections</title>
        </head>
        <body>
                <div id="header">
                        <h1 style="text-align: center;">My Collections</h1>
                </div>
                <div id="list">
                        <center>
                                <?php
                                        if ($typeOfRecord != "Text") {
                                                echo "<img src=\"http://www.idaillinois.org/utils/ajaxhelper/?CISOROOT=$collection&CISOPTR=$pointer&action=2&DMSCALE=100&DMWIDTH=3000&DMHEIGHT=3000&DMX=0&DMY=0\">\n";
                                        }
                                        else if ($typeOfRecord == "Text") {
                                                echo $tableStr;
                                        }
                                ?>
                        </center>
                        <p />
                        <table border="1">
                        <?php
                                foreach ($sxe->children() as $child) {

                                        // getName gets the name of the XML element.

                                        $str = $child->getName();
                                        $title = $xml[0]->{"$str"};
                                        $id = searchForId($str, $result);
                                        if (isset($id)) {
                                                $fieldName = $result[$id]["name"];
                                                echo "<tr><td width=\"25%\"><strong>$fieldName</strong></td>\n";
                                                echo "<td width=\"75%\">$title</td></tr>\n";
                                        }
                                }
                        ?>

                        </table>
                </div>
                <div id="footer">
                        <p />
                        <strong>Our Library</strong>
                </div>
        </body>
</html>