Linking to search results from item show.php

We are using a field called People Depicted under the Item type metadata tab to populate names of people in pictures. We are standardizing names through the use of an alumni directory, so if the person appears in more than one photograph, the name in this field is the same. A person will always be "Smith, Joe", not "Smith, Joseph" or "Smith, Joey".

Would it be possible to have links generated for names that appear multiple times on our site? So if "Smith, Joe" appears 4 times throughout our site, a link that leads to the search results for "Smith, Joe" in the people depicted field would be shown in this field. I could do this manually for each name, but it would be nice if I could do it automatically with some php.

Thanks.

I haven't tested this yet, but I think this is basically what I want to have done. Currently the people depicted field is in the format "last1, first1; last2, first2; last3, first3;...etc." So for each item, the people depicted field is one long string of names separated by semi-colons. So I think I could use the php explode to break it up into an array. Then I should be able to add html to each element of the array with foreach.

<div class="element-text">
<h3>People Depicted:&nbsp;</h3>
<?php $people=item('Item Type Metadata', 'People Depicted');
$divided = explode(";", $people);
foreach($divided as &$value) {
$value= <a href="https://www.mc.vanderbilt.edu/omeka/items/browse?search=&advanced[0][element_id]=90&advanced[0][type]=contains&advanced[0][terms]=$value&submit_search=Search">$value</a>
echo $value;
}
?>

I'm sure there are glaring problems with this, but I think the basic idea should work.

Hi Andy,

This is the way I would do it:

First of all, if the element People Depicted is repeatable there is not need of separate names by a semicolon. Just add them one by one (or put them in different columns if you are exporting them through the CSV plugin).

Second, install the Metadata Browser plugin by Kevin Reiss, and in the items/show.php file of your theme use a code similar to this:

<?php if ($peopledepicted = item('Item Type Metadata', 'People Depicted', array('all'=>'true'))): ?>
		<h3>People Depicted</h3>
        <?php foreach($peopledepicted as $value) {?>
            <div class="element-text"><?php echo metadata_browser_element_link("People Depicted", $value);?></div>
        <?php }?>
<?php endif; ?>

The only inconvenient that you have with this is that if you keep using <?php echo custom_show_item_metadata(); ?> in the items/show.php file, the element People Depicted will appears twice, so you would have to stop using this function and call each element that you want to show individually, like in the following example for the element Creator:

<?php  (($creator = item('Dublin Core', 'Creator', array('delimiter' => '; ')))): ?>
<h3>Creator:<h3>
<p><?php echo $creator; ?></p>
<?php endif; ?>

I´m sure there must be other easier ways to accomplish this but that is the direction I took to achieve something similar.

Hope it helps,
Dani

Thanks for the response Dani. Unfortunately we have already populated the People Depicted field with names divided by semi-colons for over 1000 records. Also we chose to do it this way because some of the pictures have close to 100 people in them, and it's more compact to display them this way. However, thanks for pointing me to the plugin you mentioned, I think I should be able to use some of its code to work for our site. Specifically the metadata_browser_element_link function should be useful.

Here's what I've added to my show.php file

<div class="element">
<h3>People Depicted:&nbsp;</h3>
<div class="element-text">
<?php $people=item('Item Type Metadata', 'People Depicted');
$names=str_replace(", ", "%2C+", $people);
$names=str_replace(" ", "+", $names);
$divided = explode(";", $names);
foreach($divided as &$value) {
$link=uri("items/browse?search=&advanced[0][element_id]=91&advanced[0][type]=contains&advanced[0][terms]=".$value."&submit_search=Search");
$cleanvalue=str_replace("%2C+", ", ", $value);
$cleanvalue=str_replace("+", " ", $cleanvalue);
$value='<a class="browse-link" href="' . $link . '" title="Browse ' . $cleanvalue . '">' .$cleanvalue . "</a>; ";
echo $value;
     }
?>

It works, but now as Dani mentioned above, I have two People Depicted fields, one with text, and the other with links to searches. I was thinking there might be a way to prevent the first field from showing with the config.ini file of my theme. Currently I can select Dublin Core fields that I want to display in the admin, but I'm not seeing how to select which Item Type Metadata fields to display. Is there something I could add to the config.ini file that would allow me to set which Item Type Metadata fields to display?

Thanks.

I've added the custom_show_item_metadata to my custom.php file and renamed it. I've added a part so that it looks to see if any item type metadata fields have been added to the customized section of the theme. So basically now I can control both Dublin Core and Item Type Metadata fields that display from the admin screen.

