Recipes/Using Search Results to Create Custom Next and Previous Links

Problem

When you search for a term, you get a list of results on the item browse page, and the term you searched is displayed in the search box. When you view an individual item of this search, and then click next or previous links, you do not get the next or previous item in the search. Instead you get the next or previous item ID in the entire collection.

Solution

I noticed if I added the search string from the url of the item browse page to the item show page the search term appears in the search box.

So omeka/items/show/123 would be omeka/items/show/123?search=search+term&submit_search=Search. The following modifications create next and previous links based on items in the search.

Functions Used

The first thing I did was modify the browse.php file to create links to items that would have the search term. I used a conditional statement to have these links only appear when a search has been run.

<?php
if (item_has_thumbnail()):
    if($search=$_GET['search']): 
        $imagelink = item_uri().'?search='.$_GET['search'].'&submit_search=Search';
        echo '<div class="item-img"><a href="'.$imagelink.'">'.item_square_thumbnail(array('alt'=>item('Dublin Core', 'Title'))).'</a></div>'; 
    else: 
        echo '<div class="item-img">'.link_to_item(item_square_thumbnail(array('alt'=>item('Dublin Core', 'Title')))).'</div>';
    endif;
endif;
if($search=$_GET['search']):
    $searchlink = item_uri().'?search='.$_GET['search'].'&submit_search=Search';
    echo '<h2><a href="'.$searchlink.'">'.item('Dublin Core','Title').'</a></h2>'; 
else: 
    echo '<h2>'.link_to_item(item('Dublin Core', 'Title'), array('class'=>'permalink')).'</h2>'; 
endif;
?>

Next I created a custom function called custom_next_previous(), which determines if the item belongs to a larger search group, finds its position, and creates appropriate next and previous links. I've added comments to the code to explain how it works.

function custom_next_previous()
{
    //Starts a conditional statement that determines a search has been run
    if ($search = $_GET['search']) {
        // Sets the current item ID to the variable $current
        $current = item('id');
 
        // Get an array of all the items from the search, and all the IDs
        $list = get_items(array('search'=>$search),total_results());
        foreach ($list as &$value) {
            $itemIds[] = $value->id;
        }
 
        // Find where we currently are in the result set
        $key = array_search($current, $itemIds);
 
        // If we aren't at the beginning, print a Previous link
        if ($key > 0) {
            $previousItem = $list[$key - 1];
            $previousUrl = item_uri('show', $previousItem) . '?' . $_SERVER['QUERY_STRING'];
            echo '<li><a href="' . $previousUrl . '">Previous Item</a></li>';
        }
 
        // If we aren't at the end, print a Next link
        if ($key < count($list) - 1) {
            $nextItem = $list[$key + 1];
            $nextUrl = item_uri('show', $nextItem) . '?' . $_SERVER['QUERY_STRING'];
            echo '<li class="next"><a href="' . $nextUrl . '">Next Item</a></li>';
        }
    } else {
        // If a search was not run, then the normal next/previous navigation is displayed.
        echo '<li>'.link_to_previous_item('Previous Item').'</li>';
        echo '<li class="next">'.link_to_next_item('Next Item').'</li>';
    }
}

Finally, I replaced the following in the items/show.php file:

 <ul class="item-pagination navigation">
    <li><?php echo link_to_previous_item('Previous Item'); ?></li>
    <li class="next">	<?php echo link_to_next_item('Next Item'); ?></li>
 </ul>

with a call to my custom_next_previous function:

<ul class="item-pagination navigation">
    <?php custom_next_previous(); ?>
</ul>

This modification works for simple searches.