Subversion Repositories SmartDukaan

Rev

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