Subversion Repositories SmartDukaan

Rev

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