Subversion Repositories SmartDukaan

Rev

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