Subversion Repositories SmartDukaan

Rev

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