Subversion Repositories SmartDukaan

Rev

Rev 17212 | Rev 17222 | 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);
17220 naman 219
		// if($type == 0 || $type == 1)
220
		// {
221
		// 	$type = "sent";
222
		// }
223
		// echo "type", $type;
17212 naman 224
		$url = Configure::read('nodeurl')."/getPushNotificationDetailsByType/?type=".$type."&notification_campaign_id=".$id."&status=".$status;//remove hardcoded value
225
		// echo $url;
226
		$result = $this->make_request($url,null);
17220 naman 227
		// debug($result);
14954 manas 228
		$sqlQuery1 = "SELECT * FROM notification_campaigns where id=$id" ;
229
		$resultData=$this->NotificationCampaign->query($sqlQuery1);
17212 naman 230
 
231
		$this->set(compact('result'));
232
		// $this->set('data', $result);
14954 manas 233
		$this->set('notificationData', $resultData);
234
	}
235
 
236
	public function admin_user($id = null) {
15004 manas 237
		$sqlQuery = "SELECT * FROM pushnotifications where user_id=$id order by id desc" ;
14954 manas 238
		$resul=$this->NotificationCampaign->query($sqlQuery);
239
		$this->set('userdata', $resul);
240
	}
15261 anikendra 241
 
14954 manas 242
	public function admin_sort($id = null) {
243
		$sort = $this->request->query('type');
244
		$direction = $this->request->query('order');
245
		//$cid = $this->request->query('cid');
246
		if($sort=='user_id'){
247
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by $sort $direction" ;			
248
		}
249
		else{
250
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by type='$sort' $direction" ;
251
		}
252
		$resul=$this->NotificationCampaign->query($sqlQuery);
253
		$userActions = array();
254
		foreach ($resul as $key => $value) {
255
			if($value['pushnotifications']['type']=='sent'){
256
			$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['created'];
257
			$userActions[$value['pushnotifications']['user_id']]['status'] =$value['pushnotifications']['status'];
258
			}else{
259
				$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['response_time'];
260
			}			
261
		}
262
		$this->set('notificationId',$id);
263
		$this->set('sortdata', $userActions);
264
	}
265
 
14964 manas 266
	public function notificationactive(){
15669 anikendra 267
		session_write_close();
14964 manas 268
		$cid = $this->request->query('cid');
15669 anikendra 269
		$this->log("cid $cid",'notificationactive');
15767 anikendra 270
		$options = array('conditions' => array('id'=> $cid,'status'=>'active','expiresat >'=>date('Y-m-d H:i:s',time())),'recursive'=>-1);
14964 manas 271
		$count = $this->NotificationCampaign->find('count',$options);
15669 anikendra 272
		$this->log("count ".print_r($count,1),'notificationactive');
14964 manas 273
		if(!$count){
274
			$result = array('success'=>false);
275
		}else{
276
			$result = array('success'=>true);
14954 manas 277
		}
14964 manas 278
 
279
		$this->response->type('json');
280
		$this->layout = 'ajax';
281
		$this->set(array(
282
		    'result' => $result,
283
		    '_serialize' => array('result')
284
		));
285
		$this->render('/Elements/json');
286
	}
15261 anikendra 287
 
288
	public function admin_send(){
289
		if ($this->request->is('post')) {			
290
			$sql = $this->request->data['NotificationCampaign']['sql'];
291
			if(!empty($sql)){
292
				$users = $this->NotificationCampaign->query($sql);
293
				// debug($users);
294
				$message = $this->request->data['NotificationCampaign'];
295
				// $data = array('name'=>$message['name'],'title'=>$message['title'],'type'=>$message['type'],'message'=>$message['message'],'url'=>$message['url'],'expiresat'=>$message['expiresat'],'sql'=>$message['sql']);
296
				$this->NotificationCampaign->create();
297
				if($this->NotificationCampaign->save($message)){
298
					$message['cid'] = $this->NotificationCampaign->getLastInsertId();
299
				} else{
300
					debug($this->NotificationCampaign->validationErrors);
301
					$message['cid'] = $message['name'];
302
				}				
303
				$this->loadModel('Pushnotification');
15583 anikendra 304
				$sentUsers = array();
17170 anikendra 305
				$payload = array();
15583 anikendra 306
				foreach ($users as $key => $value) {	
307
					if(!in_array($value['User']['id'], $sentUsers)) {
308
						$sentUsers[] = $value['User']['id'];								
17202 anikendra 309
						$data = array('notification_campaign_id'=>$message['cid'],'user_id'=>$value['User']['id'],'type'=>'pending','status'=>0);
17170 anikendra 310
						// $this->Pushnotification->create();
311
						// $this->Pushnotification->save($data);
312
						$payload[] = $data;
15583 anikendra 313
					}
15261 anikendra 314
				}
17208 anikendra 315
				$url = Configure::read('nodeurl')."/addPushNotification";//remove hardcoded value
17184 anikendra 316
				$this->make_request($url,json_encode($payload,JSON_NUMERIC_CHECK));
15261 anikendra 317
				$this->Session->setFlash(__('Push notification scheduled for '.sizeof($users).' users'));
318
				return $this->redirect(array('controller' => 'administration', 'action' => 'dashboard', 'admin' => false));
319
			}
320
		}
321
	}
17202 anikendra 322
}