Subversion Repositories SmartDukaan

Rev

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