Subversion Repositories SmartDukaan

Rev

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