function mycustom_show_item_metadata(array $options = array(), $item = null)
    {
        if (!$item) {
            $item = get_current_item();
        }
    	if ($dcFieldsList = get_theme_option('display_dublin_core_fields')) {
    	    $html = '';
    	    $dcFields = explode(',', $dcFieldsList);
    	    foreach ($dcFields as $field) {
    	        $field = trim($field);
    	        if (element_exists('Dublin Core', $field)) {
        	        if ($fieldValue = item('Dublin Core', $field)) {
        	            $html .= '<div class="element"><h3>'.$field.': </h3>';
        	            if (!item_field_uses_html('Dublin Core', $field)) {
        	                $fieldValue = ($fieldValue);
        	            }
        	            $html .= '<div class="element-text">'.$fieldValue. '</div></div>';
        	        }
        	    }
if (element_exists('Item Type Metadata', $field)) {
        	        if ($fieldValue = item('Item Type Metadata', $field)) {
        	            $html .= '<div class="element"><h3>'.$field.': </h3>';
        	            if (!item_field_uses_html('Item Type Metadata', $field)) {
        	                $fieldValue = ($fieldValue);
        	            }
        	            $html .= '<div class="element-text">'.$fieldValue. '</div></div>';
        	        }
        	    }

    	    }

    	    return $html;
    	} else {
    	    return show_item_metadata($options, $item);
        }
    }

So that gets rid of the duplicate people depicted fields without having to individually call each element that I want to display. Now I just need to make it so the People Depicted field only displays when there is data in it.

Another option, instead of duplicating the People Depicted field and manually removing its automatic display, you can replace the normal display of the field using the Element Display Filter

Something like the following in your theme's custom.php (or a plugin, if you want):

add_filter(array('Display', 'Item', 'Item Type Metadata', 'People Depicted'), 'mytheme_display_people_depicted');

function mytheme_display_people_depicted($people, $item, $elementTextRecord)
{
    $text = '';
    $divided = explode(";", $people);
    foreach($divided as &$value) {
        $link = uri("items/browse?search=&advanced[0][element_id]=91&advanced[0][type]=contains&advanced[0][terms]=" . urlencode($value) . "&submit_search=Search");
        $text .= '<a class="browse-link" href="' . $link . '" title="Browse ' . $cleanvalue . '">' . $value . '</a>; ';
    }
    return $text;
}

Very cool. Thanks! Much simpler. Also we should be able to use this method for other elements if we need to. Also I didn't know about the php urlencode function. That works a lot better than my method of replacing commas and spaces. I still have to figure out what to do with records that don't have any info in the people depicted field. Right now it's showing up with a semi-colon in the field, but I imagine I can work around that with a conditional statement. Thanks again.

John, I've added what you posted to my custom.php file with a few changes. I'm using if(strlen($people)>4 to prevent empty fields from displaying with a semi-colon. I added $value=trim($value) to get rid of some spaces that were creating incomplete searches.

Also I'm working with both a development and a production server version Omeka. I noticed that in one, the People Depicted was element_id 90 and the other version was 91. So instead of having a static link to 91, I used some more from the Metadata Browser plugin mentioned above. So here's what I have in my custom.php:

add_filter(array('Display', 'Item', 'Item Type Metadata', 'People Depicted'), 'mytheme_display_people_depicted');

function mytheme_display_people_depicted($people, $item, $elementTextRecord)
{
    if(strlen($people)>4){
$name="People Depicted";
$db = get_db();
$elementTable = $db->getTable('Element');
$elementSelect = $elementTable->getSelect()->where("e.name = '$name'");
$element = $elementTable->fetchObject($elementSelect);
$id=$element->id;
   $text = '';
    $divided = explode(";", $people);
    foreach($divided as &$value) {
	$value=trim($value);
$link = uri("items/browse?search=&advanced[0][element_id]=".$id."&advanced[0][type]=contains&advanced[0][terms]=" . urlencode($value) . "&submit_search=Search");
        $text .= '<a class="browse-link" href="' . $link . '" title="Browse ' . $value . '">' . $value . '</a>; ';
    }
    return $text;
}
}

It might not be pretty, but it seems to work. There are a few issues that I may still try to resolve. The names always have a semi-colon after them. Even when they are the last name of the list or if there is just one name. Also it would be nice to have a way to show how many search results you'd get before clicking on a name. So it would display "Smith, Joe (34); Thomas, Tom T. (1);.." Not sure how to do that, but I think I can figure something out about the extraneous semi-colons.
Thanks again,
Andy;

For the extra semicolons issue, the easiest thing to use would be PHP's implode:

$links = array();
foreach ($divided as &$value) {
    // Your existing code inside the loop except the last line...
    $links[] = '<a class="browse-link" ...
}
return implode('; ', $links);

Thanks John, that worked great. I also had to remove the semi-colon at the end of this statement:
$links[] = '<a class="browse-link".... '</a>; ';
so now it's
$links[] = '<a class="browse-link".... '</a>';

Andy