Subversion Repositories SmartDukaan

Rev

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