Subversion Repositories SmartDukaan

Rev

Rev 15403 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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');
17
	public $apihost;
18
 
19
	public function beforeFilter() {
20
		parent::beforeFilter();
21
		$this->Auth->allow('redirecttosaholic','add');
22
		$this->apihost = Configure::read('pythonapihost');
23
	}
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
 */
54
	public function add($userId=null,$storeProductId=null,$storeId=null) {
55
		session_write_close();
15461 manish.sha 56
		$this->Click->setDatabase('mobilehotdtr');
15403 manish.sha 57
		$url = $this->request->query('url');
58
		$available_price = $this->request->query('price');
59
		//Get StoreProduct Info
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;
64
 
65
		$cachekey = 'store-'.$storeId;
66
		//$store = Cache::read($cachekey,'month');
67
		if(empty($store)) {
68
			$store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeId)));
69
			Cache::write($cachekey,$store,'month');			
70
		}
71
		$prefix = "SHA".$storeId;
72
		$tag = $prefix.time();
73
		if($storeId == 2){				
74
			// $url = str_replace('www','m',$url);
75
			$url = str_replace('www.','',$url);
76
            $url = str_replace('http://','',$url);
77
            $url = str_replace('flipkart.com','http://dl.flipkart.com/dl',$url);
78
		} elseif($storeId == 3) {
79
			$url_parts = parse_url($url);
80
			$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
81
			if(isset($url_parts['query'])) {
82
				$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
83
			}else{
84
				$url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
85
			}
86
		} elseif($storeId == 4){
87
			$urlparts = parse_url($url);
88
			$url = "http://m.saholic.com".$urlparts['path'];
89
			if(!empty($urlparts['query'])){
90
				$url .= '?'.$urlparts['query'];	
91
			}				
92
			//$url = $this->getAutoLoginUrl($userId,$url);
93
		}/*else{
94
			$url = $storeProduct['marketPlaceUrl'];
95
		}*/
96
		if( strpos($url, '?') === false ) {
97
			$firstChar = '?';
98
		} else {
99
			$firstChar = '&';
100
		}
101
		$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
102
		if(!empty($store['Store']['sub_tag_param'])){
103
			$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
104
		}
105
		$extras = array('store'=>$store['Store']['name']);
106
		$data = array('user_id'=>$userId,'store_product_id'=>$storeProductId,'price'=>$available_price,'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
107
		$this->Click->create();
108
		if ($this->Click->save($data)) {
109
			$result = array('success'=>true,'message'=>__('The click has been saved.'),'type'=>'redirect','url'=>$url);
110
		} else {
111
			$result = array('success'=>false,'message'=>__('The click could not be saved. Please, try again.'));
112
		}
113
		$this->response->type('json');
114
		$this->layout = 'ajax';
115
		$callback = $this->request->query('callback');
116
		$this->set(array(
117
		    'result' => $result,
118
		    'callback' => $callback,
119
		    '_serialize' => array('result')
120
		));
121
		$this->render('/Elements/jsonp');
122
	}
123
 
124
	public function redirecttosaholic() {
125
		$userId = $this->request->query('user_id');
126
		if(isset($userId) && !empty($userId)){	
127
			$next = "&next=/private-deals/1";					
128
			$redirectUrl = $this->getAutoLoginUrl($userId,$next);
129
			header('location:'.$redirectUrl);
130
			exit();
131
		}
132
	}	
133
 
134
/**
135
 * edit method
136
 *
137
 * @throws NotFoundException
138
 * @param string $id
139
 * @return void
140
 */
141
	public function edit($id = null) {
142
		if (!$this->Click->exists($id)) {
143
			throw new NotFoundException(__('Invalid click'));
144
		}
145
		if ($this->request->is(array('post', 'put'))) {
146
			if ($this->Click->save($this->request->data)) {
147
				$this->Session->setFlash(__('The click has been saved.'));
148
				return $this->redirect(array('action' => 'index'));
149
			} else {
150
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
151
			}
152
		} else {
153
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
154
			$this->request->data = $this->Click->find('first', $options);
155
		}
156
		$users = $this->Click->User->find('list');
157
		$storeProducts = $this->Click->StoreProduct->find('list');
158
		$this->set(compact('users', 'storeProducts'));
159
	}
