Subversion Repositories SmartDukaan

Rev

Rev 20465 | Rev 20467 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14518 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Devices Controller
5
 *
6
 * @property Device $Device
7
 * @property PaginatorComponent $Paginator
8
 */
9
class DevicesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
18
	public function beforeFilter() {
19
		parent::beforeFilter();
20
		$this->Auth->allow('add');
21
	}
22
/**
23
 * index method
24
 *
25
 * @return void
26
 */
27
	public function index() {
28
		$this->Device->recursive = 0;
29
		$this->set('devices', $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->Device->exists($id)) {
41
			throw new NotFoundException(__('Invalid device'));
42
		}
43
		$options = array('conditions' => array('Device.' . $this->Device->primaryKey => $id));
44
		$this->set('device', $this->Device->find('first', $options));
45
	}
46
 
47
/**
48
 * add method
49
 *
50
 * @return void
51
 */
52
	public function add() {
53
		if ($this->request->is('post')) {
54
			$this->log(print_r($this->request->data,1),'devices');
20465 amit.gupta 55
			$device = $this->Device->find('first',array('conditions'=>$this->request->data));
20466 amit.gupta 56
			$this->log(print_r($this->request->data,1),'devices');			
20465 amit.gupta 57
			if(!$device) {
18793 manas 58
				$cachekey = 'webnotifications-'.$this->request->data['user_id'];
59
				$activeNotifications = Cache::read($cachekey,$config = 'hour');
60
				$this->log(print_r($activeNotifications,1),'checknotification');
61
				if(!empty($activeNotifications)){
62
					Cache::delete($cachekey, $config = 'hour');
63
				}
14518 anikendra 64
				$this->Device->create();
65
				if ($this->Device->save($this->request->data)) {
66
					$result = array('success'=>true,'message'=>__('Record Saved.'));
67
				} else {
68
					$result = array('success'=>false,'message'=>print_r($this->Device->validationErrors,1));
69
				}
70
			} else {
20466 amit.gupta 71
				$this->log(print_r($device, 1, 'devices');
20465 amit.gupta 72
				$device->modified=time();
73
				$this->Device->save($device);
74
				$result = array('success'=>false,'message'=>__('Identical record found. Updating.'));
14518 anikendra 75
			}
76
			$this->response->type('json');
77
			$this->layout = 'ajax';
78
			$this->set(array(
79
			    'result' => $response,
80
			    // 'callback' => $callback,
81
			    '_serialize' => array('result')
82
			));
83
			$this->render('/Elements/json');
84
		}
85
	}
86
 
87
/**
88
 * edit method
89
 *
90
 * @throws NotFoundException
91
 * @param string $id
92
 * @return void
93
 */
94
	public function edit($id = null) {
95
		if (!$this->Device->exists($id)) {
96
			throw new NotFoundException(__('Invalid device'));
97
		}
98
		if ($this->request->is(array('post', 'put'))) {
99
			if ($this->Device->save($this->request->data)) {
100
				$this->Session->setFlash(__('The device has been saved.'));
101
				return $this->redirect(array('action' => 'index'));
102
			} else {
103
				$this->Session->setFlash(__('The device could not be saved. Please, try again.'));
104
			}
105
		} else {
106
			$options = array('conditions' => array('Device.' . $this->Device->primaryKey => $id));
107
			$this->request->data = $this->Device->find('first', $options);
108
		}
109
		$users = $this->Device->User->find('list');
110
		$this->set(compact('users'));
111
	}
112
 
113
/**
114
 * delete method
115
 *
116
 * @throws NotFoundException
117
 * @param string $id
118
 * @return void
119
 */
120
	public function delete($id = null) {
121
		$this->Device->id = $id;
122
		if (!$this->Device->exists()) {
123
			throw new NotFoundException(__('Invalid device'));
124
		}
125
		$this->request->onlyAllow('post', 'delete');
126
		if ($this->Device->delete()) {
127
			$this->Session->setFlash(__('The device has been deleted.'));
128
		} else {
129
			$this->Session->setFlash(__('The device could not be deleted. Please, try again.'));
130
		}
131
		return $this->redirect(array('action' => 'index'));
132
	}
133
 
134
/**
135
 * admin_index method
136
 *
137
 * @return void
138
 */
139
	public function admin_index() {
140
		$this->Device->recursive = 0;
141
		$this->set('devices', $this->Paginator->paginate());
142
	}
143
 
144
/**
145
 * admin_view method
146
 *
147
 * @throws NotFoundException
148
 * @param string $id
149
 * @return void
150
 */
151
	public function admin_view($id = null) {
152
		if (!$this->Device->exists($id)) {
153
			throw new NotFoundException(__('Invalid device'));
154
		}
155
		$options = array('conditions' => array('Device.' . $this->Device->primaryKey => $id));
156
		$this->set('device', $this->Device->find('first', $options));
157
	}
158
 
14547 anikendra 159
	public function admin_by($userId = null) {
160
		$options = array('conditions' => array('Device.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
161
		$this->Paginator->settings = $options;
162
		$devices = $this->Paginator->paginate();		
163
		$this->set(compact('devices'));
164
		$this->render('admin_index');
165
	}
166
 
14518 anikendra 167
/**
168
 * admin_add method
169
 *
170
 * @return void
171
 */
172
	public function admin_add() {
173
		if ($this->request->is('post')) {
174
			$this->Device->create();
175
			if ($this->Device->save($this->request->data)) {
176
				$this->Session->setFlash(__('The device has been saved.'));
177
				return $this->redirect(array('action' => 'index'));
178
			} else {
179
				$this->Session->setFlash(__('The device could not be saved. Please, try again.'));
180
			}
181
		}
182
		$users = $this->Device->User->find('list');
183
		$this->set(compact('users'));
184
	}
185
 
186
/**
187
 * admin_edit method
188
 *
189
 * @throws NotFoundException
190
 * @param string $id
191
 * @return void
192
 */
193
	public function admin_edit($id = null) {
194
		if (!$this->Device->exists($id)) {
195
			throw new NotFoundException(__('Invalid device'));
196
		}
197
		if ($this->request->is(array('post', 'put'))) {
198
			if ($this->Device->save($this->request->data)) {
199
				$this->Session->setFlash(__('The device has been saved.'));
200
				return $this->redirect(array('action' => 'index'));
201
			} else {
202
				$this->Session->setFlash(__('The device could not be saved. Please, try again.'));
203
			}
204
		} else {
205
			$options = array('conditions' => array('Device.' . $this->Device->primaryKey => $id));
206
			$this->request->data = $this->Device->find('first', $options);
207
		}
208
		$users = $this->Device->User->find('list');
209
		$this->set(compact('users'));
210
	}
211
 
212
/**
213
 * admin_delete method
214
 *
215
 * @throws NotFoundException
216
 * @param string $id
217
 * @return void
218
 */
219
	public function admin_delete($id = null) {
220
		$this->Device->id = $id;
221
		if (!$this->Device->exists()) {
222
			throw new NotFoundException(__('Invalid device'));
223
		}
224
		$this->request->onlyAllow('post', 'delete');
225
		if ($this->Device->delete()) {
226
			$this->Session->setFlash(__('The device has been deleted.'));
227
		} else {
228
			$this->Session->setFlash(__('The device could not be deleted. Please, try again.'));
229
		}
230
		return $this->redirect(array('action' => 'index'));
231
	}}