Subversion Repositories SmartDukaan

Rev

Rev 14547 | Rev 20465 | 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');
55
			$count = $this->Device->find('count',array('conditions'=>$this->request->data));
56
			if(!$count) {
18793 manas 57
				$cachekey = 'webnotifications-'.$this->request->data['user_id'];
58
				$activeNotifications = Cache::read($cachekey,$config = 'hour');
59
				$this->log(print_r($activeNotifications,1),'checknotification');
60
				if(!empty($activeNotifications)){
61
					Cache::delete($cachekey, $config = 'hour');
62
				}
14518 anikendra 63
				$this->Device->create();
64
				if ($this->Device->save($this->request->data)) {
65
					$result = array('success'=>true,'message'=>__('Record Saved.'));
66
				} else {
67
					$result = array('success'=>false,'message'=>print_r($this->Device->validationErrors,1));
68
				}
69
			} else {
70
				$result = array('success'=>false,'message'=>__('Identical record found. Ignoring.'));
71
			}
72
			$this->response->type('json');
73
			$this->layout = 'ajax';
74
			$this->set(array(
75
			    'result' => $response,
76
			    // 'callback' => $callback,
77
			    '_serialize' => array('result')
78
			));
79
			$this->render('/Elements/json');
80
		}
81
	}
82
 
83
/**
84
 * edit method
85
 *
86
 * @throws NotFoundException
87
 * @param string $id
88
 * @return void
89
 */
90
	public function edit($id = null) {
91
		if (!$this->Device->exists($id)) {
92
			throw new NotFoundException(__('Invalid device'));
93
		}
94
		if ($this->request->is(array('post', 'put'))) {
95
			if ($this->Device->save($this->request->data)) {
96
				$this->Session->setFlash(__('The device has been saved.'));
97
				return $this->redirect(array('action' => 'index'));
98
			} else {
99
				$this->Session->setFlash(__('The device could not be saved. Please, try again.'));
100
			}
101
		} else {
102
			$options = array('conditions' => array('Device.' . $this->Device->primaryKey => $id));
103
			$this->request->data = $this->Device->find('first', $options);
104
		}
105
		$users = $this->Device->User->find('list');
106
		$this->set(compact('users'));
107
	}
108
 
109
/**
110
 * delete method
111
 *
112
 * @throws NotFoundException
113
 * @param string $id
114
 * @return void
115
 */
116
	public function delete($id = null) {
117
		$this->Device->id = $id;
118
		if (!$this->Device->exists()) {
119
			throw new NotFoundException(__('Invalid device'));
120
		}
121
		$this->request->onlyAllow('post', 'delete');
122
		if ($this->Device->delete()) {
123
			$this->Session->setFlash(__('The device has been deleted.'));
124
		} else {
125
			$this->Session->setFlash(__('The device could not be deleted. Please, try again.'));
126
		}
127
		return $this->redirect(array('action' => 'index'));
128
	}
129
 
130
/**
131
 * admin_index method
132
 *
133
 * @return void
134
 */
135
	public function admin_index() {
136
		$this->Device->recursive = 0;
137
		$this->set('devices', $this->Paginator->paginate());
138
	}
139
 
140
/**
141
 * admin_view method
142
 *
143
 * @throws NotFoundException
144
 * @param string $id
145
 * @return void
146
 */
147
	public function admin_view($id = null) {
148
		if (!$this->Device->exists($id)) {
149
			throw new NotFoundException(__('Invalid device'));
150
		}
151
		$options = array('conditions' => array('Device.' . $this->Device->primaryKey => $id));
152
		$this->set('device', $this->Device->find('first', $options));
153
	}
154
 
14547 anikendra 155
	public function admin_by($userId = null) {
156
		$options = array('conditions' => array('Device.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
157
		$this->Paginator->settings = $options;
158
		$devices = $this->Paginator->paginate();		
159
		$this->set(compact('devices'));
160
		$this->render('admin_index');
161
	}
162
 
14518 anikendra 163
/**
164
 * admin_add method
165
 *
166
 * @return void
167
 */
168
	public function admin_add() {
169
		if ($this->request->is('post')) {
170
			$this->Device->create();
171
			if ($this->Device->save($this->request->data)) {
172
				$this->Session->setFlash(__('The device has been saved.'));
173
				return $this->redirect(array('action' => 'index'));
174
			} else {
175
				$this->Session->setFlash(__('The device could not be saved. Please, try again.'));
176
			}
177
		}
178
		$users = $this->Device->User->find('list');
179
		$this->set(compact('users'));
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->Device->exists($id)) {
191
			throw new NotFoundException(__('Invalid device'));
192
		}
193
		if ($this->request->is(array('post', 'put'))) {
194
			if ($this->Device->save($this->request->data)) {
195
				$this->Session->setFlash(__('The device has been saved.'));
196
				return $this->redirect(array('action' => 'index'));
197
			} else {
198
				$this->Session->setFlash(__('The device could not be saved. Please, try again.'));
199
			}
200
		} else {
201
			$options = array('conditions' => array('Device.' . $this->Device->primaryKey => $id));
202
			$this->request->data = $this->Device->find('first', $options);
203
		}
204
		$users = $this->Device->User->find('list');
205
		$this->set(compact('users'));
206
	}
207
 
208
/**
209
 * admin_delete method
210
 *
211
 * @throws NotFoundException
212
 * @param string $id
213
 * @return void
214
 */
215
	public function admin_delete($id = null) {
216
		$this->Device->id = $id;
217
		if (!$this->Device->exists()) {
218
			throw new NotFoundException(__('Invalid device'));
219
		}
220
		$this->request->onlyAllow('post', 'delete');
221
		if ($this->Device->delete()) {
222
			$this->Session->setFlash(__('The device has been deleted.'));
223
		} else {
224
			$this->Session->setFlash(__('The device could not be deleted. Please, try again.'));
225
		}
226
		return $this->redirect(array('action' => 'index'));
227
	}}