Subversion Repositories SmartDukaan

Rev

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