Subversion Repositories SmartDukaan

Rev

Rev 15410 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15262 anikendra 1
<?php
2
class PushnotificationShell extends AppShell {
3
    public $uses = array('Pushnotification','GcmUser','Store','User','Click');
4
 
5
    public function main() {
6
		$this->Pushnotification->Behaviors->attach('Containable');
7
		$options = array('conditions'=>array('Pushnotification.type'=>'pending'),'contain'=>array('NotificationCampaign'));
8
		$data = $this->Pushnotification->find('all',$options);
9
		if(!empty($data)) {
10
			if($data[0]['NotificationCampaign']['type'] == 'url' && !empty($data[0]['NotificationCampaign']['url'])) {
11
				$url = $data[0]['NotificationCampaign']['url'];
12
				$store = $this->Store->getByUrl($url);					
13
			}
14
			foreach($data AS $user) {
15
				// $this->out(print_r($user,1));				
16
				$options = array('conditions'=>array('user_id'=>$user['Pushnotification']['user_id']),'fields'=>array('gcm_regid'),'order'=>array('id'=>'desc'));
17
				$gcmUser = $this->GcmUser->find('first',$options);
18
				$regIds = array($gcmUser['GcmUser']['gcm_regid']);
19
				$message = $user['NotificationCampaign'];					
20
				if($user['NotificationCampaign']['type'] == 'url' && !empty($user['NotificationCampaign']['url'])) {
21
					$message['url'] = $this->generateAffiliateUrl($user['NotificationCampaign']['url'],$user['Pushnotification']['user_id'],$store);
22
				}	
15410 manas 23
				$result = $this->send_push_notification($regIds,$message,$user['Pushnotification']['user_id'],$user['Pushnotification']['id']);
15262 anikendra 24
			}
25
		}
26
    }
27
 
15410 manas 28
    private function send_push_notification($registatoin_ids, $message, $user_id,$cid) {
15263 anikendra 29
    	Configure::load('live');
15262 anikendra 30
		$msg = array(
31
		    'message'       => $message['message'],
32
		    'cid'       	=> $message['id'],
33
		    'title'         => $message['title'],
34
		    'type'      	=> $message['type'],
35
		    'url'		    => $message['url'],
36
		    'vibrate'       => 1,
37
		    'sound'         => 1,
38
		    'largeIcon'     => 'large_icon',
39
		    'smallIcon'     => 'small_icon'
40
		);
41
		// Set POST variables
42
		$url = 'https://android.googleapis.com/gcm/send';
43
 
44
		$fields = array(
45
		    'registration_ids' => $registatoin_ids,
46
		    'data' => $msg,
47
		);
48
		$headers = array(
49
		    'Authorization: key=' . Configure::read('googleapikey'),
50
		    'Content-Type: application/json'
51
		);
15311 anikendra 52
		$this->log(print_r($msg,1),'pushnotifications');
15262 anikendra 53
		// Open connection
54
		$ch = curl_init();
55
 
56
		// Set the url, number of POST vars, POST data
57
		curl_setopt($ch, CURLOPT_URL, $url);
58
		curl_setopt($ch, CURLOPT_POST, true);
59
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
60
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
61
 
62
		// Disabling SSL Certificate support temporarly
63
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
64
 
65
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
66
 
67
		// Execute post
68
		$result = curl_exec($ch);
69
		if ($result === FALSE) {
70
		    die('Curl failed: ' . curl_error($ch));
71
		}
72
		$res = json_decode($result,1);
73
		// $data = array('notification_campaign_id'=>$message['id'],'user_id'=>$user_id,'status'=>$res['success']);
74
		// $this->Pushnotification->create();
75
		// $this->Pushnotification->save($data);
76
		// Close connection
77
		curl_close($ch);
78
		$this->log("For $user_id ".$registatoin_ids[0]." ".print_r($result,1),'pushnotifications');
15410 manas 79
		if($res['success']==1){
80
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>'success');
81
			$this->Pushnotification->save($data);
82
		}else if($res['success']==0){
83
			$message=$res['results'][0]['error'];
84
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>$message);
85
			$sqlQuery="update gcm_users set failurecount=failurecount+1 where gcm_regid='".$registatoin_ids[0]."'";
86
			$resultData=$this->GcmUser->query($sqlQuery);
87
			$this->Pushnotification->save($data);
88
		}
15262 anikendra 89
		return $res['success'];
90
	}
91
 
92
	private function generateAffiliateUrl($url,$user_id,$store){
93
		//Get StoreProduct Info		
94
		$storeId = $store['Store']['id'];
95
		$prefix = "SHA".$storeId;
96
		$tag = $prefix.time();
97
		if($storeId == 2){				
98
			$url = str_replace('www','m',$url);
99
		} elseif($storeId == 3) {
100
			$url_parts = parse_url($url);
101
			$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
102
			if(isset($url_parts['query'])) {
103
				$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
104
			}else{
105
				$url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
106
			}
107
		} elseif($storeId == 4){
108
			$next = str_replace('www','m',$url);		
109
			$url = $this->getAutoLoginUrl($userId,$next);
110
			$url .= '?utm_source=profitmandi';
15311 anikendra 111
		} else {
112
			$url .= "?user_id=$user_id";
15262 anikendra 113
		}
114
		if( strpos($url, '?') === false ) {
115
			$firstChar = '?';
116
		} else {
117
			$firstChar = '&';
118
		}
119
		$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
120
		if(!empty($store['Store']['sub_tag_param'])){
121
			$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
122
		}
123
		$extras = array('store'=>$store['Store']['name'],'source'=>'notification');
124
		$data = array('user_id' => $user_id,'store_product_id'=>0,'tag'=>$tag,'url'=>$url,'price'=>0,'extras'=>json_encode($extras));
125
		// $this->loadModel('Click');
126
		$this->Click->create();
127
		$this->Click->save($data);
128
		return $url;
129
	}
130
 
131
	private function getAutoLoginUrl($userId,$next) {
132
		$saholicoffline = Configure::read('saholicoffline');
133
		if($saholicoffline) {
134
			$url = "/abouts/saholicoffline";
135
			return $url;
136
		}		
137
		$this->User->Behaviors->attach('Containable');
138
		$options = array('contain'=>array('UserAccount'), 'conditions'=>array('User.id'=>$userId),'fields'=>array('username','email'),'recursive'=>-1);
139
		$user = $this->User->find('first',$options);
140
		$data = array('email'=>$user['User']['email'],'Id'=>$user['UserAccount'][0]['account_key'],'cartId' => $user['UserAccount'][1]['account_key'],'isPrivateDealUser'=>1,'next'=>$next);
141
		$data = '?data='.base64_encode(serialize($data));
142
		$token = '&token='.md5(Configure::read('saholicapikey').'|'.$user['UserAccount'][0]['account_key']);		
143
		return Configure::read('saholicauthurl').$data.$token;
144
	}
145
}