Subversion Repositories SmartDukaan

Rev

Rev 21208 | Go to most recent revision | 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');
15767 anikendra 62
				$data = $this->GcmUser->find('first',array('conditions'=>array('user_id'=>$this->request->data['user_id'],'imeinumber'=>$this->request->data['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)) {
21209 amit.gupta 67
						$dataForModel['notification_type'] = 'gcm';
21208 amit.gupta 68
					} 
19237 manas 69
					$dataForModel['failurecount'] = 0;					
15508 anikendra 70
				}else{
71
					$this->GcmUser->create();
72
				}			
73
				$this->log('lets save '.print_r($dataForModel,1),'gcm');
74
				if ($this->GcmUser->save($dataForModel)) {
19237 manas 75
					$result = array('success'=>true);	
76
					if(!empty($dataForModel['androidid'])){
77
						$url = Configure::read('nodeurl')."/addingTopNotifications?user_id=".$dataForModel['user_id']."&android_id=".$dataForModel['androidid'];
78
						$this->make_request($url,null);
79
					}
15508 anikendra 80
				} else {
81
					$result = array('success'=>false);
15767 anikendra 82
					$this->log("failure ".print_r($this->GcmUser->validationErrors,1));
15508 anikendra 83
				}
13532 anikendra 84
			}
85
		}
13698 anikendra 86
		$this->set(array(
87
		    'result' => $result,
88
		    '_serialize' => array('result')
89
		));		
90
		$this->render('/Elements/json');
13532 anikendra 91
	}
92
 
93
/**
94
 * edit method
95
 *
96
 * @throws NotFoundException
97
 * @param string $id
98
 * @return void
99
 */
100
	public function edit($id = null) {
101
		if (!$this->GcmUser->exists($id)) {
102
			throw new NotFoundException(__('Invalid gcm user'));
103
		}
104
		if ($this->request->is(array('post', 'put'))) {
105
			if ($this->GcmUser->save($this->request->data)) {
106
				$this->Session->setFlash(__('The gcm user has been saved.'));
107
				return $this->redirect(array('action' => 'index'));
108
			} else {
109
				$this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
110
			}
111
		} else {
112
			$options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
113
			$this->request->data = $this->GcmUser->find('first', $options);
114
		}
115
	}
116
 
117
/**
118
 * delete method
119
 *
120
 * @throws NotFoundException
121
 * @param string $id
122
 * @return void
123
 */
124
	public function delete($id = null) {
125
		$this->GcmUser->id = $id;
126
		if (!$this->GcmUser->exists()) {
127
			throw new NotFoundException(__('Invalid gcm user'));
128
		}
129
		$this->request->onlyAllow('post', 'delete');
130
		if ($this->GcmUser->delete()) {
131
			$this->Session->setFlash(__('The gcm user has been deleted.'));
132
		} else {
133
			$this->Session->setFlash(__('The gcm user could not be deleted. Please, try again.'));
134
		}
135
		return $this->redirect(array('action' => 'index'));
136
	}
137
 
138
/**
139
 * admin_index method
140
 *
141
 * @return void
142
 */
143
	public function admin_index() {
144
		$this->GcmUser->recursive = 0;
145
		$this->set('gcmUsers', $this->Paginator->paginate());
146
	}
147
 
148
/**
149
 * admin_view method
150
 *
151
 * @throws NotFoundException
152
 * @param string $id
153
 * @return void
154
 */
155
	public function admin_view($id = null) {
156
		if (!$this->GcmUser->exists($id)) {
157
			throw new NotFoundException(__('Invalid gcm user'));
158
		}
159
		$options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
160
		$this->set('gcmUser', $this->GcmUser->find('first', $options));
161
	}
162
 
163
/**
164
 * admin_add method
165
 *
166
 * @return void
167
 */
168
	public function admin_add() {
169
		if ($this->request->is('post')) {
170
			$this->GcmUser->create();
171
			if ($this->GcmUser->save($this->request->data)) {
172
				$this->Session->setFlash(__('The gcm user has been saved.'));
173
				return $this->redirect(array('action' => 'index'));
174
			} else {
175
				$this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
176
			}
177
		}
178
	}
179
 
180
/**
181
 * admin_edit method
182
 *
183
 * @throws NotFoundException
184
 * @param string $id
185
 * @return void
186
 */
187
	public function admin_edit($id = null) {
188
		if (!$this->GcmUser->exists($id)) {
189
			throw new NotFoundException(__('Invalid gcm user'));
190
		}
191
		if ($this->request->is(array('post', 'put'))) {
192
			if ($this->GcmUser->save($this->request->data)) {
193
				$this->Session->setFlash(__('The gcm user has been saved.'));
194
				return $this->redirect(array('action' => 'index'));
195
			} else {
196
				$this->Session->setFlash(__('The gcm user could not be saved. Please, try again.'));
197
			}
198
		} else {
199
			$options = array('conditions' => array('GcmUser.' . $this->GcmUser->primaryKey => $id));
200
			$this->request->data = $this->GcmUser->find('first', $options);
201
		}
202
	}
203
 
204
/**
205
 * admin_delete method
206
 *
207
 * @throws NotFoundException
208
 * @param string $id
209
 * @return void
210
 */
211
	public function admin_delete($id = null) {
212
		$this->GcmUser->id = $id;
213
		if (!$this->GcmUser->exists()) {
214
			throw new NotFoundException(__('Invalid gcm user'));
215
		}
216
		$this->request->onlyAllow('post', 'delete');
217
		if ($this->GcmUser->delete()) {
218
			$this->Session->setFlash(__('The gcm user has been deleted.'));
219
		} else {
220
			$this->Session->setFlash(__('The gcm user could not be deleted. Please, try again.'));
221
		}
222
		return $this->redirect(array('action' => 'index'));
223
	}}