public function onAfterProfileUpdate($userid, $save) { if ($save) { // Update user's IP location $usermodel = CFactory::getModel('user'); $user = CFactory::getUser($userid); $juser =& JFactory::getUser($userid); CFactory::load('libraries', 'mapping'); // Build the address string $address = $user->getAddress(); // Store the location $data = CMapping::getAddressData($address); // reset it to null; $latitude = COMMUNITY_LOCATION_NULL; $longitude = COMMUNITY_LOCATION_NULL; if ($data) { if ($data->status == 'OK') { $latitude = $data->results[0]->geometry->location->lat; $longitude = $data->results[0]->geometry->location->lng; } } $usermodel->storeLocation($user->id, $latitude, $longitude); // Update user's firstname and lastname. Only update if both is not // empty and is actually specifies $givenName = $user->getInfo('FIELD_GIVENNAME'); $familyName = $user->getInfo('FIELD_FAMILYNAME'); if (!empty($givenName) && !empty($familyName)) { $juser->name = $givenName . ' ' . $familyName; // We need to update the cuser object too since it is static, // it might still be used $user->name = $juser->name; if (!$juser->save()) { // save failed ? } } // Update all user counts $friendModel = CFactory::getModel('friends'); $friendModel->updateFriendCount($userid); //$user->_friendcount = $numFriend; //echo $user->save(); //echo $user->_friendcount; } }
/** * Onload, we try to load existing data, if any. If not, query from Google */ public function load($address) { // lowercase the incoming address $address = JString::strtolower($address); $db =& JFactory::getDBO(); $query = 'SELECT * FROM ' . $db->nameQuote('#__community_location_cache'); $query .= ' WHERE '; $query .= $db->nameQuote('address') . '= ' . $db->quote($address); $query .= ' LIMIT 1'; $db->setQuery($query); $obj = $db->loadObject(); if ($obj) { $this->bind($obj); } else { CFactory::load('libraries', 'mapping'); $data = CMapping::getAddressData($address); $this->address = $address; $this->latitude = COMMUNITY_LOCATION_NULL; $this->longitude = COMMUNITY_LOCATION_NULL; $this->data = ''; $this->status = 'ZERO_RESULTS'; if ($data != null) { require_once AZRUL_SYSTEM_PATH . DS . 'pc_includes' . DS . 'JSON.php'; $json = new Services_JSON(); $content = $json->encode($data); if ($data->status == 'OK') { $this->latitude = $data->results[0]->geometry->location->lat; $this->longitude = $data->results[0]->geometry->location->lng; $this->data = $content; $this->status = $data->status; } } $date = new JDate(); $this->created = $date->toMySQL(true); $this->store(); } return true; }
/** * caller should verify that the address is valid */ public function searchWithin($address, $distance) { $db = JFactory::getDBO(); $longitude = null; $latitude = null; $data = CMapping::getAddressData($address); if ($data) { if ($data->status == 'OK') { $latitude = (double) $data->results[0]->geometry->location->lat; $longitude = (double) $data->results[0]->geometry->location->lng; } } if (is_null($latitude) || is_null($longitude)) { return null; } /* * code from * http://blog.fedecarg.com/2009/02/08/geo-proximity-search-the-haversine-equation/ */ $radius = 20; // in miles $lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69); $lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69); $lat_min = $latitude - $radius / 69; $lat_max = $latitude + $radius / 69; $now = new JDate(); $sql = "SELECT * FROM " . $db->quoteName("#__community_events") . " WHERE " . $db->quoteName("longitude") . " > " . $db->quote($lng_min) . " AND " . $db->quoteName("longitude") . " < " . $db->quote($lng_max) . " AND " . $db->quoteName("latitude") . " > " . $db->quote($lat_min) . " AND " . $db->quoteName("latitude") . " < " . $db->quote($lat_max) . " AND " . $db->quoteName("enddate") . " > " . $db->quote($now->toSql()); $db->setQuery($sql); $results = $db->loadObjectList(); return $results; }
/** * Get the Formated address from google */ public static function getFormatedAdd($address) { $data = CMapping::getAddressData($address); return $data->results[0]->formatted_address; }
/** Interface fucntions **/ public function resolveLocation($address) { CFactory::load('libraries', 'mapping'); $data = CMapping::getAddressData($address); // reset it to null; $this->latitude = COMMUNITY_LOCATION_NULL; $this->longitude = COMMUNITY_LOCATION_NULL; if ($data) { if ($data->status == 'OK') { $this->latitude = $data->results[0]->geometry->location->lat; $this->longitude = $data->results[0]->geometry->location->lng; } } }
/** * caller should verify that the address is valid */ public function searchWithin($address, $distance) { $db =& JFactory::getDBO(); $longitude = null; $latitude = null; CFactory::load('libraries', 'mapping'); $data = CMapping::getAddressData($address); if ($data) { if ($data->status == 'OK') { $latitude = (double) $data->results[0]->geometry->location->lat; $longitude = (double) $data->results[0]->geometry->location->lng; } } if (is_null($latitude) || is_null($longitude)) { return $null; } /* code from http://blog.fedecarg.com/2009/02/08/geo-proximity-search-the-haversine-equation/ */ //$longitude = (float) 101.678; //$latitude = (float) 3.11966 ; // $radius = $radius_in_km * 0.621371192; $radius = 20; // in miles $lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69); $lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69); $lat_min = $latitude - $radius / 69; $lat_max = $latitude + $radius / 69; //echo 'lng (min/max): ' . $lng_min . '/' . $lng_max . PHP_EOL; //echo 'lat (min/max): ' . $lat_min . '/' . $lat_max; $now = new JDate(); $sql = "SELECT *\n\n\t\t\t\tFROM\n\t\t\t\t\t\t" . $db->nameQuote("#__community_events") . "\n\t\t\t\tWHERE\n\t\t\t\t\t\t" . $db->nameQuote("longitude") . " > " . $db->quote($lng_min) . " AND\n\t\t\t\t\t\t" . $db->nameQuote("longitude") . " < " . $db->quote($lng_max) . " AND\n\t\t\t\t\t\t" . $db->nameQuote("latitude") . " > " . $db->quote($lat_min) . " AND\n\t\t\t\t\t\t" . $db->nameQuote("latitude") . " < " . $db->quote($lat_max) . " AND\n\t\t\t\t\t\t" . $db->nameQuote("enddate") . " > " . $db->quote($now->toMySQL()); $db->setQuery($sql); echo $db->getQuery(); $results = $db->loadObjectList(); return $results; }
/** Interface fucntions **/ public function resolveLocation($address) { CFactory::load('libraries', 'mapping'); $data = CMapping::getAddressData($address); //print_r($data); if ($data) { if ($data->status == 'OK') { $this->latitude = $data->results[0]->geometry->location->lat; $this->longitude = $data->results[0]->geometry->location->lng; } } }
/** * Update user location with user's address information * @param type $userid */ public function updateLocationData($userid) { $usermodel = CFactory::getModel('user'); $user = CFactory::getUser($userid); // Build the address string $address = str_replace('COM_COMMUNITY_LANG_NAME_', '', $user->getAddress()); // Store the location $data = CMapping::getAddressData($address); // reset it to null; $latitude = COMMUNITY_LOCATION_NULL; $longitude = COMMUNITY_LOCATION_NULL; if ($data) { if ($data->status == 'OK') { $latitude = $data->results[0]->geometry->location->lat; $longitude = $data->results[0]->geometry->location->lng; } } $usermodel->storeLocation($user->id, $latitude, $longitude); return $this; }
/** * Add marker point to the given address */ public function addMarker($targetId, $address, $title = '', $info = '') { $html = ''; $data = CMapping::getAddressData($address); if ($data) { if ($data->status == 'OK') { $lat = $data->results[0]->geometry->location->lat; $lng = $data->results[0]->geometry->location->lng; $html = '<script type="text/javascript">' . 'joms.jQuery(document).ready( function() {' . 'joms.maps.addMarker(\'' . $targetId . '\', ' . $lat . ', ' . $lng . ', \'' . $title . '\', \'' . $info . '\');' . '});' . '</script>'; } } return $html; }