Subversion Repositories SmartDukaan

Rev

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