Updating dc:coverage with Geolocation address?

I’m using the Geolocation and Contribution plugins, and I’m interested in capturing whatever the user enters into the “find” (address) box and storing it directly in an item element, like dc:coverage.

Would it be possible to do this with some slight modifications to the Geolocation plugin? Perhaps somewhere in the hook below? I’m new to coding with Omeka plugins so any help or ideas at all would be appreciated.

 public function hookAfterSaveItem($args)
    {
        if (!($post = $args['post'])) {
            return;
        }
        $item = $args['record'];
        // If we don't have the geolocation form on the page, don't do anything!
        if (!isset($post['geolocation'])) {
            return;
        }
        // Find the location object for the item
        $location = $this->_db->getTable('Location')->findLocationByItem($item, true);
        // If we have filled out info for the geolocation, then submit to the db
        $geolocationPost = $post['geolocation'];
        if (!empty($geolocationPost)
            && $geolocationPost['latitude'] != ''
            && $geolocationPost['longitude'] != ''
        ) {
            if (!$location) {
                $location = new Location;
                $location->item_id = $item->id;
            }
            $location->setPostData($geolocationPost);
            $location->save();
        } else {
            // If the form is empty, then we want to delete whatever location is
            // currently stored
            if ($location) {
                $location->delete();
            }
        }
    }

I’ve been trying to address this issue with a plugin that uses hookBeforeSaveItem. I’ve figured out how to add the address from geolocation into a DC element, but I don’t understand how to delete or replace existing element texts for that element. Here’s what I have so far:

?php

class GeoToCoveragePlugin extends Omeka_Plugin_AbstractPlugin
{
    protected $_hooks = array('before_save_item');

    public function hookBeforeSaveItem($args)
    {
        if (!($post = $args['post'])) {
           return;
        }

        $item = $args['record'];
        // If we don't have the geolocation form on the page, don't do anything!
        if (!isset($post['geolocation'])) {
            return;
        }

        $geolocationPost = $post['geolocation'];

        $db = get_db();
        $elementTable = $db->getTable('Element');
        $coverageElement = $elementTable->findByElementSetNameAndElementName('Dublin Core', 'Subject');

        if (!empty($geolocationPost)
            && $geolocationPost['latitude'] != ''
            && $geolocationPost['longitude'] != ''
            && $geolocationPost['address'] != ''
        ) {
            //This next line doesn't actually delete element texts
            $item->deleteElementTextsByElementId(array($coverageElement->id));
            //This next line DOES add the address from post into the DC field
            $item->addTextForElement($coverageElement, $geolocationPost['address']);
        } else {
            //Still need to complete this
            }
        }
    }
}
?>

Any help would be much appreciated.

I suspect that you’ve walked into one of the odder aspects of how Omeka stores data: the element texts aren’t necessarily stable. I can’t be sure without directly inspecting the database before and after each save, but my guess is that your code does happily delete the element text by element id. Then, when the Item itself is saved, the data from the form is still there, and so it gets re-saved with a new id in the database.

So, that’s what I’d check. If that’s the case, you might be able to dig through the POST data to remove what you don’t want. Alternatively, the after save hook might be a way to do the post-processing cleaning up you need. That’d be making use of both hooks, one to add data before the save, and one to clear stuff out afterward.

Eureka! Thanks for the help. I hacked together an after save hook to delete all but the last entered Element Text for the field, and that seems to work (knock on wood).

Your post got me thinking, though, about whether I’m going about this the wrong way. Should I just do the whole thing with an afterSaveHook and look up whatever is stored in the item’s location record and stuff it in the metadata field at that point? Is there an advantage to doing it the before AND after way?

That’s a good question, and I really can’t answer it without trying it out with both approaches to see what happens. So, advantage goes to what works as you’ve put things together. Maybe there’s something about style or database efficiency, but my eye isn’t precise enough for that.

Got it. I’ve got a just afterSave prototype working now, and I think for what I’m doing it might be more stable. I appreciate the helpful clues!

If anybody else is interested in this functionality, I have a prototype plugin here:

This topic was automatically closed after 250 days. New replies are no longer allowed.