public function browseAction()
 {
     // Need to use a plugin hook here to make sure that this search retrieves
     // only items that are on the map.
     $this->_setParam('only_map_items', true);
     $this->_setParam('use_map_per_page', true);
     $results = $this->_helper->searchItems();
     $items = $results['items'];
     $totalItems = $results['total_results'];
     $locations = geolocation_get_location_for_item($items);
     // Make the pagination values accessible from the plugin template
     // helpers.
     $params = array('page' => $results['page'], 'per_page' => geolocation_get_map_items_per_page(), 'total_results' => $results['total_results']);
     Zend_Registry::set('map_params', $params);
     // Make the pagination values accessible from pagination_links().
     Zend_Registry::set('pagination', $params);
     $this->view->assign(compact('items', 'totalItems', 'locations'));
 }
function geolocation_item_browse_sql($select, $params)
{
    // It would be nice if the item_browse_sql hook also passed in the request
    // object.
    if ($request = Omeka_Context::getInstance()->getRequest()) {
        $db = get_db();
        // Get the address, latitude, longitude, and the radius from parameters
        $address = trim($request->getParam('geolocation-address'));
        $currentLat = trim($request->getParam('geolocation-latitude'));
        $currentLng = trim($request->getParam('geolocation-longitude'));
        $radius = trim($request->getParam('geolocation-radius'));
        if ($request->get('only_map_items') || $address != '') {
            //INNER JOIN the locations table
            $select->joinInner(array('l' => $db->Location), 'l.item_id = i.id', array('latitude', 'longitude', 'address'));
        }
        // Limit items to those that exist within a geographic radius if an address and radius are provided
        if ($address != '' && is_numeric($currentLat) && is_numeric($currentLng) && is_numeric($radius)) {
            // SELECT distance based upon haversine forumula
            $select->columns('3956 * 2 * ASIN(SQRT(  POWER(SIN((' . $currentLat . ' - l.latitude) * pi()/180 / 2), 2) + COS(' . $currentLat . ' * pi()/180) *  COS(l.latitude * pi()/180) *  POWER(SIN((' . $currentLng . ' -l.longitude) * pi()/180 / 2), 2)  )) as distance');
            // WHERE the distance is within radius miles of the specified lat & long
            $select->where('(latitude BETWEEN ' . $currentLat . ' - ' . $radius . '/69 AND ' . $currentLat . ' + ' . $radius . '/69)
             AND (longitude BETWEEN ' . $currentLng . ' - ' . $radius . '/69 AND ' . $currentLng . ' + ' . $radius . '/69)');
            //ORDER by the closest distances
            $select->order('distance');
        }
        // This would be better as a filter that actually manipulated the
        // 'per_page' value via this plugin. Until then, we need to hack the
        // LIMIT clause for the SQL query that determines how many items to
        // return.
        if ($request->get('use_map_per_page')) {
            // If the limit of the SQL query is 1, we're probably doing a
            // COUNT(*)
            $limitCount = $select->getPart(Zend_Db_Select::LIMIT_COUNT);
            if ($limitCount != 1) {
                $select->reset(Zend_Db_Select::LIMIT_COUNT);
                $select->reset(Zend_Db_Select::LIMIT_OFFSET);
                $pageNum = $request->get('page') or $pageNum = 1;
                $select->limitPage($pageNum, geolocation_get_map_items_per_page());
            }
        }
    }
}