Subversion Repositories SmartDukaan

Rev

Rev 16243 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14776 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Pushnotifications Controller
5
 *
6
 * @property Pushnotification $Pushnotification
7
 * @property PaginatorComponent $Paginator
8
 */
9
class PushnotificationsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
14778 anikendra 18
	public function beforeFilter() {
19
		parent::beforeFilter();
15667 anikendra 20
		$this->Auth->allow('add','generateAffiliateUrl','sendPushNotification');
14778 anikendra 21
	}
14776 anikendra 22
/**
23
 * index method
24
 *
25
 * @return void
26
 */
27
	public function index() {
28
		$this->Pushnotification->recursive = 0;
29
		$this->set('pushnotifications', $this->Paginator->paginate());
30
	}
31
 
32
/**
33
 * view method
34
 *
35
 * @throws NotFoundException
36
 * @param string $id
37
 * @return void
38
 */
39
	public function view($id = null) {
15410 manas 40
 
14776 anikendra 41
		if (!$this->Pushnotification->exists($id)) {
42
			throw new NotFoundException(__('Invalid pushnotification'));
43
		}
15410 manas 44
		$total = $this->Article->find('count');
45
		/*$options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));
46
		$this->set('pushnotification', $this->Pushnotification->find('first', $options));*/
14776 anikendra 47
	}
48
 
49
/**
50
 * add method
51
 *
52
 * @return void
53
 */
54
	public function add() {
55
		if ($this->request->is('post')) {
14778 anikendra 56
			$this->log(print_r($this->request->data,1),'pushnotifications');
14783 anikendra 57
			$data = $this->request->data;
17208 anikendra 58
			$data['pushed_by'] = 'php';
59
			$url = Configure::read('nodeurl')."/addPushNotificationByApk";//remove hardcoded value
60
			$response = $this->post_request($url,$data);
61
			$this->log(print_r($response,1),'pushnotifications');
62
			if(!empty($response)){
63
				$result = array('success' => true,'message'=>'The pushnotification has been saved.');
64
			} else {
65
				$result = array('success' => false,'message'=>'The pushnotification could not be saved. Please, try again.');
66
			}
67
			/*$data['type'] = $data['result'];
14783 anikendra 68
			$data['status'] = 1;
14786 anikendra 69
			$data['response_time'] = date('Y-m-d H:i:s',strtotime($data['timestamp']));
14783 anikendra 70
			$data['notification_campaign_id'] = $data['cid'];
71
			unset($data['result']);
72
			unset($data['cid']);
14784 anikendra 73
			unset($data['timestamp']);
14786 anikendra 74
			if(empty($data['user_id'])){
75
				unset($data['user_id']);
17208 anikendra 76
			}			
14776 anikendra 77
			$this->Pushnotification->create();
14783 anikendra 78
			if ($this->Pushnotification->save($data)) {
79
				$result = array('success' => true,'message'=>'The pushnotification has been saved.');
14776 anikendra 80
			} else {
14783 anikendra 81
				$result = array('success' => false,'message'=>'The pushnotification could not be saved. Please, try again.');
17208 anikendra 82
			}*/
14776 anikendra 83
		}
14783 anikendra 84
		$this->set(array(
85
		    'result' => $result,
86
		    '_serialize' => array('result')
87
		));
88
		$this->render('/Elements/json');
14776 anikendra 89
	}
90
 
91
/**
92
 * edit method
93
 *
94
 * @throws NotFoundException
95
 * @param string $id
96
 * @return void
97
 */
98
	public function edit($id = null) {
99
		if (!$this->Pushnotification->exists($id)) {
100
			throw new NotFoundException(__('Invalid pushnotification'));
101
		}
102
		if ($this->request->is(array('post', 'put'))) {
103
			if ($this->Pushnotification->save($this->request->data)) {
104
				$this->Session->setFlash(__('The pushnotification has been saved.'));
105
				return $this->redirect(array('action' => 'index'));
106
			} else {
107
				$this->Session->setFlash(__('The pushnotification could not be saved. Please, try again.'));
108
			}
109
		} else {
110
			$options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));
111
			$this->request->data = $this->Pushnotification->find('first', $options);
112
		}
113
		$users = $this->Pushnotification->User->find('list');
114
		$this->set(compact('users'));
115
	}
116
 
117
/**
118
 * delete method
119
 *
120
 * @throws NotFoundException
121
 * @param string $id
122
 * @return void
123
 */
