Subversion Repositories SmartDukaan

Rev

Rev 18480 | Rev 20131 | 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";
20075 amit.gupta 128
			} elseif($storeId==1){
129
				$url = "http://clk.omgt5.com/?PID=13092&r=$url";
16397 anikendra 130
			}
13558 anikendra 131
			if( strpos($url, '?') === false ) {
132
				$firstChar = '?';
133
			} else {
13561 anikendra 134
				$firstChar = '&';
13558 anikendra 135
			}
18164 amit.gupta 136
			if ($storeId!=3) {
137
				$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
138
				if(!empty($store['Store']['sub_tag_param'])){
139
					$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
140
				}
13558 anikendra 141
			}
18263 manas 142
			else{
143
                $url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
144
                if(!empty($store['Store']['sub_tag_param'])){
145
                        $url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
146
                }
147
    	    }
13558 anikendra 148
			$extras = array('store'=>$store['Store']['name']);
18479 manish.sha 149
			$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 150
			$this->Click->create();
18168 amit.gupta 151
			$curl = $this->livepriceurl."!latestPriceById?id=$storeProductId&source_id=$storeId&url=".base64_encode($url1);
15188 anikendra 152
			$response = $this->make_request($curl,null);
153
			$this->log("live price response ".print_r($response,1),'api');
13558 anikendra 154
			if ($this->Click->save($data)) {
15188 anikendra 155
				if($response['result']){
156
					$result = array('success'=>true,'message'=> $response['message'],'type'=>'redirect','url'=>$url,'showmessage'=>1);
157
				}else{
158
					$result = array('success'=>true,'message'=> __('The click has been saved.'),'type'=>'redirect','url'=>$url,'showmessage'=>0);
159
				}				
13532 anikendra 160
			} else {
13558 anikendra 161
				$result = array('success'=>false,'message'=>__('The click could not be saved. Please, try again.'));
13532 anikendra 162
			}
13558 anikendra 163
			$this->response->type('json');
164
			$this->layout = 'ajax';
165
			$callback = $this->request->query('callback');
166
			$this->set(array(
167
			    'result' => $result,
168
			    'callback' => $callback,
169
			    '_serialize' => array('result')
170
			));
15188 anikendra 171
			// $url = $this->apihost.'Catalog/fetchLivePrices/?id='.$storeProductId;			
13558 anikendra 172
			$this->render('/Elements/jsonp');
13532 anikendra 173
		}
174
	}
175
 
14302 anikendra 176
	public function redirecttosaholic() {
177
		$userId = $this->request->query('user_id');
15767 anikendra 178
		$tokenValidated = $this->checkToken($userId);
179
		if(isset($userId) && !empty($userId) && $tokenValidated==1){
180
			$next = "&next=/private-deals/1";
181
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
182
			header('location:'.$redirectUrl);
183
			exit();
184
		} else{
16315 anikendra 185
			// $this->redirect(array('controller'=>'special','action'=>'native','login'));
16308 anikendra 186
			if($tokenValidated == 0){
15651 anikendra 187
				$this->redirect(array('controller'=>'special','action'=>'native','login'));
188
			}elseif($tokenValidated == -1){
189
				$this->redirect(array('controller'=>'abouts','action'=>'askforupdate'));
16308 anikendra 190
			}
15767 anikendra 191
		}
14302 anikendra 192
	}	
193
 
13532 anikendra 194
/**
195
 * edit method
196
 *
197
 * @throws NotFoundException
198
 * @param string $id
199
 * @return void
200
 */
201
	public function edit($id = null) {
202
		if (!$this->Click->exists($id)) {
203
			throw new NotFoundException(__('Invalid click'));
204
		}
205
		if ($this->request->is(array('post', 'put'))) {
206
			if ($this->Click->save($this->request->data)) {
207
				$this->Session->setFlash(__('The click has been saved.'));
208
				return $this->redirect(array('action' => 'index'));
209
			} else {
210
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
211
			}
212
		} else {
213
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
214
			$this->request->data = $this->Click->find('first', $options);
215
		}
216
		$users = $this->Click->User->find('list');
217
		$storeProducts = $this->Click->StoreProduct->find('list');
218
		$this->set(compact('users', 'storeProducts'));
219
	}
220
 
221
/**
222
 * delete method
223
 *
224
 * @throws NotFoundException
225
 * @param string $id
226
 * @return void
227
 */
