Subversion Repositories SmartDukaan

Rev

Rev 15581 | Rev 15669 | 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;
31
		$this->Paginator->settings = array('order' => array('id'=>'desc'));
32
		$vari = $this->Paginator->paginate();
15581 manas 33
		$checkArray=array();
14954 manas 34
		foreach ($vari as $key => $value) {
35
			$cid=$value['NotificationCampaign']['id'];
36
			$sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications group by notification_campaign_id, type,status";
15581 manas 37
			#$successQuery="SELECT count(*) as count from pushnotifications where status=1 and type='sent' and notification_campaign_id=$cid";
38
			$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 39
			$resul=$this->NotificationCampaign->query($sqlQuery);	
15581 manas 40
			$success_result=$this->NotificationCampaign->query($successQuery);	
41
			array_push($checkArray, $success_result[0][0]['count']);
14954 manas 42
		}
43
		$finalResult = array();
44
		foreach ($resul as $key => $value) {
45
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
46
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
47
			//$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];			
48
		}
15581 manas 49
		$this->set('notificationWithVersionCode', $checkArray);
14954 manas 50
		$this->set('notificationCampaigns', $vari);
51
		$this->set('notificationCampaignsCount', $finalResult);
52
 
53
	}
54
 
55
/**
56
 * admin_view method
57
 *
58
 * @throws NotFoundException
59
 * @param string $id
60
 * @return void
61
 */
62
	public function admin_view($id = null) {
63
		if (!$this->NotificationCampaign->exists($id)) {
64
			throw new NotFoundException(__('Invalid notification campaign'));
65
		}
66
		$sort = $this->request->query('type');
67
		$direction = $this->request->query('order');
68
		$options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
69
		$userData=$this->NotificationCampaign->find('first', $options);
70
		$userActions = array();
71
		foreach ($userData['Pushnotification'] as $key => $value) {
72
			if($value['type']=='sent'){
73
			$userActions[$value['user_id']][$value['type']] = $value['created'];
74
			$userActions[$value['user_id']]['status'] =$value['status'];
75
			}else{
76
				$userActions[$value['user_id']][$value['type']] = $value['response_time'];
77
			}
78
			 //debug($value);
79
		}
80
		$cid=$userData['NotificationCampaign']['id'];
81
		$sqlQuery = "SELECT status as status,notification_campaign_id,type,count(*) as count FROM pushnotifications where notification_campaign_id=$cid group by type,status";
82
		$resul=$this->NotificationCampaign->query($sqlQuery);	
83
		$finalResult = array();
84
		foreach ($resul as $key => $value) {
85
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['count'];
86
			$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['status']]=$value[0]['count'];
87
			//$finalResult[$value['pushnotifications']['notification_campaign_id']][$value['pushnotifications']['type']]=$value[0]['status'];			
88
		}
89
		//debug($finalResult);
90
		$this->set('notificationData', $userData);
91
		$this->set('notificationCampaign', $userActions);
92
		$this->set('notificationCount', $finalResult);
93
	}
94
 
95
/**
96
 * admin_add method
97
 *
98
 * @return void
99
 */
100
	public function admin_add() {
101
		if ($this->request->is('post')) {
102
			$this->NotificationCampaign->create();
103
			if ($this->NotificationCampaign->save($this->request->data)) {
104
				$this->Session->setFlash(__('The notification campaign has been saved.'));
105
				return $this->redirect(array('action' => 'index'));
106
			} else {
107
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
108
			}
109
		}
110
	}
111
 
112
/**
113
 * admin_edit method
114
 *
115
 * @throws NotFoundException
116
 * @param string $id
117
 * @return void
118
 */
119
	public function admin_edit($id = null) {
120
		if (!$this->NotificationCampaign->exists($id)) {
121
			throw new NotFoundException(__('Invalid notification campaign'));
122
		}
123
		if ($this->request->is(array('post', 'put'))) {
124
			if ($this->NotificationCampaign->save($this->request->data)) {
125
				$this->Session->setFlash(__('The notification campaign has been saved.'));
126
				return $this->redirect(array('action' => 'index'));
127
			} else {
128
				$this->Session->setFlash(__('The notification campaign could not be saved. Please, try again.'));
129
			}
130
		} else {
131
			$options = array('conditions' => array('NotificationCampaign.' . $this->NotificationCampaign->primaryKey => $id));
132
			$this->request->data = $this->NotificationCampaign->find('first', $options);
133
		}
134
	}
135
 
136
/**
137
 * admin_delete method
138
 *
139
 * @throws NotFoundException
140
 * @param string $id
141
 * @return void
142
 */
