Subversion Repositories SmartDukaan

Rev

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