Subversion Repositories SmartDukaan

Rev

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