Subversion Repositories SmartDukaan

Rev

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