Subversion Repositories SmartDukaan

Rev

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