From 9899e44b2d7fbd5ec755f36659905c7d68185a43 Mon Sep 17 00:00:00 2001 From: Magnus Enger Date: Thu, 5 Aug 2010 10:59:31 +0000 Subject: [PATCH] Issue 3. Add sorting with XSLT --- api/index.php | 16 +++++++--------- inc.glitre.php | 39 +++++++++++++++++++++++++++++++++++++++ xslt/simplesort.xslt | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 xslt/simplesort.xslt diff --git a/api/index.php b/api/index.php index dc3dd26..0747d15 100644 --- a/api/index.php +++ b/api/index.php @@ -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)); } diff --git a/inc.glitre.php b/inc.glitre.php index 144f587..8611ee7 100644 --- a/inc.glitre.php +++ b/inc.glitre.php @@ -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']); @@ -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 */ diff --git a/xslt/simplesort.xslt b/xslt/simplesort.xslt new file mode 100644 index 0000000..049e78e --- /dev/null +++ b/xslt/simplesort.xslt @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file