Subversion Repositories SmartDukaan

Rev

Rev 13633 | Rev 13808 | 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');
17
 
13558 anikendra 18
	public function beforeFilter() {
19
		parent::beforeFilter();
20
		$this->Auth->allow('add');
21
	}
13532 anikendra 22
/**
23
 * index method
24
 *
25
 * @return void
26
 */
27
	public function index() {
28
		$this->Click->recursive = 0;
29
		$this->set('clicks', $this->Paginator->paginate());
30
	}
31
 
32
/**
33
 * view method
34
 *
35
 * @throws NotFoundException
36
 * @param string $id
37
 * @return void
38
 */
39
	public function view($id = null) {
40
		if (!$this->Click->exists($id)) {
41
			throw new NotFoundException(__('Invalid click'));
42
		}
43
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
44
		$this->set('click', $this->Click->find('first', $options));
45
	}
46
 
47
/**
48
 * add method
49
 *
50
 * @return void
51
 */
13558 anikendra 52
	public function add($userId=null,$storeProductId=null) {
53
		if(!empty($userId) && !empty($storeProductId)){
54
			//Get StoreProduct Info
55
			$this->loadModel('StoreProduct');
56
			$options = array('conditions'=>array('id'=>$storeProductId),'fields'=>array('store_id','available_price','url'),'recursive'=>-1);
57
			$storeProduct = $this->StoreProduct->find('first',$options);
58
			$store = $this->StoreProduct->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeProduct['StoreProduct']['store_id'])));
13599 anikendra 59
			$prefix = "SHA".$storeProduct['StoreProduct']['store_id'];
60
			$tag = $prefix.time();
13558 anikendra 61
			if($storeProduct['StoreProduct']['store_id'] == 2){
13698 anikendra 62
				$url_parts = parse_url($storeProduct['StoreProduct']['url']);
63
				$url = "http://dl.flipkart.com/dl".$url_parts['path'].'?'.$url_parts['query'];
13633 anikendra 64
			} elseif($storeProduct['StoreProduct']['store_id'] == 3) {
65
				$url = $storeProduct['StoreProduct']['url']."?utm_source=aff_prog&utm_campaign=afts&offer_id=17";
13558 anikendra 66
			}else{
67
				$url = $storeProduct['StoreProduct']['url'];
68
			}
69
			if( strpos($url, '?') === false ) {
70
				$firstChar = '?';
71
			} else {
13561 anikendra 72
				$firstChar = '&';
13558 anikendra 73
			}
74
			$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
75
			if(!empty($store['Store']['sub_tag_param'])){
76
				$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
77
			}
78
			$extras = array('store'=>$store['Store']['name']);
79
			$data = array('user_id'=>$userId,'store_product_id'=>$storeProductId,'price'=>$storeProduct['StoreProduct']['available_price'],'tag'=>$tag,'url'=>$url,'extras'=>json_encode($extras));
13532 anikendra 80
			$this->Click->create();
13558 anikendra 81
			if ($this->Click->save($data)) {
82
				$result = array('success'=>true,'message'=>__('The click has been saved.'),'type'=>'redirect','url'=>$url);
13532 anikendra 83
			} else {
13558 anikendra 84
				$result = array('success'=>false,'message'=>__('The click could not be saved. Please, try again.'));
13532 anikendra 85
			}
13558 anikendra 86
			$this->response->type('json');
87
			$this->layout = 'ajax';
88
			$callback = $this->request->query('callback');
89
			$this->set(array(
90
			    'result' => $result,
91
			    'callback' => $callback,
92
			    '_serialize' => array('result')
93
			));
94
			$this->render('/Elements/jsonp');
13532 anikendra 95
		}
96
	}
97
 
98
/**
99
 * edit method
100
 *
101
 * @throws NotFoundException
102
 * @param string $id
103
 * @return void
104
 */
