Subversion Repositories SmartDukaan

Rev

Rev 13532 | Rev 13567 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Categories Controller
5
 *
6
 * @property Category $Category
7
 * @property PaginatorComponent $Paginator
8
 */
9
class CategoriesController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
17
 
18
/**
19
 * index method
20
 *
21
 * @return void
22
 */
23
	public function index() {
24
		// $this->Category->recursive = 0;
25
		// $this->set('categories', $this->Paginator->paginate());
13541 anikendra 26
		$userId = $this->request->query('user_id');
27
		$this->loadModel('UserCategory');
28
		$options = array('conditions' => array('user_id'=>$userId),'recursive'=>-1);
29
		$userCategories = $this->UserCategory->find('all',$options);		
13532 anikendra 30
		$this->response->type('json');
31
		$this->layout = 'ajax';
13541 anikendra 32
		$conditions = array(array('Category.parent_id !='=>0));
33
		if(!empty($userCategories)){
34
			foreach ($userCategories as $key => $value) {
35
				$categoryIds[] = $value['UserCategory']['category_id'];
36
			}
37
			array_push($conditions,array('Category.id'=>$categoryIds));
38
		}
39
		$this->Category->recursive = -1;		
13532 anikendra 40
		$categories = $this->Paginator->paginate(null,$conditions);
41
		$callback = $this->request->query('callback');
42
		$result = array('categories' => $categories);
43
		$this->set(array(
44
		    'result' => $result,
45
		    'callback' => $callback,
46
		    '_serialize' => array('result')
47
		));
48
		$this->render('/Elements/jsonp');
49
	}
50
 
51
/**
52
 * view method
53
 *
54
 * @throws NotFoundException
55
 * @param string $id
56
 * @return void
57
 */
58
	public function view($id = null) {
59
		if (!$this->Category->exists($id)) {
60
			throw new NotFoundException(__('Invalid category'));
61
		}
62
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
63
		$this->set('category', $this->Category->find('first', $options));
64
	}
65
 
66
/**
67
 * add method
68
 *
69
 * @return void
70
 */
71
	public function add() {
72
		if ($this->request->is('post')) {
73
			$this->Category->create();
74
			if ($this->Category->save($this->request->data)) {
75
				$this->Session->setFlash(__('The category has been saved.'));
76
				return $this->redirect(array('action' => 'index'));
77
			} else {
78
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
79
			}
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->Category->exists($id)) {
92
			throw new NotFoundException(__('Invalid category'));
93
		}
94
		if ($this->request->is(array('post', 'put'))) {
95
			if ($this->Category->save($this->request->data)) {
96
				$this->Session->setFlash(__('The category has been saved.'));
97
				return $this->redirect(array('action' => 'index'));
98
			} else {
99
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
100
			}
101
		} else {
102
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
103
			$this->request->data = $this->Category->find('first', $options);
104
		}
105
	}
106
 
107
/**
108
 * delete method
109
 *
110
 * @throws NotFoundException
111
 * @param string $id
112
 * @return void
113
 */
114
	public function delete($id = null) {
115
		$this->Category->id = $id;
116
		if (!$this->Category->exists()) {
117
			throw new NotFoundException(__('Invalid category'));
118
		}
119
		$this->request->onlyAllow('post', 'delete');
120
		if ($this->Category->delete()) {
121
			$this->Session->setFlash(__('The category has been deleted.'));
122
		} else {
123
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
124
		}
125
		return $this->redirect(array('action' => 'index'));
126
	}
127
 
128
/**
129
 * admin_index method
130
 *
131
 * @return void
132
 */
133
	public function admin_index() {
134
		$this->Category->recursive = 0;
135
		$this->set('categories', $this->Paginator->paginate());
136
	}
137
 
138
/**
139
 * admin_view method
140
 *
141
 * @throws NotFoundException
142
 * @param string $id
143
 * @return void
144
 */
145
	public function admin_view($id = null) {
146
		if (!$this->Category->exists($id)) {
147
			throw new NotFoundException(__('Invalid category'));
148
		}
149
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
150
		$this->set('category', $this->Category->find('first', $options));
151
	}
152
 
153
/**
154
 * admin_add method
155
 *
156
 * @return void
157
 */
158
	public function admin_add() {
159
		if ($this->request->is('post')) {
160
			// print_r($this->request->data);die;
161
			$this->Category->create();
162
			if ($this->Category->save($this->request->data)) {
163
				$this->Session->setFlash(__('The category has been saved.'));
164
				return $this->redirect(array('action' => 'index'));
165
			} else {
166
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
167
			}
168
		}
169
		$this->set('parent_id',$this->Category->find('list'));
170
	}
171
 
172
/**
173
 * admin_edit method
174
 *
175
 * @throws NotFoundException
176
 * @param string $id
177
 * @return void
178
 */
179
	public function admin_edit($id = null) {
180
		if (!$this->Category->exists($id)) {
181
			throw new NotFoundException(__('Invalid category'));
182
		}
183
		if ($this->request->is(array('post', 'put'))) {
184
			if ($this->Category->save($this->request->data)) {
185
				$this->Session->setFlash(__('The category has been saved.'));
186
				return $this->redirect(array('action' => 'index'));
187
			} else {
188
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
189
			}
190
		} else {
191
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
192
			$this->request->data = $this->Category->find('first', $options);
193
		}
194
	}
195
 
196
/**
197
 * admin_delete method
198
 *
199
 * @throws NotFoundException
200
 * @param string $id
201
 * @return void
202
 */
203
	public function admin_delete($id = null) {
204
		$this->Category->id = $id;
205
		if (!$this->Category->exists()) {
206
			throw new NotFoundException(__('Invalid category'));
207
		}
208
		$this->request->onlyAllow('post', 'delete');
209
		if ($this->Category->delete()) {
210
			$this->Session->setFlash(__('The category has been deleted.'));
211
		} else {
212
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
213
		}
214
		return $this->redirect(array('action' => 'index'));
215
	}}