Subversion Repositories SmartDukaan

Rev

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