Subversion Repositories SmartDukaan

Rev

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