Subversion Repositories SmartDukaan

Rev

Rev 15070 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15051 manas 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Agents Controller
5
 *
6
 * @property Agent $Agent
7
 * @property PaginatorComponent $Paginator
8
 */
9
class AgentsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
15405 manas 18
	public function beforeFilter() {
19
        parent::beforeFilter();
20
        $this->Auth->allow(array('authenticate'));
21
        }
22
 
15051 manas 23
/**
24
 * index method
25
 *
26
 * @return void
27
 */
28
	public function index() {
29
		$this->Agent->recursive = 0;
30
		$this->set('agents', $this->Paginator->paginate());
31
	}
32
 
33
/**
34
 * view method
35
 *
36
 * @throws NotFoundException
37
 * @param string $id
38
 * @return void
39
 */
40
	public function view($id = null) {
41
		if (!$this->Agent->exists($id)) {
42
			throw new NotFoundException(__('Invalid agent'));
43
		}
44
		$options = array('conditions' => array('Agent.' . $this->Agent->primaryKey => $id));
45
		$this->set('agent', $this->Agent->find('first', $options));
46
	}
47
 
48
/**
49
 * add method
50
 *
51
 * @return void
52
 */
53
	public function add() {
54
		if ($this->request->is('post')) {
55
			$this->Agent->create();
56
			if ($this->Agent->save($this->request->data)) {
57
				$this->Session->setFlash(__('The agent has been saved.'));
58
				return $this->redirect(array('action' => 'index'));
59
			} else {
60
				$this->Session->setFlash(__('The agent could not be saved. Please, try again.'));
61
			}
62
		}
63
	}
64
 
65
/**
66
 * edit method
67
 *
68
 * @throws NotFoundException
69
 * @param string $id
70
 * @return void
71
 */
72
	public function edit($id = null) {
73
		if (!$this->Agent->exists($id)) {
74
			throw new NotFoundException(__('Invalid agent'));
75
		}
76
		if ($this->request->is(array('post', 'put'))) {
77
			if ($this->Agent->save($this->request->data)) {
78
				$this->Session->setFlash(__('The agent has been saved.'));
79
				return $this->redirect(array('action' => 'index'));
80
			} else {
81
				$this->Session->setFlash(__('The agent could not be saved. Please, try again.'));
82
			}
83
		} else {
84
			$options = array('conditions' => array('Agent.' . $this->Agent->primaryKey => $id));
85
			$this->request->data = $this->Agent->find('first', $options);
86
		}
87
	}
88
 
89
/**
90
 * delete method
91
 *
92
 * @throws NotFoundException
93
 * @param string $id
94
 * @return void
95
 */
96
	public function delete($id = null) {
97
		$this->Agent->id = $id;
98
		if (!$this->Agent->exists()) {
99
			throw new NotFoundException(__('Invalid agent'));
100
		}
101
		$this->request->onlyAllow('post', 'delete');
102
		if ($this->Agent->delete()) {
103
			$this->Session->setFlash(__('The agent has been deleted.'));
104
		} else {
105
			$this->Session->setFlash(__('The agent could not be deleted. Please, try again.'));
106
		}
107
		return $this->redirect(array('action' => 'index'));
108
	}
109
 
110
/**
111
 * admin_index method
112
 *
113
 * @return void
114
 */
115
	public function admin_index() {
116
		$this->Agent->recursive = 0;
117
		$this->set('agents', $this->Paginator->paginate());
118
	}
119
 
120
/**
121
 * admin_view method
122
 *
123
 * @throws NotFoundException
124
 * @param string $id
125
 * @return void
126
 */
127
	public function admin_view($id = null) {
128
		if (!$this->Agent->exists($id)) {
129
			throw new NotFoundException(__('Invalid agent'));
130
		}
131
		$options = array('conditions' => array('Agent.' . $this->Agent->primaryKey => $id));
132
		$this->set('agent', $this->Agent->find('first', $options));
133
	}
134
 
135
/**
136
 * admin_add method
137
 *
138
 * @return void
139
 */
140
	public function admin_add() {
141
		if ($this->request->is('post')) {
142
			$this->Agent->create();
143
			if ($this->Agent->save($this->request->data)) {
144
				$this->Session->setFlash(__('The agent has been saved.'));
145
				return $this->redirect(array('action' => 'index'));
146
			} else {
147
				$this->Session->setFlash(__('The agent could not be saved. Please, try again.'));
148
			}
149
		}
150
	}
151
 
152
/**
153
 * admin_edit method
154
 *
155
 * @throws NotFoundException
156
 * @param string $id
157
 * @return void
158
 */
159
	public function admin_edit($id = null) {
160
		if (!$this->Agent->exists($id)) {
161
			throw new NotFoundException(__('Invalid agent'));
162
		}
163
		if ($this->request->is(array('post', 'put'))) {
164
			if ($this->Agent->save($this->request->data)) {
165
				$this->Session->setFlash(__('The agent has been saved.'));
166
				return $this->redirect(array('action' => 'index'));
167
			} else {
168
				$this->Session->setFlash(__('The agent could not be saved. Please, try again.'));
169
			}
170
		} else {
171
			$options = array('conditions' => array('Agent.' . $this->Agent->primaryKey => $id));
172
			$this->request->data = $this->Agent->find('first', $options);
173
		}
174
	}
175
 
176
/**
177
 * admin_delete method
178
 *
179
 * @throws NotFoundException
180
 * @param string $id
181
 * @return void
182
 */
183
	public function admin_delete($id = null) {
184
		$this->Agent->id = $id;
185
		if (!$this->Agent->exists()) {
186
			throw new NotFoundException(__('Invalid agent'));
187
		}
188
		$this->request->onlyAllow('post', 'delete');
189
		if ($this->Agent->delete()) {
190
			$this->Session->setFlash(__('The agent has been deleted.'));
191
		} else {
192
			$this->Session->setFlash(__('The agent could not be deleted. Please, try again.'));
193
		}
194
		return $this->redirect(array('action' => 'index'));
15070 manas 195
	}
15405 manas 196
 
15070 manas 197
	public function authenticate(){
198
		$this->log(print_r($this->request->data,1),'authenticatication');
15405 manas 199
		if (!$this->Agent->exists($email)) {
200
			$result = array('success'=>false,'message'=>'Invalid user1');
201
		}else{
202
			$agentEmail = $this->request->data['email'];
203
			$agentPassword = $this->request->data['password'];
204
			$agentRole = $this->request->data['role'];
205
			$conditions = array('email'=>$agentEmail,'password'=>$agentPassword);
206
			$fields = array('id');
207
			$agentExists = $this->Agent->find('first',array('conditions'=>$conditions,'fields'=>$fields));
208
			if(empty($agentExists)){
209
				$result = array('success'=>false,'message'=>'Invalid Role');
210
			}else{
211
	       		$this->loadModel('Agent_Role');
212
	       		$options = array('conditions' => array('id'=> $agentId,'status'=>$agentRole));
213
				$count = $this->AgentRole->find('count',$options);
214
 
215
	       		if(!$count){
216
					$result = array('success'=>false,'message'=>'Invalid Role');
217
				}else{
218
					$result = array('success'=>true,'message'=>'Valid User');
219
				}
220
       		}
221
		}
222
		$this->set(array(
223
		    'result' => $result,
224
		    '_serialize' => array('result')
225
		)); 
226
		$this->render('/Elements/json'); 
227
 
15070 manas 228
	}
229
}