Subversion Repositories SmartDukaan

Rev

Rev 19121 | Rev 20284 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14954 manas 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * NotificationCampaigns Controller
5
 *
6
 * @property NotificationCampaign $NotificationCampaign
7
 * @property PaginatorComponent $Paginator
8
 */
9
class NotificationCampaignsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
14964 manas 18
	public function beforeFilter() {		
19
		parent::beforeFilter();
20
		$this->Auth->allow('notificationactive');
21
		$this->apihost = Configure::read('pythonapihost');
22
	}
14954 manas 23
 
24
/**
25
 * admin_index method
26
 *
27
 * @return void
28
 */
29
	public function admin_index() {
19084 naman 30
 
31
		$page = $this->request->query('page');
32
		if(!isset($page)){
33
			$page = 1;
34
		}
35
		$limit = 20;
36
		$offset = ($page - 1)*$limit;
37
		$url = Configure::read('nodeurl')."/getLatestNotificationCampaigns/?notification_type=GENERAL_NOTIFICATION&offset=".$offset."&limit=".$limit;
38
// 		echo $url;
39
		$response = $this->make_request($url,null);
40
		$NotificationCampaign = array();
41
		$newNotification = array();
15581 manas 42
		$checkArray=array();
17212 naman 43
		$id_set = '';
19084 naman 44
 
45
			foreach ($response['result'] as $key => $value) {
17212 naman 46
				if($id_set == '')
47
				{
19084 naman 48
					$id_set = $value['_id'];
17212 naman 49
				}
50
				else
51
				{
19084 naman 52
					$id_set = $id_set.','.$value['_id'];
17212 naman 53
				}
19084 naman 54
				$newNotification[$key]["NotificationCampaign"] = $value;
55
				$newNotification[$key]["NotificationCampaign"]["id"] = $value["_id"];
56
				$newNotification[$key]["NotificationCampaign"]["expiresat"] = date('Y-m-d H:i:s', $value["expiresat"]/1000);
57
				$newNotification[$key]["NotificationCampaign"]["created"] = date('Y-m-d H:i:s', $value["created"]/1000);
58
// 				unset($value["_id"]);
17212 naman 59
			}
19084 naman 60
		$url = Configure::read('nodeurl')."/getPushNotificationsStats/?campaigns=".$id_set;
61
// 		echo $url;
62
		$result = $this->make_request($url,null);
63
// 		echo "Before Modification";
64
// 		debug($result);
65
 
66
// 		Logic for new result start
67
 
68
		foreach ($result as $notkey =>$notval){
17212 naman 69
 
19084 naman 70
				$total = 0;
71
				$total_open = 0;
72
				$total_rec = 0;
73
				$success = 0;
74
				if(isset($notval["recieved"]))
75
				{
76
					$total = $notval["recieved"] + $total;
77
					$total_rec = $notval["recieved"];
78
				}
79
 
80
				if(isset($notval["expired"]))
81
				{
82
					$total = $notval["expired"] + $total;
83
				}
84
 
85
				if(isset($notval["opened"]))
86
				{
87
					$total = $notval["opened"] + $total;
88
					$total_rec = $total_rec + $notval["opened"];
89
					$total_open = $notval["opened"];
90
				}
91
 
92
				if(isset($notval["failed"]))
93
				{
94
					$total = $notval["failed"] + $total;
95
				}
96
 
97
				if(isset($notval["sent"]))
98
				{
99
					$total = $notval["sent"] + $total;
100
				}
101
 
102
				$success = $total;
103
 
104
				if(isset($notval["failed"]))
105
				{
106
					$success = $total - $notval["failed"] ;
107
				}
108
 
109
				if(isset($notval["referrer"]))
110
				{
111
					$total_open = $notval["referrer"] + $total_open;
112
				}
113
 
114
				if(isset($notval["login"]))
115
				{
116
					$total_open = $notval["login"] + $total_open;
117
				}
118
 
119
				$result[$notkey]["opened"] = $total_open;
120
				$result[$notkey]["recieved"] = $total_rec;
121
				$result[$notkey]["total"] = $total;
122
				$result[$notkey]["success"] = $success;
123
 
124
 
125
// 			if(isset($notval["recieved"])&& isset($notval["opened"])){
126
// 				$result[$notkey]["recieved"] = $notval["opened"] + $notval["recieved"];
127
 
128
// 			}
129
 
130
// 			if(isset($notval["opened"]))
131
// 			{
132
// 				$total_open = $notval["opened"];
133
// 				if(isset($notval["referrer"]))
134
// 				{
135
// 					$total_open = $notval["referrer"] + $total_open;
136
// 				}
137
 
138
// 				if(isset($notval["login"]))
139
// 				{
140
// 					$total_open = $notval["login"] + $total_open;
141
// 				}
142
// 				$result[$notkey]["opened"] = $total_open;
143
// 			}
144
 
145
// 			$result[$notkey]["opened"] = $notval["login"] + $notval["expired"] + $notval["opened"] +$notval['recieved'];
146
 
17212 naman 147
		}
19084 naman 148
// 		echo "After Modification";
149
// 		debug($result);
150
 
151
// 		Logic end
152
 
17212 naman 153
		// echo "Idset", $id_set;
154
		// debug($result);
155
		// $campaign_array = array();
156
		// foreach ($vari as $key => $value) {
157
		// 	echo $value['NotificationCampaign']['id'];
158
 
159
		// }
160
		/*
14954 manas 161
		foreach ($vari as $key => $value) {
162
			$cid=$value['NotificationCampaign']['id'];
163
			$sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications group by notification_campaign_id, type,status";
15581 manas 164
			#$successQuery="SELECT count(*) as count from pushnotifications where status=1 and type='sent' and notification_campaign_id=$cid";
165
			$successQuery="SELECT count(distinct p.user_id) as count from pushnotifications p join devices d on p.user_id=d.user_id where status=1 and type='sent' and notification_campaign_id='$cid'  and d.versioncode>=14";
14954 manas 166
			$resul=$this->NotificationCampaign->query($sqlQuery);	
15581 manas 167
			$success_result=$this->NotificationCampaign->query($successQuery);	
168
			array_push($checkArray, $success_result[0][0]['count']);
14954 manas 169
		}
17212 naman 170
		// debug($checkArray);
14954 manas 171
		$finalResult = array();
172
		foreach ($resul as $key => $value) {
173
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
174
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
175
			//$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];			
176
		}
17212 naman 177
		// debug($finalResult);
178
		*/
179
		// $this->set('notificationWithVersionCode', $checkArray);
19084 naman 180
		$this->set('notificationCampaigns', $newNotification);
17212 naman 181
		//$this->set('notificationCampaignsCount', $finalResult);
19084 naman 182
		$this->set(compact('result','page'));
14954 manas 183
	}
