Subversion Repositories SmartDukaan

Rev

Rev 17225 | Rev 18174 | 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() {
30
		$this->NotificationCampaign->recursive = 0;
17212 naman 31
		// $this->Paginator->settings = array('order' => array('id'=>'desc'),'fields'=>('NotificationCampaign.id'));
14954 manas 32
		$this->Paginator->settings = array('order' => array('id'=>'desc'));
33
		$vari = $this->Paginator->paginate();
17212 naman 34
		// debug($vari);
15581 manas 35
		$checkArray=array();
17212 naman 36
		$id_set = '';
37
		foreach ($vari as $key => $row) {
38
			foreach ($row as $key => $value) {
39
				if($id_set == '')
40
				{
41
					$id_set = $value['id'];
42
				}
43
				else
44
				{
45
					$id_set = $id_set.','.$value['id'];
46
				}
47
			}
48
 
49
		}
50
 
51
		$url = Configure::read('nodeurl')."/getPushNotificationsStats/?campaigns=".$id_set;
52
		// echo $url;
53
		$result = $this->make_request($url,null);
54
		// echo "Idset", $id_set;
55
		// debug($result);
56
		// $campaign_array = array();
57
		// foreach ($vari as $key => $value) {
58
		// 	echo $value['NotificationCampaign']['id'];
59
 
60
		// }
61
		/*
14954 manas 62
		foreach ($vari as $key => $value) {
63
			$cid=$value['NotificationCampaign']['id'];
64
			$sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications group by notification_campaign_id, type,status";
15581 manas 65
			#$successQuery="SELECT count(*) as count from pushnotifications where status=1 and type='sent' and notification_campaign_id=$cid";
66
			$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 67
			$resul=$this->NotificationCampaign->query($sqlQuery);	
15581 manas 68
			$success_result=$this->NotificationCampaign->query($successQuery);	
69
			array_push($checkArray, $success_result[0][0]['count']);
14954 manas 70
		}
17212 naman 71
		// debug($checkArray);
14954 manas 72
		$finalResult = array();
73
		foreach ($resul as $key => $value) {
74
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
75
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
76
			//$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];			
77
		}
17212 naman 78
		// debug($finalResult);
79
		*/
80
		// $this->set('notificationWithVersionCode', $checkArray);
14954 manas 81
		$this->set('notificationCampaigns', $vari);
17212 naman 82
		//$this->set('notificationCampaignsCount', $finalResult);
83
		$this->set(compact('result'));
14954 manas 84
	}
85
 
86
/**
87
 * admin_view method
88
 *
89
 * @throws NotFoundException
90
 * @param string $id
91
 * @return void
92
 */
93
	public function admin_view($id = null) {
94
		if (!$this->NotificationCampaign->exists($id)) {
95
			throw new NotFoundException(__('Invalid notification campaign'));
96
		}
97
		$sort = $this->request->query('type');
98
		$direction = $this->request->query('order');
99
		$options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
100
		$userData=$this->NotificationCampaign->find('first', $options);
17212 naman 101
		// $userActions = array();
102
		// foreach ($userData['Pushnotification'] as $key => $value) {
103
		// 	if($value['type']=='sent'){
104
		// 	$userActions[$value['user_id']][$value['type']] = $value['created'];
105
		// 	$userActions[$value['user_id']]['status'] =$value['status'];
106
		// 	}else{
107
		// 		$userActions[$value['user_id']][$value['type']] = $value['response_time'];
108
		// 	}
109
		// 	 //debug($value);
110
		// }
111
 
14954 manas 112
		$userActions = array();
17212 naman 113
		$url = Configure::read('nodeurl')."/getPushNotificationByCampaignId/?notification_campaign_id=".$id;
114
		// echo $url;
115
		$result = $this->make_request($url,null);
116
		foreach ($result as $key => $value) {
14954 manas 117
			if($value['type']=='sent'){
17224 naman 118
				// date_default_timezone_set('GMT');
17222 naman 119
				$epoch = $value['created']/1000;
17212 naman 120
				$dt = date('Y-m-d H:i:s', $epoch);
121
				// echo $dt->format('Y-m-d H:i:s');
122
				$userActions[$value['user_id']][$value['type']] = $dt;//$value['created'];
123
				$userActions[$value['user_id']]['status'] =$value['status'];
17349 manish.sha 124
			}else if(isset($value['response_time'])){
17222 naman 125
				$epoch = $value['response_time']/1000;
17224 naman 126
				// date_default_timezone_set('GMT');
17212 naman 127
				$dt = date('Y-m-d H:i:s', $epoch);
128
				$userActions[$value['user_id']][$value['type']] = $dt;//$value['response_time'];
17349 manish.sha 129
			}else{
130
				$epoch = $value['created']/1000;
131
				// date_default_timezone_set('GMT');
132
				$dt = date('Y-m-d H:i:s', $epoch);
133
				$userActions[$value['user_id']][$value['type']] = $dt;
14954 manas 134
			}
135
			 //debug($value);
136
		}
17212 naman 137
		// debug($userData);
138
		// debug($result);
139
		// debug($userActions);
140
		// debug($userAct);
141
 
142
		// $this->set(compact('result'));
143
 
14954 manas 144
		$cid=$userData['NotificationCampaign']['id'];
145
		$sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications where notification_campaign_id=$cid group by type,status";
146
		$resul=$this->NotificationCampaign->query($sqlQuery);	
17212 naman 147
		$finalResult = array();	
14954 manas 148
		foreach ($resul as $key => $value) {
149
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
150
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
151
			//$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];			
152
		}
17212 naman 153
 
14954 manas 154
		$this->set('notificationData', $userData);
155
		$this->set('notificationCampaign', $userActions);
156
		$this->set('notificationCount', $finalResult);
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->NotificationCampaign->create();
167
			if ($this->NotificationCampaign->save($this->request->data)) {
168
				$this->Session->setFlash(__('The notification campaign has been saved.'));
169
				return $this->redirect(array('action' => 'index'));
170
			} else {
171
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
172
			}
173
		}
