Subversion Repositories SmartDukaan

Rev

Rev 14681 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
class SaholiccheckerShell extends AppShell {
    public $uses = array('User','UserAccount');         

    public function main() {
                Configure::load('live');
                $conditions = array('UserAccount.account_key' => -1);
                $userAccounts = $this->UserAccount->find('all',array('conditions' => $conditions,'recursive'=>-1));
                if(!empty($userAccounts)) {
                        foreach($userAccounts AS $account) {
                                $sql = "DELETE FROM user_accounts WHERE user_id =".$account['UserAccount']['user_id'];
                                $this->out($sql);
                                $this->UserAccount->query($sql);
                        }
                }
        $apihost = Configure::read('saholicapihost');
        $users = $this->User->find('all',array('fields'=>array('id','email'),'recursive'=>-1));
                foreach ($users as $key => $user) {
                        $userId = $user['User']['id'];
                        $email = $user['User']['email'];
                        $options = array('conditions'=>array('user_id' => $userId,'account_type' => 'saholic'),'recursive'=>-1);
                        $exists = $this->UserAccount->find('count',$options);
                        if(!$exists){
                                $url = $apihost."register?email=$email&from=profitmandi";
                                $response = $this->make_request($url,null);
                                $this->out('response '.print_r($response,1));
                                if(!empty($response)) {
                                        if($response['userId']<1)return;
                                        $data = array('account_type'=>'saholic','user_id'=>$userId,'account_key'=>$response['userId']);
                                        $this->UserAccount->create();
                                        $this->UserAccount->save($data);
                                        $data = array('account_type'=>'cartId','user_id'=>$userId,'account_key'=>$response['cartId']);
                                        $this->UserAccount->create();
                                        $this->UserAccount->save($data);
                                }
                        }
                }
        }
        
        function make_request($url,$fields,$format='json'){
                $this->out("[url] $url");
                $this->out("[fields] ".print_r($fields,1));
                $fields_string = '';
                //open connection
                $ch = curl_init();
                //set the url, number of POST vars, POST data
                curl_setopt($ch,CURLOPT_URL, $url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER , true);
                if(!empty($fields)) {
                        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                            'Content-Type: application/json',                                                                                
                            // 'Content-Length: ' . sizeof($fields))                                                                       
                            'Content-Length: ' . strlen($fields))                                                                       
                        );   
                }
                //execute post
                $result = curl_exec($ch);
                $this->log("[response] ".print_r($result,1),'api');
                //close connection
                curl_close($ch);
                switch($format){
                        case 'json':
                        $response = json_decode($result,1);
                        break;
                }
                return $response;       
        }
}