184
 
19084 naman 185
	public function admin_batchcredit() {
186
 
187
		$page = $this->request->query('page');
188
		if(!isset($page)){
189
			$page = 1;
190
		}
191
		$limit = 20;
192
		$offset = ($page - 1)*$limit;
193
		$url = Configure::read('nodeurl')."/getLatestNotificationCampaigns/?notification_type=BATCH_CREDIT&offset=".$offset."&limit=".$limit;
194
		$response = $this->make_request($url,null);
195
		$NotificationCampaign = array();
196
		$newNotification = array();
197
		$checkArray=array();
198
		$id_set = '';
199
 
200
				foreach ($response['result'] as $key => $value) {
201
					if($id_set == '')
202
					{
203
						$id_set = $value['_id'];
204
					}
205
					else
206
					{
207
						$id_set = $id_set.','.$value['_id'];
208
					}
209
					$newNotification[$key]["NotificationCampaign"] = $value;
210
					$newNotification[$key]["NotificationCampaign"]["id"] = $value["_id"];
211
					$newNotification[$key]["NotificationCampaign"]["expiresat"] = date('Y-m-d H:i:s', $value["expiresat"]/1000);
212
					$newNotification[$key]["NotificationCampaign"]["created"] = date('Y-m-d H:i:s', $value["created"]/1000);
213
				}
214
 
215
				$url = Configure::read('nodeurl')."/getPushNotificationsStats/?campaigns=".$id_set;
216
				$result = $this->make_request($url,null);
217
 
218
				// 		Logic for new result start
219
 
220
				foreach ($result as $notkey =>$notval){
221
 
222
					$total = 0;
223
					$total_open = 0;
224
					$total_rec = 0;
225
					$success = 0;
226
					if(isset($notval["recieved"]))
227
					{
228
						$total = $notval["recieved"] + $total;
229
						$total_rec = $notval["recieved"];
230
					}
231
 
232
					if(isset($notval["expired"]))
233
					{
234
						$total = $notval["expired"] + $total;
235
					}
236
 
237
					if(isset($notval["opened"]))
238
					{
239
						$total = $notval["opened"] + $total;
240
						$total_rec = $total_rec + $notval["opened"];
241
						$total_open = $notval["opened"];
242
					}
243
 
244
					if(isset($notval["failed"]))
245
					{
246
						$total = $notval["failed"] + $total;
247
					}
248
 
249
					if(isset($notval["sent"]))
250
					{
251
						$total = $notval["sent"] + $total;
252
					}
253
 
254
					$success = $total;
255
 
256
					if(isset($notval["failed"]))
257
					{
258
						$success = $total - $notval["failed"] ;
259
					}
260
 
261
					if(isset($notval["referrer"]))
262
					{
263
						$total_open = $notval["referrer"] + $total_open;
264
					}
265
 
266
					if(isset($notval["login"]))
267
					{
268
						$total_open = $notval["login"] + $total_open;
269
					}
270
 
271
					$result[$notkey]["opened"] = $total_open;
272
					$result[$notkey]["recieved"] = $total_rec;
273
					$result[$notkey]["total"] = $total;
274
					$result[$notkey]["success"] = $success;					
275
				}					
276
				// 		Logic end
277
 
278
				$this->set('notificationCampaigns', $newNotification);
279
				//$this->set('notificationCampaignsCount', $finalResult);
280
				$this->set(compact('result','page'));
281
		}
