Skip to content

Commit

Permalink
Issue 3. Add sorting with XSLT
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus Enger committed Aug 5, 2010
1 parent 8092de9 commit 9899e44
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
16 changes: 7 additions & 9 deletions api/index.php
Expand Up @@ -38,23 +38,21 @@
echo('Missing parameter: library');
exit;
}
if (empty($_GET['format'])) {
echo('Missing parameter: format');
exit;
}
if (empty($_GET['q']) && empty($_GET['id'])) {
echo('Missing parameter: q OR id');
exit;
}

// Search
if (!empty($_GET['q']) && !empty($_GET['library']) && !empty($_GET['format'])) {
if (!empty($_GET['q']) && !empty($_GET['library'])) {
$args = array(
'q' => $_GET['q'],
'library' => $_GET['library'],
'format' => $_GET['format'],
'page' => $_GET['page'] ? $_GET['page'] : 1,
'per_page' => $_GET['per_page'] ? $_GET['per_page'] : 10
'library' => $_GET['library'],
'format' => $_GET['format'] ? $_GET['format'] : 'plugin.simple',
'page' => $_GET['page'] ? $_GET['page'] : 1,
'per_page' => $_GET['per_page'] ? $_GET['per_page'] : 10,
'sort_by' => $_GET['sort_by'] ? $_GET['sort_by'] : 'year',
'sort_order' => $_GET['sort_order'] ? $_GET['sort_order'] : 'descending'
);
echo(glitre_search($args));
}
Expand Down
39 changes: 39 additions & 0 deletions inc.glitre.php
Expand Up @@ -45,6 +45,9 @@ function glitre_search($args) {
$query = $args['q'] ? "any=" . masser_input($args['q']) : 'tnr=' . urlencode($args['id']);
$marcxml = get_z($query);
}

// Sort the records
$marcxml = glitre_sort($marcxml, $args['sort_by'], $args['sort_order']);

// Format the records
return glitre_format($marcxml, $args['format']);
Expand Down Expand Up @@ -78,6 +81,42 @@ function glitre_format($marcxml, $format){

}

function glitre_sort($marcxml, $sort_by = 'year', $sort_order = 'descending') {

// Check that sort_by and sort_order are valid
$allowed_sort_by = array('author', 'year', 'title');
if (!in_array($sort_by, $allowed_sort_by)) {
exit("Invalid sort_by: $sort_by");
}
$allowed_sort_order = array('descending', 'ascending');
if (!in_array($sort_order, $allowed_sort_order)) {
exit("Invalid sort_order: $sort_order");
}

// Create a DOM and load the data
$xml = new DOMDocument;
$xml->loadXML($marcxml);

// Create a dom and load the XSLT
$xsl = new DOMDocument;
$xsl->load('/home/sites/div.libriotech.no/public/glitre/xslt/simplesort.xslt', LIBXML_NOCDATA);

// Configure the XSLT processor
$proc = new XSLTProcessor;
// Parameters
$proc->setParameter('', 'sortBy', $sort_by);
$proc->setParameter('', 'sortOrder', $sort_order);
// Add the XSLT DOM to the processor
$proc->importStyleSheet($xsl);

// Do the transformation
$dom = $proc->transformToDoc($xml);

// Return the XML
return $dom->saveXML();

}

/*
Utfører Z39.50-søket og returnerer postene i MARCXML-format, som en streng
*/
Expand Down
40 changes: 40 additions & 0 deletions xslt/simplesort.xslt
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:zs="http://www.loc.gov/zing/srw/">
<xsl:output
method="xml"
encoding="UTF-8"/>

<xsl:param name="sortBy"/>
<xsl:param name="sortOrder"/>

<xsl:template match="/">

<xsl:element name="records">

<xsl:choose>
<xsl:when test="$sortBy='author'">
<xsl:apply-templates select="//record">
<xsl:sort select="datafield[@tag=100]/subfield[@code='a']" data-type="text" order="{$sortOrder}"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="$sortBy='year'">
<xsl:apply-templates select="//record">
<xsl:sort select="translate(datafield[@tag=260]/subfield[@code='c'], 'cop.[]', '')" data-type="text" order="{$sortOrder}"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="$sortBy='title'">
<xsl:apply-templates select="//record">
<xsl:sort select="datafield[@tag=245]/subfield[@code='a']" data-type="text" order="{$sortOrder}"/>
</xsl:apply-templates>
</xsl:when>
</xsl:choose>

</xsl:element>

</xsl:template>

<xsl:template match="//record">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

0 comments on commit 9899e44

Please sign in to comment.