Subversion Repositories SmartDukaan

Rev

Rev 14441 | Rev 14654 | 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
 * Clicks Controller
5
 *
6
 * @property Click $Click
7
 * @property PaginatorComponent $Paginator
8
 */
9
class ClicksController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator');
13808 anikendra 17
	public $apihost;
13532 anikendra 18
 
13558 anikendra 19
	public function beforeFilter() {
20
		parent::beforeFilter();
14302 anikendra 21
		$this->Auth->allow('redirecttosaholic');
13808 anikendra 22
		$this->apihost = Configure::read('pythonapihost');
13558 anikendra 23
	}
13532 anikendra 24
/**
25
 * index method
26
 *
27
 * @return void
28
 */
29
	public function index() {
30
		$this->Click->recursive = 0;
31
		$this->set('clicks', $this->Paginator->paginate());
32
	}
33
 
34
/**
35
 * view method
36
 *
37
 * @throws NotFoundException
38
 * @param string $id
39
 * @return void
40
 */
41
	public function view($id = null) {
42
		if (!$this->Click->exists($id)) {
43
			throw new NotFoundException(__('Invalid click'));
44
		}
45
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
46
		$this->set('click', $this->Click->find('first', $options));
47
	}
48
 
49
/**
50
 * add method
51
 *
52
 * @return void
53
 */
14345 anikendra 54
	public function add($userId=null,$storeProductId=null,$storeId=null) {
14387 anikendra 55
		session_write_close();
13558 anikendra 56
		if(!empty($userId) && !empty($storeProductId)){
14345 anikendra 57
			$url = $this->request->query('url');
58
			$available_price = $this->request->query('price');
13558 anikendra 59
			//Get StoreProduct Info
13808 anikendra 60
			$this->loadModel('Store');
61
			// $options = array('conditions'=>array('id'=>$storeProductId),'fields'=>array('source_id','available_price','url'),'recursive'=>-1);
62
			// $storeProduct = $this->StoreProduct->find('first',$options);
63
			$cachekey = 'storeproduct-'.$storeProductId;
14345 anikendra 64
 
65
			if($url=='undefined' || $available_price=='undefined' || $storeId=='undefined') {
66
				$product = Cache::read($cachekey,'five');
67
				if(empty($product)) {
68
					$url = $this->apihost.'masterData/getSkuById/'.$storeProductId;
69
					$product = $this->make_request($url,null);
70
					Cache::write($cachekey,$product,'five');			
71
				}
72
				$storeProduct = json_decode($product[0],1);
73
				$url = $storeProduct['marketPlaceUrl'];
74
				$storeId = 	$storeProduct['source_id'];
75
				$available_price = $storeProduct['available_price'];
13808 anikendra 76
			}
14345 anikendra 77
			$cachekey = 'store-'.$storeId;
14346 anikendra 78
			$store = Cache::read($cachekey,'month');
79
			if(empty($store)) {
13903 anikendra 80
				$store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeProduct['source_id'])));
81
				Cache::write($cachekey,$store,'month');			
82
			}
14345 anikendra 83
			$prefix = "SHA".$storeId;
13599 anikendra 84
			$tag = $prefix.time();
14345 anikendra 85
			if($storeId == 2){				
86
				$url = str_replace('www','m',$url);
87
			} elseif($storeId == 3) {
88
				$url_parts = parse_url($url);
13903 anikendra 89
				$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
13901 anikendra 90
				if(isset($url_parts['query'])) {
13903 anikendra 91
					$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
13901 anikendra 92
				}else{
13903 anikendra 93
					$url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
13901 anikendra 94
				}
14345 anikendra 95
			} elseif($storeId == 4){
14574 anikendra 96
				$urlparts = parse_url($url);
97
				$url = $urlparts['path'];
98
				if(!empty($urlparts['query'])){
99
					$url .= '?'.$urlparts['query'];	
100
				}				
101
				$url = $this->getAutoLoginUrl($userId,$url);
14346 anikendra 102
			}/*else{
13808 anikendra 103
				$url = $storeProduct['marketPlaceUrl'];
14345 anikendra 104
			}*/
13558 anikendra 105
			if( strpos($url, '?') === false ) {
106
				$firstChar = '?';
107
			} else {
13561 anikendra 108
				$firstChar = '&';
13558 anikendra 109
			}
110
			$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
111
			if(!empty($store['Store']['sub_tag_param'])){
112
				$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
113
			}
114
			$extras = array('store'=>$store['Store']['name']);
14345 anikendra 115
			$data = array('user_id'=>$userId,'store_product_id'=>$storeProductId,'price'=>$available_price,'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
13532 anikendra 116
			$this->Click->create();
13558 anikendra 117
			if ($this->Click->save($data)) {
118
				$result = array('success'=>true,'message'=>__('The click has been saved.'),'type'=>'redirect','url'=>$url);
13532 anikendra 119
			} else {
13558 anikendra 120
				$result = array('success'=>false,'message'=>__('The click could not be saved. Please, try again.'));
13532 anikendra 121
			}
13558 anikendra 122
			$this->response->type('json');
123
			$this->layout = 'ajax';
124
			$callback = $this->request->query('callback');
125
			$this->set(array(
126
			    'result' => $result,
127
			    'callback' => $callback,
128
			    '_serialize' => array('result')
129
			));
13903 anikendra 130
			$url = $this->apihost.'Catalog/fetchLivePrices/?id='.$storeProductId;
131
			$this->make_request($url,null);
13558 anikendra 132
			$this->render('/Elements/jsonp');
13532 anikendra 133
		}
