Subversion Repositories SmartDukaan

Rev

Rev 15410 | Rev 16243 | 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';
253
		}
254
		if( strpos($url, '?') === false ) {
255
			$firstChar = '?';
256
		} else {
257
			$firstChar = '&';
258
		}
259
		$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
260
		if(!empty($store['Store']['sub_tag_param'])){
261
			$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
262
		}
263
		// $extras = array('store'=>$store['Store']['name'],'source'=>'notification');
264
		// $data = array('user_id' => $user_id,'store_product_id'=>0,'tag'=>$tag,'url'=>$url,'price'=>0,'extras'=>json_encode($extras));
265
		// $this->loadModel('Click');
266
		// $this->Click->create();
267
		// $this->Click->save($data);
268
		// return $url;
269
		$result = array('url' => $url );
270
		$this->layout = "ajax";
271
        $this->response->type('json');
272
        $this->set(array(
273
            'result' => $result,
274
            '_serialize' => array('result')
275
        ));
276
        $this->render('/Elements/json');
277
	}
278
 
279
	public function sendPushNotification() {
280
		$gcm_id = $this->request->data['gcmId'];
281
	 	$user_id =  $this->request->data['userId'];	 	
282
	 	$cid =  $this->request->data['cid'];
283
	 	$title =  $this->request->data['title'];
284
	 	$message =  $this->request->data['message'];
285
	 	$type =  $this->request->data['type'];
286
	 	$url =  $this->request->data['url'];
287
		session_write_close();
288
    	Configure::load('live');    	
289
		$msg = array(
290
		    'message'       => $message,
291
		    'cid'       	=> $cid,
292
		    'title'         => $title,
293
		    'type'      	=> $type,
294
		    'url'		    => $url,
295
		    'vibrate'       => 1,
296
		    'sound'         => 1,
297
		    'largeIcon'     => 'large_icon',
298
		    'smallIcon'     => 'small_icon'
299
		);
300
		// Set POST variables
301
		$url = 'https://android.googleapis.com/gcm/send';
302
 
303
		$registration_ids = array($gcm_id);
304
		$fields = array(
305
		    'registration_ids' => $registration_ids,
306
		    'data' => $msg,
307
		);
308
		$headers = array(
309
		    'Authorization: key=' . Configure::read('googleapikey'),
310
		    'Content-Type: application/json'
311
		);
312
		$this->log(print_r($msg,1),'pushnotifications');
313
		// Open connection
314
		$ch = curl_init();
315
 
316
		// Set the url, number of POST vars, POST data
317
		curl_setopt($ch, CURLOPT_URL, $url);
318
		curl_setopt($ch, CURLOPT_POST, true);
319
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
320
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
321
 
322
		// Disabling SSL Certificate support temporarly
323
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
324
 
325
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
326
 
327
		// Execute post
328
		$result = curl_exec($ch);
329
		if ($result === FALSE) {
330
		    die('Curl failed: ' . curl_error($ch));
331
		}
332
		$result = json_decode($result,1);
333
 
334
		// Close connection
335
		curl_close($ch);
336
		$this->log("For $user_id ".$registatoin_ids[0]." ".print_r($result,1),'pushnotifications');
337
		if($res['success']==1){
338
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>'success');
339
			$this->Pushnotification->save($data);
340
		}else if($res['success']==0){
341
			$message=$res['results'][0]['error'];
342
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>$message);
343
			$sqlQuery="update gcm_users set failurecount=failurecount+1 where gcm_regid='".$registration_ids[0]."'";
344
			$resultData=$this->GcmUser->query($sqlQuery);
345
			$this->Pushnotification->save($data);
346
		}
347
		$this->layout = "ajax";
348
        $this->response->type('json');
349
        $this->set(array(
350
            'result' => $result,
351
            '_serialize' => array('result')
352
        ));
353
        $this->render('/Elements/json');
354
	}
355
}