Subversion Repositories SmartDukaan

Rev

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