Subversion Repositories SmartDukaan

Rev

Rev 14681 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14681 Rev 14685
Line 1... Line 1...
1
<?php
1
<?php
2
class SaholiccheckerShell extends AppShell {
2
class SaholiccheckerShell extends AppShell {
3
    public $uses = array('User','UserAccount');		
3
    public $uses = array('User','UserAccount');		
4
 
4
 
5
    public function main() {
5
    public function main() {
-
 
6
		Configure::load('live');
-
 
7
		$conditions = array('UserAccount.account_key' => -1);
-
 
8
		$userAccounts = $this->UserAccount->find('all',array('conditions' => $conditions,'recursive'=>-1));
-
 
9
		if(!empty($userAccounts)) {
-
 
10
			foreach($userAccounts AS $account) {
-
 
11
				$sql = "DELETE FROM user_accounts WHERE user_id =".$account['UserAccount']['user_id'];
-
 
12
				$this->out($sql);
-
 
13
				$this->UserAccount->query($sql);
-
 
14
			}
-
 
15
		}
6
    	$apihost = Configure::read('saholicapihost');
16
    	$apihost = Configure::read('saholicapihost');
7
    	$users = $this->User->find('all',array('fields'=>array('id','email'),'recursive'=>-1));
17
    	$users = $this->User->find('all',array('fields'=>array('id','email'),'recursive'=>-1));
8
		foreach ($users as $key => $user) {			
18
		foreach ($users as $key => $user) {
9
			$userId = $user['User']['id'];
19
			$userId = $user['User']['id'];
10
			$email = $user['User']['email'];
20
			$email = $user['User']['email'];
11
			$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
21
			$options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
12
			$exists = $this->UserAccount->find('count',$options);
22
			$exists = $this->UserAccount->find('count',$options);
13
			if(!$exists){
23
			if(!$exists){
14
				$url = $apihost."register?email=$email";
24
				$url = $apihost."register?email=$email&from=profitmandi";
15
				$response = $this->make_request($url,null);
25
				$response = $this->make_request($url,null);
16
				$this->log('response '.print_r($response,1),'registration');
26
				$this->out('response '.print_r($response,1));
17
				if(!empty($response)) {
27
				if(!empty($response)) {
-
 
28
					if($response['userId']<1)return;
18
					$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
29
					$data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
19
					$this->UserAccount->create();
30
					$this->UserAccount->create();
20
					$this->UserAccount->save($data);
31
					$this->UserAccount->save($data);
21
					$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
32
					$data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
22
					$this->UserAccount->create();
33
					$this->UserAccount->create();
23
					$this->UserAccount->save($data);
34
					$this->UserAccount->save($data);
24
				}
35
				}
25
			}
36
			}
26
		}
37
		}
27
	}
38
	}
-
 
39
	
-
 
40
	function make_request($url,$fields,$format='json'){
-
 
41
		$this->out("[url] $url");
-
 
42
		$this->out("[fields] ".print_r($fields,1));
-
 
43
		$fields_string = '';
-
 
44
		//open connection
-
 
45
		$ch = curl_init();
-
 
46
		//set the url, number of POST vars, POST data
-
 
47
		curl_setopt($ch,CURLOPT_URL, $url);
-
 
48
		curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
-
 
49
		if(!empty($fields)) {
-
 
50
			curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
-
 
51
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
-
 
52
			    'Content-Type: application/json',                                                                                
-
 
53
			    // 'Content-Length: ' . sizeof($fields))                                                                       
-
 
54
			    'Content-Length: ' . strlen($fields))                                                                       
-
 
55
			);   
-
 
56
		}
-
 
57
		//execute post
-
 
58
		$result = curl_exec($ch);
-
 
59
		$this->log("[response] ".print_r($result,1),'api');
-
 
60
		//close connection
-
 
61
		curl_close($ch);
-
 
62
		switch($format){
-
 
63
			case 'json':
-
 
64
			$response = json_decode($result,1);
-
 
65
			break;
-
 
66
		}
-
 
67
		return $response;	
-
 
68
	}
28
}
69
}
29
70