Subversion Repositories SmartDukaan

Rev

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