Subversion Repositories SmartDukaan

Rev

Rev 14574 | Rev 14734 | 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');
13558 anikendra 23
	}
13532 anikendra 24
/**
25
 * index method
26
 *
27
 * @return void
28
 */
29
	public function index() {
30
		$this->Click->recursive = 0;
31
		$this->set('clicks', $this->Paginator->paginate());
32
	}
33
 
34
/**
35
 * view method
36
 *
37
 * @throws NotFoundException
38
 * @param string $id
39
 * @return void
40
 */
41
	public function view($id = null) {
42
		if (!$this->Click->exists($id)) {
43
			throw new NotFoundException(__('Invalid click'));
44
		}
45
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
46
		$this->set('click', $this->Click->find('first', $options));
47
	}
48
 
49
/**
50
 * add method
51
 *
52
 * @return void
53
 */
14345 anikendra 54
	public function add($userId=null,$storeProductId=null,$storeId=null) {
14387 anikendra 55
		session_write_close();
13558 anikendra 56
		if(!empty($userId) && !empty($storeProductId)){
14345 anikendra 57
			$url = $this->request->query('url');
58
			$available_price = $this->request->query('price');
13558 anikendra 59
			//Get StoreProduct Info
13808 anikendra 60
			$this->loadModel('Store');
61
			// $options = array('conditions'=>array('id'=>$storeProductId),'fields'=>array('source_id','available_price','url'),'recursive'=>-1);
62
			// $storeProduct = $this->StoreProduct->find('first',$options);
63
			$cachekey = 'storeproduct-'.$storeProductId;
14345 anikendra 64
 
65
			if($url=='undefined' || $available_price=='undefined' || $storeId=='undefined') {
66
				$product = Cache::read($cachekey,'five');
67
				if(empty($product)) {
68
					$url = $this->apihost.'masterData/getSkuById/'.$storeProductId;
69
					$product = $this->make_request($url,null);
70
					Cache::write($cachekey,$product,'five');			
71
				}
72
				$storeProduct = json_decode($product[0],1);
73
				$url = $storeProduct['marketPlaceUrl'];
74
				$storeId = 	$storeProduct['source_id'];
75
				$available_price = $storeProduct['available_price'];
13808 anikendra 76
			}
14345 anikendra 77
			$cachekey = 'store-'.$storeId;
14346 anikendra 78
			$store = Cache::read($cachekey,'month');
79
			if(empty($store)) {
13903 anikendra 80
				$store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeProduct['source_id'])));
81
				Cache::write($cachekey,$store,'month');			
82
			}
14345 anikendra 83
			$prefix = "SHA".$storeId;
13599 anikendra 84
			$tag = $prefix.time();
14345 anikendra 85
			if($storeId == 2){				
14654 anikendra 86
				// $url = str_replace('www','m',$url);
87
				$url = str_replace('www.','',$url);
88
                $url = str_replace('http://','',$url);
89
                $url = str_replace('flipkart.com','http://dl.flipkart.com/dl',$url);
14345 anikendra 90
			} elseif($storeId == 3) {
91
				$url_parts = parse_url($url);
13903 anikendra 92
				$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
13901 anikendra 93
				if(isset($url_parts['query'])) {
13903 anikendra 94
					$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
13901 anikendra 95
				}else{
13903 anikendra 96
					$url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
13901 anikendra 97
				}
14345 anikendra 98
			} elseif($storeId == 4){
14574 anikendra 99
				$urlparts = parse_url($url);
100
				$url = $urlparts['path'];
101
				if(!empty($urlparts['query'])){
102
					$url .= '?'.$urlparts['query'];	
103
				}				
104
				$url = $this->getAutoLoginUrl($userId,$url);
14346 anikendra 105
			}/*else{
13808 anikendra 106
				$url = $storeProduct['marketPlaceUrl'];
14345 anikendra 107
			}*/
13558 anikendra 108
			if( strpos($url, '?') === false ) {
109
				$firstChar = '?';
110
			} else {
13561 anikendra 111
				$firstChar = '&';
13558 anikendra 112
			}
113
			$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
114
			if(!empty($store['Store']['sub_tag_param'])){
115
				$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
116
			}
117
			$extras = array('store'=>$store['Store']['name']);
14345 anikendra 118
			$data = array('user_id'=>$userId,'store_product_id'=>$storeProductId,'price'=>$available_price,'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
13532 anikendra 119
			$this->Click->create();
13558 anikendra 120
			if ($this->Click->save($data)) {
121
				$result = array('success'=>true,'message'=>__('The click has been saved.'),'type'=>'redirect','url'=>$url);
13532 anikendra 122
			} else {
13558 anikendra 123
				$result = array('success'=>false,'message'=>__('The click could not be saved. Please, try again.'));
13532 anikendra 124
			}
13558 anikendra 125
			$this->response->type('json');
126
			$this->layout = 'ajax';
127
			$callback = $this->request->query('callback');
128
			$this->set(array(
129
			    'result' => $result,
130
			    'callback' => $callback,
131
			    '_serialize' => array('result')
132
			));
13903 anikendra 133
			$url = $this->apihost.'Catalog/fetchLivePrices/?id='.$storeProductId;
134
			$this->make_request($url,null);
13558 anikendra 135
			$this->render('/Elements/jsonp');
13532 anikendra 136
		}
