Subversion Repositories SmartDukaan

Rev

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