124
	public function delete($id = null) {
125
		$this->Pushnotification->id = $id;
126
		if (!$this->Pushnotification->exists()) {
127
			throw new NotFoundException(__('Invalid pushnotification'));
128
		}
129
		$this->request->onlyAllow('post', 'delete');
130
		if ($this->Pushnotification->delete()) {
131
			$this->Session->setFlash(__('The pushnotification has been deleted.'));
132
		} else {
133
			$this->Session->setFlash(__('The pushnotification could not be deleted. Please, try again.'));
134
		}
135
		return $this->redirect(array('action' => 'index'));
136
	}
137
 
138
/**
139
 * admin_index method
140
 *
141
 * @return void
142
 */
143
	public function admin_index() {
144
		$this->Pushnotification->recursive = 0;
145
		$this->set('pushnotifications', $this->Paginator->paginate());
146
	}
147
 
148
/**
149
 * admin_view method
150
 *
151
 * @throws NotFoundException
152
 * @param string $id
153
 * @return void
154
 */
155
	public function admin_view($id = null) {
156
		if (!$this->Pushnotification->exists($id)) {
157
			throw new NotFoundException(__('Invalid pushnotification'));
158
		}
15410 manas 159
		$total = $this->Article->find('count');
160
/*		$options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));
161
		$this->set('pushnotification', $this->Pushnotification->find('first', $options));*/
14776 anikendra 162
	}
163
 
164
/**
165
 * admin_add method
166
 *
167
 * @return void
168
 */
169
	public function admin_add() {
170
		if ($this->request->is('post')) {
171
			$this->Pushnotification->create();
172
			if ($this->Pushnotification->save($this->request->data)) {
173
				$this->Session->setFlash(__('The pushnotification has been saved.'));
174
				return $this->redirect(array('action' => 'index'));
175
			} else {
176
				$this->Session->setFlash(__('The pushnotification could not be saved. Please, try again.'));
177
			}
178
		}
179
		$users = $this->Pushnotification->User->find('list');
180
		$this->set(compact('users'));
181
	}
182
 
183
/**
184
 * admin_edit method
185
 *
186
 * @throws NotFoundException
187
 * @param string $id
188
 * @return void
189
 */
190
	public function admin_edit($id = null) {
191
		if (!$this->Pushnotification->exists($id)) {
192
			throw new NotFoundException(__('Invalid pushnotification'));
193
		}
194
		if ($this->request->is(array('post', 'put'))) {
195
			if ($this->Pushnotification->save($this->request->data)) {
196
				$this->Session->setFlash(__('The pushnotification has been saved.'));
197
				return $this->redirect(array('action' => 'index'));
198
			} else {
199
				$this->Session->setFlash(__('The pushnotification could not be saved. Please, try again.'));
200
			}
201
		} else {
202
			$options = array('conditions' => array('Pushnotification.' . $this->Pushnotification->primaryKey => $id));
203
			$this->request->data = $this->Pushnotification->find('first', $options);
204
		}
205
		$users = $this->Pushnotification->User->find('list');
206
		$this->set(compact('users'));
207
	}
208
 
209
/**
210
 * admin_delete method
211
 *
212
 * @throws NotFoundException
213
 * @param string $id
214
 * @return void
215
 */
216
	public function admin_delete($id = null) {
217
		$this->Pushnotification->id = $id;
218
		if (!$this->Pushnotification->exists()) {
219
			throw new NotFoundException(__('Invalid pushnotification'));
220
		}
221
		$this->request->onlyAllow('post', 'delete');
222
		if ($this->Pushnotification->delete()) {
223
			$this->Session->setFlash(__('The pushnotification has been deleted.'));
224
		} else {
225
			$this->Session->setFlash(__('The pushnotification could not be deleted. Please, try again.'));
226
		}
227
		return $this->redirect(array('action' => 'index'));
15667 anikendra 228
	}
229
 
230
	public function generateAffiliateUrl(){
231
		$url = $this->request->data['url'];
232
		$user_id = $this->request->data['userId'];
233
		$storeId = $this->request->data['storeId'];
234
		//Get StoreProduct Info		
235
		$cachekey = 'store-'.$storeId;
236
		$store = Cache::read($cachekey,'month');
237
		if(empty($store)) {
238
			$this->loadModel('Store');
239
			$store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeId)));
240
			Cache::write($cachekey,$store,'month');			
241
		}
242
		$prefix = "SHA".$storeId;