282
 
283
 
284
 
14954 manas 285
/**
286
 * admin_view method
287
 *
288
 * @throws NotFoundException
289
 * @param string $id
290
 * @return void
291
 */
292
	public function admin_view($id = null) {
19084 naman 293
		$url = Configure::read('nodeurl')."/getNotificationCampaignById?id=".$id;
294
		$result = $this->make_request($url,null);
295
		$getdata = array();
296
		$getdata["NotificationCampaign"] = $result;
297
		$getdata["NotificationCampaign"]["expiresat"] = date('Y-m-d H:i:s', $result["expiresat"]/1000);
19274 manas 298
		$getdata["NotificationCampaign"]["created"] = date('Y-m-d H:i:s', $result["created"]/1000);
19084 naman 299
		unset($getdata["NotificationCampaign"]["_id"]);
300
//		debug($getdata);
301
 
302
// 		if (!$this->NotificationCampaign->exists($id)) {
303
// 			throw new NotFoundException(__('Invalid notification campaign'));
304
// 		}
305
// 		$sort = $this->request->query('type');
306
// 		$direction = $this->request->query('order');
307
// 		$options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
308
// 		$userData=$this->NotificationCampaign->find('first', $options);
309
// 		debug($userData);
17212 naman 310
		// $userActions = array();
311
		// foreach ($userData['Pushnotification'] as $key => $value) {
312
		// 	if($value['type']=='sent'){
313
		// 	$userActions[$value['user_id']][$value['type']] = $value['created'];
314
		// 	$userActions[$value['user_id']]['status'] =$value['status'];
315
		// 	}else{
316
		// 		$userActions[$value['user_id']][$value['type']] = $value['response_time'];
317
		// 	}
318
		// 	 //debug($value);
319
		// }
320
 
14954 manas 321
		$userActions = array();
17212 naman 322
		$url = Configure::read('nodeurl')."/getPushNotificationByCampaignId/?notification_campaign_id=".$id;
323
		// echo $url;
324
		$result = $this->make_request($url,null);
325
		foreach ($result as $key => $value) {
19274 manas 326
			/*if($value['type']=='sent'){
17222 naman 327
				$epoch = $value['created']/1000;
17212 naman 328
				$dt = date('Y-m-d H:i:s', $epoch);
329
				// echo $dt->format('Y-m-d H:i:s');
330
				$userActions[$value['user_id']][$value['type']] = $dt;//$value['created'];
331
				$userActions[$value['user_id']]['status'] =$value['status'];
17349 manish.sha 332
			}else if(isset($value['response_time'])){
17222 naman 333
				$epoch = $value['response_time']/1000;
17212 naman 334
				$dt = date('Y-m-d H:i:s', $epoch);
18174 manish.sha 335
				if(isset($value['user_id'])){
336
					$userActions[$value['user_id']][$value['type']] = $dt;//$value['response_time'];
337
				}
17349 manish.sha 338
			}else{
339
				$epoch = $value['created']/1000;
340
				$dt = date('Y-m-d H:i:s', $epoch);
18174 manish.sha 341
				if(isset($value['user_id'])){
342
					$userActions[$value['user_id']][$value['type']] = $dt;//$value['response_time'];
343
				}
19274 manas 344
			}*/
345
			if($value['sent_timestamp']!=null){
346
				$epoch = $value['sent_timestamp']/1000;
347
				$dt = date('Y-m-d H:i:s', $epoch);
348
				$userActions[$value['user_id'].'_'.$value['_id']]['sent'] = $dt;
14954 manas 349
			}
19274 manas 350
			if($value['receive_timestamp']!=null){
351
				$epoch = $value['receive_timestamp']/1000;
352
				$dt = date('Y-m-d H:i:s', $epoch);
353
				$userActions[$value['user_id'].'_'.$value['_id']]['recieved'] = $dt;
354
			}
355
			if($value['open_timestamp']!=null){
356
				$epoch = $value['open_timestamp']/1000;			
357
				$dt = date('Y-m-d H:i:s', $epoch);
358
				$userActions[$value['user_id'].'_'.$value['_id']]['opened'] = $dt;
359
			}		
14954 manas 360
		}
17212 naman 361
 
19084 naman 362
// 		$cid=$userData['NotificationCampaign']['id'];
363
// 		$sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications where notification_campaign_id=4120 group by type,status";
364
// 		$resul=$this->NotificationCampaign->query($sqlQuery);	
365
// 		debug($resul);
366
// 		$finalResult = array();	
367
// 		foreach ($resul as $key => $value) {
368
// 			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
369
// 			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
370
// 			//$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];			
371
// 		}
17212 naman 372
 
19084 naman 373
		$this->set('notificationData', $getdata);
14954 manas 374
		$this->set('notificationCampaign', $userActions);
19084 naman 375
// 		$this->set('notificationCount', $finalResult);
14954 manas 376
	}
