Subversion Repositories SmartDukaan

Rev

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