Subversion Repositories SmartDukaan

Rev

Rev 14547 | Go to most recent revision | Details | 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());
142
	}
143
 
144
/**
145
 * admin_view method
146
 *
147
 * @throws NotFoundException
148
 * @param string $id
149
 * @return void
150
 */
151
	public function admin_view($id = null) {
152
		if (!$this->Campaign->exists($id)) {
153
			throw new NotFoundException(__('Invalid campaign'));
154
		}
155
		$options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
156
		$this->set('campaign', $this->Campaign->find('first', $options));
157
	}
158
 
159
/**
160
 * admin_add method
161
 *
162
 * @return void
163
 */
164
	public function admin_add() {
165
		if ($this->request->is('post')) {
166
			$this->log(print_r($this->request->data,1));
167
			$this->Campaign->create();
168
			if ($this->Campaign->save($this->request->data)) {
169
				$this->Session->setFlash(__('The campaign has been saved.'));
170
				return $this->redirect(array('action' => 'index'));
171
			} else {
172
				$this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
173
			}
174
		}
175
	}
176
 
177
/**
178
 * admin_edit method
179
 *
180
 * @throws NotFoundException
181
 * @param string $id
182
 * @return void
183
 */
184
	public function admin_edit($id = null) {
185
		if (!$this->Campaign->exists($id)) {
186
			throw new NotFoundException(__('Invalid campaign'));
187
		}
188
		if ($this->request->is(array('post', 'put'))) {
189
			if ($this->Campaign->save($this->request->data)) {
190
				$this->Session->setFlash(__('The campaign has been saved.'));
191
				return $this->redirect(array('action' => 'index'));
192
			} else {
193
				$this->Session->setFlash(__('The campaign could not be saved. Please, try again.'));
194
			}
195
		} else {
196
			$options = array('conditions' => array('Campaign.' . $this->Campaign->primaryKey => $id));
197
			$this->request->data = $this->Campaign->find('first', $options);
198
		}
199
	}
200
 
201
/**
202
 * admin_delete method
203
 *
204
 * @throws NotFoundException
205
 * @param string $id
206
 * @return void
207
 */
208
	public function admin_delete($id = null) {
209
		$this->Campaign->id = $id;
210
		if (!$this->Campaign->exists()) {
211
			throw new NotFoundException(__('Invalid campaign'));
212
		}
213
		$this->request->onlyAllow('post', 'delete');
214
		if ($this->Campaign->delete()) {
215
			$this->Session->setFlash(__('The campaign has been deleted.'));
216
		} else {
217
			$this->Session->setFlash(__('The campaign could not be deleted. Please, try again.'));
218
		}
219
		return $this->redirect(array('action' => 'index'));
220
	}
221
}
222
 
223
if (!function_exists('getallheaders')) 
224
{ 
225
    function getallheaders() 
226
    { 
227
           $headers = ''; 
228
       foreach ($_SERVER as $name => $value) 
229
       { 
230
           if (substr($name, 0, 5) == 'HTTP_') 
231
           { 
232
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
233
           } 
234
       } 
235
       return $headers; 
236
    } 
237
}