<?php

//This code adapts the ccl example at indexdata.dk and the interface design for php refdb

?>

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>RefDB search external resource (php_yaz)</title>
        <link href="refdbqxhtml.css" type="text/css" rel="stylesheet"/>
    </head>
    <body>
                <h1>RefDB search external resource (php_yaz)</h1>
        <div class="navigation">
            <h2>Navigation</h2>
            <ul class="navigationMenu">
                <li>
                    <a href="index.html">Login screen</a>
                </li>
                <li>
                    <a href="refdbquery.html">Search references</a>
                </li>
                <li>
                    <a href="refdbkajquery.html">Search keywords, authors, journals</a>
                </li>
                <li>
                    <a href="refdbadd.html">Add new references</a>
                </li>
                <li>
                    <a href="refdbdbquery.php">Change database</a>
                </li>
                <li>
                    <a href="external.php">Search external database</a>
                </li>
                <li>
                    <a href="refdbadmin.php">Administrative Interface</a>
                </li>
                <li>
                    <a href="refdblogout.php">Logout</a>
                </li>
            </ul>
        </div>
        <div class="content">
        
<?php
// This adds  subtracts slashes from in front of apostrophes as requires by the PHP server's "magic quotes" status 
function myAddSlashes( $string ) 
            {
               if (get_magic_quotes_gpc()==1) 
               {
                  return ( $string );
               } 
               else 
               {
                  return ( addslashes ( $string ) );
               }
            }
?>
<?
    $host=$_GET['host'];
    $term=$_GET['term'];
    $num_hosts = count($host);
    //The following checks the status of the term input and the number of hosts open. If either is null, then it displays the search field.
    if (empty($term) || count($host) == 0)
    {
        echo '<h2>Select resource</h2>
                 <p><form method="get">
                 <input type="checkbox"
                   name="host[]" value="melvyl.cdlib.org/CDL90"/>
                   MELVYL&nbsp;
                 <input type="checkbox" checked="1"
                   name="host[]" value="z3950.loc.gov:7090/voyager"/>
                   Library of Congress&nbsp;</p>
                 <hr/>
                 <h2>Search</h2>
                 <p><input type="text" size="30" name="term"/>
                 <input type="submit" name="action" value="Search"/></p>
                 <p>Supported CCL fields: ti (title), au (author), ab (abstract), date, isbn and issn. You can combine fields using operators "and" and "or": e.g. <pre>ti="submarine" and au="parrish thomas"</pre>.</p>';
      }
      //If term or open hosts are not equal to null, parse the input field and prepare the search: 
      else
      {
      //Define each property of bibl1 (todo: add year)
        $bib1["term"] = "u=1016 s=pw t=l,r";
        $bib1["ti"] = "u=4 s=pw t=l,r";
        $bib1["au"] = "u=1003";
        $bib1["ab"] = "u=62 s=pw t=l,r";
        $bib1["isbn"] = "u=7 s=pw";
        $bib1["issn"] = "u=8 s=pw";
        $bib1["date"] = "u=30 r=o";
        
        // Repeat search terms back to user; htmlspecialchars ensures that things like & in input are rendered &amp;.
        // See http://ca3.php.net/htmlspecialchars
        echo '<p>You searched for ' . htmlspecialchars($term) . '</p>';
        
        //Assigns ids to hosts: 1) begin with i = 0, as long as i < that the num_hosts (defined above) keep adding 1 
        for ($i = 0; $i < $num_hosts; $i++) {
            //for each host assign an index id
            $id[] = yaz_connect($host[$i]);

            // Mapping CCL terms to RPN as defined above (bib1) for each search 
            yaz_ccl_conf($id[$i], $bib1);
            // Parse input against yaz_ccl_conf; if error report message; defines cclresult as output of parsing
            if (!yaz_ccl_parse($id[$i], $term, &$cclresult)) {
                echo '<p>Error: ' .
                    $cclresult["errorstring"] . '</p><p>Back to <a href="external.php">search</a></p>';
                continue;
            }
            //Specify z39.50 preferred syntax for retrieval from the server 
            yaz_syntax($id[$i],"usmarc");
            //Prepare search (excuted by yaz_wait). Attributes are (resource id, string type [must be "rpn", string query). 
            // cclresult is produced by yaz_ccl_parse above.
            yaz_search($id[$i],"rpn",$cclresult["rpn"]);
            // Specify number of records to be retrieved. (Should be called befiore yaz_search: http://www.php.net/manual/en/function.yaz-range.php). This is replaced below.
            yaz_range($id[$i], 0, 0);
        }
        
        //Allows searches to be executed: returns when server completes or aborts.
        yaz_wait();
        
        //run through each host for counts
        for ($i = 0; $i < $num_hosts; $i++) {
            $count=yaz_hits($id[$i]);
            
            //sets yaz_range in response to number of hits
            if ($count >= 4)
                $count = 4;
            yaz_range($id[$i], 1, $count);
            
            //prepares for retrieval of number of records specified in yaz_range above
            yaz_present($id[$i]);
        }
        
        //Allows searches to be executed: returns when server completes or aborts.
        yaz_wait();
        for ($i = 0;  $i <$num_hosts; $i++)
        {
            //add line, indicate the host
            echo '<hr/>' . $host[$i] . ":";
            //define error variable
            $error = yaz_error($id[$i]);
            
            //if there is an error, report it.
            if (!empty($error)) {
                echo "<p>Error: $error (Back to <a href='external.php'>search</a>)</p>";
            } 
            
            //Otherwise report the number of hits
            else 
            {
                $hits = yaz_hits($id[$i]);
                echo "<p>Result Count $hits (Back to <a href='external.php'>search</a>)</p>";
            }
            
            // Now present the output
            echo '<ul>';
            
            //Restrict following to 20 records
            for ($p = 1; $p <= 20; $p++)
            {
                // present record of idx in position p, in format "string"; convert marc-8 to utf-8
                $rec = yaz_record($id[$i],$p,"string");
                
                //if the record is empty go on to the next host?
                if (empty($rec)) continue;
                
                //Print the position number
                echo "<li><strong>$p</strong><ul><li>";
                
                //Go through each record looking for \n and replacing it with html:br element
                echo ereg_replace("\n", "<br/>\n",$rec);
                echo "</li></ul></li>";
            }
            echo '</ul>';
        }
    }

?>
</div>
        <div class="footer">
            <p><a href="http://www.indexdata.dk/phpyaz/">PHP_YAZ</a> PHP-based Z39.50 client. <!--VERSION-->0.9.8-1<!--/VERSION--></p>
        </div>
</body>
</html>
