Skip to content

Instantly share code, notes, and snippets.

@psycharo-zz
Created June 1, 2012 22:24
Show Gist options
  • Save psycharo-zz/2855481 to your computer and use it in GitHub Desktop.
Save psycharo-zz/2855481 to your computer and use it in GitHub Desktop.
<?php
/**
* Application level Controller
*
* This file is application-wide controller file. You can put all
* application-wide controller-related methods here.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.app
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package cake
* @subpackage cake.app
*/
App::import('Core', 'HttpSocket');
class AppController extends Controller {
var $name = 'App';
var $uses = array('Language', 'User', 'LanguagesUser', 'Coordinate', 'Nationality');
function addressToCoordinates($address, $country) {
$BASE_URL = 'http://maps.googleapis.com/maps/api/geocode/json';
$socket = new HttpSocket();
$args = array('address' => $address . ' ' . $country,
'sensor' => 'false');
$results = json_decode($socket->get($BASE_URL, $args));
if ($results != null && sizeof($results->results) >= 1) {
$loc = $results->results[0]->geometry->location;
return $loc;
} else {
return null;
}
}
function usersWithinDistance($user, $nationalities, $languages, $distance) {
if (sizeof($user['Coordinate']) == 0) {
return null;
}
$latitude = $user['Coordinate'][0]['latitude'];
$longitude = $user['Coordinate'][0]['longitude'];
// selecting all the language ids
$lang_ids = $this->Language->find('list', array('fields' => array('id'),
'conditions'=> array('name' => $languages)));
$lang_ids_str = implode(',', $lang_ids);
// selecting all the nationality ids
$nation_ids = $this->Nationality->find('list', array('fields' => array('id'),
'conditions'=> array('name' => $nationalities)));
$nation_ids_str = implode(',', $nation_ids);
$sub_query = 'SELECT languages_users.user_id '
.'FROM languages_users RIGHT JOIN users ON user_id = users.id '
.'WHERE languages_users.language_id IN (%s) AND nationality_id IN (%s) AND user_id != %s ' //
.'GROUP BY user_id HAVING COUNT(languages_users.user_id) = %s';
$sub_query = sprintf($sub_query, $lang_ids_str, $nation_ids_str, $user['User']['id'], sizeof($lang_ids));
$distance_call = sprintf('DISTANCE_EARTH(coordinates.latitude, coordinates.longitude, %s, %s) as distance', $latitude, $longitude);
$distance_condition = sprintf('HAVING distance < %s', $distance);
$qry = sprintf('SELECT coordinates.user_id, %s '
.'FROM coordinates '
.'WHERE coordinates.user_id IN (%s)'
.' %s', $distance_call, $sub_query, $distance_condition);
$fetch = $this->Coordinate->query($qry);
$result = array();
foreach ($fetch as &$f) {
$result[] = $f['coordinates']['user_id'];
}
return $result;
}
function index() {
$COUNTRY = 'NL';
$user_id = 2;
$user = $this->User->find('first', array('conditions' => array('User.id' => $user_id)));
$src_postal_code = $user['User']['postal_code'];
$location = $this->addressToCoordinates($src_postal_code, $COUNTRY);
pr($location);
$nationalities = array('Russia', 'Mexico');
$languages = array('Russian', 'English');
pr($this->usersWithinDistance($user, $nationalities, $languages, 5.6));
/*
$latitude = $location->lat;
$longitude = $location->lng;
$all_users = array(1,2,3,4);
foreach ($all_users as &$uid) {
$user = $this->User->find('first', array('conditions' => array('User.id' => $uid)));
$postal_code = $user['User']['postal_code'];
//pr($src_postal_code . ' ' . $postal_code);
$location = $this->addressToCoordinates($postal_code);
if ($uid != $user_id) {
$this->Coordinate->create();
$data = array('user_id' => $uid,
'latitude' => $location->lat,
'longitude' => $location->lng);
$this->Coordinate->save($data);
//$user->Coordinate['latitude'] = $location->lat;
//$user->Coordinate->longitude = $location->lng;
}
//$distance_call = sprintf('DISTANCE_EARTH(%s, %s, %s, %s)', $location->lat, $location->lng, $latitude, $longitude);
}
*/
/*
$latitude = ;
$longitude = ;
$distance_call = sprintf('DISTANCE_EARTH(coordinates.latitude, coordinates.longitude, %s, %s) as distance', $latitude, $longitude);
*/
/*
$nationalities = array('Mexico');
$languages = array('English', 'Russian');
$distance = 10;
$result = $this->usersWithinDistance($user, $nationalities, $languages, $distance);
pr($result);
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment