Subversion Repositories SmartDukaan

Rev

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