Subversion Repositories SmartDukaan

Rev

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