Subversion Repositories SmartDukaan

Rev

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