Subversion Repositories SmartDukaan

Rev

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