377
 
378
/**
379
 * admin_add method
380
 *
381
 * @return void
382
 */
383
	public function admin_add() {
384
		if ($this->request->is('post')) {
385
			$this->NotificationCampaign->create();
386
			if ($this->NotificationCampaign->save($this->request->data)) {
387
				$this->Session->setFlash(__('The notification campaign has been saved.'));
388
				return $this->redirect(array('action' => 'index'));
389
			} else {
390
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
391
			}
392
		}
393
	}
394
 
395
/**
396
 * admin_edit method
397
 *
398
 * @throws NotFoundException
399
 * @param string $id
400
 * @return void
401
 */
402
	public function admin_edit($id = null) {
19084 naman 403
// 		if (!$this->NotificationCampaign->exists($id)) {
404
// 			throw new NotFoundException(__('Invalid notification campaign'));
405
// 		}
14954 manas 406
		if ($this->request->is(array('post', 'put'))) {
19084 naman 407
			$message= $this->request->data['NotificationCampaign'];
408
			if($message['expiresat']['meridian']=='pm'){
409
				$message['expiresat'] = 1000*mktime(($message['expiresat']['hour'])+12,$message['expiresat']['min'],0,$message['expiresat']['month'],$message['expiresat']['day'],$message['expiresat']['year']);
410
			}else{
411
				$message['expiresat'] = 1000*mktime($message['expiresat']['hour'],$message['expiresat']['min'],0,$message['expiresat']['month'],$message['expiresat']['day'],$message['expiresat']['year']);
412
			}
413
// 			$senddata = '{"id":"56d80b7514fb6c4a5183a74a","name":"Naman","title":"Naman","message":"Naman","type":"url","url":"http:\/\/api.profittill.com","expiresat":1344988800000,"status":"active","sendsms":0,"messagetext":"","smsprocessed":0}';
414
//			debug($message);
415
			$url = Configure::read('nodeurl')."/updatedNotificationCampaign";//remove hardcoded value
416
			$debres = $this->make_request($url,json_encode($message,JSON_NUMERIC_CHECK));
417
// 			debug($debres);
418
 
419
// 			if ($this->NotificationCampaign->save($this->request->data)) {
420
			if($debres['message'] == 'Success'){
14954 manas 421
				$this->Session->setFlash(__('The notification campaign has been saved.'));
422
				return $this->redirect(array('action' => 'index'));
423
			} else {
424
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
425
			}
426
		} else {
19084 naman 427
// 			$options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
428
// 			$this->request->data = $this->NotificationCampaign->find('first', $options);
429
			$url = Configure::read('nodeurl')."/getNotificationCampaignById?id=".$id;
430
			$result = $this->make_request($url,null);			
431
			$getdata = array();
432
			$getdata["NotificationCampaign"] = $result;
433
			$getdata["NotificationCampaign"]["expiresat"] = date('Y-m-d H:i:s', $result["expiresat"]/1000);
434
			$getdata["NotificationCampaign"]["id"] =  $result["_id"];
435
			unset($getdata["NotificationCampaign"]["_id"]);
436
			$this->request->data = $getdata;
14954 manas 437
		}
438
	}
