Subversion Repositories SmartDukaan

Rev

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