137
	}
138
 
14302 anikendra 139
	public function redirecttosaholic() {
140
		$userId = $this->request->query('user_id');
141
		if(isset($userId) && !empty($userId)){	
142
			$next = "&next=/private-deals/1";					
143
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
144
			header('location:'.$redirectUrl);
145
			exit();
146
		}
147
	}	
148
 
13532 anikendra 149
/**
150
 * edit method
151
 *
152
 * @throws NotFoundException
153
 * @param string $id
154
 * @return void
155
 */
156
	public function edit($id = null) {
157
		if (!$this->Click->exists($id)) {
158
			throw new NotFoundException(__('Invalid click'));
159
		}
160
		if ($this->request->is(array('post', 'put'))) {
161
			if ($this->Click->save($this->request->data)) {
162
				$this->Session->setFlash(__('The click has been saved.'));
163
				return $this->redirect(array('action' => 'index'));
164
			} else {
165
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
166
			}
167
		} else {
168
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
169
			$this->request->data = $this->Click->find('first', $options);
170
		}
171
		$users = $this->Click->User->find('list');
172
		$storeProducts = $this->Click->StoreProduct->find('list');
173
		$this->set(compact('users', 'storeProducts'));
174
	}
175
 
176
/**
177
 * delete method
178
 *
179
 * @throws NotFoundException
180
 * @param string $id
181
 * @return void
182
 */
183
	public function delete($id = null) {
184
		$this->Click->id = $id;
185
		if (!$this->Click->exists()) {
186
			throw new NotFoundException(__('Invalid click'));
187
		}
188
		$this->request->onlyAllow('post', 'delete');
189
		if ($this->Click->delete()) {
190
			$this->Session->setFlash(__('The click has been deleted.'));
191
		} else {
192
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
193
		}
194
		return $this->redirect(array('action' => 'index'));
195
	}
196
 
197
/**
198
 * admin_index method
199
 *
200
 * @return void
201
 */
202
	public function admin_index() {
203
		$this->Click->recursive = 0;
204
		$this->set('clicks', $this->Paginator->paginate());
205
	}
206
 
207
/**
208
 * admin_view method
209
 *
210
 * @throws NotFoundException
211
 * @param string $id
212
 * @return void
213
 */
214
	public function admin_view($id = null) {
215
		if (!$this->Click->exists($id)) {
216
			throw new NotFoundException(__('Invalid click'));
217
		}
218
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
219
		$this->set('click', $this->Click->find('first', $options));
220
	}
221
 
222
/**
223
 * admin_add method
224
 *
225
 * @return void
226
 */
227
	public function admin_add() {
228
		if ($this->request->is('post')) {
229
			$this->Click->create();
230
			if ($this->Click->save($this->request->data)) {
231
				$this->Session->setFlash(__('The click has been saved.'));
232
				return $this->redirect(array('action' => 'index'));
233
			} else {
234
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
235
			}
236
		}
237
		$users = $this->Click->User->find('list');
238
		$storeProducts = $this->Click->StoreProduct->find('list');
239
		$this->set(compact('users', 'storeProducts'));
240
	}
241
 
242
/**
243
 * admin_edit method
244
 *
245
 * @throws NotFoundException
246
 * @param string $id
247
 * @return void
248
 */
249
	public function admin_edit($id = null) {
250
		if (!$this->Click->exists($id)) {
251
			throw new NotFoundException(__('Invalid click'));
252
		}
253
		if ($this->request->is(array('post', 'put'))) {
254
			if ($this->Click->save($this->request->data)) {
255
				$this->Session->setFlash(__('The click has been saved.'));
256
				return $this->redirect(array('action' => 'index'));
257
			} else {
258
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
259
			}
260
		} else {
261
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
262
			$this->request->data = $this->Click->find('first', $options);
263
		}
264
		$users = $this->Click->User->find('list');
265
		$storeProducts = $this->Click->StoreProduct->find('list');
266
		$this->set(compact('users', 'storeProducts'));
267
	}
268
 
269
/**
270
 * admin_delete method
271
 *
272
 * @throws NotFoundException
273
 * @param string $id
274
 * @return void
275
 */
276
	public function admin_delete($id = null) {
277
		$this->Click->id = $id;
278
		if (!$this->Click->exists()) {
279
			throw new NotFoundException(__('Invalid click'));
280
		}
281
		$this->request->onlyAllow('post', 'delete');
282
		if ($this->Click->delete()) {
283
			$this->Session->setFlash(__('The click has been deleted.'));
284
		} else {
285
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
286
		}
287
		return $this->redirect(array('action' => 'index'));
14302 anikendra 288
	}
289
}