143
	public function admin_delete($id = null) {
144
		$this->NotificationCampaign->id = $id;
145
		if (!$this->NotificationCampaign->exists()) {
146
			throw new NotFoundException(__('Invalid notification campaign'));
147
		}
148
		$this->request->onlyAllow('post', 'delete');
149
		if ($this->NotificationCampaign->delete()) {
150
			$this->Session->setFlash(__('The notification campaign has been deleted.'));
151
		} else {
152
			$this->Session->setFlash(__('The notification campaign could not be deleted. Please, try again.'));
153
		}
154
		return $this->redirect(array('action' => 'index'));
155
	}
156
 
157
	public function admin_show($id = null,$type=null,$status=null) {
158
		$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id=$id and type='$type' and status=$status" ;
159
		$resul=$this->NotificationCampaign->query($sqlQuery);
160
 
161
		$sqlQuery1 = "SELECT * FROM notification_campaigns where id=$id" ;
162
		$resultData=$this->NotificationCampaign->query($sqlQuery1);
163
 
164
		$this->set('data', $resul);
165
		$this->set('notificationData', $resultData);
166
	}
167
 
168
	public function admin_user($id = null) {
15004 manas 169
		$sqlQuery = "SELECT * FROM pushnotifications where user_id=$id order by id desc" ;
14954 manas 170
		$resul=$this->NotificationCampaign->query($sqlQuery);
171
		$this->set('userdata', $resul);
172
	}
15261 anikendra 173
 
14954 manas 174
	public function admin_sort($id = null) {
175
		$sort = $this->request->query('type');
176
		$direction = $this->request->query('order');
177
		//$cid = $this->request->query('cid');
178
		if($sort=='user_id'){
179
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by $sort $direction" ;			
180
		}
181
		else{
182
			$sqlQuery = "SELECT * FROM pushnotifications where notification_campaign_id =$id order by type='$sort' $direction" ;
183
		}
184
		$resul=$this->NotificationCampaign->query($sqlQuery);
185
		$userActions = array();
186
		foreach ($resul as $key => $value) {
187
			if($value['pushnotifications']['type']=='sent'){
188
			$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['created'];
189
			$userActions[$value['pushnotifications']['user_id']]['status'] =$value['pushnotifications']['status'];
190
			}else{
191
				$userActions[$value['pushnotifications']['user_id']][$value['pushnotifications']['type']] = $value['pushnotifications']['response_time'];
192
			}			
193
		}
194
		$this->set('notificationId',$id);
195
		$this->set('sortdata', $userActions);
196
	}
197
 
14964 manas 198
	public function notificationactive(){
199
		$cid = $this->request->query('cid');
14967 manas 200
		$options = array('conditions' => array('id'=> $cid,'status'=>'active','expiresAt >'=>date('Y-m-d H:i:s',time())));
14964 manas 201
		$count = $this->NotificationCampaign->find('count',$options);
202
 
203
		if(!$count){
204
			$result = array('success'=>false);
205
		}else{
206
			$result = array('success'=>true);
14954 manas 207
		}
14964 manas 208
 
209
		$this->response->type('json');
210
		$this->layout = 'ajax';
211
		$this->set(array(
212
		    'result' => $result,
213
		    '_serialize' => array('result')
214
		));
215
		$this->render('/Elements/json');
216
	}
15261 anikendra 217
 
218
	public function admin_send(){
219
		if ($this->request->is('post')) {			
220
			$sql = $this->request->data['NotificationCampaign']['sql'];
221
			if(!empty($sql)){
222
				$users = $this->NotificationCampaign->query($sql);
223
				// debug($users);
224
				$message = $this->request->data['NotificationCampaign'];
225
				// $data = array('name'=>$message['name'],'title'=>$message['title'],'type'=>$message['type'],'message'=>$message['message'],'url'=>$message['url'],'expiresat'=>$message['expiresat'],'sql'=>$message['sql']);
226
				$this->NotificationCampaign->create();
227
				if($this->NotificationCampaign->save($message)){
228
					$message['cid'] = $this->NotificationCampaign->getLastInsertId();
229
				} else{
230
					debug($this->NotificationCampaign->validationErrors);
231
					$message['cid'] = $message['name'];
232
				}				
233
				$this->loadModel('Pushnotification');
15583 anikendra 234
				$sentUsers = array();
235
				foreach ($users as $key => $value) {	
236
					if(!in_array($value['User']['id'], $sentUsers)) {
237
						$sentUsers[] = $value['User']['id'];								
238
						$data = array('notification_campaign_id'=>$message['cid'],'user_id'=>$value['User']['id'],'type'=>'pending');
239
						$this->Pushnotification->create();
240
						$this->Pushnotification->save($data);
241
					}
15261 anikendra 242
				}
243
				$this->Session->setFlash(__('Push notification scheduled for '.sizeof($users).' users'));
244
				return $this->redirect(array('controller' => 'administration', 'action' => 'dashboard', 'admin' => false));
245
			}
246
		}
247
	}
15583 anikendra 248
}