Subversion Repositories SmartDukaan

Rev

Rev 15263 | Go to most recent revision | Details | 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) {
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
		);
53
		//print_r($headers);
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');
80
		return $res['success'];
81
	}
82
 
83
	private function generateAffiliateUrl($url,$user_id,$store){
84
		//Get StoreProduct Info		
85
		$storeId = $store['Store']['id'];
86
		$prefix = "SHA".$storeId;
87
		$tag = $prefix.time();
88
		if($storeId == 2){				
89
			$url = str_replace('www','m',$url);
90
		} elseif($storeId == 3) {
91
			$url_parts = parse_url($url);
92
			$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
93
			if(isset($url_parts['query'])) {
94
				$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
95
			}else{
96
				$url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
97
			}
98
		} elseif($storeId == 4){
99
			$next = str_replace('www','m',$url);		
100
			$url = $this->getAutoLoginUrl($userId,$next);
101
			$url .= '?utm_source=profitmandi';
102
		}
103
		if( strpos($url, '?') === false ) {
104
			$firstChar = '?';
105
		} else {
106
			$firstChar = '&';
107
		}
108
		$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
109
		if(!empty($store['Store']['sub_tag_param'])){
110
			$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
111
		}
112
		$extras = array('store'=>$store['Store']['name'],'source'=>'notification');
113
		$data = array('user_id' => $user_id,'store_product_id'=>0,'tag'=>$tag,'url'=>$url,'price'=>0,'extras'=>json_encode($extras));
114
		// $this->loadModel('Click');
115
		$this->Click->create();
116
		$this->Click->save($data);
117
		return $url;
118
	}
119
 
120
	private function getAutoLoginUrl($userId,$next) {
121
		$saholicoffline = Configure::read('saholicoffline');
122
		if($saholicoffline) {
123
			$url = "/abouts/saholicoffline";
124
			return $url;
125
		}		
126
		$this->User->Behaviors->attach('Containable');
127
		$options = array('contain'=>array('UserAccount'), 'conditions'=>array('User.id'=>$userId),'fields'=>array('username','email'),'recursive'=>-1);
128
		$user = $this->User->find('first',$options);
129
		$data = array('email'=>$user['User']['email'],'Id'=>$user['UserAccount'][0]['account_key'],'cartId' => $user['UserAccount'][1]['account_key'],'isPrivateDealUser'=>1,'next'=>$next);
130
		$data = '?data='.base64_encode(serialize($data));
131
		$token = '&token='.md5(Configure::read('saholicapikey').'|'.$user['UserAccount'][0]['account_key']);		
132
		return Configure::read('saholicauthurl').$data.$token;
133
	}
134
}