Subversion Repositories SmartDukaan

Rev

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