Subversion Repositories SmartDukaan

Rev

Details | 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
	public function beforeFilter() {
19
		parent::beforeFilter();
20
	//	$token = $this->request->query('token');
21
	//	$this->log("[Categories] [token] $token");			
22
	}
23
/**
24
 * index method
25
 *
26
 * @return void
27
 */
28
	public function index() {
29
		$this->response->type('json');
30
		$this->layout = 'ajax';
31
		$this->Category->recursive = -1;
32
		$conditions = array('Category.parent_id !='=>0);
33
		$categories = $this->Paginator->paginate(null,$conditions);
34
		$callback = $this->request->query('callback');
35
		$result = array('categories' => $categories);
36
		$this->set(array(
37
		    'result' => $result,
38
		    'callback' => $callback,
39
		    '_serialize' => array('result')
40
		));
41
		$this->render('/Elements/jsonp');
42
	}
43
 
44
/**
45
 * view method
46
 *
47
 * @throws NotFoundException
48
 * @param string $id
49
 * @return void
50
 */
51
	public function view($id = null) {
52
//		if (!$this->Category->exists($id)) {
53
			throw new NotFoundException(__('Invalid category'));
54
//		}
55
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
56
		$this->set('category', $this->Category->find('first', $options));
57
	}
58
 
59
/**
60
 * add method
61
 *
62
 * @return void
63
 */
64
	public function add() {
65
		if ($this->request->is('post')) {
66
			$this->Category->create();
67
			if ($this->Category->save($this->request->data)) {
68
				$this->Session->setFlash(__('The category has been saved.'));
69
				return $this->redirect(array('action' => 'index'));
70
			} else {
71
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
72
			}
73
		}
74
		$parentCategories = $this->Category->ParentCategory->find('list');
75
		$this->set(compact('parentCategories'));
76
	}
77
 
78
/**
79
 * edit method
80
 *
81
 * @throws NotFoundException
82
 * @param string $id
83
 * @return void
84
 */
85
	public function edit($id = null) {
86
		if (!$this->Category->exists($id)) {
87
			throw new NotFoundException(__('Invalid category'));
88
		}
89
		if ($this->request->is(array('post', 'put'))) {
90
			if ($this->Category->save($this->request->data)) {
91
				$this->Session->setFlash(__('The category has been saved.'));
92
				return $this->redirect(array('action' => 'index'));
93
			} else {
94
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
95
			}
96
		} else {
97
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
98
			$this->request->data = $this->Category->find('first', $options);
99
		}
100
		$parentCategories = $this->Category->ParentCategory->find('list');
101
		$this->set(compact('parentCategories'));
102
	}
103
 
104
/**
105
 * delete method
106
 *
107
 * @throws NotFoundException
108
 * @param string $id
109
 * @return void
110
 */
111
	public function delete($id = null) {
112
		$this->Category->id = $id;
113
		if (!$this->Category->exists()) {
114
			throw new NotFoundException(__('Invalid category'));
115
		}
116
		$this->request->onlyAllow('post', 'delete');
117
		if ($this->Category->delete()) {
118
			$this->Session->setFlash(__('The category has been deleted.'));
119
		} else {
120
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
121
		}
122
		return $this->redirect(array('action' => 'index'));
123
	}
124
 
125
/**
126
 * admin_index method
127
 *
128
 * @return void
129
 */
130
	public function admin_index() {
131
		// $this->Category->recursive = 0;
132
		// $this->set('categories', $this->Paginator->paginate());
133
	 	$data = $this->Category->generateTreeList(
134
          	null,
135
          	null,
136
          	null,
137
          	'&nbsp;&nbsp;&nbsp;'
138
        );
139
	}
140
 
141
/**
142
 * admin_view method
143
 *
144
 * @throws NotFoundException
145
 * @param string $id
146
 * @return void
147
 */
148
	public function admin_view($id = null) {
149
		if (!$this->Category->exists($id)) {
150
			throw new NotFoundException(__('Invalid category'));
151
		}
152
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
153
		$this->set('category', $this->Category->find('first', $options));
154
	}
155
 
156
/**
157
 * admin_add method
158
 *
159
 * @return void
160
 */
161
	public function admin_add() {
162
		if ($this->request->is('post')) {
163
			$this->Category->create();
164
			if ($this->Category->save($this->request->data)) {
165
				$this->Session->setFlash(__('The category has been saved.'));
166
				return $this->redirect(array('action' => 'index'));
167
			} else {
168
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
169
			}
170
		}
171
		// $parentCategories = $this->Category->ParentCategory->find('list');
172
		// $this->set(compact('parentCategories'));
173
	}
174
 
175
/**
176
 * admin_edit method
177
 *
178
 * @throws NotFoundException
179
 * @param string $id
180
 * @return void
181
 */
182
	public function admin_edit($id = null) {
183
		if (!$this->Category->exists($id)) {
184
			throw new NotFoundException(__('Invalid category'));
185
		}
186
		if ($this->request->is(array('post', 'put'))) {
187
			if ($this->Category->save($this->request->data)) {
188
				$this->Session->setFlash(__('The category has been saved.'));
189
				return $this->redirect(array('action' => 'index'));
190
			} else {
191
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
192
			}
193
		} else {
194
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
195
			$this->request->data = $this->Category->find('first', $options);
196
		}
197
		$parentCategories = $this->Category->ParentCategory->find('list');
198
		$this->set(compact('parentCategories'));
199
	}
200
 
201
/**
202
 * admin_delete method
203
 *
204
 * @throws NotFoundException
205
 * @param string $id
206
 * @return void
207
 */
208
	public function admin_delete($id = null) {
209
		$this->Category->id = $id;
210
		if (!$this->Category->exists()) {
211
			throw new NotFoundException(__('Invalid category'));
212
		}
213
		$this->request->onlyAllow('post', 'delete');
214
		if ($this->Category->delete()) {
215
			$this->Session->setFlash(__('The category has been deleted.'));
216
		} else {
217
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
218
		}
219
		return $this->redirect(array('action' => 'index'));
220
	}}