Subversion Repositories SmartDukaan

Rev

Rev 14509 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14509 anikendra 1
<?php
2
App::uses('AppController', 'Controller');
3
/**
4
 * Campaigns Controller
5
 *
6
 * @property Campaign $Campaign
7
 * @property PaginatorComponent $Paginator
8
 */
9
class CampaignsController extends AppController {
10
 
11
/**
12
 * Components
13
 *
14
 * @var array
15
 */
16
	public $components = array('Paginator','RequestHandler');
17
 
18
/**
19
 * index method
20
 *
21
 * @return void
22
 */
23
	public function index() {
24
		$country = 'IN';
25
		foreach (getallheaders() as $name => $value) {
26
			if($name == "Cf-Ipcountry"){
27
				//error_log("[XFWDF] [country]- $value");
28
				//if($value != 'XX')
29
				//	$country = $value;
30
			}
31
		}
32
		$this->Campaign->recursive = 0;
33
		$options = array('conditions'=>array('status'=>'active','OR' => array(array('target_countries LIKE'=>'%'.$country.'%'),array('target_countries' => null))),'order'=>array('clicks'=>'asc'));
34
		$cachekey = "campaign-$country";
35
		$response = Cache::read($cachekey,'hour');
36
		if(empty($response)) {
37
			$response = $this->Campaign->find('first',$options);
38
			Cache::write($cachekey,$response,'hour');
39
		}
40
		if ($this->request->is('requested')) {
41
		    return $response;
42
		} else {
43
		    $this->set('campaign', $response);
44
		}	
45
	}
46
 
47
/**
48
 * view method
49
 *
50
 * @throws NotFoundException
51
 * @param string $id
52
 * @return void
53
 */
54
	public function view($id = null) {
55
		$this->layout= 'ajax';
56
		if (!$this->Campaign->exists($id)) {
57
			throw new NotFoundException(__('Invalid campaign'));
58
		}
59
		$options = array('conditions' => array('Campaign.id'=> $id));
60
		$campaign  = $this->Campaign->find('first', $options);
61
		$campaign['Campaign']['clicks'] = $campaign['Campaign']['clicks'] + 1;
62
		$this->Campaign->save($campaign);
63
		$url = $campaign['Campaign']['url'];
64
		$url = str_replace('INSERTPUBLISHERID','hotbhojpuri',$url);
65
		$url = str_replace('INSERTS2STRACKING',$campaign['Campaign']['id'],$url);
66
		$this->set('url',$url);
67
	}
68
 
69
/**
70
 * add method
71
 *
72
 * @return void
73
 */
74
	public function add() {
75
		throw new NotFoundException(__('Invalid campaign'));
76
		if ($this->request->is('post')) {
77
			$this->Campaign->create();
78
			if ($this->Campaign->save($this->request->data)) {
79
				$this->Session->setFlash(__('The campaign has been saved.'));
80
				return $this->redirect(array('action' => 'index'));
81
			} else {
82
				$this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
83
			}
84
		}
85
	}
86
 
87
/**
88
 * edit method
89
 *
90
 * @throws NotFoundException
91
 * @param string $id
92
 * @return void
93
 */
94
	public function edit($id = null) {
95
		throw new NotFoundException(__('Invalid campaign'));
96
		if (!$this->Campaign->exists($id)) {
97
			throw new NotFoundException(__('Invalid campaign'));
98
		}
99
		if ($this->request->is(array('post', 'put'))) {
100
			if ($this->Campaign->save($this->request->data)) {
101
				$this->Session->setFlash(__('The campaign has been saved.'));
102
				return $this->redirect(array('action' => 'index'));
103
			} else {
104
				$this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
105
			}
106
		} else {
107
			$options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
108
			$this->request->data = $this->Campaign->find('first', $options);
109
		}
110
	}
111
 
112
/**
113
 * delete method
114
 *
115
 * @throws NotFoundException
116
 * @param string $id
117
 * @return void
118
 */
119
	public function delete($id = null) {
120
		throw new NotFoundException(__('Invalid campaign'));
121
		$this->Campaign->id = $id;
122
		if (!$this->Campaign->exists()) {
123
			throw new NotFoundException(__('Invalid campaign'));
124
		}
125
		$this->request->onlyAllow('post', 'delete');
126
		if ($this->Campaign->delete()) {
127
			$this->Session->setFlash(__('The campaign has been deleted.'));
128
		} else {
129
			$this->Session->setFlash(__('The campaign could not be deleted. Please, try again.'));
130
		}
131
		return $this->redirect(array('action' => 'index'));
132
	}
133
 
134
/**
135
 * admin_index method
136
 *
137
 * @return void
138
 */
139
	public function admin_index() {
140
		$this->Campaign->recursive = 0;
141
		$this->set('campaigns', $this->Paginator->paginate());
14547 anikendra 142
		$this->loadModel('Store');
143
		$this->set('stores',$this->Store->find('list'));
14509 anikendra 144
	}
145
 
146
/**
147
 * admin_view method
148
 *
149
 * @throws NotFoundException
150
 * @param string $id
151
 * @return void
152
 */
153
	public function admin_view($id = null) {
154
		if (!$this->Campaign->exists($id)) {
155
			throw new NotFoundException(__('Invalid campaign'));
156
		}
157
		$options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
158
		$this->set('campaign', $this->Campaign->find('first', $options));
159
	}
160
 
161
/**
162
 * admin_add method
163
 *
164
 * @return void
165
 */
166
	public function admin_add() {
167
		if ($this->request->is('post')) {
168
			$this->log(print_r($this->request->data,1));
169
			$this->Campaign->create();
170
			if ($this->Campaign->save($this->request->data)) {
171
				$this->Session->setFlash(__('The campaign has been saved.'));
172
				return $this->redirect(array('action' => 'index'));
173
			} else {
174
				$this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
175
			}
176
		}
14547 anikendra 177
		$this->loadModel('Store');
178
		$this->set('stores',$this->Store->find('list'));
14509 anikendra 179
	}
180
 
181
/**
182
 * admin_edit method
183
 *
184
 * @throws NotFoundException
185
 * @param string $id
186
 * @return void
187
 */
188
	public function admin_edit($id = null) {
189
		if (!$this->Campaign->exists($id)) {
190
			throw new NotFoundException(__('Invalid campaign'));
191
		}
192
		if ($this->request->is(array('post', 'put'))) {
193
			if ($this->Campaign->save($this->request->data)) {
194
				$this->Session->setFlash(__('The campaign has been saved.'));
195
				return $this->redirect(array('action' => 'index'));
196
			} else {
197
				$this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
198
			}
199
		} else {
200
			$options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
201
			$this->request->data = $this->Campaign->find('first', $options);
202
		}
14547 anikendra 203
		$this->loadModel('Store');
204
		$this->set('stores',$this->Store->find('list'));
14509 anikendra 205
	}
206
 
207
/**
208
 * admin_delete method
209
 *
210
 * @throws NotFoundException
211
 * @param string $id
212
 * @return void
213
 */
214
	public function admin_delete($id = null) {
215
		$this->Campaign->id = $id;
216
		if (!$this->Campaign->exists()) {
217
			throw new NotFoundException(__('Invalid campaign'));
218
		}
219
		$this->request->onlyAllow('post', 'delete');
220
		if ($this->Campaign->delete()) {
221
			$this->Session->setFlash(__('The campaign has been deleted.'));
222
		} else {
223
			$this->Session->setFlash(__('The campaign could not be deleted. Please, try again.'));
224
		}
225
		return $this->redirect(array('action' => 'index'));
226
	}
227
}
228
 
229
if (!function_exists('getallheaders')) 
230
{ 
231
    function getallheaders() 
232
    { 
233
           $headers = ''; 
234
       foreach ($_SERVER as $name => $value) 
235
       { 
236
           if (substr($name, 0, 5) == 'HTTP_') 
237
           { 
238
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
239
           } 
240
       } 
241
       return $headers; 
242
    } 
243
}