228
	public function delete($id = null) {
229
		$this->Click->id = $id;
230
		if (!$this->Click->exists()) {
231
			throw new NotFoundException(__('Invalid click'));
232
		}
233
		$this->request->onlyAllow('post', 'delete');
234
		if ($this->Click->delete()) {
235
			$this->Session->setFlash(__('The click has been deleted.'));
236
		} else {
237
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
238
		}
239
		return $this->redirect(array('action' => 'index'));
240
	}
241
 
14734 anikendra 242
	public function admin_by($userId = null) {		
243
		$this->Click->recursive = 0;
244
		$q = $this->request->query('q');
245
		if(isset($q) && !empty($q)){
246
			$options = array('conditions' => array('Click.user_id' => $userId, 'Click.url LIKE'=>'%'.$q.'%'),'order'=>array('id'=>'desc'),'limit' => 100);
247
 			$this->set(compact('q'));
248
		} else {
249
			$options = array('conditions' => array('Click.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
250
 		}
251
		$this->Paginator->settings = $options;
252
		$clicks = $this->Paginator->paginate();		
253
		$this->set(compact('clicks'));
254
	}
255
 
13532 anikendra 256
/**
257
 * admin_index method
258
 *
259
 * @return void
260
 */
261
	public function admin_index() {
262
		$this->Click->recursive = 0;
14734 anikendra 263
		$q = $this->request->query('q');
264
		if(isset($q) && !empty($q)){
265
			$this->Paginator->settings = array('conditions' => array('Click.url LIKE'=>'%'.$q.'%'),'order' => array('id'=>'desc'));
266
 			$this->set(compact('q'));
267
		} else {
268
			$this->Paginator->settings = array('order' => array('id'=>'desc'));
269
 		}
13532 anikendra 270
		$this->set('clicks', $this->Paginator->paginate());
271
	}
272
 
273
/**
274
 * admin_view method
275
 *
276
 * @throws NotFoundException
277
 * @param string $id
278
 * @return void
279
 */
280
	public function admin_view($id = null) {
281
		if (!$this->Click->exists($id)) {
282
			throw new NotFoundException(__('Invalid click'));
283
		}
284
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
285
		$this->set('click', $this->Click->find('first', $options));
286
	}
287
 
288
/**
289
 * admin_add method
290
 *
291
 * @return void
292
 */
293
	public function admin_add() {
294
		if ($this->request->is('post')) {
295
			$this->Click->create();
296
			if ($this->Click->save($this->request->data)) {
297
				$this->Session->setFlash(__('The click has been saved.'));
298
				return $this->redirect(array('action' => 'index'));
299
			} else {
300
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
301
			}
302
		}
303
		$users = $this->Click->User->find('list');
304
		$storeProducts = $this->Click->StoreProduct->find('list');
305
		$this->set(compact('users', 'storeProducts'));
306
	}
307
 
308
/**
309
 * admin_edit method
310
 *
311
 * @throws NotFoundException
312
 * @param string $id
313
 * @return void
314
 */
315
	public function admin_edit($id = null) {
316
		if (!$this->Click->exists($id)) {
317
			throw new NotFoundException(__('Invalid click'));
318
		}
319
		if ($this->request->is(array('post', 'put'))) {
320
			if ($this->Click->save($this->request->data)) {
321
				$this->Session->setFlash(__('The click has been saved.'));
322
				return $this->redirect(array('action' => 'index'));
323
			} else {
324
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
325
			}
326
		} else {
327
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
328
			$this->request->data = $this->Click->find('first', $options);
329
		}
330
		$users = $this->Click->User->find('list');
331
		$storeProducts = $this->Click->StoreProduct->find('list');
332
		$this->set(compact('users', 'storeProducts'));
333
	}
334
 
335
/**
336
 * admin_delete method
337
 *
338
 * @throws NotFoundException
339
 * @param string $id
340
 * @return void
341
 */
342
	public function admin_delete($id = null) {
343
		$this->Click->id = $id;
344
		if (!$this->Click->exists()) {
345
			throw new NotFoundException(__('Invalid click'));
346
		}
347
		$this->request->onlyAllow('post', 'delete');
348
		if ($this->Click->delete()) {
349
			$this->Session->setFlash(__('The click has been deleted.'));
350
		} else {
351
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
352
		}
353
		return $this->redirect(array('action' => 'index'));
14302 anikendra 354
	}
18164 amit.gupta 355
}