Subversion Repositories SmartDukaan

Rev

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