439
 
440
/**
441
 * admin_delete method
442
 *
443
 * @throws NotFoundException
444
 * @param string $id
445
 * @return void
446
 */
447
	public function admin_delete($id = null) {
448
		$this->NotificationCampaign->id = $id;
449
		if (!$this->NotificationCampaign->exists()) {
450
			throw new NotFoundException(__('Invalid notification campaign'));
451
		}
452
		$this->request->onlyAllow('post', 'delete');
453
		if ($this->NotificationCampaign->delete()) {
454
			$this->Session->setFlash(__('The notification campaign has been deleted.'));
455
		} else {
456
			$this->Session->setFlash(__('The notification campaign could not be deleted. Please, try again.'));
457
		}
458
		return $this->redirect(array('action' => 'index'));
459
	}
17212 naman 460
	// $this->set(compact('result'));
461
	// 
14954 manas 462
	public function admin_show($id = null,$type=null,$status=null) {
17212 naman 463
		// $sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id=$id and type='$type' and status=$status" ;
14954 manas 464
 
17212 naman 465
		// $resul=$this->NotificationCampaign->query($sqlQuery1ry);
17220 naman 466
		// if($type == 0 || $type == 1)
467
		// {
468
		// 	$type = "sent";
469
		// }
470
		// echo "type", $type;
19084 naman 471
 		$url = Configure::read('nodeurl')."/getPushNotificationDetailsByType/?type=".$type."&notification_campaign_id=".$id."&status=".$status;//remove hardcoded value
472
 
473
 		$result = $this->make_request($url,null);
17220 naman 474
		// debug($result);
19084 naman 475
// 		$sqlQuery1 = "SELECT * FROM notification_campaigns where id=$id" ;
476
// 		$resultData=$this->NotificationCampaign->query($sqlQuery1);
477
// 		debug($resultData);
478
		$url = Configure::read('nodeurl')."/getNotificationCampaignById?id=".$id;
479
		$res = $this->make_request($url,null);
480
		debug($res);
481
		$getdata = array();
482
		if(!empty($res)){
483
			$getdata["NotificationCampaign"] = $res;
484
			$getdata["NotificationCampaign"]["expiresat"] = date('Y-m-d H:i:s', $res["expiresat"]/1000);
485
			$getdata["NotificationCampaign"]["id"] =  $res["_id"];
486
			unset($getdata["NotificationCampaign"]["_id"]);
487
		}
488
 
489
 
490
 
17212 naman 491
 
492
		$this->set(compact('result'));
493
		// $this->set('data', $result);
19084 naman 494
		$this->set('notificationData', $getdata);
14954 manas 495
	}
