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
 * Products Controller
5
 *
6
 * @property Product $Product
7
 * @property PaginatorComponent $Paginator
8
 */
9
class ProductsController 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
		$this->Auth->allow('viaweb');
21
		$callback = $this->request->query('callback');
22
	}
23
 
24
/**
25
 * index method
26
 *
27
 * @return void
28
 */
29
	public function index() {
30
		$this->response->type('json');
31
		$this->layout = 'ajax';
32
		// $this->Product->recursive = -1;
33
		$this->Product->recursive = 1;
34
		$result = array('products' => $this->Paginator->paginate());
35
		$callback = $this->request->query('callback');
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->Product->exists($id)) {
53
			throw new NotFoundException(__('Invalid product'));
54
		}
55
		$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
56
		$this->set('product', $this->Product->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->Product->create();
67
			if ($this->Product->save($this->request->data)) {
68
				$this->Session->setFlash(__('The product has been saved.'));
69
				return $this->redirect(array('action' => 'index'));
70
			} else {
71
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
72
			}
73
		}
74
		$categories = $this->Product->Category->find('list');
75
		$this->set(compact('categories'));
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->Product->exists($id)) {
87
			throw new NotFoundException(__('Invalid product'));
88
		}
89
		if ($this->request->is(array('post', 'put'))) {
90
			if ($this->Product->save($this->request->data)) {
91
				$this->Session->setFlash(__('The product has been saved.'));
92
				return $this->redirect(array('action' => 'index'));
93
			} else {
94
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
95
			}
96
		} else {
97
			$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
98
			$this->request->data = $this->Product->find('first', $options);
99
		}
100
		$categories = $this->Product->Category->find('list');
101
		$this->set(compact('categories'));
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->Product->id = $id;
113
		if (!$this->Product->exists()) {
114
			throw new NotFoundException(__('Invalid product'));
115
		}
116
		$this->request->onlyAllow('post', 'delete');
117
		if ($this->Product->delete()) {
118
			$this->Session->setFlash(__('The product has been deleted.'));
119
		} else {
120
			$this->Session->setFlash(__('The product 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->Product->recursive = 0;
132
		$this->set('products', $this->Paginator->paginate());
133
	}
134
 
135
/**
136
 * admin_view method
137
 *
138
 * @throws NotFoundException
139
 * @param string $id
140
 * @return void
141
 */
142
	public function admin_view($id = null) {
143
		if (!$this->Product->exists($id)) {
144
			throw new NotFoundException(__('Invalid product'));
145
		}
146
		$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
147
		$this->set('product', $this->Product->find('first', $options));
148
	}
149
 
150
/**
151
 * admin_add method
152
 *
153
 * @return void
154
 */
155
	public function admin_add() {
156
		if ($this->request->is('post')) {
157
			$this->Product->create();
158
			if ($this->Product->save($this->request->data)) {
159
				$this->Session->setFlash(__('The product has been saved.'));
160
				return $this->redirect(array('action' => 'index'));
161
			} else {
162
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
163
			}
164
		}
165
		$categories = $this->Product->Category->find('list');
166
		$this->set(compact('categories'));
167
	}
168
 
169
/**
170
 * admin_edit method
171
 *
172
 * @throws NotFoundException
173
 * @param string $id
174
 * @return void
175
 */
176
	public function admin_edit($id = null) {
177
		if (!$this->Product->exists($id)) {
178
			throw new NotFoundException(__('Invalid product'));
179
		}
180
		if ($this->request->is(array('post', 'put'))) {
181
			if ($this->Product->save($this->request->data)) {
182
				$this->Session->setFlash(__('The product has been saved.'));
183
				return $this->redirect(array('action' => 'index'));
184
			} else {
185
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
186
			}
187
		} else {
188
			$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
189
			$this->request->data = $this->Product->find('first', $options);
190
		}
191
		$categories = $this->Product->Category->find('list');
192
		$this->set(compact('categories'));
193
	}
194
 
195
/**
196
 * admin_delete method
197
 *
198
 * @throws NotFoundException
199
 * @param string $id
200
 * @return void
201
 */
202
	public function admin_delete($id = null) {
203
		$this->Product->id = $id;
204
		if (!$this->Product->exists()) {
205
			throw new NotFoundException(__('Invalid product'));
206
		}
207
		$this->request->onlyAllow('post', 'delete');
208
		if ($this->Product->delete()) {
209
			$this->Session->setFlash(__('The product has been deleted.'));
210
		} else {
211
			$this->Session->setFlash(__('The product could not be deleted. Please, try again.'));
212
		}
213
		return $this->redirect(array('action' => 'index'));
214
	}
215
 
216
	public function viaweb() {
217
		// debug($this->params->url);die;
218
		if(!$this->isAuthorized()){
219
			$hosturl = 'http://' . $_SERVER['SERVER_NAME'] . Router::url('/');
220
			// $this->redirect($hosturl."users/login/?next=".$hosturl.$this->params->url);
221
			$next = $hosturl.$this->params->url;
222
			$this->Session->write('next', $next);
223
			$this->redirect($hosturl."users/login");
224
		}
225
		$this->loadModel('User');
226
		$this->User->unbindModel(
227
			array('belongsTo' => array('Group'))
228
		);
229
		$user = $this->User->read(null,$this->Auth->user('id'));
230
		$img = base64_decode($this->params['named']['img']);
231
		$url = base64_decode($this->params['named']['url']);
232
		$des = base64_decode($this->params['named']['des']);
233
		$name= base64_decode($this->params['named']['name']);
234
		$store = base64_decode($this->params['named']['store']);
235
		$price = $this->params['named']['price'];
236
		//$this->layout = 'homepage';
237
		$conditions = array('parent_id !='>0);
238
		$categories = $this->Product->Category->find('list',array('conditions'=>$conditions));
239
		$this->set(compact('img','url','des','categories','user','name','store','price'));
240
	}
241
 
242
	public function addviabookmarklet() {
243
		if ($this->request->is('post')) {
244
			$this->Product->create();
245
			if ($this->Product->save($this->request->data)) {
246
				$this->loadModel('Store');
247
				$conditions = array('name'=>$this->request->data['store']);
248
				$store = $this->Store->find('first',array('conditions'=>$conditions));
249
				$data = $this->request->data;
250
				$data['store_id'] = $store['Store']['id'];
251
				$data['product_id'] = $this->Product->getLastInsertId();
252
				$data['price'] = str_replace(',','',$data['price']);
253
				unset($data['store']);
254
				unset($data['name']);
255
				unset($data['category_id']);
256
				unset($data['name']);
257
				if($this->Product->StoreProduct->save($data)) {
258
					$this->Session->setFlash(__('The product has been saved.'));
259
				}else{
260
					print_r($this->Product->StoreProduct->validationErrors);
261
					$this->Product->id = $this->Product->getLastInsertId();
262
					$this->Product->delete();
263
				}	
264
			} else {
265
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
266
			}
267
		}
268
	}
269
}