Subversion Repositories SmartDukaan

Rev

Rev 17349 | Rev 18999 | 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);
18174 manish.sha 128
				if(isset($value['user_id'])){
129
					$userActions[$value['user_id']][$value['type']] = $dt;//$value['response_time'];
130
				}
17349 manish.sha 131
			}else{
132
				$epoch = $value['created']/1000;
133
				// date_default_timezone_set('GMT');
134
				$dt = date('Y-m-d H:i:s', $epoch);
18174 manish.sha 135
				if(isset($value['user_id'])){
136
					$userActions[$value['user_id']][$value['type']] = $dt;//$value['response_time'];
137
				}
14954 manas 138
			}
139
			 //debug($value);
140
		}
17212 naman 141
		// debug($userData);
142
		// debug($result);
143
		// debug($userActions);
144
		// debug($userAct);
145
 
146
		// $this->set(compact('result'));
147
 
14954 manas 148
		$cid=$userData['NotificationCampaign']['id'];
149
		$sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications where notification_campaign_id=$cid group by type,status";
150
		$resul=$this->NotificationCampaign->query($sqlQuery);	
17212 naman 151
		$finalResult = array();	
14954 manas 152
		foreach ($resul as $key => $value) {
153
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
154
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
155
			//$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];			
156
		}
17212 naman 157
 
14954 manas 158
		$this->set('notificationData', $userData);
159
		$this->set('notificationCampaign', $userActions);
160
		$this->set('notificationCount', $finalResult);
161
	}
162
 
163
/**
164
 * admin_add method
165
 *
166
 * @return void
167
 */
168
	public function admin_add() {
169
		if ($this->request->is('post')) {
170
			$this->NotificationCampaign->create();
171
			if ($this->NotificationCampaign->save($this->request->data)) {
172
				$this->Session->setFlash(__('The notification campaign has been saved.'));
173
				return $this->redirect(array('action' => 'index'));
174
			} else {
175
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
176
			}
177
		}
178
	}
179
 
180
/**
181
 * admin_edit method
182
 *
183
 * @throws NotFoundException
184
 * @param string $id
185
 * @return void
186
 */
187
	public function admin_edit($id = null) {
188
		if (!$this->NotificationCampaign->exists($id)) {
189
			throw new NotFoundException(__('Invalid notification campaign'));
190
		}
191
		if ($this->request->is(array('post', 'put'))) {
192
			if ($this->NotificationCampaign->save($this->request->data)) {
193
				$this->Session->setFlash(__('The notification campaign has been saved.'));
194
				return $this->redirect(array('action' => 'index'));
195
			} else {
196
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
197
			}
198
		} else {
199
			$options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
200
			$this->request->data = $this->NotificationCampaign->find('first', $options);
201
		}
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->NotificationCampaign->id = $id;
213
		if (!$this->NotificationCampaign->exists()) {
214
			throw new NotFoundException(__('Invalid notification campaign'));
215
		}
216
		$this->request->onlyAllow('post', 'delete');
217
		if ($this->NotificationCampaign->delete()) {
218
			$this->Session->setFlash(__('The notification campaign has been deleted.'));
219
		} else {
220
			$this->Session->setFlash(__('The notification campaign could not be deleted. Please, try again.'));
221
		}
222
		return $this->redirect(array('action' => 'index'));
223
	}
17212 naman 224
	// $this->set(compact('result'));
225
	// 
14954 manas 226
	public function admin_show($id = null,$type=null,$status=null) {
17212 naman 227
		// $sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id=$id and type='$type' and status=$status" ;
14954 manas 228
 
17212 naman 229
		// $resul=$this->NotificationCampaign->query($sqlQuery1ry);
17220 naman 230
		// if($type == 0 || $type == 1)
231
		// {
232
		// 	$type = "sent";
233
		// }
234
		// echo "type", $type;
17212 naman 235
		$url = Configure::read('nodeurl')."/getPushNotificationDetailsByType/?type=".$type."&notification_campaign_id=".$id."&status=".$status;//remove hardcoded value
236
		// echo $url;
237
		$result = $this->make_request($url,null);
17220 naman 238
		// debug($result);
14954 manas 239
		$sqlQuery1 = "SELECT * FROM notification_campaigns where id=$id" ;
240
		$resultData=$this->NotificationCampaign->query($sqlQuery1);
17212 naman 241
 
242
		$this->set(compact('result'));
243
		// $this->set('data', $result);
14954 manas 244
		$this->set('notificationData', $resultData);
245
	}
246
 
