Subversion Repositories SmartDukaan

Rev

Rev 20075 | Rev 20389 | 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
 
18478 manish.sha 66
			$product = Cache::read($cachekey,'five');
67
			if(empty($product)) {
18480 manish.sha 68
				$producturl = $this->apihost.'masterData/getSkuById/'.$storeProductId;
69
				$product = $this->make_request($producturl,null);
18478 manish.sha 70
				Cache::write($cachekey,$product,'five');			
71
			}
72
			$storeProduct = json_decode($product[0],1);
73
			$storeProductName = $storeProduct['source_product_name'];
74
			$storeProductBrand = $storeProduct['brand'];
75
			$storeProductCategory = $storeProduct['category_id'];
14345 anikendra 76
			if($url=='undefined' || $available_price=='undefined' || $storeId=='undefined') {
77
				$url = $storeProduct['marketPlaceUrl'];
78
				$storeId = 	$storeProduct['source_id'];
79
				$available_price = $storeProduct['available_price'];
13808 anikendra 80
			}
18170 amit.gupta 81
			$url1=$url;
14345 anikendra 82
			$cachekey = 'store-'.$storeId;
15085 anikendra 83
			//$store = Cache::read($cachekey,'month');
14346 anikendra 84
			if(empty($store)) {
14786 anikendra 85
				$store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeId)));
13903 anikendra 86
				Cache::write($cachekey,$store,'month');			
87
			}
14345 anikendra 88
			$prefix = "SHA".$storeId;
13599 anikendra 89
			$tag = $prefix.time();
14345 anikendra 90
			if($storeId == 2){				
14654 anikendra 91
				// $url = str_replace('www','m',$url);
92
				$url = str_replace('www.','',$url);
93
                $url = str_replace('http://','',$url);
94
                $url = str_replace('flipkart.com','http://dl.flipkart.com/dl',$url);
14345 anikendra 95
			} elseif($storeId == 3) {
18263 manas 96
				//$tag='SHASNAPDEAL123';
14345 anikendra 97
				$url_parts = parse_url($url);
13903 anikendra 98
				$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
18263 manas 99
				if(isset($url_parts['query'])) {
18164 amit.gupta 100
					//$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
18263 manas 101
					$url = "http://m.snapdeal.com".$url_parts['path'].'?'.'utm_source=aff_prog&utm_campaign=afts&offer_id=17';
13901 anikendra 102
				}else{
18164 amit.gupta 103
					$url = "http://m.snapdeal.com".$url_parts['path'].'?';
104
				}	
14345 anikendra 105
			} elseif($storeId == 4){
14574 anikendra 106
				$urlparts = parse_url($url);
107
				$url = $urlparts['path'];
108
				if(!empty($urlparts['query'])){
109
					$url .= '?'.$urlparts['query'];	
15248 anikendra 110
				}		
15335 anikendra 111
				$tokenValidated = $this->checkToken($userId);
15651 anikendra 112
				if(isset($userId) && !empty($userId) && $tokenValidated==1){			
15248 anikendra 113
					$url = $this->getAutoLoginUrl($userId,$url);
16308 anikendra 114
				} else{					
115
					if($tokenValidated == 0){
116
						$url = '/special/native/login';
117
					}elseif($tokenValidated == -1){
118
						$url = '/abouts/askforupdate';
119
					}
15248 anikendra 120
				}
16308 anikendra 121
				$this->log(print_r($url,1),'headers');
15920 anikendra 122
			} elseif($storeId==5){
123
				$url = "http://clk.omgt5.com/?PID=10314&r=$url";
16397 anikendra 124
			} elseif($storeId==6){
125
				$url = "http://clk.omgt5.com/?PID=11365&r=$url";
17025 anikendra 126
			} elseif($storeId==7){
127
				$url = "http://clk.omgt5.com/?PID=9419&r=$url";
16397 anikendra 128
			}
13558 anikendra 129
			if( strpos($url, '?') === false ) {
130
				$firstChar = '?';
131
			} else {
13561 anikendra 132
				$firstChar = '&';
13558 anikendra 133
			}
18164 amit.gupta 134
			if ($storeId!=3) {
135
				$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
136
				if(!empty($store['Store']['sub_tag_param'])){
137
					$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
138
				}
13558 anikendra 139
			}