105
	public function edit($id = null) {
106
		if (!$this->Click->exists($id)) {
107
			throw new NotFoundException(__('Invalid click'));
108
		}
109
		if ($this->request->is(array('post', 'put'))) {
110
			if ($this->Click->save($this->request->data)) {
111
				$this->Session->setFlash(__('The click has been saved.'));
112
				return $this->redirect(array('action' => 'index'));
113
			} else {
114
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
115
			}
116
		} else {
117
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
118
			$this->request->data = $this->Click->find('first', $options);
119
		}
120
		$users = $this->Click->User->find('list');
121
		$storeProducts = $this->Click->StoreProduct->find('list');
122
		$this->set(compact('users', 'storeProducts'));
123
	}
124
 
125
/**
126
 * delete method
127
 *
128
 * @throws NotFoundException
129
 * @param string $id
130
 * @return void
131
 */
132
	public function delete($id = null) {
133
		$this->Click->id = $id;
134
		if (!$this->Click->exists()) {
135
			throw new NotFoundException(__('Invalid click'));
136
		}
137
		$this->request->onlyAllow('post', 'delete');
138
		if ($this->Click->delete()) {
139
			$this->Session->setFlash(__('The click has been deleted.'));
140
		} else {
141
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
142
		}
143
		return $this->redirect(array('action' => 'index'));
144
	}
145
 
146
/**
147
 * admin_index method
148
 *
149
 * @return void
150
 */
151
	public function admin_index() {
152
		$this->Click->recursive = 0;
153
		$this->set('clicks', $this->Paginator->paginate());
154
	}
155
 
156
/**
157
 * admin_view method
158
 *
159
 * @throws NotFoundException
160
 * @param string $id
161
 * @return void
162
 */
163
	public function admin_view($id = null) {
164
		if (!$this->Click->exists($id)) {
165
			throw new NotFoundException(__('Invalid click'));
166
		}
167
		$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
168
		$this->set('click', $this->Click->find('first', $options));
169
	}
170
 
171
/**
172
 * admin_add method
173
 *
174
 * @return void
175
 */
176
	public function admin_add() {
177
		if ($this->request->is('post')) {
178
			$this->Click->create();
179
			if ($this->Click->save($this->request->data)) {
180
				$this->Session->setFlash(__('The click has been saved.'));
181
				return $this->redirect(array('action' => 'index'));
182
			} else {
183
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
184
			}
185
		}
186
		$users = $this->Click->User->find('list');
187
		$storeProducts = $this->Click->StoreProduct->find('list');
188
		$this->set(compact('users', 'storeProducts'));
189
	}
190
 
191
/**
192
 * admin_edit method
193
 *
194
 * @throws NotFoundException
195
 * @param string $id
196
 * @return void
197
 */
198
	public function admin_edit($id = null) {
199
		if (!$this->Click->exists($id)) {
200
			throw new NotFoundException(__('Invalid click'));
201
		}
202
		if ($this->request->is(array('post', 'put'))) {
203
			if ($this->Click->save($this->request->data)) {
204
				$this->Session->setFlash(__('The click has been saved.'));
205
				return $this->redirect(array('action' => 'index'));
206
			} else {
207
				$this->Session->setFlash(__('The click could not be saved. Please, try again.'));
208
			}
209
		} else {
210
			$options = array('conditions' => array('Click.' . $this->Click->primaryKey => $id));
211
			$this->request->data = $this->Click->find('first', $options);
212
		}
213
		$users = $this->Click->User->find('list');
214
		$storeProducts = $this->Click->StoreProduct->find('list');
215
		$this->set(compact('users', 'storeProducts'));
216
	}
217
 
218
/**
219
 * admin_delete method
220
 *
221
 * @throws NotFoundException
222
 * @param string $id
223
 * @return void
224
 */
225
	public function admin_delete($id = null) {
226
		$this->Click->id = $id;
227
		if (!$this->Click->exists()) {
228
			throw new NotFoundException(__('Invalid click'));
229
		}
230
		$this->request->onlyAllow('post', 'delete');
231
		if ($this->Click->delete()) {
232
			$this->Session->setFlash(__('The click has been deleted.'));
233
		} else {
234
			$this->Session->setFlash(__('The click could not be deleted. Please, try again.'));
235
		}
236
		return $this->redirect(array('action' => 'index'));
237
	}}