134
	}
135
 
14302 anikendra 136
	public function redirecttosaholic() {
137
		$userId = $this->request->query('user_id');
138
		if(isset($userId) && !empty($userId)){	
139
			$next = "&next=/private-deals/1";					
140
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
141
			header('location:'.$redirectUrl);
142
			exit();
143
		}
144
	}	
145
 
13532 anikendra 146
/**
147
 * edit method
148
 *
149
 * @throws NotFoundException
150
 * @param string $id
151
 * @return void
152
 */
153
	public function edit($id = null) {
154
		if (!$this->Click->exists($id)) {
155
			throw new NotFoundException(__('Invalid click'));
156
		}
157
		if ($this->request->is(array('post', 'put'))) {
158
			if ($this->Click->save($this->request->data)) {
159
				$this->Session->setFlash(__('The click has been saved.'));
160
				return $this->redirect(array('action' => 'index'));
161
			} else {
162
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
163
			}
164
		} else {
165
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
166
			$this->request->data = $this->Click->find('first', $options);
167
		}
168
		$users = $this->Click->User->find('list');
169
		$storeProducts = $this->Click->StoreProduct->find('list');
170
		$this->set(compact('users', 'storeProducts'));
171
	}
172
 
173
/**
174
 * delete method
175
 *
176
 * @throws NotFoundException
177
 * @param string $id
178
 * @return void
179
 */
180
	public function delete($id = null) {
181
		$this->Click->id = $id;
182
		if (!$this->Click->exists()) {
183
			throw new NotFoundException(__('Invalid click'));
184
		}
185
		$this->request->onlyAllow('post', 'delete');
186
		if ($this->Click->delete()) {
187
			$this->Session->setFlash(__('The click has been deleted.'));
188
		} else {
189
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
190
		}
191
		return $this->redirect(array('action' => 'index'));
192
	}
193
 
194
/**
195
 * admin_index method
196
 *
197
 * @return void
198
 */
199
	public function admin_index() {
200
		$this->Click->recursive = 0;
201
		$this->set('clicks', $this->Paginator->paginate());
202
	}
203
 
204
/**
205
 * admin_view method
206
 *
207
 * @throws NotFoundException
208
 * @param string $id
209
 * @return void
210
 */
211
	public function admin_view($id = null) {
212
		if (!$this->Click->exists($id)) {
213
			throw new NotFoundException(__('Invalid click'));
214
		}
215
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
216
		$this->set('click', $this->Click->find('first', $options));
217
	}
218
 
219
/**
220
 * admin_add method
221
 *
222
 * @return void
223
 */
224
	public function admin_add() {
225
		if ($this->request->is('post')) {
226
			$this->Click->create();
227
			if ($this->Click->save($this->request->data)) {
228
				$this->Session->setFlash(__('The click has been saved.'));
229
				return $this->redirect(array('action' => 'index'));
230
			} else {
231
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
232
			}
233
		}
234
		$users = $this->Click->User->find('list');
235
		$storeProducts = $this->Click->StoreProduct->find('list');
236
		$this->set(compact('users', 'storeProducts'));
237
	}
238
 
239
/**
240
 * admin_edit method
241
 *
242
 * @throws NotFoundException
243
 * @param string $id
244
 * @return void
245
 */
246
	public function admin_edit($id = null) {
247
		if (!$this->Click->exists($id)) {
248
			throw new NotFoundException(__('Invalid click'));
249
		}
250
		if ($this->request->is(array('post', 'put'))) {
251
			if ($this->Click->save($this->request->data)) {
252
				$this->Session->setFlash(__('The click has been saved.'));
253
				return $this->redirect(array('action' => 'index'));
254
			} else {
255
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
256
			}
257
		} else {
258
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
259
			$this->request->data = $this->Click->find('first', $options);
260
		}
261
		$users = $this->Click->User->find('list');
262
		$storeProducts = $this->Click->StoreProduct->find('list');
263
		$this->set(compact('users', 'storeProducts'));
264
	}
265
 
266
/**
267
 * admin_delete method
268
 *
269
 * @throws NotFoundException
270
 * @param string $id
271
 * @return void
272
 */
273
	public function admin_delete($id = null) {
274
		$this->Click->id = $id;
275
		if (!$this->Click->exists()) {
276
			throw new NotFoundException(__('Invalid click'));
277
		}
278
		$this->request->onlyAllow('post', 'delete');
279
		if ($this->Click->delete()) {
280
			$this->Session->setFlash(__('The click has been deleted.'));
281
		} else {
282
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
283
		}
284
		return $this->redirect(array('action' => 'index'));
14302 anikendra 285
	}
286
}