Subversion Repositories SmartDukaan

Rev

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