Subversion Repositories SmartDukaan

Rev

Rev 21209 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * GcmUsers Controller
5
 *
6
 * @property GcmUser $GcmUser
7
 * @property PaginatorComponent $Paginator
8
 */
9
class GcmUsersController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
13698 anikendra 18
	public function beforeFilter() {
19
		parent::beforeFilter();
20
		$this->Auth->allow('add');
21
	}
13532 anikendra 22
/**
23
 * index method
24
 *
25
 * @return void
26
 */
27
	public function index() {
28
		$this->GcmUser->recursive = 0;
29
		$this->set('gcmUsers', $this->Paginator->paginate());
30
	}
31
 
32
/**
33
 * view method
34
 *
35
 * @throws NotFoundException
36
 * @param string $id
37
 * @return void
38
 */
39
	public function view($id = null) {
40
		if (!$this->GcmUser->exists($id)) {
41
			throw new NotFoundException(__('Invalid gcm user'));
42
		}
43
		$options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
44
		$this->set('gcmUser', $this->GcmUser->find('first', $options));
45
	}
46
 
47
/**
48
 * add method
49
 *
50
 * @return void
51
 */
52
	public function add() {
13698 anikendra 53
		$this->layout = 'ajax';
54
		$this->response->type('json');
13532 anikendra 55
		if ($this->request->is('post')) {
15508 anikendra 56
			if(empty($this->request->data['user_id']) || empty($this->request->data['gcm_regid'])){
57
				$result = array('success'=>false);
18187 manas 58
				$this->log('error'.print_r($this->request->data,1),'gcm');
13532 anikendra 59
			} else {
15508 anikendra 60
				$dataForModel = $this->request->data;
61
				$this->log('before find '.print_r($dataForModel,1),'gcm');
21219 amit.gupta 62
				$data = $this->GcmUser->find('first',array('conditions'=>array('user_id'=>$dataForModel['user_id'],'imeinumber'=>$dataForModel['imeinumber']),'order'=>array('id'=>'desc')));
15508 anikendra 63
				$this->log("found row ".print_r($data,1),'gcm');
64
				if(!empty($data)){
65
					$dataForModel['id'] = $data['GcmUser']['id'];
21208 amit.gupta 66
					if(!array_key_exists('notification_type', $dataForModel)) {
21219 amit.gupta 67
						if($dataForModel['gcm_regid']!=$data['GcmUser']['gcm_regid']){
68
							$dataForModel['notification_type'] = 'gcm';
69
						}
21208 amit.gupta 70
					} 
19237 manas 71
					$dataForModel['failurecount'] = 0;					
15508 anikendra 72
				}else{
73
					$this->GcmUser->create();
74
				}			
75
				$this->log('lets save '.print_r($dataForModel,1),'gcm');
76
				if ($this->GcmUser->save($dataForModel)) {
19237 manas 77
					$result = array('success'=>true);	
78
					if(!empty($dataForModel['androidid'])){
79
						$url = Configure::read('nodeurl')."/addingTopNotifications?user_id=".$dataForModel['user_id']."&android_id=".$dataForModel['androidid'];
80
						$this->make_request($url,null);
81
					}
15508 anikendra 82
				} else {
83
					$result = array('success'=>false);
15767 anikendra 84
					$this->log("failure ".print_r($this->GcmUser->validationErrors,1));
15508 anikendra 85
				}
13532 anikendra 86
			}
87
		}
13698 anikendra 88
		$this->set(array(
89
		    'result' => $result,
90
		    '_serialize' => array('result')
91
		));		
92
		$this->render('/Elements/json');
13532 anikendra 93
	}
94
 
95
/**
96
 * edit method
97
 *
98
 * @throws NotFoundException
99
 * @param string $id
100
 * @return void
101
 */
