Subversion Repositories SmartDukaan

Rev

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