Subversion Repositories SmartDukaan

Rev

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