18263 manas 140
			else{
141
                $url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
142
                if(!empty($store['Store']['sub_tag_param'])){
143
                        $url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
144
                }
145
    	    }
13558 anikendra 146
			$extras = array('store'=>$store['Store']['name']);
18479 manish.sha 147
			$data = array('user_id'=>$userId,'store_product_id'=>$storeProductId,'price'=>$available_price, 'product_name'=>$storeProductName, 'brand'=>$storeProductBrand, 'category_id'=>$storeProductCategory, 'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
13532 anikendra 148
			$this->Click->create();
18168 amit.gupta 149
			$curl = $this->livepriceurl."!latestPriceById?id=$storeProductId&source_id=$storeId&url=".base64_encode($url1);
15188 anikendra 150
			$response = $this->make_request($curl,null);
151
			$this->log("live price response ".print_r($response,1),'api');
13558 anikendra 152
			if ($this->Click->save($data)) {
15188 anikendra 153
				if($response['result']){
154
					$result = array('success'=>true,'message'=> $response['message'],'type'=>'redirect','url'=>$url,'showmessage'=>1);
155
				}else{
156
					$result = array('success'=>true,'message'=> __('The click has been saved.'),'type'=>'redirect','url'=>$url,'showmessage'=>0);
157
				}				
13532 anikendra 158
			} else {
13558 anikendra 159
				$result = array('success'=>false,'message'=>__('The click could not be saved. Please, try again.'));
13532 anikendra 160
			}
13558 anikendra 161
			$this->response->type('json');
162
			$this->layout = 'ajax';
163
			$callback = $this->request->query('callback');
164
			$this->set(array(
165
			    'result' => $result,
166
			    'callback' => $callback,
167
			    '_serialize' => array('result')
168
			));
15188 anikendra 169
			// $url = $this->apihost.'Catalog/fetchLivePrices/?id='.$storeProductId;			
13558 anikendra 170
			$this->render('/Elements/jsonp');
13532 anikendra 171
		}
172
	}
173
 
14302 anikendra 174
	public function redirecttosaholic() {
175
		$userId = $this->request->query('user_id');
15767 anikendra 176
		$tokenValidated = $this->checkToken($userId);
177
		if(isset($userId) && !empty($userId) && $tokenValidated==1){
178
			$next = "&next=/private-deals/1";
179
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
180
			header('location:'.$redirectUrl);
181
			exit();
182
		} else{
16315 anikendra 183
			// $this->redirect(array('controller'=>'special','action'=>'native','login'));
16308 anikendra 184
			if($tokenValidated == 0){
15651 anikendra 185
				$this->redirect(array('controller'=>'special','action'=>'native','login'));
186
			}elseif($tokenValidated == -1){
187
				$this->redirect(array('controller'=>'abouts','action'=>'askforupdate'));
16308 anikendra 188
			}
15767 anikendra 189
		}
14302 anikendra 190
	}	
191
 
13532 anikendra 192
/**
193
 * edit method
194
 *
195
 * @throws NotFoundException
196
 * @param string $id
197
 * @return void
198
 */
199
	public function edit($id = null) {
200
		if (!$this->Click->exists($id)) {
201
			throw new NotFoundException(__('Invalid click'));
202
		}
203
		if ($this->request->is(array('post', 'put'))) {
204
			if ($this->Click->save($this->request->data)) {
205
				$this->Session->setFlash(__('The click has been saved.'));
206
				return $this->redirect(array('action' => 'index'));
207
			} else {
208
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
209
			}
210
		} else {
211
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
212
			$this->request->data = $this->Click->find('first', $options);
213
		}
214
		$users = $this->Click->User->find('list');
215
		$storeProducts = $this->Click->StoreProduct->find('list');
216
		$this->set(compact('users', 'storeProducts'));
217
	}
218
 
219
/**
220
 * delete method
221
 *
222
 * @throws NotFoundException
223
 * @param string $id
224
 * @return void
225
 */
