Subversion Repositories SmartDukaan

Rev

Rev 14150 | Rev 14302 | 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();
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
 
123
/**
124
 * edit method
125
 *
126
 * @throws NotFoundException
127
 * @param string $id
128
 * @return void
129
 */
130
	public function edit($id = null) {
131
		if (!$this->Click->exists($id)) {
132
			throw new NotFoundException(__('Invalid click'));
133
		}
134
		if ($this->request->is(array('post', 'put'))) {
135
			if ($this->Click->save($this->request->data)) {
136
				$this->Session->setFlash(__('The click has been saved.'));
137
				return $this->redirect(array('action' => 'index'));
138
			} else {
139
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
140
			}
141
		} else {
142
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
143
			$this->request->data = $this->Click->find('first', $options);
144
		}
145
		$users = $this->Click->User->find('list');
146
		$storeProducts = $this->Click->StoreProduct->find('list');
147
		$this->set(compact('users', 'storeProducts'));
148
	}
149
 
150
/**
151
 * delete method
152
 *
153
 * @throws NotFoundException
154
 * @param string $id
155
 * @return void
156
 */
157
	public function delete($id = null) {
158
		$this->Click->id = $id;
159
		if (!$this->Click->exists()) {
160
			throw new NotFoundException(__('Invalid click'));
161
		}
162
		$this->request->onlyAllow('post', 'delete');
163
		if ($this->Click->delete()) {
164
			$this->Session->setFlash(__('The click has been deleted.'));
165
		} else {
166
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
167
		}
168
		return $this->redirect(array('action' => 'index'));
169
	}
170
 
171
/**
172
 * admin_index method
173
 *
174
 * @return void
175
 */
176
	public function admin_index() {
177
		$this->Click->recursive = 0;
178
		$this->set('clicks', $this->Paginator->paginate());
179
	}
180
 
181
/**
182
 * admin_view method
183
 *
184
 * @throws NotFoundException
185
 * @param string $id
186
 * @return void
187
 */
188
	public function admin_view($id = null) {
189
		if (!$this->Click->exists($id)) {
190
			throw new NotFoundException(__('Invalid click'));
191
		}
192
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
193
		$this->set('click', $this->Click->find('first', $options));
194
	}
195
 
196
/**
197
 * admin_add method
198
 *
199
 * @return void
200
 */
201
	public function admin_add() {
202
		if ($this->request->is('post')) {
203
			$this->Click->create();
204
			if ($this->Click->save($this->request->data)) {
205
				$this->Session->setFlash(__('The click has been saved.'));
206
				return $this->redirect(array('action' => 'index'));
207
			} else {
208
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
209
			}
210
		}
211
		$users = $this->Click->User->find('list');
212
		$storeProducts = $this->Click->StoreProduct->find('list');
213
		$this->set(compact('users', 'storeProducts'));
214
	}
215
 
216
/**
217
 * admin_edit method
218
 *
219
 * @throws NotFoundException
220
 * @param string $id
221
 * @return void
222
 */
223
	public function admin_edit($id = null) {
224
		if (!$this->Click->exists($id)) {
225
			throw new NotFoundException(__('Invalid click'));
226
		}
227
		if ($this->request->is(array('post', 'put'))) {
228
			if ($this->Click->save($this->request->data)) {
229
				$this->Session->setFlash(__('The click has been saved.'));
230
				return $this->redirect(array('action' => 'index'));
231
			} else {
232
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
233
			}
234
		} else {
235
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
236
			$this->request->data = $this->Click->find('first', $options);
237
		}
238
		$users = $this->Click->User->find('list');
239
		$storeProducts = $this->Click->StoreProduct->find('list');
240
		$this->set(compact('users', 'storeProducts'));
241
	}
242
 
243
/**
244
 * admin_delete method
245
 *
246
 * @throws NotFoundException
247
 * @param string $id
248
 * @return void
249
 */
250
	public function admin_delete($id = null) {
251
		$this->Click->id = $id;
252
		if (!$this->Click->exists()) {
253
			throw new NotFoundException(__('Invalid click'));
254
		}
255
		$this->request->onlyAllow('post', 'delete');
256
		if ($this->Click->delete()) {
257
			$this->Session->setFlash(__('The click has been deleted.'));
258
		} else {
259
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
260
		}
261
		return $this->redirect(array('action' => 'index'));
262
	}}