174
	}
175
 
176
/**
177
 * admin_edit method
178
 *
179
 * @throws NotFoundException
180
 * @param string $id
181
 * @return void
182
 */
183
	public function admin_edit($id = null) {
184
		if (!$this->NotificationCampaign->exists($id)) {
185
			throw new NotFoundException(__('Invalid notification campaign'));
186
		}
187
		if ($this->request->is(array('post', 'put'))) {
188
			if ($this->NotificationCampaign->save($this->request->data)) {
189
				$this->Session->setFlash(__('The notification campaign has been saved.'));
190
				return $this->redirect(array('action' => 'index'));
191
			} else {
192
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
193
			}
194
		} else {
195
			$options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
196
			$this->request->data = $this->NotificationCampaign->find('first', $options);
197
		}
198
	}
199
 
200
/**
201
 * admin_delete method
202
 *
203
 * @throws NotFoundException
204
 * @param string $id
205
 * @return void
206
 */
207
	public function admin_delete($id = null) {
208
		$this->NotificationCampaign->id = $id;
209
		if (!$this->NotificationCampaign->exists()) {
210
			throw new NotFoundException(__('Invalid notification campaign'));
211
		}
212
		$this->request->onlyAllow('post', 'delete');
213
		if ($this->NotificationCampaign->delete()) {
214
			$this->Session->setFlash(__('The notification campaign has been deleted.'));
215
		} else {
216
			$this->Session->setFlash(__('The notification campaign could not be deleted. Please, try again.'));
217
		}
218
		return $this->redirect(array('action' => 'index'));
219
	}
17212 naman 220
	// $this->set(compact('result'));
221
	// 
14954 manas 222
	public function admin_show($id = null,$type=null,$status=null) {
17212 naman 223
		// $sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id=$id and type='$type' and status=$status" ;
14954 manas 224
 
17212 naman 225
		// $resul=$this->NotificationCampaign->query($sqlQuery1ry);
17220 naman 226
		// if($type == 0 || $type == 1)
227
		// {
228
		// 	$type = "sent";
229
		// }
230
		// echo "type", $type;
17212 naman 231
		$url = Configure::read('nodeurl')."/getPushNotificationDetailsByType/?type=".$type."&notification_campaign_id=".$id."&status=".$status;//remove hardcoded value
232
		// echo $url;
233
		$result = $this->make_request($url,null);
17220 naman 234
		// debug($result);
14954 manas 235
		$sqlQuery1 = "SELECT * FROM notification_campaigns where id=$id" ;
236
		$resultData=$this->NotificationCampaign->query($sqlQuery1);
17212 naman 237
 
238
		$this->set(compact('result'));
239
		// $this->set('data', $result);
14954 manas 240
		$this->set('notificationData', $resultData);
241
	}
242
 