226
	public function delete($id = null) {
227
		$this->Click->id = $id;
228
		if (!$this->Click->exists()) {
229
			throw new NotFoundException(__('Invalid click'));
230
		}
231
		$this->request->onlyAllow('post', 'delete');
232
		if ($this->Click->delete()) {
233
			$this->Session->setFlash(__('The click has been deleted.'));
234
		} else {
235
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
236
		}
237
		return $this->redirect(array('action' => 'index'));
238
	}
239
 
14734 anikendra 240
	public function admin_by($userId = null) {		
241
		$this->Click->recursive = 0;
242
		$q = $this->request->query('q');
243
		if(isset($q) && !empty($q)){
244
			$options = array('conditions' => array('Click.user_id' => $userId, 'Click.url LIKE'=>'%'.$q.'%'),'order'=>array('id'=>'desc'),'limit' => 100);
245
 			$this->set(compact('q'));
246
		} else {
247
			$options = array('conditions' => array('Click.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
248
 		}
249
		$this->Paginator->settings = $options;
250
		$clicks = $this->Paginator->paginate();		
251
		$this->set(compact('clicks'));
252
	}
253
 
13532 anikendra 254
/**
255
 * admin_index method
256
 *
257
 * @return void
258
 */
259
	public function admin_index() {
260
		$this->Click->recursive = 0;
14734 anikendra 261
		$q = $this->request->query('q');
262
		if(isset($q) && !empty($q)){
263
			$this->Paginator->settings = array('conditions' => array('Click.url LIKE'=>'%'.$q.'%'),'order' => array('id'=>'desc'));
264
 			$this->set(compact('q'));
265
		} else {
266
			$this->Paginator->settings = array('order' => array('id'=>'desc'));
267
 		}
13532 anikendra 268
		$this->set('clicks', $this->Paginator->paginate());
269
	}
270
 
271
/**
272
 * admin_view method
273
 *
274
 * @throws NotFoundException
275
 * @param string $id
276
 * @return void
277
 */
278
	public function admin_view($id = null) {
279
		if (!$this->Click->exists($id)) {
280
			throw new NotFoundException(__('Invalid click'));
281
		}
282
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
283
		$this->set('click', $this->Click->find('first', $options));
284
	}
285
 
286
/**
287
 * admin_add method
288
 *
289
 * @return void
290
 */
291
	public function admin_add() {
292
		if ($this->request->is('post')) {
293
			$this->Click->create();
294
			if ($this->Click->save($this->request->data)) {
295
				$this->Session->setFlash(__('The click has been saved.'));
296
				return $this->redirect(array('action' => 'index'));
297
			} else {
298
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
299
			}
300
		}
301
		$users = $this->Click->User->find('list');
302
		$storeProducts = $this->Click->StoreProduct->find('list');
303
		$this->set(compact('users', 'storeProducts'));
304
	}
305
 
306
/**
307
 * admin_edit method
308
 *
309
 * @throws NotFoundException
310
 * @param string $id
311
 * @return void
312
 */
313
	public function admin_edit($id = null) {
314
		if (!$this->Click->exists($id)) {
315
			throw new NotFoundException(__('Invalid click'));
316
		}
317
		if ($this->request->is(array('post', 'put'))) {
318
			if ($this->Click->save($this->request->data)) {
319
				$this->Session->setFlash(__('The click has been saved.'));
320
				return $this->redirect(array('action' => 'index'));
321
			} else {
322
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
323
			}
324
		} else {
325
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
326
			$this->request->data = $this->Click->find('first', $options);
327
		}
328
		$users = $this->Click->User->find('list');
329
		$storeProducts = $this->Click->StoreProduct->find('list');
330
		$this->set(compact('users', 'storeProducts'));
331
	}
332
 
333
/**
334
 * admin_delete method
335
 *
336
 * @throws NotFoundException
337
 * @param string $id
338
 * @return void
339
 */
340
	public function admin_delete($id = null) {
341
		$this->Click->id = $id;
342
		if (!$this->Click->exists()) {
343
			throw new NotFoundException(__('Invalid click'));
344
		}
345
		$this->request->onlyAllow('post', 'delete');
346
		if ($this->Click->delete()) {
347
			$this->Session->setFlash(__('The click has been deleted.'));
348
		} else {
349
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
350
		}
351
		return $this->redirect(array('action' => 'index'));
14302 anikendra 352
	}
18164 amit.gupta 353
}