247
	public function admin_user($id = null) {
17225 naman 248
		$url = Configure::read('nodeurl')."/getPushNotificationByUserId/?user_id=".$id;
249
		$userdata = $this->make_request($url,null);
250
		// debug($userdata);
251
		$this->set(compact('userdata'));
252
		// $sqlQuery = "SELECT * FROM pushnotifications where user_id=$id order by id desc" ;
253
		// $resul=$this->NotificationCampaign->query($sqlQuery);
254
		// $this->set('userdata', $resul);
14954 manas 255
	}
15261 anikendra 256
 
14954 manas 257
	public function admin_sort($id = null) {
258
		$sort = $this->request->query('type');
259
		$direction = $this->request->query('order');
260
		//$cid = $this->request->query('cid');
261
		if($sort=='user_id'){
262
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by $sort $direction" ;			
263
		}
264
		else{
265
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by type='$sort' $direction" ;
266
		}
267
		$resul=$this->NotificationCampaign->query($sqlQuery);
268
		$userActions = array();
269
		foreach ($resul as $key => $value) {
270
			if($value['pushnotifications']['type']=='sent'){
271
			$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['created'];
272
			$userActions[$value['pushnotifications']['user_id']]['status'] =$value['pushnotifications']['status'];
273
			}else{
274
				$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['response_time'];
275
			}			
276
		}
277
		$this->set('notificationId',$id);
278
		$this->set('sortdata', $userActions);
279
	}
280
 
14964 manas 281
	public function notificationactive(){
15669 anikendra 282
		session_write_close();
14964 manas 283
		$cid = $this->request->query('cid');
18174 manish.sha 284
		$cidVals = explode("_", $cid);
285
		$cid = $cidVals[0];
15669 anikendra 286
		$this->log("cid $cid",'notificationactive');
15767 anikendra 287
		$options = array('conditions' => array('id'=> $cid,'status'=>'active','expiresat >'=>date('Y-m-d H:i:s',time())),'recursive'=>-1);
14964 manas 288
		$count = $this->NotificationCampaign->find('count',$options);
15669 anikendra 289
		$this->log("count ".print_r($count,1),'notificationactive');
14964 manas 290
		if(!$count){
291
			$result = array('success'=>false);
292
		}else{
293
			$result = array('success'=>true);
14954 manas 294
		}
14964 manas 295
 
296
		$this->response->type('json');
297
		$this->layout = 'ajax';
298
		$this->set(array(
299
		    'result' => $result,
300
		    '_serialize' => array('result')
301
		));
302
		$this->render('/Elements/json');
303
	}
15261 anikendra 304
 
305
	public function admin_send(){
306
		if ($this->request->is('post')) {			
307
			$sql = $this->request->data['NotificationCampaign']['sql'];
308
			if(!empty($sql)){
309
				$users = $this->NotificationCampaign->query($sql);
310
				// debug($users);
311
				$message = $this->request->data['NotificationCampaign'];
312
				// $data = array('name'=>$message['name'],'title'=>$message['title'],'type'=>$message['type'],'message'=>$message['message'],'url'=>$message['url'],'expiresat'=>$message['expiresat'],'sql'=>$message['sql']);
313
				$this->NotificationCampaign->create();
314
				if($this->NotificationCampaign->save($message)){
315
					$message['cid'] = $this->NotificationCampaign->getLastInsertId();
316
				} else{
317
					debug($this->NotificationCampaign->validationErrors);
318
					$message['cid'] = $message['name'];
319
				}				
320
				$this->loadModel('Pushnotification');
15583 anikendra 321
				$sentUsers = array();
17170 anikendra 322
				$payload = array();
15583 anikendra 323
				foreach ($users as $key => $value) {	
324
					if(!in_array($value['User']['id'], $sentUsers)) {
325
						$sentUsers[] = $value['User']['id'];								
17202 anikendra 326
						$data = array('notification_campaign_id'=>$message['cid'],'user_id'=>$value['User']['id'],'type'=>'pending','status'=>0);
17170 anikendra 327
						// $this->Pushnotification->create();
328
						// $this->Pushnotification->save($data);
329
						$payload[] = $data;
15583 anikendra 330
					}
15261 anikendra 331
				}
17208 anikendra 332
				$url = Configure::read('nodeurl')."/addPushNotification";//remove hardcoded value
17184 anikendra 333
				$this->make_request($url,json_encode($payload,JSON_NUMERIC_CHECK));
15261 anikendra 334
				$this->Session->setFlash(__('Push notification scheduled for '.sizeof($users).' users'));
335
				return $this->redirect(array('controller' => 'administration', 'action' => 'dashboard', 'admin' => false));
336
			}
337
		}
338
	}
17202 anikendra 339
}