PageRenderTime 123ms CodeModel.GetById 11ms app.highlight 98ms RepoModel.GetById 1ms app.codeStats 1ms

/app/controllers/users_controller.php

https://github.com/rogerwu99/randomizr
PHP | 1768 lines | 1376 code | 241 blank | 151 comment | 321 complexity | afbf92807913ffc6e7890d599ffa49ed MD5 | raw file
   1<?php
   2App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'oauth_consumer.php'));
   3App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'OAuth.php'));
   4App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'OAuth2.php'));
   5
   6class UsersController extends AppController {
   7
   8	var $name = 'Users';
   9	var $helpers = array('Html', 'Form', 'Ajax');
  10	var $components = array('Auth', 'Email','Paypal','Session');
  11	var $uses = array('User', 'Mail', 'Movie', 'Interest','Place','Work','School','Userprofile','Wlookup','Twitter');
  12
  13	function index()
  14	{
  15		if(is_null($this->Auth->getUserId())){
  16       		Controller::render('/deny');
  17        }
  18		else {
  19			$this->redirect(array('controller'=>'users','action'=>'view_my_profile'));
  20		}
  21	}
  22
  23	function _login($username=null, $password=null)
  24	{
  25		if ($username && $password){
  26			$user_record_1=array();
  27			$user_record_1['Auth']['username']=$username;
  28			$user_record_1['Auth']['password']=$password;
  29			$this->Auth->authenticate_from_oauth($user_record_1['Auth']);
  30			return;		
  31		}
  32	}
  33	
  34	function login()
  35	{
  36		$this->_login($this->data['Auth']['username'],$this->Auth->hasher($this->data['Auth']['password']));
  37		if ($this->Session->check('hash_value')){
  38			$this->redirect(array('controller'=>'beta','action'=>'index',$this->Session->read('hash_value')));
  39		}
  40		else {
  41			$this->redirect(array('action'=>'view_my_profile'));
  42		}
  43	}
  44	
  45	function register($step=null)
  46	{
  47		if (!empty($this->data)){
  48			if ($this->data['User']['step']==1){
  49				$email = $this->data['User']['email'];
  50				$name=$this->data['User']['screen_name'];
  51				$password = $this->data['User']['new_password'];
  52				$confirm =$this->data['User']['confirm_password'];
  53				$accept = $this->data['User']['accept'];
  54				$this->data=array();
  55				$this->User->create();
  56				$this->data['User']['screen_name']=strtolower($name);
  57				$this->data['User']['email'] = (string) $email;
  58				$this->data['User']['new_password']=$password;
  59				$this->data['User']['confirm_password']=$confirm;
  60				$this->data['User']['accept']=$accept;
  61				$password = $this->data['User']['password'] = $this->Auth->hasher($password); 
  62				$username = $this->data['User']['username']= (string) $email;
  63				$this->data['User']['path']='default.png';
  64		
  65				$this->User->set($this->data);
  66				if ($this->User->validates()){
  67					$this->User->save();
  68					$this->_login($username,$password);
  69					$this->set('step',2);
  70				}
  71				else {
  72					$this->set('errors', $this->User->validationErrors);
  73					unset($this->data['User']['new_password']);
  74	    			unset($this->data['User']['confirm_password']);
  75					$this->set('step',1);
  76				}
  77			}
  78		}
  79		elseif ($step==2){
  80			$this->set('step',2);
  81		}
  82		elseif ($step==3){ //after fb auth
  83			list ($master, $fb_data, $fb_movies, $fb_user_likes) = $this->getFacebookData();
  84			$this->set('master',$fb_user_likes);
  85			$this->set('step',3);
  86			//,$fb_user_likes,$fb_data,$fb_movies);
  87		}	
  88		else {
  89			$this->set('step',1);
  90			$this->render();
  91		}
  92		
  93	}
  94	
  95	
  96	function logout()
  97	{
  98		$user=$this->Auth->getUserInfo();
  99		$this->Session->destroy();
 100		if(!empty($session)){
 101			$this->Auth->logout($url);
 102		}
 103		else {
 104		    $this->Auth->logout();
 105		}
 106	}
 107	private function createConsumer($type) {
 108		switch ($type) {
 109			case 'facebook':
 110				return new OAuth_Consumer('189267044425329','b127d742f40502d8a9c05b31d6acc43b');
 111		}
 112    }
 113	
 114	function getOAuth($service=NULL){
 115		$consumer = $this->createConsumer($service);
 116		$redirect_url = '';
 117		switch ($service){
 118			case 'facebook':
 119				$redirect_url = 'https://www.facebook.com/dialog/oauth?client_id=189267044425329&redirect_uri='.ROOT_URL.'/users/callback/facebook'.'&scope=user_about_me,user_activities,user_birthday,user_education_history,user_events,user_groups,user_hometown,user_interests,user_relationships,user_religion_politics,user_status,user_website,user_work_history,email,user_checkins,user_likes,friends_likes,friends_interests,friends_checkins,friends_activities,friends_work_history,friends_relationship_details,friends_website,friends_religion_politics,friends_relationships,friends_location,friends_relationship_details,friends_hometown,friends_education_history,friends_birthday,friends_about_me';
 120				break;
 121		}
 122		$this->redirect($redirect_url);
 123	}
 124	
 125	
 126	function callback($service=NULL){
 127		$consumer = $this->createConsumer($service);
 128		$requestTokenName = $service.'_request_token';
 129		$accessTokenName = $service.'_access_token';
 130		$accessKeyName = $service.'_access_key';
 131		$accessSecretName = $service.'_access_secret';
 132		$access_url = '';
 133		switch ($service){
 134			case 'facebook':
 135				$access_url = 'https://graph.facebook.com/oauth/access_token?client_id=189267044425329&redirect_uri='.ROOT_URL.'/users/callback/facebook&client_secret=b127d742f40502d8a9c05b31d6acc43b&code='.$this->params['url']['code'];
 136				break;
 137		}
 138	//	echo 'done';
 139		$this->User->read(null,$this->Auth->getUserId());
 140			$accessToken = file_get_contents($access_url);
 141			
 142			if ($service=='facebook'){
 143				$this->data['User']['facebook_access_key'] = $accessToken;
 144				$this->Session->write('facebook_access_key',$accessToken);
 145			}
 146		//	echo 'saved';
 147		$this->User->save($this->data);
 148		//$this->new_data($service);
 149		//echo 'pull data';
 150		$this->getFacebookData();
 151		
 152		$this->redirect(array('action'=>'view_my_friends/50'));
 153
 154	}
 155	
 156
 157
 158	function getFacebookData(){
 159		$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
 160		$accessToken = $this->Session->read('facebook_access_key');
 161		$fb_user = json_decode(file_get_contents('https://graph.facebook.com/me?' . $accessToken));
 162	
 163		$name = preg_split('/[\f\n\r\t\v ]/',$fb_user->name);
 164		$last_name = '';
 165		$first_name = '';
 166		for ($counter=0;$counter<sizeof($name);$counter++){
 167			if($counter==(sizeof($name)-1)) {
 168				$last_name = $name[$counter];
 169			}
 170			else {
 171				$first_name .= $name[$counter];
 172			}
 173		}
 174		
 175		if (empty($user['Userprofile']['id'])){
 176			$this->Userprofile->create();
 177		}
 178		else {
 179			$this->Userprofile->read(null,$user['Userprofile']['id']);
 180		}
 181		if (sizeof($name) == 1) $this->data['Userprofile']['first_name'] = $name[0];
 182		else {
 183			$this->data['Userprofile']['first_name']=ucwords($first_name);
 184			$this->data['Userprofile']['last_name'] = ucwords($last_name);
 185		}
 186		$this->data['Userprofile']['hometown']= $fb_user->hometown->name;
 187		$this->data['Userprofile']['birthday']=date("Y-m-d H:i:s", strtotime($fb_user->birthday));
 188		$this->data['Userprofile']['gender']=$fb_user->gender;
 189		$this->data['Userprofile']['location']=$fb_user->location->name;
 190		$this->data['Userprofile']['relationship']=$fb_user->relationship_status;
 191		$this->data['Userprofile']['religion']=$fb_user->religion;
 192		$this->data['Userprofile']['political']=$fb_user->political;
 193		$this->data['Userprofile']['user_id']=$this->Auth->getUserId();
 194		$this->Userprofile->set($this->data);
 195		$this->Userprofile->save();
 196		$this->User->read(null,$this->Auth->getUserId());
 197		$this->data['User']['name']=$this->data['Userprofile']['first_name'].' '.$this->data['Userprofile']['last_name'];
 198		$this->User->set($this->data);
 199		$this->User->save();
 200//		echo 'Userprofile saved';
 201		$fb_user_likes = json_decode(file_get_contents('https://graph.facebook.com/me/likes?'.$accessToken));
 202		$fb_user_activities = json_decode(file_get_contents('https://graph.facebook.com/me/activities?'.$accessToken));
 203		$fb_user_interests = json_decode(file_get_contents('https://graph.facebook.com/me/interests?'.$accessToken));
 204
 205		if (empty($user['Interest']['id'])){
 206			$this->Interest->create();
 207		}
 208		else {
 209			$this->Interest->read(null,$user['Interest']['id']);
 210		}
 211		$this->data['Interest']['likes']=json_encode($fb_user_likes->data);
 212		$this->data['Interest']['activities']=json_encode($fb_user_activities->data);
 213		$this->data['Interest']['interests']=json_encode($fb_user_interests->data);
 214		$this->data['Interest']['user_id']=$this->Auth->getUserId();
 215		$this->Interest->set($this->data);
 216		$this->Interest->save();
 217//		echo 'interests saved';
 218		if (empty($user['Work']['id'])){
 219			$this->Work->create();
 220		}
 221		else {
 222			$this->Work->read(null,$user['Work']['id']);
 223		}
 224		$this->data['Work']['body'] = json_encode($fb_user->work);		
 225		$this->Work->set($this->data);
 226		$this->Work->save();
 227	//	echo 'work saved';		
 228		if (empty($user['School']['id'])){
 229			$this->School->create();
 230		}
 231		else {
 232			$this->School->read(null,$user['School']['id']);
 233		}
 234		$this->data['School']['body'] = json_encode($fb_user->education);		
 235		$this->School->set($this->data);
 236		$this->School->save();
 237	//	echo 'school saved';
 238
 239		
 240		$fb_user_checkins = json_decode(file_get_contents('https://graph.facebook.com/me/checkins?'.$accessToken));
 241		if (empty($user['Place']['id'])){
 242			$this->Place->create();
 243		}
 244		else {
 245			$this->Place->read(null,$user['Place']['id']);
 246		}
 247		$this->data['Place']['body'] = json_encode($fb_user_checkins->data);
 248		$this->Place->set($this->data);
 249		$this->Place->save();
 250	}
 251
 252	
 253	
 254	function new_data($type){
 255		switch ($type){
 256			case 'facebook':
 257				$this->getFacebookData();
 258				break;		
 259		}
 260	}
 261	
 262	
 263	
 264	
 265	function friends($flag=false){
 266		/*if (!$flag){
 267			$this->Session->write('friends','true');
 268			$this->getOAuth('facebook');
 269		}
 270		else {*/
 271			$count=0;
 272			$friend_array = array();
 273			$accessToken = $this->Session->read('facebook_access_key');
 274			$friend_url = json_decode(file_get_contents('https://graph.facebook.com/me/friends?' . $accessToken));
 275		//	var_dump($friend_url);
 276			$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
 277			$control = json_decode($user['Interest']['likes']);
 278			for ($counter=0;$counter<sizeof($friend_url->data); $counter++){
 279				$likes = json_decode(file_get_contents('https://graph.facebook.com/'.$friend_url->data[$counter]->id.'/likes?'.$accessToken));
 280				$intersect = array();
 281				for ($mid_counter = 0; $mid_counter<sizeof($control);$mid_counter++){
 282					for($inner_counter=0;$inner_counter<sizeof($likes->data);$inner_counter++){
 283						if ($control[$mid_counter]->id == $likes->data[$inner_counter]->id){
 284							array_push($intersect,$control[$mid_counter]);
 285						}
 286					}
 287					
 288				}
 289				if (!empty($intersect)){
 290					$friend_array[$count]->person = $friend_url->data[$counter];
 291					$friend_array[$count]->likes = $intersect;
 292					$friend_array[$count]->pic = 'http://graph.facebook.com/'.$friend_url->data[$counter]->id.'/picture';
 293
 294				}
 295			}
 296			//$this->redirect(array('action'=>'view_my_profile'));
 297			return $friend_array;
 298		//}
 299	}
 300	function getFriends(){
 301		$friend_array = array();
 302		//$count =0;
 303		$user = $this->Auth->getUserInfo(); 
 304		$friend_url = json_decode(file_get_contents('https://graph.facebook.com/me/friends?' . $user['facebook_access_key']));
 305		
 306			usort($friend_url->data, array(&$this, "friend_sort"));
 307
 308		return $friend_url->data;
 309	}
 310	function friend_sort($a,$b){
 311		if ($a->name > $b->name) return 1;
 312		elseif($a->name == $b->name) return 0;
 313		else return -1;
 314	}
 315	
 316	function fbcompare($fb_id){
 317		$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
 318
 319		$remote_user = json_decode(file_get_contents('https://graph.facebook.com/'.$fb_id.'?'. $user['User']['facebook_access_key']));
 320		
 321		
 322		
 323		$remote_user_likes = json_decode(file_get_contents('https://graph.facebook.com/'.$fb_id.'/likes?'.$user['User']['facebook_access_key']));
 324	
 325			$control = json_decode($user['Interest']['likes']);
 326			
 327				$likes = json_decode(file_get_contents('https://graph.facebook.com/'.$fb_id.'/likes?'.$user['User']['facebook_access_key']));
 328				$intersect = array();
 329				for ($mid_counter = 0; $mid_counter<sizeof($control);$mid_counter++){
 330					for($inner_counter=0;$inner_counter<sizeof($likes->data);$inner_counter++){
 331						if ($control[$mid_counter]->id == $likes->data[$inner_counter]->id){
 332							array_push($intersect,$control[$mid_counter]);
 333						}
 334					}
 335					
 336				}
 337		$this->set('user',$user);
 338		$this->set('remote_user',$remote_user);
 339		$this->set('intersect',$intersect);
 340		
 341		
 342		
 343		if (!is_null($user['Userprofile']['hometown']) && !is_null($remote_user->hometown->name)){
 344			$url = "http://where.yahooapis.com/geocode?line2=".urlencode($user['Userprofile']['hometown'])."&flags=J&gflags=R&appid=cENXMi4g";
 345			$address = json_decode(file_get_contents($url));
 346					
 347			$lat_home = $address->ResultSet->Results[0]->latitude;
 348			$long_home = $address->ResultSet->Results[0]->longitude;
 349					
 350			$url2 = "http://where.yahooapis.com/geocode?line2=".urlencode($remote_user->hometown->name)."&flags=J&gflags=R&appid=cENXMi4g";
 351			$address2 = json_decode(file_get_contents($url2));
 352					
 353			$lat_away = $address2->ResultSet->Results[0]->latitude;
 354			$long_away = $address2->ResultSet->Results[0]->longitude;
 355					
 356			$earth_radius = 6371;
 357			$delta_lat = deg2rad($lat_home - $lat_away);
 358			$delta_long = deg2rad($long_home - $long_away);
 359			$a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($lat_home)) * cos(deg2rad($lat_away)) * sin($delta_long/2) * sin($delta_long/2);
 360			$c = 2 * atan2(sqrt($a),sqrt(1-$a));
 361			$distance = $earth_radius * $c;
 362					
 363			$d_miles = $distance * 0.621371192;
 364			$this->set('d_miles',$d_miles);
 365		}
 366		
 367		if (!is_null($user['Userprofile']['birthday']) && !is_null($remote_user->birthday)){
 368			$user_sign = $this->getSign($user['Userprofile']['birthday']);
 369			$other_user_sign = $this->getSign($remote_user->birthday);
 370			$diff = abs(strtotime($user['Userprofile']['birthday']) - strtotime($remote_user->birthday));
 371			$years = floor($diff / (365*60*60*24));
 372			$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
 373			$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
 374			
 375			if (date('Y',strtotime($remote_user->birthday)) == date('Y') || date('Y',strtotime($user['Userprofile']['birthday'])) == date('Y')){
 376				$years = 0;
 377			}
 378			
 379			
 380			
 381			$this->set('user_sign',$user_sign);
 382			$this->set('other_user_sign',$other_user_sign);
 383			$this->set('years',$years);
 384			$this->set('months',$months);
 385			$this->set('days',$days);
 386		}
 387		if (!is_null($user['Userprofile']['location']) && !is_null($remote_user->location->name)){
 388			$url = "http://where.yahooapis.com/geocode?line2=".urlencode($user['Userprofile']['location'])."&flags=J&gflags=R&appid=cENXMi4g";
 389			$address = json_decode(file_get_contents($url));
 390					
 391			$lat_home = $address->ResultSet->Results[0]->latitude;
 392			$long_home = $address->ResultSet->Results[0]->longitude;
 393					
 394			$url2 = "http://where.yahooapis.com/geocode?line2=".urlencode($remote_user->location->name)."&flags=J&gflags=R&appid=cENXMi4g";
 395			$address2 = json_decode(file_get_contents($url2));
 396					
 397			$lat_away = $address2->ResultSet->Results[0]->latitude;
 398			$long_away = $address2->ResultSet->Results[0]->longitude;
 399					
 400			$earth_radius = 6371;
 401			$delta_lat = deg2rad($lat_home - $lat_away);
 402			$delta_long = deg2rad($long_home - $long_away);
 403			$a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($lat_home)) * cos(deg2rad($lat_away)) * sin($delta_long/2) * sin($delta_long/2);
 404			$c = 2 * atan2(sqrt($a),sqrt(1-$a));
 405			$distance = $earth_radius * $c;
 406					
 407			$d_miles = $distance * 0.621371192;
 408			$this->set('l_miles',$d_miles);
 409		}
 410		// compare school and work
 411		$matches = array();
 412		$count = 0;
 413		var_dump($user['School']['body']);
 414		var_dump($remote_user->education);
 415		if (!is_null($user['School']['body']) && !is_null($remote_user->education)){
 416			$my_schools = json_decode($user['School']['body']);
 417			for ($counter = 0;$counter<sizeof($my_schools);$counter++){
 418				for ($inner_counter =0;$inner_counter<sizeof($remote_user->education); $inner_counter++){
 419					similar_text($my_schools[$counter]->school->name, $remote_user->education[$inner_counter]->school->name,$percent);
 420					if (($my_schools[$counter]->school->id==$remote_user->education[$inner_counter]->school->id &&
 421						$my_schools[$counter]->school->id!=0) || $percent > 90 ){
 422						$matches[$count]->value = $my_schools[$counter]->school;
 423						$matches[$count]->type = 'school';
 424						$count++;	
 425					}
 426					if ($my_schools[$counter]->degree->id == $remote_user->education[$inner_counter]->degree->id &&
 427						$my_schools[$counter]->degree->id!=0){
 428						$matches[$count]->value = $my_schools[$counter]->degree;
 429						$matches[$count]->type = 'degree';
 430						$count++;
 431					}
 432					for ($concentration_counter = 0;$concentration_counter <  sizeof($my_schools[$counter]->concentration); $concentration_counter++){
 433						if ($my_schools[$counter]->concentration[$concentration_counter]->id == $remote_user->education[$inner_counter]->concentration[$concentration_counter]->id &&
 434							$my_schools[$counter]->concentration[$concentration_counter]->id!=0){
 435							//echo 'match';
 436							$matches[$count]->value = $my_schools[$counter]->concentration[$concentration_counter];
 437							$matches[$count]->type = 'concentration';
 438							$count++;
 439						}
 440					}
 441				}
 442			}
 443		}
 444		$this->set('school_matches',$matches);
 445	//	var_dump($remote_user->work);
 446	//	var_dump($user['Work']['body']);
 447	
 448	
 449	// let's do a free text search match on work as well i.e. the teddy platt problem of Bloomberg and Bloomberg LP
 450	
 451		$matches = array();
 452		if (!is_null($user['Work']['body']) && !is_null($remote_user->work)){
 453			$my_work = json_decode($user['Work']['body']);
 454			for ($counter = 0;$counter<sizeof($my_work);$counter++){
 455				for ($inner_counter =0;$inner_counter<sizeof($remote_user->work); $inner_counter++){
 456					similar_text($my_work[$counter]->employer->name, $remote_user->work[$inner_counter]->employer->name,$percent);
 457					if (($my_work[$counter]->employer->id==$remote_user->work[$inner_counter]->employer->id && 
 458						$my_work[$counter]->employer->id!=0) || $percent > 80){
 459						$match->value = $my_work[$counter]->employer;
 460						$match->type = 'employer';
 461						array_push($matches, $match);	
 462					}
 463					if ($my_work[$counter]->location->id == $remote_user->work[$inner_counter]->location->id &&
 464						$my_work[$counter]->location->id!=0){
 465						$match->value = $my_work[$counter]->location;
 466						$match->type = 'location';
 467						array_push($matches, $match);
 468					}
 469					if ($my_work[$counter]->position->id == $remote_user->work[$inner_counter]->position->id &&
 470						$my_work[$counter]->position->id!=0){
 471						$match->value = $my_work[$counter]->position;
 472						$match->type = 'position';
 473						array_push($matches, $match);
 474					}
 475				}
 476			}
 477		}
 478		$this->set('work_matches',$matches);
 479		
 480		
 481		// get activities and interests
 482		
 483		
 484		
 485		
 486	}
 487	
 488	function view_my_profile(){
 489	
 490		if(is_null($this->Auth->getUserId())){
 491       		Controller::render('/deny');
 492        }
 493		else {
 494						
 495	
 496			$user = $this->Auth->getUserInfo();
 497			$db_results = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
 498			if (!empty($db_results)){
 499				$movie_data = (isset($db_results['Movie']['body'])) ? true : false;
 500			}
 501			
 502		
 503			$this->set('pic',$db_results['User']['path']);
 504			if (!empty($db_results['Movie']['body'])){
 505				$movies = json_decode($db_results['Movie']['body']);
 506				$top_movies = array();
 507				foreach ($movies as $key=>$value){
 508					array_push($top_movies,$key);
 509				}
 510				$this->set('top_movies',$top_movies);
 511			}
 512			if (!empty($db_results['Interest']['body'])){
 513				$interests = json_decode($db_results['Interest']['body']);
 514				$top_interests = array();
 515				foreach ($interests as $key=>$value){
 516					array_push($top_interests,$key);
 517				}
 518				$this->set('top_interests',$top_interests);
 519			}
 520			if (!empty($db_results['Place']['locations'])){
 521				$locations = json_decode($db_results['Place']['locations']);
 522				$top_locations = array();
 523				foreach ($locations as $key=>$value){
 524					array_push($top_locations,$key);
 525				}
 526				$this->set('top_locations',$top_locations);
 527			}
 528			if (!empty($db_results['Place']['categories'])){
 529				$categories = json_decode($db_results['Place']['categories']);
 530				$top_categories = array();
 531				//var_dump($db_results['Place']['categories']);
 532				foreach ($categories as $key=>$value){
 533					array_push($top_categories,$key);
 534				}
 535				$this->set('top_categories',$top_categories);
 536			}
 537			if (!empty($db_results['Interest']['you_body'])){
 538				$you = json_decode($db_results['Interest']['you_body']);
 539				$top_you = array();
 540				foreach ($you as $key=>$value){
 541					array_push($top_you,$key);
 542				}
 543				$this->set('top_you',$top_you);
 544			}
 545			
 546			if (!empty($db_results['Work']['body'])){
 547				$work = json_decode($db_results['Work']['body']);
 548				
 549				
 550			}
 551			if (!empty($db_results['Work']['titles'])){
 552				$work = json_decode($db_results['Work']['titles']);
 553		
 554				foreach ($work as $key=>$value){
 555					$this->set('titles',$key);			
 556					break;
 557				}
 558			
 559			}
 560			if (!empty($db_results['Work']['industries'])){
 561				$work = json_decode($db_results['Work']['industries'],true);
 562				$first = true;
 563				arsort($work, SORT_NUMERIC);
 564				$top_industries = array();
 565				foreach ($work as $key=>$value){
 566					if ($first){
 567						$this->set('industries',$key);			
 568						$first = false;
 569					}
 570					array_push($top_industries, $key);
 571				}
 572				$this->set('top_industries',$top_industries);
 573				
 574			}
 575			if (!empty($db_results['School']['body'])){
 576				$school = json_decode($db_results['School']['body'],true);
 577				$parsable_array = array();
 578				$schools =array();
 579				$areas_of_focus = array();
 580				for ($counter = 0;$counter<sizeof($school);$counter++){
 581					foreach($school[$counter] as $key=>$value){
 582						if ($key == 'degree') $parsable_array[$key].=$value;
 583						if ($key == 'school') array_push($schools,$value);
 584						if ($key == 'major') array_push($areas_of_focus,$value); 
 585					}
 586				}
 587				$master_degree = false;
 588				$bach_degree = false;
 589				$doctor_degree = false;
 590				$doctor_array = array('/\b(?i)phd*\b/','/\bdoctor*\b/');
 591				$master_array = array('/\bMS\b/','/\b(?i)master*/');
 592				$bach_array = array('/\b(?i)bachelor*\b/','/\bBS\b/');
 593				preg_replace($doctor_array,'',$parsable_array['degree'],-1,$doctor);
 594				if ($doctor>0) $doctor_degree = true;
 595				preg_replace($master_array,'',$parsable_array['degree'],-1,$masters);
 596				if ($masters>0) $master_degree = true;
 597				preg_replace($bach_array,'',$parsable_array['degree'],-1,$bachelors);
 598				if ($bachelors>0) $bach_degree = true;
 599				$this->set('bach_degree',$bach_degree);
 600				$this->set('master_degree',$master_degree);
 601				$this->set('areas_of_focus',$areas_of_focus);
 602				$this->set('schools',$schools);
 603				
 604			}
 605			//list($your_interests,$aboutme) = $this->getCategories($db_results['Interest']['body']);
 606			//$this->set('your_interests',$your_interests);
 607			//$this->set(compact('aboutme'));
 608
 609/*			$books = array();
 610			if (!empty($db_results['Interest']['likes'])){
 611				$likes = json_decode($db_results['Interest']['likes']);
 612				for ($counter =0;$counter<sizeof($likes);$counter++){
 613					if ($likes[$counter]->category == "Book"){
 614						array_push($books , $likes[$counter]);
 615					}
 616				}
 617					
 618			}
 619	*/
 620	
 621			$this->set('user',$db_results);
 622			
 623	
 624			
 625		}
 626		
 627		// for travel look at 4sq for outside of the country
 628	}
 629	function view_my_friends($limit){
 630		$user = $this->Auth->getUserInfo();
 631		$this->set('pic',$user['path']);
 632		$friend_array = $this->getFriends();
 633		$this->set('friends',$friend_array);
 634		$this->set('start',$limit-50);
 635		$this->set('limit',$limit);
 636	}
 637	function getFacebookInterests($tag_cloud,$fb_user_likes){
 638		$fb_interests=array();
 639		for($counter=0;$counter<sizeof($fb_user_likes->data);$counter++){
 640			//if($fb_user_likes->data[$counter]->category == "Interest"){
 641				array_push($fb_interests,$fb_user_likes->data[$counter]->name);
 642			//}
 643		}
 644		
 645		// weighting is heavier because of explicit interest on fb
 646		for($counter=0;$counter<sizeof($fb_interests);$counter++){
 647			if (!isset($tag_cloud[$fb_interests[$counter]])){
 648				$tag_cloud[$fb_interests[$counter]]=5;   
 649			}
 650			else $tag_cloud[$fb_interests[$counter]]+=5; 
 651		}
 652		return $tag_cloud;
 653	}
 654	
 655	function scrub_interests($tag_cloud,$mu_interests,$tw_following=NULL,$tw_lists=NULL){
 656		$sizes = array(sizeof($mu_interests),sizeof($tw_following),sizeof($tw_lists));
 657		arsort($sizes,SORT_NUMERIC);
 658			
 659			
 660		if (!is_null($tw_lists) && !is_null($tw_following)){
 661			
 662			if ($sizes[0]==sizeof($mu_interests)) $small=$mu_interests;
 663			elseif ($sizes[0]==sizeof($tw_following)) $small=$tw_following;
 664			else $small=$tw_lists;
 665		
 666			if ($sizes[1]==sizeof($mu_interests)) $med=$mu_interests;
 667			elseif ($sizes[1]==sizeof($tw_following)) $med=$tw_following;
 668			else $med=$tw_lists;
 669		
 670			if ($sizes[2]==sizeof($mu_interests)) $large=$mu_interests;
 671			elseif ($sizes[2]==sizeof($tw_following)) $large=$tw_following;
 672			else $large=$tw_lists;
 673			
 674			$sort_size=3;
 675		}
 676		elseif (!is_null($tw_following)) {
 677			if (sizeof($mu_interests)<sizeof($tw_following)) {
 678				$med=$mu_interests;
 679				$large=$tw_following;
 680			}
 681			else {
 682				$med=$tw_following;
 683				$large=$mu_interests;
 684			}
 685			$sort_size=2;
 686		}
 687		else {
 688			$large=$mu_interests;
 689			$sort_size=1;
 690		}
 691		if ($sort_size>0){
 692			//echo $sizes[0]. ' SIZES';
 693			//for($counter=$sizes[1];$counter<$sizes[2];$counter++){
 694			for($counter=0;$counter<$sizes[0];$counter++){
 695				$interest_string_lge = preg_split('/[,? ]+/',$large[$counter]->description); 
 696				$interest_string_lge = preg_replace($this->stop_words,'',$interest_string_lge);
 697				//$interest_string_lge = preg_split('/[,? ]+/',$interest_string_lge); 
 698				$large_in = $interest_string_lge;
 699			
 700				for ($inner_counter=0;$inner_counter<sizeof($interest_string_lge);$inner_counter++){
 701					if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
 702						//echo $large_in[$inner_counter];
 703						$tag_cloud[strtolower($large_in[$inner_counter])]=1;
 704					}
 705					else $tag_cloud[strtolower($large_in[$inner_counter])]=$tag_cloud[strtolower($large_in[$inner_counter])]+1;
 706				}
 707			}
 708		}
 709		if ($sort_size>1){
 710			for($counter=$sizes[0];$counter<$sizes[1];$counter++){
 711				$interest_string_med = preg_split('/[,? ]+/',$med[$counter]->description); 
 712				$interest_string_med = preg_replace($this->stop_words,'',$interest_string_med);
 713			
 714				$interest_string_lge = preg_split('/[,? ]+/',$large[$counter]->description); 
 715				$interest_string_lge = preg_replace($this->stop_words,'',$interest_string_lge);
 716			
 717				$inner_loop_strings = array(sizeof($interest_string_med),sizeof($interest_string_lge));
 718				sort($inner_loop_strings,SORT_NUMERIC);
 719			
 720				if ($inner_loop_strings[0]==sizeof($interest_string_med)) {
 721					$med_in=$interest_string_med;
 722					$large_in=$interest_string_lge;
 723				}
 724				else {
 725					$med_in=$interest_string_lge;
 726					$large_in=$interest_string_med;
 727				}
 728	
 729				for ($inner_counter=0;$inner_counter<$inner_loop_strings[0];$inner_counter++){
 730					if (!isset($tag_cloud[strtolower($med_in[$inner_counter])])){
 731						$tag_cloud[strtolower($med_in[$inner_counter])]=1;
 732					}
 733					else $tag_cloud[strtolower($med_in[$inner_counter])]++;
 734					if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
 735						$tag_cloud[strtolower($large_in[$inner_counter])]=1;
 736					}
 737					else $tag_cloud[strtolower($large_in[$inner_counter])]++;
 738				}
 739				for ($inner_counter=$inner_loop_strings[0];$inner_counter<$inner_loop_strings[1];$inner_counter++){
 740					if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
 741						$tag_cloud[strtolower($large_in[$inner_counter])]=1;
 742					}
 743					else $tag_cloud[strtolower($large_in[$inner_counter])]++;
 744				}
 745			}
 746		}
 747		if ($sort_size>2){
 748			for($counter=$sizes[1];$counter<$sizes[2];$counter++){
 749				$interest_string_mu = preg_split('/[,? ]+/',$small[$counter]); 
 750				$interest_string_mu = preg_replace($this->stop_words,'',$interest_string_mu->description);
 751			
 752				$interest_string_twf = preg_split('/[,? ]+/',$med[$counter]->description); 
 753				$interest_string_twf = preg_replace($this->stop_words,'',$interest_string_twf);
 754				
 755				$interest_string_twl = preg_split('/[,? ]+/',$large[$counter]->description); 
 756				$interest_string_twl = preg_replace($this->stop_words,'',$interest_string_twl);
 757			
 758				$inner_loop_strings = array(sizeof($interest_string_mu),sizeof($interest_string_twf),sizeof($interest_string_twl));
 759				sort($inner_loop_strings,SORT_NUMERIC);
 760			
 761				if ($inner_loop_strings[0]==sizeof($interest_string_mu)) $small_in=$interest_string_mu;
 762				elseif ($inner_loop_strings[0]==sizeof($interest_string_twf)) $small_in=$interest_string_twf;
 763				else $small_in=$interest_string_twl;
 764			
 765				if ($inner_loop_strings[1]==sizeof($interest_string_mu)) $med_in=$interest_string_mu;
 766				elseif ($inner_loop_strings[1]==sizeof($interest_string_twf)) $med_in=$interest_string_twf;
 767				else $med_in=$interest_string_twl;
 768		
 769				if ($inner_loop_strings[2]==sizeof($interest_string_mu)) $large_in=$interest_string_mu;
 770				elseif ($inner_loop_strings[2]==sizeof($interest_string_twf)) $large_in=$interest_string_twf;
 771				else $large_in=$interest_string_twl;
 772			
 773				for ($inner_counter=0;$inner_counter<$inner_loop_strings[0];$inner_counter++){
 774					if (!isset($tag_cloud[strtolower($interest_string_mu[$inner_counter])])){
 775						$tag_cloud[strtolower($interest_string_mu[$inner_counter])]=1;
 776					}
 777					else $tag_cloud[strtolower($interest_string_mu[$inner_counter])]++;
 778					if (!isset($tag_cloud[strtolower($interest_string_twf[$inner_counter])])){
 779						$tag_cloud[strtolower($interest_string_twf[$inner_counter])]=1;
 780					}
 781					else $tag_cloud[strtolower($interest_string_twf[$inner_counter])]++;
 782					if (!isset($tag_cloud[strtolower($interest_string_twl[$inner_counter])])){
 783						$tag_cloud[strtolower($interest_string_twl[$inner_counter])]=1;
 784					}
 785					else $tag_cloud[strtolower($interest_string_twl[$inner_counter])]++;
 786				}
 787				for ($inner_counter=$inner_loop_strings[0];$inner_counter<$inner_loop_strings[1];$inner_counter++){
 788					if (!isset($tag_cloud[strtolower($med_in[$inner_counter])])){
 789						$tag_cloud[strtolower($med_in[$inner_counter])]=1;
 790					}
 791					else $tag_cloud[strtolower($med_in[$inner_counter])]++;
 792					if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
 793						$tag_cloud[strtolower($large_in[$inner_counter])]=1;
 794					}
 795					else $tag_cloud[strtolower($large_in[$inner_counter])]++;
 796				}
 797				for ($inner_counter=$inner_loop_strings[1];$inner_counter<$inner_loop_strings[2];$inner_counter++){
 798					if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
 799						$tag_cloud[strtolower($large_in[$inner_counter])]=1;
 800					}
 801					else $tag_cloud[strtolower($large_in[$inner_counter])]++;
 802				}
 803			}
 804		}
 805		
 806		
 807		//	$lists_f[$counter]->name = $tw_user->lists[$counter]->name;
 808		//	$lists_f[$counter]->description = $tw_user->lists[$counter]->description;
 809		
 810		arsort($tag_cloud, SORT_NUMERIC);
 811		unset($tag_cloud['']);
 812		unset($tag_cloud[' ']);
 813	//	var_dump($tag_cloud);
 814		
 815		return $tag_cloud;
 816		
 817	}
 818	function like_sort($a,$b){
 819		if ($a['category'] > $b['category']) return -1;
 820		elseif ($a['category'] == $b['category']) return 0;
 821		else return 1;
 822	}
 823	function edit($type=null){
 824		
 825	 	if(is_null($this->Auth->getUserId())){
 826          Controller::render('/deny');
 827         }
 828		else {
 829			if (!empty($this->data)) {
 830				if ($this->data['User']['new_password']!='' && $this->data['User']['new_password']==$this->data['User']['confirm_password']){
 831					$this->data['User']['password'] = $this->Auth->hasher($this->data['User']['new_password']); 
 832				}
 833				$this->data['User']['screen_name']=strtolower($this->data['User']['screen_name']);
 834				switch ($type){
 835				}
 836				$username=$this->User->read(null,$this->Auth->getUserId());
 837				
 838				$this->User->set($this->data);
 839	    		if ($this->User->validates()){
 840				    $this->User->save();
 841					$this->_login($username['User']['email'],$username['User']['password']);
 842		    	    $this->redirect(array( 'action'=>'view_my_profile'));
 843				}	
 844				else {
 845					$this->set('errors', $this->User->validationErrors);
 846				}
 847			}
 848			else {
 849				$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
 850				
 851				
 852				// let's sort this by category
 853				$interest = json_decode($user['Interest']['likes'],true);
 854				
 855			
 856				usort($interest, array(&$this, "like_sort"));
 857				//	var_dump($interest);
 858				
 859				$aspirations = json_decode($user['Interest']['body'],true);
 860				$work = json_decode($user['Work']['body'],true);
 861				$schools = json_decode($user['School']['body'],true);
 862				$this->set(compact('schools'));
 863				$this->set(compact('work'));
 864				$this->set(compact('aspirations'));
 865				$this->set(compact('interest'));
 866				$this->set(compact('user'));
 867				$relationship = array(
 868								'Single'=>'Single',
 869								'In a relationship'=>'In a relationship',
 870								'Engaged'=>'Engaged',
 871								'Married'=>'Married',
 872								'Widowed'=>'Widowed',
 873								'Separated'=>'Separated',
 874								'Divorced'=>'Divorced'
 875							);
 876				$this->set(compact('relationship'));			
 877				$months = array(
 878							"Jan"=>"Jan",
 879							"Feb"=>"Feb",
 880							"Mar"=>"Mar",
 881							"Apr"=>"Apr",
 882							"May"=>"May",
 883							"Jun"=>"Jun",
 884							"Jul"=>"Jul",
 885							"Aug"=>"Aug",
 886							"Sep"=>"Sep",
 887							"Oct"=>"Oct",
 888							"Nov"=>"Nov",
 889							"Dec"=>"Dec"
 890							);
 891				$this->set(compact('months'));
 892				$this->set('dates',range(1,31));
 893				$this->set('years',range(1900,(int)date('Y')-18));
 894			}
 895		}
 896	}
 897	function edit_interests($type){
 898		$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
 899		if ($type==1){
 900			// need to pull new data 
 901			$interest = json_decode($user['Interest']['likes'],true);
 902		//	var_dump($this->data);
 903			$new_array = array();
 904			for ($counter=0;$counter<sizeof($interest);$counter++){
 905				$name = 'delete_'.$counter;
 906				if (!$this->data['User'][$name]){
 907					array_push($new_array,$interest[$counter]);
 908				}
 909			}
 910			$this->data['Interest']['likes'] = json_encode($new_array);
 911			$this->Interest->read(null,$user['Interest']['id']);
 912			$this->Interest->set($this->data);
 913			$this->Interest->save();
 914			// if the registration cookie is set then go thru each page
 915			//otherwise redirect to view my profile
 916			//var_dump($this->data);
 917		}
 918		elseif ($type==2){
 919			$aspirations = json_decode($user['Interest']['body'],true);
 920			$new_array = array();
 921			foreach ($aspirations as $key=>$value){
 922				$name = 'delete_'.$key;
 923				if (!$this->data['User'][$name]){
 924					$new_array[$key]=$aspirations[$key];
 925				}
 926			}
 927			$this->data['Interest']['body'] = json_encode($new_array);
 928			$this->Interest->read(null,$user['Interest']['id']);
 929			$this->Interest->set($this->data);
 930			$this->Interest->save();
 931			//var_dump($this->data);
 932				
 933		}
 934		$this->redirect(array('action'=>'view_my_profile'));
 935	}
 936	function edit_pic($service){
 937		if(is_null($this->Auth->getUserId())){
 938    		Controller::render('/deny');
 939        }
 940		else {
 941			//echo 'hihihi';
 942			$user = $this->Auth->getUserInfo();
 943			$consumer = $this->createConsumer($service);
 944			$access_key = $service.'_access_key';
 945			$access_secret = $service.'_access_secret';
 946			switch ($service){
 947				case 'linkedin':
 948					$pic_url = $consumer->get($user[$access_key],$user[$access_secret],'http://api.linkedin.com/v1/people/~:(picture-url)', array());;
 949					$li_user = simplexml_load_string($pic_url);
 950	//			var_dump($li_user->children());
 951//							$pic = $li_user->children[0];
 952					foreach($li_user->children() as $child){
 953						if ($child->getName()=='picture-url'){
 954							$pic = $child;
 955						}
 956					}
 957			//}
 958					break;
 959			case 'foursquare':
 960				$fs_user = json_decode(file_get_contents('https://api.foursquare.com/v2/users/self?oauth_token='.$user['foursquare_access_token']));
 961				$pic = $fs_user->response->user->photo;
 962				break;
 963			
 964			case 'meetup':
 965				$getData = array('relation'=>'self',
 966						 'sess'=>'oauth_session');
 967				$pic_url = $consumer->get($user[$access_key],$user[$access_secret],'https://api.meetup.com/members.json',$getData);
 968				$mu_user = json_decode($pic_url);
 969	//			var_dump($mu_user);
 970				$pic = $mu_user->results[0]->photo_url;
 971				break;
 972		
 973			case 'facebook':
 974				$fb_user = json_decode(file_get_contents('https://graph.facebook.com/me?' . $user['facebook_access_key']));
 975				$pic = 'http://graph.facebook.com/'.$fb_user->id.'/picture';
 976				
 977				
 978				break;
 979				
 980				
 981				
 982			case 'twitter':
 983
 984				$pic_url = $consumer->get($user[$access_key],$user[$access_secret],'http://api.twitter.com/1/account/verify_credentials.json', array());;
 985				$tw_user = json_decode($pic_url);
 986				$pic = $tw_user->profile_image_url;
 987				break;
 988		}
 989		//var_dump($user);
 990		$this->User->read(null,$this->Auth->getUserId());
 991		$this->data['User']['path']=$pic;
 992		$this->User->set($this->data);
 993		$this->User->save();
 994		$this->redirect(array('action'=>'view_my_profile'));
 995			
 996	}
 997	}
 998	
 999	function view($screen_name = null){
1000		$user = $this->Auth->getUserInfo();
1001		$screen_name = strtolower($screen_name);
1002		if ($screen_name == $user['screen_name']){
1003			$this->set('own_profile', true);
1004		}
1005		else $this->set('own_profile',false);
1006		$db_results = $this->User->find('first', array('conditions' => (array('User.screen_name'=>$screen_name))));
1007		//var_dump($db_results);
1008		if (empty($db_results)){
1009			$this->set('exists',false);
1010		}
1011		else{
1012			$this->set('exists',true);
1013			$this->set('pic',$db_results['User']['path']);
1014			if (!empty($db_results['Movie']['body'])){
1015				$movies = json_decode($db_results['Movie']['body']);
1016				$top_movies = array();
1017				foreach ($movies as $key=>$value){
1018					array_push($top_movies,$key);
1019				}
1020				$this->set('top_movies',$top_movies);
1021			}
1022			if (!empty($db_results['Interest']['body'])){
1023				$interests = json_decode($db_results['Interest']['body']);
1024				$top_interests = array();
1025				foreach ($interests as $key=>$value){
1026					array_push($top_interests,$key);
1027				}
1028				$this->set('top_interests',$top_interests);
1029			}
1030			if (!empty($db_results['Interest']['likes'])){
1031				$interests = json_decode($db_results['Interest']['likes']);
1032				$hobbies = array();
1033				foreach ($interests as $key=>$value){
1034					array_push($hobbies,$key);
1035				}
1036				$this->set('top_hobbies',$hobbies);
1037			}
1038			if (!empty($db_results['Place']['locations'])){
1039				$locations = json_decode($db_results['Place']['locations']);
1040				$top_locations = array();
1041				foreach ($locations as $key=>$value){
1042					array_push($top_locations,$key);
1043				}
1044				$this->set('top_locations',$top_locations);
1045			}
1046			if (!empty($db_results['Place']['categories'])){
1047				$categories = json_decode($db_results['Place']['categories']);
1048				$top_categories = array();
1049				foreach ($categories as $key=>$value){
1050					array_push($top_categories,$key);
1051				}
1052				$this->set('top_categories',$top_categories);
1053			}
1054			if (!empty($db_results['Interest']['you_body'])){
1055				$you = json_decode($db_results['Interest']['you_body']);
1056				$top_you = array();
1057				foreach ($you as $key=>$value){
1058					array_push($top_you,$key);
1059				}
1060				$this->set('top_you',$top_you);
1061			}
1062			if (!empty($db_results['Work']['body'])){
1063				$work = json_decode($db_results['Work']['body']);
1064			}	
1065			if (!empty($db_results['Work']['titles'])){
1066				$work = json_decode($db_results['Work']['titles']);
1067		
1068				foreach ($work as $key=>$value){
1069					$this->set('titles',$key);			
1070					break;
1071				}
1072			}
1073			if (!empty($db_results['Work']['industries'])){
1074				$work = json_decode($db_results['Work']['industries'],true);
1075				$first = true;
1076				arsort($work, SORT_NUMERIC);
1077				$top_industries = array();
1078				foreach ($work as $key=>$value){
1079					if ($first){
1080						$this->set('industries',$key);			
1081						$first = false;
1082					}
1083					array_push($top_industries, $key);
1084				}
1085				$this->set('top_industries',$top_industries);
1086			}
1087			if (!empty($db_results['School']['body'])){
1088				$school = json_decode($db_results['School']['body']);
1089				$parsable_array = array();
1090				$schools =array();
1091				$areas_of_focus = array();
1092				for ($counter = 0;$counter<sizeof($school);$counter++){
1093					foreach($school[$counter] as $key=>$value){
1094						if ($key == 'degree') {
1095							if (!isset($parsable_array['degree'])) $parsable_array[$key]=$value;
1096							else $parsable_array[$key].=(string)$value;
1097						}
1098						if ($key == 'school') array_push($schools,$value);
1099						if ($key == 'major') array_push($areas_of_focus,$value); 
1100					}
1101				}
1102				$master_degree = false;
1103				$bach_degree = false;
1104				$doctor_degree = false;
1105				$doctor_array = array('/\b(?i)phd*\b/','/\bdoctor*\b/');
1106				$master_array = array('/\bMS\b/','/\b(?i)master*/');
1107				$bach_array = array('/\b(?i)bachelor*\b/','/\bBS\b/');
1108				if (isset($parsable_array['degree'])){
1109					preg_replace($doctor_array,'',$parsable_array['degree'],-1,$doctor);
1110					if ($doctor>0) $doctor_degree = true;
1111					preg_replace($master_array,'',$parsable_array['degree'],-1,$masters);
1112					if ($masters>0) $master_degree = true;
1113					preg_replace($bach_array,'',$parsable_array['degree'],-1,$bachelors);
1114					if ($bachelors>0) $bach_degree = true;
1115				}
1116				$this->set('bach_degree',$bach_degree);
1117				$this->set('master_degree',$master_degree);
1118				$this->set('doctor_degree',$doctor_degree);
1119				$this->set('areas_of_focus',$areas_of_focus);
1120				$this->set('schools',$schools);
1121			}
1122			$sign = $this->getSign($db_results['Userprofile']['birthday']);
1123			$this->set('zodiac', $sign);
1124			$this->set('user',$db_results);
1125			$this->set('screen_name',$screen_name);
1126		}
1127	}
1128	function compare($screen_name=null){
1129		if(is_null($this->Auth->getUserId())){
1130    		Controller::render('/deny');
1131        }
1132		
1133		$this->set('exists',true);
1134		$this->set('screen_name',$screen_name);
1135		$user = $this->Auth->getUserInfo();
1136		if ($screen_name == $user['screen_name']){
1137			// you are comparing yourself to yourself
1138			$this->redirect(array('action'=>'view_my_profile'));
1139		}
1140		else {
1141			$score = 0;
1142			$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
1143			$other_user = $this->User->find('first', array('conditions' => (array('User.screen_name'=>$screen_name))));
1144			$this->set(compact('user'));
1145			$this->set('other_user',$other_user);
1146			if (empty($other_user)) $this->set('exists',false);
1147			// compare interests
1148			//var_dump($user['Interest']);
1149			
1150			
1151			
1152//			var_dump($other_user['Interest']);
1153			$astrology = 0;
1154			$personal = 0;
1155			
1156			if (!is_null($user['Userprofile']['id']) && !is_null($other_user['Userprofile']['id'])){
1157				// check difference in birthday
1158				// same sign?
1159				$user_sign = $this->getSign($user['Userprofile']['birthday']);
1160				$other_user_sign = $this->getSign($other_user['Userprofile']['birthday']);
1161				
1162				$diff = abs(strtotime($user['Userprofile']['birthday']) - strtotime($other_user['Userprofile']['birthday']));
1163				$years = floor($diff / (365*60*60*24));
1164				$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
1165				$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
1166				
1167				if ($months == 0 && $days < 10) {
1168					$astrology += 10;
1169					$score += 5;
1170				}
1171				elseif ($month > 1 && $month < 3) {
1172					$astrology += 5;
1173					$score+=2;
1174				}
1175				if ($years < 10){
1176					$score++;
1177				}
1178				if ($user_sign == $other_user_sign){
1179					$astrology += 10;
1180					$score += 3;
1181				}
1182			
1183				if ($user['Userprofile']['gender']==$other_user['Userprofile']['gender']){
1184					$score++;
1185					$personal++;
1186				}
1187				similar_text($user['Userprofile']['relationship'],$other_user['Userprofile']['relationship'],$r_match);
1188				if ($r_match > 50) {
1189					$personal++;
1190					$score++;	
1191				}
1192				similar_text($user['Userprofile']['religion'],$other_user['Userprofile']['religion'],$re_match);
1193				if ($re_match > 50) {
1194					$personal += 2;
1195					$score+=2;			
1196				}
1197				similar_text($user['Userprofile']['political'],$other_user['Userprofile']['political'],$p_match);
1198				if ($p_match > 50) {
1199					$personal += 3;
1200					$score+=3;			
1201				}
1202				
1203				if (!is_null($user['Userprofile']['hometown']) && !is_null($other_user['Userprofile']['hometown'])){
1204					
1205					$url = "http://where.yahooapis.com/geocode?line2=".urlencode($user['Userprofile']['hometown'])."&flags=J&gflags=R&appid=cENXMi4g";
1206					$address = json_decode(file_get_contents($url));
1207					
1208					
1209					$lat_home = $address->ResultSet->Results[0]->latitude;
1210					$long_home = $address->ResultSet->Results[0]->longitude;
1211					
1212					
1213					
1214					$url2 = "http://where.yahooapis.com/geocode?line2=".urlencode($other_user['Userprofile']['hometown'])."&flags=J&gflags=R&appid=cENXMi4g";
1215					$address2 = json_decode(file_get_contents($url2));
1216					
1217					$lat_away = $address2->ResultSet->Results[0]->latitude;
1218					$long_away = $address2->ResultSet->Results[0]->longitude;
1219					
1220					
1221					$earth_radius = 6371;
1222					$delta_lat = deg2rad($lat_home - $lat_away);
1223					$delta_long = deg2rad($long_home - $long_away);
1224					$a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($lat_home)) * cos(deg2rad($lat_away)) * sin($delta_long/2) * sin($delta_long/2);
1225					$c = 2 * atan2(sqrt($a),sqrt(1-$a));
1226					$distance = $earth_radius * $c;
1227					
1228					
1229					$d_miles = $distance * 0.621371192;
1230	
1231					if ($d_miles < 1) {
1232						$score += 20;
1233					}
1234					elseif ($d_miles < 10) {
1235						$score += 16;
1236					}
1237					elseif ($d_miles < 25) {
1238						$score += 12;
1239					}
1240					elseif ($d_miles < 50) {
1241						$score += 10;
1242					}
1243					elseif ($d_miles < 100) {
1244						$score += 7;
1245					}
1246					elseif ($d_miles < 150) {
1247						$score += 3;
1248					}
1249					elseif ($d_miles < 200) {
1250						$score++;
1251					}
1252					$this->set('distance',$d_miles);
1253				}
1254			}
1255			$this->set('astrology',$astrology);
1256			$this->set('personal',$personal);
1257			
1258			if (!is_null($user['Interest']['id']) && !is_null($other_user['Interest']['id'])){
1259				
1260				$user_interest = json_decode($user['Interest']['body'], true);
1261				$other_user_interest =json_decode($other_user['Interest']['body'], true);
1262				arsort($user_interest,SORT_NUMERIC);
1263				arsort($other_user_interest,SORT_NUMERIC);
1264				$common_interests = array_intersect_key($user_interest,$other_user_interest);
1265	//			var_dump($user_interest);
1266	//			var_dump($other_user_interest);
1267				
1268			}
1269			
1270			
1271			
1272			
1273			/***************
1274			
1275			array_ intersect_ key
1276			
1277			
1278			array_count_values
1279			
1280			
1281			
1282			******************/			
1283			/* put data into arrays
1284			   if simliar tag a 1
1285			   if not skip and continue
1286			   
1287			*/
1288			
1289			// use fb ids?
1290			
1291			
1292			// compare places
1293			if (!is_null($user['Place']['id']) && !is_null($other_user['Place']['id'])){
1294				//var_dump($user['Place']['body']);
1295		
1296				//var_dump($other_user['Place']['body']);			echo 'checking places';
1297			}
1298			
1299			
1300			// locations
1301			// categories
1302			
1303			
1304			// compare movies
1305			if (!is_null($user['Movie']['id']) && !is_null($other_user['Movie']['id'])){
1306							echo 'checking movies';
1307	
1308				
1309			}
1310			
1311			// compare schools
1312			if (!is_null($user['School']['id']) && !is_null($other_user['School']['id'])){
1313				
1314				//var_dump($user['School']['body']);
1315				$my_schools = json_decode($user['School']['body'],true);
1316				$your_schools = json_decode($other_user['School']['body'],true);
1317				$earth_radius = 6371;
1318				$education = 0;
1319				
1320				
1321			
1322				
1323				$doctor_array = array('/\b(?i)phd*\b/','/\bdoctor*\b/');
1324				$master_array = array('/\bMS\b/','/\b(?i)master*/');
1325				$bach_array = array('/\b(?i)bachelor*\b/','/\bBS\b/');
1326				
1327				for ($counter = 0;$counter<sizeof($my_schools);$counter++){
1328					if (isset($my_schools[$counter]['degree'])){
1329						preg_replace($doctor_array,'',$my_schools[$counter]['degree'],-1,$doctor);
1330						if ($doctor>0) $my_doctor_degree = true;
1331						preg_replace($master_array,'',$my_schools[$counter]['degree'],-1,$masters);
1332						if ($masters>0) $my_master_degree = true;
1333						preg_replace($bach_array,'',$my_schools[$counter]['degree'],-1,$bachelors);
1334						if ($bachelors>0) $my_bach_degree = true;
1335					}
1336				}
1337				for ($counter = 0;$counter<sizeof($your_schools);$counter++){
1338					if (isset($your_schools[$counter]['degree'])){
1339						preg_replace($doctor_array,'',$your_schools[$counter]['degree'],-1,$doctor);
1340						if ($doctor>0) $your_doctor_degree = true;
1341						preg_replace($master_array,'',$your_schools[$counter]['degree'],-1,$masters);
1342						if ($masters>0) $your_master_degree = true;
1343						preg_replace($bach_array,'',$your_schools[$counter]['degree'],-1,$bachelors);
1344						if ($bachelors>0) $your_bach_degree = true;
1345					}
1346				}
1347				
1348				
1349				if ($my_bach_degree && $your_bach_degree) $bach_degree = true;
1350				if ($my_master_degree && $your_master_degree) $master_degree = true;
1351				if ($my_doctor_degree && $your_doctor_degree) $doctor_degree = true;
1352				
1353				
1354				$this->set('bachelor',$bach_degree);
1355				$this->set('master',$master_degree);
1356				$this->set('doctor',$doctor_degree);
1357				
1358				
1359				$my_major = array();
1360				$your_major = array();
1361				// look at majors
1362				for ($counter = 0;$counter<sizeof($my_schools);$counter++){
1363					if (isset($my_schools[$counter]['major'])){
1364						$tok = strtok($my_schools[$counter]['major']," ");
1365						while ($tok !== false) {
1366							array_push($my_major, $tok);
1367							$tok = strtok(" ");
1368						}
1369	
1370					}
1371				}
1372				for ($counter = 0;$counter<sizeof($your_schools);$counter++){
1373					if (isset($your_schools[$counter]['major'])){
1374						$tok = strtok($your_schools[$counter]['major']," ");
1375						while ($tok !== false) {
1376							array_push($your_major, $tok);
1377							$tok = strtok(" ");
1378						}
1379					}
1380				}
1381				
1382				$majors = 0;
1383				for ($counter = 0; $counter < sizeof($my_major); $counter++){
1384					for ($inner_counter = 0; $inner_counter<sizeof($your_major); $inner_counter++){
1385						similar_text($my_major[$counter],$your_major[$inner_counter],$match);
1386						if ($match > 50) {	
1387//							var_dump($my_major[$counter]);
1388	//						var_dump($your_major[$inner_counter]);
1389							$score += 20;
1390							$majors += 10;
1391						}
1392					}
1393				}
1394				$this->set('majors',$majors);
1395				
1396				for ($counter=0;$counter<sizeof($my_schools);$counter++){
1397					$url = "http://where.yahooapis.com/geocode?q=".urlencode($my_schools[$counter]['school'])."&flags=J&gflags=R&appid=cENXMi4g";
1398					$address = json_decode(file_get_contents($url));
1399				//	echo $my_schools['counter']['school'];
1400					//var_dump($address);
1401					if (!$address->ResultSet->Error){
1402						$my_lat[$counter] = $address->ResultSet->Results[0]->latitude;
1403						$my_long[$counter] = $address->ResultSet->Results[0]->longitude;
1404					}
1405					
1406					for ($inner_counter=0;$inner_counter<sizeof($your_schools);$inner_counter++){
1407						similar_text($my_schools[$counter]['school'],$your_schools[$inner_counter]['school'],$match);
1408						if ($match > 75) {
1409							$education += 10;
1410							$score+=30;
1411						}
1412						if ($counter==0){
1413							$url = "http://where.yahooapis.com/geocode?q=".urlencode($your_schools[$inner_counter]['school'])."&flags=J&gflags=R&appid=cENXMi4g";
1414							$address = json_decode(file_get_contents($url));
1415								//	echo $your_schools[$inner_counter]['school'];
1416									//		var_dump($address);
1417
1418							if (!$address->ResultSet->Error){
1419								$your_lat[$inner_counter] = $address->ResultSet->Results[0]->latitude;
1420								$your_long[$inner_counter] = $address->ResultSet->Results[0]->longitude;
1421							}
1422						}
1423						
1424						$delta_lat = deg2rad($my_lat[$counter] - $your_lat[$inner_counter]);
1425						$delta_long = deg2rad($my_long[$counter] - $your_long[$inner_counter]);
1426						$a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($my_lat[$counter])) * cos(deg2rad($your_lat[$inner_counter])) * sin($delta_long/2) * sin($delta_long/2);
1427						$c = 2 * atan2(sqrt($a),sqrt(1-$a));
1428						$distance = $earth_radius * $c;
1429						$d_miles = $distance * 0.621371192;
1430						
1431						if ($d_miles < 1) $score += 20;
1432						elseif ($d_miles < 10) $score += 16;
1433						elseif ($d_miles < 25) $score += 12;
1434						elseif ($d_miles < 50) $score += 10;
1435						elseif ($d_miles < 100) $score += 7;
1436						elseif ($d_miles < 150) $score += 3;
1437						elseif ($d_miles < 200) $score++;
1438						
1439						$this->set('edu_distance',$d_miles);
1440						
1441					}
1442				}
1443				$this->set('education',$education);
1444				
1445			}
1446			// compare works
1447			if (!is_null($user['Work']['id']) && !is_null($other_user['Work']['id'])){
1448				$my_industries = array();
1449				$your_industries = array();
1450				$work = 0;
1451				$my_work = json_decode($user['Work']['body']);
1452				$your_work = json_decode($other_user['Work']['body']);
1453				for($counter = 0; $counter < sizeof($my_work); $counter++){
1454					$cats = $this->Wlookup->find('first',array('conditions' => (array('Wlookup.description'=>$my_work[$counter]->industry))));
1455					$tok = strtok($cats['Wlookup']['group']," ");
1456					while ($tok !== false) {
1457						array_push($my_industries, $tok);
1458						$tok = strtok(" ");
1459					}
1460				}
1461				for($counter = 0; $counter < sizeof($your_work); $counter++){
1462					$cats = $this->Wlookup->find('first',array('conditions' => (array('Wlookup.description'=>$your_work[$counter]->industry))));
1463					$tok = strtok($cats['Wlookup']['group']," ");
1464					while ($tok !== false) {
1465						array_push($your_industries, $tok);
1466						$tok = strtok(" ");
1467					}
1468				}
1469				
1470				$result = array_intersect($my_industries, $your_industries);
1471				if (!empty($result)) {
1472					$score += sizeof($result) * 2;
1473					$work += 10;
1474				}
1475			
1476				
1477				
1478				// titles
1479				
1480				// we need a list of titles  that are similar
1481				
1482				
1483				for($counter = 0; $counter < sizeof($my_work); $counter++){
1484					for($inner_counter = 0; $inner_counter < sizeof($your_work); $inner_counter++){
1485						similar_text($my_work[$counter]->company,$your_work[$inner_counter]->company,$match);
1486						if ($match > 50){
1487							$work += 10;
1488							$score += 10;
1489						}
1490						
1491					}
1492				}
1493				
1494				//companys
1495				// how do we compare companies
1496				
1497				
1498				for($counter = 0; $counter < sizeof($my_work); $counter++){
1499					for($inner_counter = 0; $inner_counter < sizeof($your_work); $inner_counter++){
1500						similar_text($my_work[$counter]->title,$your_work[$inner_counter]->title,$match);
1501						if ($match > 50){
1502							$work += 10;
1503							$score += 10;
1504						}
1505						
1506					}
1507				}
1508				
1509				
1510				
1511				
1512				$this->set('work',$work);
1513	
1514				
1515				
1516				
1517				
1518				
1519				
1520				
1521				
1522				
1523				
1524				
1525			}
1526		
1527			
1528			$this->set('score',$score);
1529		
1530		}
1531	}
1532	function getSign($birthday){
1533		$birthmonth = date('n',strtotime($birthday));
1534		$birthdate = date('d',strtotime($birthday));
1535		$sign = '';
1536		switch($birthmonth){
1537				case '1':
1538					if ($birthdate>19) $sign = "Aquarius";
1539					else $sign = "Capricorn";
1540					break;
1541				case '2':
1542					if ($birthdate>18) $sign = "Pisces";
1543					else $sign = "Aquarius";
1544					break;
1545				case '3':
1546					if ($birthdate>20) $sign = "Aries";
1547					else $sign = "Pisces";
1548					break;
1549				case '4':
1550					if ($birthdate>19) $sign = "Taurus";
1551					else $sign = "Aries";
1552					break;
1553				case '5':
1554					if ($birthdate>20) $sign = "Gemini";
1555					else $sign = "Taurus";
1556					break;
1557				case '6':
1558					if ($birthdate>20) $sign = "Cancer";
1559					else $sign = "Gemini";
1560					break;
1561				case '7':
1562					if ($birthdate>22) $sign = "Leo";
1563					else $sign = "Cancer";
1564					break;
1565				case '8':
1566					if ($birthdate>22) $sign = "Virgo";
1567					else $sign = "Leo";
1568					break;
1569				case '9':
1570					if ($birthdate>22) $sign = "Libra";
1571					else $sign = "Virgo";
1572					break;
1573				case '10':
1574					if ($birthdate>22) $sign = "Scorpio";
1575					else $sign = "Libra";
1576					break;
1577				case '11':
1578					if ($birthdate>21) $sign = "Sagittarius";
1579					else $sign = "Scorpio";
1580					break;
1581				case '12':
1582					if ($birthdate>21) $sign = "Capricorn";
1583					else $sign = "Sagittarius";
1584					break;
1585		}
1586		return $sign;
1587	}
1588	function edit_interest($id=null){
1589		$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
1590		$interests = json_decode($user['Interest']['body'],true);
1591	//			var_dump($interests);
1592	//var_dump($id);
1593		$div_name = 'Interest_'.$id; 
1594		$this->set('editing',true);
1595		$this->set('div_name',$div_name);
1596		$this->set('id',$id);	
1597		// need to make sure you are the owner
1598		if (is_null($this->Auth->getUserId())){
1599			Controller::render('/deny');
1600		}
1601		if (!empty($this->data)){
1602	//		var_dump($this->data);
1603			$id = $this->data['User']['id'];
1604			$key = $this->data['User']['key'];
1605			$interests[$key]=(int)$interests[$id];
1606			unset($interests[$id]);
1607			arsort($interests,SORT_NUMERIC);
1608			$this->Interest->read(null,$user['Interest']['id']);
1609			$this->data['Interest']['body'] = json_encode($interests);
1610			$this->Interest->set($this->data);
1611			$this->Interest->save();
1612			$this->set('editing',false);
1613			$this->set('key',$key);
1614			$this->render();
1615		}
1616		else {
1617//			$this->set(
1618			$this->render();
1619		}	
1620	}
1621	function edit_work($id=null,$key=null,$type='Work'){
1622		$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
1623		//$work = json_decode($user[$type]['body'],true);
1624	//	var_dump($user);
1625		//echo $this->Auth->getUserId();
1626//		var_dump($user['Work']['body']);
1627				//var_dump($work);
1628	//echo $id.' id';
1629	//echo $key.' key';
1630	//var_dump($this->data);
1631		if (!empty($this->data)){
1632				$id = $this->data['User']['id'];
1633				$key = $this->data['User']['key'];
1634				$type = $this->data['User']['type'];		
1635		$work = json_decode($user[$type]['body'],true);
1636				$work[$id][$key] = $this->data['User']['val'];
1637		//			var_dump($work);
1638			
1639				if ($type=='Work'){
1640					$this->Work->read(null,$user[$type]['id']);
1641					$this->data[$type]['body'] = json_encode($work);
1642					$this->Work->set($this->data);
1643					$this->Work->save();
1644				}
1645				else { // must be school then
1646					$this->School->read(null,$user[$type]['id']);
1647					$this->data[$type]['body'] = json_encode($work);
1648					$this->School->set($this->data);
1649					$this->School->save();
1650				}
1651			
1652		//	var_dump($this->data);
1653			$this->set('editing',false);
1654			$this->set('value',$this->data['User']['val']);
1655			$this->set('id',$id);
1656			$this->set('key',$key);
1657			$this->set('type',$type);
1658		}
1659		elseif ($key == 'delete'){
1660			echo 'Data Deleted';
1661			//echo $type;
1662				$work = json_decode($user[$type]['body'],true);
1663	
1664			//echo $user['Work']['id'];
1665			//echo $user[$type]['id'];
1666				// delete the whole entry
1667							if ($type=='Work'){
1668					$this->Work->delete($user[$type]['id']);
1669					//$this->data[$type]['body'] = json_encode($work);
1670					//$this->Work->set($this->data);
1671					//$this->Work->save();
1672				}
1673				else { // must be school then
1674					$this->School->delete($user[$type]['id']);
1675					//$this->data[$type]['body'] = json_encode($work);
1676					//$this->School->set($this->data);
1677					//$this->School->save();
1678				}
1679
1680			}
1681		else {
1682			$this->set('value',$work[$id][$key]); 
1683			$this->set('editing',true);
1684				$this->set('id',$id);
1685			$this->set('type',$type);
1686			$this->set('key',$key);
1687		}
1688		$this->set('div_name','edit_'.$type.'_'.$key.'_'.$id);
1689		$this->render();
1690	}
1691		function add_work($type='Work'){
1692			$user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
1693			
1694			if (!empty($this->data)){
1695	//			var_dump($this->data);
1696				$type = $this->data['User']['type'];
1697				$work = json_decode($user[$type]['body'],true);
1698			if (sizeof($work)<1) $work = array();
1699				foreach($this->data['User'] as $key=>$value){
1700					if ($key != 'type'){
1701						$temp[$type][$key] = $value;
1702					}
1703				}
1704				array_push($work,$temp[$type]);
1705	
1706//			var_dump($work);
1707				if ($type=='Work'){
1708					$this->Work->read(null,$user[$type]['id']);
1709					$this->data[$type]['body'] = json_encode($work);
1710					$this->Work->set($this->data);
1711					$this->Work->save();
1712				}
1713				else { // must be school then
1714					$this->School->read(null,$user[$type]['id']);
1715					$this->data[$type]['body'] = json_encode($work);
1716					$this->School->set($this->data);
1717					$this->School->save();
1718				}
1719			
1720		//	var_dump($this->data);
1721			$this->set('editing',true);
1722			$this->set('value',$this->data['User']['val']);
1723			$this->set('id',$id);
1724			$this->set('key',$key);
1725			$this->set('type',$type);
1726		}
1727		
1728		else {
1729			/*if ($type=='Work'){
1730				$this->set('first','title');
1731				$this->set('second','company');
1732				$this->set('third','industry');
1733				$this->set('type',$type);
1734			
1735			}
1736			else {
1737				$this->set('value',$work[$id][$key]); 
1738				$this->set('editing',true);
1739				$this->set('id',$id);
1740				$this->set('type',$type);
1741				$this->set('key',$key);
1742			}*/
1743		}
1744		if ($type=='Work'){
1745				$this->set('first','title');
1746				$this->set('second','company');
1747				$this->set('third','industry');
1748				$this->set('type',$type);
1749			
1750			}
1751			elseif ($type=='School'){
1752				$this->set('first','degree');
1753				$this->set('second','school');
1754				$this->set('third','major');
1755				$this->set('type',$type);
1756			
1757			
1758			
1759			
1760			}
1761			
1762		$this->set('div_name','add_'.$type);
1763		$this->render();
1764	}
1765
1766}
1767
1768?>