Recipes/Linking to Related Items

Problem

I wanted to create a way to link individual items to a larger group of related items. For example, we have yearbook pictures from the early 1900s on our site as individual items. We wanted to add a link to the item show page that would bring the user to all of the related images from that specific year.

Solution

Initially, I thought of taking part of the titles, and creating search links from the title. Also I tried using the dates as a search limiter. However, those methods often produced inconsistent results, with more images than I wanted. The thought of manually grouping items that are related also didn't seem feasible.

Eventually, I decided that using the identifier field as my search limiter was my best option. We had generally uploaded items in batches with the csv import, and items that shared part of their identifier were often closely related. So 1915 medical yearbook images had the identifiers of 1915M_01, 1915M_02, etc. Basically, I needed to get rid of the part of the identifier that was specific to the item, usually the last two or three numbers, so that my search term was just the base of the identifier. I want 1915M_ instead of 1915M_01. The identifier 1915M_01 would return just that one item if used as a search term.

However, I also noticed that we don't have standard identifiers, some looked like 06-15-01 and others looked like 1916D_01. So my code had to take that into account.

The following code is placed in my custom.php file. It looks at the item's identifier for a "-" or "_". Then it finds the last instance of that character, and returns the string before the character. So 06-1940-03 becomes 06-1940-. Then it fills in the search link with that info. We decided to place this link at the end of the description field, but it could easily go elsewhere.

add_filter(array('Display', 'Item', 'Dublin Core', 'Description'), 'mytheme_display_description');
 
function mytheme_display_description($description, $item, $elementTextRecord)
{
$text = '';
$text.=$description;
$itemurl=uri(); // This gets the current page's url
if (stristr($itemurl,'show')){ //This prevents the link from showing on the browse pages
$myid=item('Dublin Core', 'Identifier');
if (stristr($myid,'-')){
$myid= substr($myid,0,strrpos($myid,'-')+strlen('-'));}//Finds last "-" and returns string preceding it. Removes individual aspect of item's identifier.
if (stristr($myid,'_')){
$myid= substr($myid,0,strrpos($myid,'_')+strlen('_'));}
$link = uri("items/browse?search=&advanced[0][element_id]=43&advanced[0][type]=contains&advanced[0][terms]=" . $myid . "&submit_search=Search");//On our site element_id=43 corresponds to the identifier element, but it might be different on other sites.
$text.='&nbsp<a class="browse-link" href="' . $link . '" title="Browse Related Items">Browse related items</a>';}
return $text;
 
}

I found how to return the part of a string preceding the last instance of a character at http://www.php.net/manual/en/function.strrchr.php#60075. There might be an easier way, but it works.

Functions Used

http://www.php.net/manual/en/function.stristr.php
http://www.php.net/manual/en/function.substr.php
http://www.php.net/manual/en/function.strpos.php
http://www.php.net/manual/en/function.strlen.php