102
	public function edit($id = null) {
103
		if (!$this->GcmUser->exists($id)) {
104
			throw new NotFoundException(__('Invalid gcm user'));
105
		}
106
		if ($this->request->is(array('post', 'put'))) {
107
			if ($this->GcmUser->save($this->request->data)) {
108
				$this->Session->setFlash(__('The gcm user has been saved.'));
109
				return $this->redirect(array('action' => 'index'));
110
			} else {
111
				$this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
112
			}
113
		} else {
114
			$options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
115
			$this->request->data = $this->GcmUser->find('first', $options);
116
		}
117
	}
118
 
119
/**
120
 * delete method
121
 *
122
 * @throws NotFoundException
123
 * @param string $id
124
 * @return void
125
 */
126
	public function delete($id = null) {
127
		$this->GcmUser->id = $id;
128
		if (!$this->GcmUser->exists()) {
129
			throw new NotFoundException(__('Invalid gcm user'));
130
		}
131
		$this->request->onlyAllow('post', 'delete');
132
		if ($this->GcmUser->delete()) {
133
			$this->Session->setFlash(__('The gcm user has been deleted.'));
134
		} else {
135
			$this->Session->setFlash(__('The gcm user could not be deleted. Please, try again.'));
136
		}
137
		return $this->redirect(array('action' => 'index'));
138
	}
139
 
140
/**
141
 * admin_index method
142
 *
143
 * @return void
144
 */
145
	public function admin_index() {
146
		$this->GcmUser->recursive = 0;
147
		$this->set('gcmUsers', $this->Paginator->paginate());
148
	}
149
 
150
/**
151
 * admin_view method
152
 *
153
 * @throws NotFoundException
154
 * @param string $id
155
 * @return void
156
 */
157
	public function admin_view($id = null) {
158
		if (!$this->GcmUser->exists($id)) {
159
			throw new NotFoundException(__('Invalid gcm user'));
160
		}
161
		$options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
162
		$this->set('gcmUser', $this->GcmUser->find('first', $options));
163
	}
164
 
165
/**
166
 * admin_add method
167
 *
168
 * @return void
169
 */
170
	public function admin_add() {
171
		if ($this->request->is('post')) {
172
			$this->GcmUser->create();
173
			if ($this->GcmUser->save($this->request->data)) {
174
				$this->Session->setFlash(__('The gcm user has been saved.'));
175
				return $this->redirect(array('action' => 'index'));
176
			} else {
177
				$this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
178
			}
179
		}
180
	}
181
 
182
/**
183
 * admin_edit method
184
 *
185
 * @throws NotFoundException
186
 * @param string $id
187
 * @return void
188
 */
189
	public function admin_edit($id = null) {
190
		if (!$this->GcmUser->exists($id)) {
191
			throw new NotFoundException(__('Invalid gcm user'));
192
		}
193
		if ($this->request->is(array('post', 'put'))) {
194
			if ($this->GcmUser->save($this->request->data)) {
195
				$this->Session->setFlash(__('The gcm user has been saved.'));
196
				return $this->redirect(array('action' => 'index'));
197
			} else {
198
				$this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
199
			}
200
		} else {
201
			$options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
202
			$this->request->data = $this->GcmUser->find('first', $options);
203
		}
204
	}
205
 
206
/**
207
 * admin_delete method
208
 *
209
 * @throws NotFoundException
210
 * @param string $id
211
 * @return void
212
 */
213
	public function admin_delete($id = null) {
214
		$this->GcmUser->id = $id;
215
		if (!$this->GcmUser->exists()) {
216
			throw new NotFoundException(__('Invalid gcm user'));
217
		}
218
		$this->request->onlyAllow('post', 'delete');
219
		if ($this->GcmUser->delete()) {
220
			$this->Session->setFlash(__('The gcm user has been deleted.'));
221
		} else {
222
			$this->Session->setFlash(__('The gcm user could not be deleted. Please, try again.'));
223
		}
224
		return $this->redirect(array('action' => 'index'));
225
	}}