Subversion Repositories SmartDukaan

Rev

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