243
		$tag = $prefix.time();
244
		if($storeId == 2){				
245
			$url = str_replace('www','m',$url);
246
		} elseif($storeId == 3) {
247
			$url_parts = parse_url($url);
248
			$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
249
			if(isset($url_parts['query'])) {
250
				$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
251
			}else{
252
				$url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
253
			}
254
		} elseif($storeId == 4){
255
			$next = str_replace('www','m',$url);	
256
			$url = $this->getAutoLoginUrl($userId,$next);
257
			$url .= '?utm_source=profitmandi';
16243 anikendra 258
		} else {
259
			$url .= "?user_id=$user_id";
15667 anikendra 260
		}
261
		if( strpos($url, '?') === false ) {
262
			$firstChar = '?';
263
		} else {
264
			$firstChar = '&';
265
		}
266
		$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
267
		if(!empty($store['Store']['sub_tag_param'])){
268
			$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
269
		}
270
		// $extras = array('store'=>$store['Store']['name'],'source'=>'notification');
271
		// $data = array('user_id' => $user_id,'store_product_id'=>0,'tag'=>$tag,'url'=>$url,'price'=>0,'extras'=>json_encode($extras));
272
		// $this->loadModel('Click');
273
		// $this->Click->create();
274
		// $this->Click->save($data);
275
		// return $url;
276
		$result = array('url' => $url );
277
		$this->layout = "ajax";
278
        $this->response->type('json');
279
        $this->set(array(
280
            'result' => $result,
281
            '_serialize' => array('result')
282
        ));
283
        $this->render('/Elements/json');
284
	}
285
 
286
	public function sendPushNotification() {
287
		$gcm_id = $this->request->data['gcmId'];
288
	 	$user_id =  $this->request->data['userId'];	 	
289
	 	$cid =  $this->request->data['cid'];
290
	 	$title =  $this->request->data['title'];
291
	 	$message =  $this->request->data['message'];
292
	 	$type =  $this->request->data['type'];
293
	 	$url =  $this->request->data['url'];
294
		session_write_close();
295
    	Configure::load('live');    	
296
		$msg = array(
297
		    'message'       => $message,
298
		    'cid'       	=> $cid,
299
		    'title'         => $title,
300
		    'type'      	=> $type,
301
		    'url'		    => $url,
302
		    'vibrate'       => 1,
303
		    'sound'         => 1,
304
		    'largeIcon'     => 'large_icon',
305
		    'smallIcon'     => 'small_icon'
306
		);
307
		// Set POST variables
308
		$url = 'https://android.googleapis.com/gcm/send';
309
 
310
		$registration_ids = array($gcm_id);
311
		$fields = array(
312
		    'registration_ids' => $registration_ids,
313
		    'data' => $msg,
314
		);
315
		$headers = array(
316
		    'Authorization: key=' . Configure::read('googleapikey'),
317
		    'Content-Type: application/json'
318
		);
319
		$this->log(print_r($msg,1),'pushnotifications');
320
		// Open connection
321
		$ch = curl_init();
322
 
323
		// Set the url, number of POST vars, POST data
324
		curl_setopt($ch, CURLOPT_URL, $url);
325
		curl_setopt($ch, CURLOPT_POST, true);
326
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
327
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
328
 
329
		// Disabling SSL Certificate support temporarly
330
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
331
 
332
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
333
 
334
		// Execute post
335
		$result = curl_exec($ch);
336
		if ($result === FALSE) {
337
		    die('Curl failed: ' . curl_error($ch));
338
		}
339
		$result = json_decode($result,1);
340
 
341
		// Close connection
342
		curl_close($ch);
343
		$this->log("For $user_id ".$registatoin_ids[0]." ".print_r($result,1),'pushnotifications');
344
		if($res['success']==1){
345
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>'success');
346
			$this->Pushnotification->save($data);
347
		}else if($res['success']==0){
348
			$message=$res['results'][0]['error'];
349
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>$message);
350
			$sqlQuery="update gcm_users set failurecount=failurecount+1 where gcm_regid='".$registration_ids[0]."'";
351
			$resultData=$this->GcmUser->query($sqlQuery);
352
			$this->Pushnotification->save($data);
353
		}
354
		$this->layout = "ajax";
355
        $this->response->type('json');
356
        $this->set(array(
357
            'result' => $result,
358
            '_serialize' => array('result')
359
        ));
360
        $this->render('/Elements/json');
361
	}
362
}