243
	public function admin_user($id = null) {
17225 naman 244
		$url = Configure::read('nodeurl')."/getPushNotificationByUserId/?user_id=".$id;
245
		$userdata = $this->make_request($url,null);
246
		// debug($userdata);
247
		$this->set(compact('userdata'));
248
		// $sqlQuery = "SELECT * FROM pushnotifications where user_id=$id order by id desc" ;
249
		// $resul=$this->NotificationCampaign->query($sqlQuery);
250
		// $this->set('userdata', $resul);
14954 manas 251
	}
15261 anikendra 252
 
14954 manas 253
	public function admin_sort($id = null) {
254
		$sort = $this->request->query('type');
255
		$direction = $this->request->query('order');
256
		//$cid = $this->request->query('cid');
257
		if($sort=='user_id'){
258
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by $sort $direction" ;			
259
		}
260
		else{
261
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by type='$sort' $direction" ;
262
		}
263
		$resul=$this->NotificationCampaign->query($sqlQuery);
264
		$userActions = array();
265
		foreach ($resul as $key => $value) {
266
			if($value['pushnotifications']['type']=='sent'){
267
			$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['created'];
268
			$userActions[$value['pushnotifications']['user_id']]['status'] =$value['pushnotifications']['status'];
269
			}else{
270
				$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['response_time'];
271
			}			
272
		}
273
		$this->set('notificationId',$id);
274
		$this->set('sortdata', $userActions);
275
	}
276
 
14964 manas 277
	public function notificationactive(){
15669 anikendra 278
		session_write_close();
14964 manas 279
		$cid = $this->request->query('cid');
15669 anikendra 280
		$this->log("cid $cid",'notificationactive');
15767 anikendra 281
		$options = array('conditions' => array('id'=> $cid,'status'=>'active','expiresat >'=>date('Y-m-d H:i:s',time())),'recursive'=>-1);
14964 manas 282
		$count = $this->NotificationCampaign->find('count',$options);
15669 anikendra 283
		$this->log("count ".print_r($count,1),'notificationactive');
14964 manas 284
		if(!$count){
285
			$result = array('success'=>false);
286
		}else{
287
			$result = array('success'=>true);
14954 manas 288
		}
14964 manas 289
 
290
		$this->response->type('json');
291
		$this->layout = 'ajax';
292
		$this->set(array(
293
		    'result' => $result,
294
		    '_serialize' => array('result')
295
		));
296
		$this->render('/Elements/json');
297
	}
15261 anikendra 298
 
299
	public function admin_send(){
300
		if ($this->request->is('post')) {			
301
			$sql = $this->request->data['NotificationCampaign']['sql'];
302
			if(!empty($sql)){
303
				$users = $this->NotificationCampaign->query($sql);
304
				// debug($users);
305
				$message = $this->request->data['NotificationCampaign'];
306
				// $data = array('name'=>$message['name'],'title'=>$message['title'],'type'=>$message['type'],'message'=>$message['message'],'url'=>$message['url'],'expiresat'=>$message['expiresat'],'sql'=>$message['sql']);
307
				$this->NotificationCampaign->create();
308
				if($this->NotificationCampaign->save($message)){
309
					$message['cid'] = $this->NotificationCampaign->getLastInsertId();
310
				} else{
311
					debug($this->NotificationCampaign->validationErrors);
312
					$message['cid'] = $message['name'];
313
				}				
314
				$this->loadModel('Pushnotification');
15583 anikendra 315
				$sentUsers = array();
17170 anikendra 316
				$payload = array();
15583 anikendra 317
				foreach ($users as $key => $value) {	
318
					if(!in_array($value['User']['id'], $sentUsers)) {
319
						$sentUsers[] = $value['User']['id'];								
17202 anikendra 320
						$data = array('notification_campaign_id'=>$message['cid'],'user_id'=>$value['User']['id'],'type'=>'pending','status'=>0);
17170 anikendra 321
						// $this->Pushnotification->create();
322
						// $this->Pushnotification->save($data);
323
						$payload[] = $data;
15583 anikendra 324
					}
15261 anikendra 325
				}
17208 anikendra 326
				$url = Configure::read('nodeurl')."/addPushNotification";//remove hardcoded value
17184 anikendra 327
				$this->make_request($url,json_encode($payload,JSON_NUMERIC_CHECK));
15261 anikendra 328
				$this->Session->setFlash(__('Push notification scheduled for '.sizeof($users).' users'));
329
				return $this->redirect(array('controller' => 'administration', 'action' => 'dashboard', 'admin' => false));
330
			}
331
		}
332
	}
17202 anikendra 333
}