496
 
497
	public function admin_user($id = null) {
17225 naman 498
		$url = Configure::read('nodeurl')."/getPushNotificationByUserId/?user_id=".$id;
499
		$userdata = $this->make_request($url,null);
500
		// debug($userdata);
501
		$this->set(compact('userdata'));
502
		// $sqlQuery = "SELECT * FROM pushnotifications where user_id=$id order by id desc" ;
503
		// $resul=$this->NotificationCampaign->query($sqlQuery);
504
		// $this->set('userdata', $resul);
14954 manas 505
	}
15261 anikendra 506
 
14954 manas 507
	public function admin_sort($id = null) {
508
		$sort = $this->request->query('type');
509
		$direction = $this->request->query('order');
510
		//$cid = $this->request->query('cid');
511
		if($sort=='user_id'){
512
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by $sort $direction" ;			
513
		}
514
		else{
515
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by type='$sort' $direction" ;
516
		}
517
		$resul=$this->NotificationCampaign->query($sqlQuery);
518
		$userActions = array();
519
		foreach ($resul as $key => $value) {
520
			if($value['pushnotifications']['type']=='sent'){
521
			$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['created'];
522
			$userActions[$value['pushnotifications']['user_id']]['status'] =$value['pushnotifications']['status'];
523
			}else{
524
				$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['response_time'];
525
			}			
526
		}
527
		$this->set('notificationId',$id);
528
		$this->set('sortdata', $userActions);
529
	}
530
 
14964 manas 531
	public function notificationactive(){
15669 anikendra 532
		session_write_close();
14964 manas 533
		$cid = $this->request->query('cid');
19121 manas 534
		/*$cidVals = explode("_", $cid);
18174 manish.sha 535
		$cid = $cidVals[0];
15669 anikendra 536
		$this->log("cid $cid",'notificationactive');
15767 anikendra 537
		$options = array('conditions' => array('id'=> $cid,'status'=>'active','expiresat >'=>date('Y-m-d H:i:s',time())),'recursive'=>-1);
14964 manas 538
		$count = $this->NotificationCampaign->find('count',$options);
15669 anikendra 539
		$this->log("count ".print_r($count,1),'notificationactive');
14964 manas 540
		if(!$count){
541
			$result = array('success'=>false);
542
		}else{
543
			$result = array('success'=>true);
19121 manas 544
		}*/
545
		$urlPush = Configure::read('nodeurl')."/getCampaignStatus?".$cid;
546
		$result = $this->make_request($urlPush,json_encode($message,JSON_NUMERIC_CHECK));
14964 manas 547
		$this->response->type('json');
548
		$this->layout = 'ajax';
549
		$this->set(array(
550
		    'result' => $result,
551
		    '_serialize' => array('result')
552
		));
553
		$this->render('/Elements/json');
554
	}
