Subversion Repositories SmartDukaan

Rev

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