Subversion Repositories SmartDukaan

Rev

Rev 13541 | Rev 13579 | 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
 
13567 anikendra 51
	public function deals(){
52
		$this->loadModel('Api');
53
		$apideals = $this->Api->getCategoryDeals($this->Auth->User('id'),1);
54
		$categorydeals = $apideals['products'];
55
		$rows = sizeof($categorydeals);
56
		if($rows>=1){
57
			$this->set('deals',$categorydeals);
58
			$this->render('categorydeals');
59
		}else{
60
			foreach ($categorydeals as $key => $dealarr) {
61
				foreach ($dealarr as $key => $deal) {
62
					$deals[] = $deal[0];
63
				}				
64
			}
65
			$this->set(compact('deals'));
66
		}		
67
	}
68
/*
69
	*
13532 anikendra 70
 * view method
71
 *
72
 * @throws NotFoundException
73
 * @param string $id
74
 * @return void
75
 */
76
	public function view($id = null) {
77
		if (!$this->Category->exists($id)) {
78
			throw new NotFoundException(__('Invalid category'));
79
		}
80
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
81
		$this->set('category', $this->Category->find('first', $options));
82
	}
83
 
84
/**
85
 * add method
86
 *
87
 * @return void
88
 */
89
	public function add() {
90
		if ($this->request->is('post')) {
91
			$this->Category->create();
92
			if ($this->Category->save($this->request->data)) {
93
				$this->Session->setFlash(__('The category has been saved.'));
94
				return $this->redirect(array('action' => 'index'));
95
			} else {
96
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
97
			}
98
		}
99
	}
100
 
101
/**
102
 * edit method
103
 *
104
 * @throws NotFoundException
105
 * @param string $id
106
 * @return void
107
 */
108
	public function edit($id = null) {
109
		if (!$this->Category->exists($id)) {
110
			throw new NotFoundException(__('Invalid category'));
111
		}
112
		if ($this->request->is(array('post', 'put'))) {
113
			if ($this->Category->save($this->request->data)) {
114
				$this->Session->setFlash(__('The category has been saved.'));
115
				return $this->redirect(array('action' => 'index'));
116
			} else {
117
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
118
			}
119
		} else {
120
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
121
			$this->request->data = $this->Category->find('first', $options);
122
		}
123
	}
124
 
125
/**
126
 * delete method
127
 *
128
 * @throws NotFoundException
129
 * @param string $id
130
 * @return void
131
 */
132
	public function delete($id = null) {
133
		$this->Category->id = $id;
134
		if (!$this->Category->exists()) {
135
			throw new NotFoundException(__('Invalid category'));
136
		}
137
		$this->request->onlyAllow('post', 'delete');
138
		if ($this->Category->delete()) {
139
			$this->Session->setFlash(__('The category has been deleted.'));
140
		} else {
141
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
142
		}
143
		return $this->redirect(array('action' => 'index'));
144
	}
145
 
146
/**
147
 * admin_index method
148
 *
149
 * @return void
150
 */
151
	public function admin_index() {
152
		$this->Category->recursive = 0;
153
		$this->set('categories', $this->Paginator->paginate());
154
	}
155
 
156
/**
157
 * admin_view method
158
 *
159
 * @throws NotFoundException
160
 * @param string $id
161
 * @return void
162
 */
163
	public function admin_view($id = null) {
164
		if (!$this->Category->exists($id)) {
165
			throw new NotFoundException(__('Invalid category'));
166
		}
167
		$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
168
		$this->set('category', $this->Category->find('first', $options));
169
	}
170
 
171
/**
172
 * admin_add method
173
 *
174
 * @return void
175
 */
176
	public function admin_add() {
177
		if ($this->request->is('post')) {
178
			// print_r($this->request->data);die;
179
			$this->Category->create();
180
			if ($this->Category->save($this->request->data)) {
181
				$this->Session->setFlash(__('The category has been saved.'));
182
				return $this->redirect(array('action' => 'index'));
183
			} else {
184
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
185
			}
186
		}
187
		$this->set('parent_id',$this->Category->find('list'));
188
	}
189
 
190
/**
191
 * admin_edit method
192
 *
193
 * @throws NotFoundException
194
 * @param string $id
195
 * @return void
196
 */
197
	public function admin_edit($id = null) {
198
		if (!$this->Category->exists($id)) {
199
			throw new NotFoundException(__('Invalid category'));
200
		}
201
		if ($this->request->is(array('post', 'put'))) {
202
			if ($this->Category->save($this->request->data)) {
203
				$this->Session->setFlash(__('The category has been saved.'));
204
				return $this->redirect(array('action' => 'index'));
205
			} else {
206
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
207
			}
208
		} else {
209
			$options = array('conditions' => array('Category.' . $this->Category->primaryKey => $id));
210
			$this->request->data = $this->Category->find('first', $options);
211
		}
212
	}
213
 
214
/**
215
 * admin_delete method
216
 *
217
 * @throws NotFoundException
218
 * @param string $id
219
 * @return void
220
 */
221
	public function admin_delete($id = null) {
222
		$this->Category->id = $id;
223
		if (!$this->Category->exists()) {
224
			throw new NotFoundException(__('Invalid category'));
225
		}
226
		$this->request->onlyAllow('post', 'delete');
227
		if ($this->Category->delete()) {
228
			$this->Session->setFlash(__('The category has been deleted.'));
229
		} else {
230
			$this->Session->setFlash(__('The category could not be deleted. Please, try again.'));
231
		}
232
		return $this->redirect(array('action' => 'index'));
233
	}}