15261 anikendra 555
 
556
	public function admin_send(){
19084 naman 557
		if ($this->request->is('post')) {
15261 anikendra 558
			$sql = $this->request->data['NotificationCampaign']['sql'];
559
			if(!empty($sql)){
19116 naman 560
// 				$users = $this->NotificationCampaign->query($sql);
15261 anikendra 561
				$message = $this->request->data['NotificationCampaign'];
18999 manas 562
				if($message['expiresat']['meridian']=='pm'){
563
					$message['expiresat'] = 1000*mktime(($message['expiresat']['hour'])+12,$message['expiresat']['min'],0,$message['expiresat']['month'],$message['expiresat']['day'],$message['expiresat']['year']);
564
				}else{
565
					$message['expiresat'] = 1000*mktime($message['expiresat']['hour'],$message['expiresat']['min'],0,$message['expiresat']['month'],$message['expiresat']['day'],$message['expiresat']['year']);					
566
				}
567
				$message['status']='active';
568
				$message['smsprocessed']=0;
19084 naman 569
				$message['notification_processed']=0;
18999 manas 570
				$urlPush = Configure::read('nodeurl')."/addNotificationCampaign";
571
				$result = $this->make_request($urlPush,json_encode($message,JSON_NUMERIC_CHECK));
572
				if ($result['msg']=='Success'){
19084 naman 573
// 					$this->loadModel('Pushnotification');
574
// 					$sentUsers = array();
575
// 					$payload = array();
576
// 					foreach ($users as $key => $value) {	
577
// 						if(!in_array($value['User']['id'], $sentUsers)) {
578
// 							$sentUsers[] = $value['User']['id'];								
579
// 							$data = array('notification_campaign_id'=>$message['cid'],'user_id'=>$value['User']['id'],'type'=>'pending','status'=>0);
580
// 							// $this->Pushnotification->create();
581
// 							// $this->Pushnotification->save($data);
582
// 							$payload[] = $data;
583
// 						}
584
// 					}
585
// 					$url = Configure::read('nodeurl')."/addPushNotification";//remove hardcoded value
586
// 					$this->make_request($url,json_encode($payload,JSON_NUMERIC_CHECK));
587
					$this->Session->setFlash(__('Push notification scheduled for users'));
18999 manas 588
					return $this->redirect(array('controller' => 'administration', 'action' => 'dashboard', 'admin' => false));
589
				}else{
590
					$this->Session->setFlash(__('Notification Campaign Insertion Failed. '));
591
					return $this->redirect(array('controller' => 'administration', 'action' => 'dashboard', 'admin' => false));
592
				}									
15261 anikendra 593
			}
594
		}
595
	}
19274 manas 596
 
597
	public function admin_user_polling(){
598
		$page = $this->request->query('page');
599
		if(!isset($page)){
600
			$page = 1;
601
		}
602
		$limit = 50;
603
		$offset = ($page - 1)*$limit;
604
		$url = Configure::read('nodeurl').'/fetchPollUsers?limit='.$limit.'&offset='.$offset;		
605
		$response = $this->make_request( $url, null );
606
		foreach ($response as $key => $value) {			
607
			$response[$key]['updatedOn'] = date('Y-m-d H:i:s', $value["updatedOn"]/1000);
608
			$response[$key]['createdOn'] = date('Y-m-d H:i:s', $value["createdOn"]/1000);
609
		}
610
		$this->set(compact('response','page'));
611
	}
17202 anikendra 612
}