Subversion Repositories SmartDukaan

Rev

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