Subversion Repositories SmartDukaan

Rev

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