160
 
161
/**
162
 * delete method
163
 *
164
 * @throws NotFoundException
165
 * @param string $id
166
 * @return void
167
 */
168
	public function delete($id = null) {
169
		$this->Click->id = $id;
170
		if (!$this->Click->exists()) {
171
			throw new NotFoundException(__('Invalid click'));
172
		}
173
		$this->request->onlyAllow('post', 'delete');
174
		if ($this->Click->delete()) {
175
			$this->Session->setFlash(__('The click has been deleted.'));
176
		} else {
177
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
178
		}
179
		return $this->redirect(array('action' => 'index'));
180
	}
181
 
182
	public function admin_by($userId = null) {		
183
		$this->Click->recursive = 0;
184
		$q = $this->request->query('q');
185
		if(isset($q) && !empty($q)){
186
			$options = array('conditions' => array('Click.user_id' => $userId, 'Click.url LIKE'=>'%'.$q.'%'),'order'=>array('id'=>'desc'),'limit' => 100);
187
 			$this->set(compact('q'));
188
		} else {
189
			$options = array('conditions' => array('Click.user_id' => $userId),'order'=>array('id'=>'desc'),'limit' => 100);
190
 		}
191
		$this->Paginator->settings = $options;
192
		$clicks = $this->Paginator->paginate();		
193
		$this->set(compact('clicks'));
194
	}
195
 
196
/**
197
 * admin_index method
198
 *
199
 * @return void
200
 */
201
	public function admin_index() {
202
		$this->Click->recursive = 0;
203
		$q = $this->request->query('q');
204
		if(isset($q) && !empty($q)){
205
			$this->Paginator->settings = array('conditions' => array('Click.url LIKE'=>'%'.$q.'%'),'order' => array('id'=>'desc'));
206
 			$this->set(compact('q'));
207
		} else {
208
			$this->Paginator->settings = array('order' => array('id'=>'desc'));
209
 		}
210
		$this->set('clicks', $this->Paginator->paginate());
211
	}
212
 
213
/**
214
 * admin_view method
215
 *
216
 * @throws NotFoundException
217
 * @param string $id
218
 * @return void
219
 */
220
	public function admin_view($id = null) {
221
		if (!$this->Click->exists($id)) {
222
			throw new NotFoundException(__('Invalid click'));
223
		}
224
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
225
		$this->set('click', $this->Click->find('first', $options));
226
	}
227
 
228
/**
229
 * admin_add method
230
 *
231
 * @return void
232
 */
233
	public function admin_add() {
234
		if ($this->request->is('post')) {
235
			$this->Click->create();
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
		}
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_edit method
250
 *
251
 * @throws NotFoundException
252
 * @param string $id
253
 * @return void
254
 */
255
	public function admin_edit($id = null) {
256
		if (!$this->Click->exists($id)) {
257
			throw new NotFoundException(__('Invalid click'));
258
		}
259
		if ($this->request->is(array('post', 'put'))) {
260
			if ($this->Click->save($this->request->data)) {
261
				$this->Session->setFlash(__('The click has been saved.'));
262
				return $this->redirect(array('action' => 'index'));
263
			} else {
264
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
265
			}
266
		} else {
267
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
268
			$this->request->data = $this->Click->find('first', $options);
269
		}
270
		$users = $this->Click->User->find('list');
271
		$storeProducts = $this->Click->StoreProduct->find('list');
272
		$this->set(compact('users', 'storeProducts'));
273
	}
274
 
275
/**
276
 * admin_delete method
277
 *
278
 * @throws NotFoundException
279
 * @param string $id
280
 * @return void
281
 */
282
	public function admin_delete($id = null) {
283
		$this->Click->id = $id;
284
		if (!$this->Click->exists()) {
285
			throw new NotFoundException(__('Invalid click'));
286
		}
287
		$this->request->onlyAllow('post', 'delete');
288
		if ($this->Click->delete()) {
289
			$this->Session->setFlash(__('The click has been deleted.'));
290
		} else {
291
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
292
		}
293
		return $this->redirect(array('action' => 'index'));
294
	}
295
}