Subversion Repositories web.creative

Rev

Rev 17 | Rev 20 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
1 mjordaan 1
<?php
2
class ControllerUserUser extends Controller {
3
	private $error = array();
4
 
5
	public function index() {
6
		$this->load->language('user/user');
7
 
8
		$this->document->setTitle($this->language->get('heading_title'));
9
 
10
		$this->load->model('user/user');
11
 
12
		$this->getList();
13
	}
14
 
15
	public function add() {
16
		$this->load->language('user/user');
17
 
18
		$this->document->setTitle($this->language->get('heading_title'));
19
 
20
		$this->load->model('user/user');
21
 
22
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
23
			$this->model_user_user->addUser($this->request->post);
24
 
25
			$this->session->data['success'] = $this->language->get('text_success');
26
 
27
			$url = '';
28
 
29
			if (isset($this->request->get['sort'])) {
30
				$url .= '&sort=' . $this->request->get['sort'];
31
			}
32
 
33
			if (isset($this->request->get['order'])) {
34
				$url .= '&order=' . $this->request->get['order'];
35
			}
36
 
37
			if (isset($this->request->get['page'])) {
38
				$url .= '&page=' . $this->request->get['page'];
39
			}
40
 
41
			$this->response->redirect($this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url, true));
42
		}
43
 
44
		$this->getForm();
45
	}
46
 
47
	public function edit() {
48
		$this->load->language('user/user');
49
 
50
		$this->document->setTitle($this->language->get('heading_title'));
51
 
52
		$this->load->model('user/user');
53
 
54
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
55
			$this->model_user_user->editUser($this->request->get['user_id'], $this->request->post);
56
 
57
			$this->session->data['success'] = $this->language->get('text_success');
58
 
59
			$url = '';
60
 
61
			if (isset($this->request->get['sort'])) {
62
				$url .= '&sort=' . $this->request->get['sort'];
63
			}
64
 
65
			if (isset($this->request->get['order'])) {
66
				$url .= '&order=' . $this->request->get['order'];
67
			}
68
 
69
			if (isset($this->request->get['page'])) {
70
				$url .= '&page=' . $this->request->get['page'];
71
			}
72
 
73
			$this->response->redirect($this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url, true));
74
		}
75
 
76
		$this->getForm();
77
	}
78
 
79
	public function delete() {
80
		$this->load->language('user/user');
81
 
82
		$this->document->setTitle($this->language->get('heading_title'));
83
 
84
		$this->load->model('user/user');
85
 
86
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
87
			foreach ($this->request->post['selected'] as $user_id) {
88
				$this->model_user_user->deleteUser($user_id);
89
			}
90
 
91
			$this->session->data['success'] = $this->language->get('text_success');
92
 
93
			$url = '';
94
 
95
			if (isset($this->request->get['sort'])) {
96
				$url .= '&sort=' . $this->request->get['sort'];
97
			}
98
 
99
			if (isset($this->request->get['order'])) {
100
				$url .= '&order=' . $this->request->get['order'];
101
			}
102
 
103
			if (isset($this->request->get['page'])) {
104
				$url .= '&page=' . $this->request->get['page'];
105
			}
106
 
107
			$this->response->redirect($this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url, true));
108
		}
109
 
110
		$this->getList();
111
	}
112
 
113
	protected function getList() {
114
		if (isset($this->request->get['sort'])) {
115
			$sort = $this->request->get['sort'];
116
		} else {
117
			$sort = 'username';
118
		}
119
 
120
		if (isset($this->request->get['order'])) {
121
			$order = $this->request->get['order'];
122
		} else {
123
			$order = 'ASC';
124
		}
125
 
126
		if (isset($this->request->get['page'])) {
127
			$page = $this->request->get['page'];
128
		} else {
129
			$page = 1;
130
		}
131
 
132
		$url = '';
133
 
134
		if (isset($this->request->get['sort'])) {
135
			$url .= '&sort=' . $this->request->get['sort'];
136
		}
137
 
138
		if (isset($this->request->get['order'])) {
139
			$url .= '&order=' . $this->request->get['order'];
140
		}
141
 
142
		if (isset($this->request->get['page'])) {
143
			$url .= '&page=' . $this->request->get['page'];
144
		}
145
 
146
		$data['breadcrumbs'] = array();
147
 
148
		$data['breadcrumbs'][] = array(
149
			'text' => $this->language->get('text_home'),
150
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
151
		);
152
 
153
		$data['breadcrumbs'][] = array(
154
			'text' => $this->language->get('heading_title'),
155
			'href' => $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url, true)
156
		);
157
 
158
		$data['add'] = $this->url->link('user/user/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
159
		$data['delete'] = $this->url->link('user/user/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
160
 
161
		$data['users'] = array();
162
 
163
		$filter_data = array(
164
			'sort'  => $sort,
165
			'order' => $order,
166
			'start' => ($page - 1) * $this->config->get('config_limit_admin'),
167
			'limit' => $this->config->get('config_limit_admin')
168
		);
169
 
170
		$user_total = $this->model_user_user->getTotalUsers();
171
 
172
		$results = $this->model_user_user->getUsers($filter_data);
173
 
174
		foreach ($results as $result) {
175
			$data['users'][] = array(
176
				'user_id'    => $result['user_id'],
177
				'username'   => $result['username'],
178
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
179
				'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
180
				'edit'       => $this->url->link('user/user/edit', 'user_token=' . $this->session->data['user_token'] . '&user_id=' . $result['user_id'] . $url, true)
181
			);
182
		}
183
 
184
		if (isset($this->error['warning'])) {
185
			$data['error_warning'] = $this->error['warning'];
186
		} else {
187
			$data['error_warning'] = '';
188
		}
189
 
190
		if (isset($this->session->data['success'])) {
191
			$data['success'] = $this->session->data['success'];
192
 
193
			unset($this->session->data['success']);
194
		} else {
195
			$data['success'] = '';
196
		}
197
 
198
		if (isset($this->request->post['selected'])) {
199
			$data['selected'] = (array)$this->request->post['selected'];
200
		} else {
201
			$data['selected'] = array();
202
		}
203
 
204
		$url = '';
205
 
206
		if ($order == 'ASC') {
207
			$url .= '&order=DESC';
208
		} else {
209
			$url .= '&order=ASC';
210
		}
211
 
212
		if (isset($this->request->get['page'])) {
213
			$url .= '&page=' . $this->request->get['page'];
214
		}
215
 
216
		$data['sort_username'] = $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . '&sort=username' . $url, true);
217
		$data['sort_status'] = $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . '&sort=status' . $url, true);
218
		$data['sort_date_added'] = $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . '&sort=date_added' . $url, true);
219
 
220
		$url = '';
221
 
222
		if (isset($this->request->get['sort'])) {
223
			$url .= '&sort=' . $this->request->get['sort'];
224
		}
225
 
226
		if (isset($this->request->get['order'])) {
227
			$url .= '&order=' . $this->request->get['order'];
228
		}
229
 
230
		$pagination = new Pagination();
231
		$pagination->total = $user_total;
232
		$pagination->page = $page;
233
		$pagination->limit = $this->config->get('config_limit_admin');
234
		$pagination->url = $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
235
 
236
		$data['pagination'] = $pagination->render();
237
 
238
		$data['results'] = sprintf($this->language->get('text_pagination'), ($user_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($user_total - $this->config->get('config_limit_admin'))) ? $user_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $user_total, ceil($user_total / $this->config->get('config_limit_admin')));
239
 
240
		$data['sort'] = $sort;
241
		$data['order'] = $order;
242
 
243
		$data['header'] = $this->load->controller('common/header');
244
		$data['column_left'] = $this->load->controller('common/column_left');
245
		$data['footer'] = $this->load->controller('common/footer');
246
 
247
		$this->response->setOutput($this->load->view('user/user_list', $data));
248
	}
249
 
250
	protected function getForm() {
251
		$data['text_form'] = !isset($this->request->get['user_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
252
 
253
		if (isset($this->error['warning'])) {
254
			$data['error_warning'] = $this->error['warning'];
255
		} else {
256
			$data['error_warning'] = '';
257
		}
258
 
259
		if (isset($this->error['username'])) {
260
			$data['error_username'] = $this->error['username'];
261
		} else {
262
			$data['error_username'] = '';
263
		}
264
 
265
		if (isset($this->error['password'])) {
266
			$data['error_password'] = $this->error['password'];
267
		} else {
268
			$data['error_password'] = '';
269
		}
270
 
271
		if (isset($this->error['confirm'])) {
272
			$data['error_confirm'] = $this->error['confirm'];
273
		} else {
274
			$data['error_confirm'] = '';
275
		}
276
 
277
		if (isset($this->error['firstname'])) {
278
			$data['error_firstname'] = $this->error['firstname'];
279
		} else {
280
			$data['error_firstname'] = '';
281
		}
282
 
283
		if (isset($this->error['lastname'])) {
284
			$data['error_lastname'] = $this->error['lastname'];
285
		} else {
286
			$data['error_lastname'] = '';
287
		}
288
 
289
		if (isset($this->error['email'])) {
290
			$data['error_email'] = $this->error['email'];
291
		} else {
292
			$data['error_email'] = '';
293
		}
294
 
295
		$url = '';
296
 
297
		if (isset($this->request->get['sort'])) {
298
			$url .= '&sort=' . $this->request->get['sort'];
299
		}
300
 
301
		if (isset($this->request->get['order'])) {
302
			$url .= '&order=' . $this->request->get['order'];
303
		}
304
 
305
		if (isset($this->request->get['page'])) {
306
			$url .= '&page=' . $this->request->get['page'];
307
		}
308
 
309
		$data['breadcrumbs'] = array();
310
 
311
		$data['breadcrumbs'][] = array(
312
			'text' => $this->language->get('text_home'),
313
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
314
		);
315
 
316
		$data['breadcrumbs'][] = array(
317
			'text' => $this->language->get('heading_title'),
318
			'href' => $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url, true)
319
		);
320
 
321
		if (!isset($this->request->get['user_id'])) {
322
			$data['action'] = $this->url->link('user/user/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
323
		} else {
324
			$data['action'] = $this->url->link('user/user/edit', 'user_token=' . $this->session->data['user_token'] . '&user_id=' . $this->request->get['user_id'] . $url, true);
325
		}
326
 
327
		$data['cancel'] = $this->url->link('user/user', 'user_token=' . $this->session->data['user_token'] . $url, true);
328
 
329
		if (isset($this->request->get['user_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
330
			$user_info = $this->model_user_user->getUser($this->request->get['user_id']);
331
		}
332
 
333
		if (isset($this->request->post['username'])) {
334
			$data['username'] = $this->request->post['username'];
335
		} elseif (!empty($user_info)) {
336
			$data['username'] = $user_info['username'];
337
		} else {
338
			$data['username'] = '';
339
		}
340
 
341
		if (isset($this->request->post['user_group_id'])) {
342
			$data['user_group_id'] = $this->request->post['user_group_id'];
343
		} elseif (!empty($user_info)) {
344
			$data['user_group_id'] = $user_info['user_group_id'];
345
		} else {
346
			$data['user_group_id'] = '';
347
		}
348
 
349
		$this->load->model('user/user_group');
350
 
351
		$data['user_groups'] = $this->model_user_user_group->getUserGroups();
352
 
353
		if (isset($this->request->post['password'])) {
354
			$data['password'] = $this->request->post['password'];
355
		} else {
356
			$data['password'] = '';
357
		}
358
 
359
		if (isset($this->request->post['confirm'])) {
360
			$data['confirm'] = $this->request->post['confirm'];
361
		} else {
362
			$data['confirm'] = '';
363
		}
364
 
365
		if (isset($this->request->post['firstname'])) {
366
			$data['firstname'] = $this->request->post['firstname'];
367
		} elseif (!empty($user_info)) {
368
			$data['firstname'] = $user_info['firstname'];
369
		} else {
370
			$data['firstname'] = '';
371
		}
372
 
373
		if (isset($this->request->post['lastname'])) {
374
			$data['lastname'] = $this->request->post['lastname'];
375
		} elseif (!empty($user_info)) {
376
			$data['lastname'] = $user_info['lastname'];
377
		} else {
378
			$data['lastname'] = '';
379
		}
380
 
381
		if (isset($this->request->post['email'])) {
382
			$data['email'] = $this->request->post['email'];
383
		} elseif (!empty($user_info)) {
384
			$data['email'] = $user_info['email'];
385
		} else {
386
			$data['email'] = '';
387
		}
388
 
389
		if (isset($this->request->post['image'])) {
390
			$data['image'] = $this->request->post['image'];
391
		} elseif (!empty($user_info)) {
392
			$data['image'] = $user_info['image'];
393
		} else {
394
			$data['image'] = '';
395
		}
396
 
397
		$this->load->model('tool/image');
398
 
399
		if (isset($this->request->post['image']) && is_file(DIR_IMAGE . $this->request->post['image'])) {
400
			$data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
401
		} elseif (!empty($user_info) && $user_info['image'] && is_file(DIR_IMAGE . $user_info['image'])) {
402
			$data['thumb'] = $this->model_tool_image->resize($user_info['image'], 100, 100);
403
		} else {
404
			$data['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);
405
		}
406
 
407
		$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);
408
 
409
		if (isset($this->request->post['status'])) {
410
			$data['status'] = $this->request->post['status'];
411
		} elseif (!empty($user_info)) {
412
			$data['status'] = $user_info['status'];
413
		} else {
414
			$data['status'] = 0;
415
		}
416
 
417
		$data['header'] = $this->load->controller('common/header');
418
		$data['column_left'] = $this->load->controller('common/column_left');
419
		$data['footer'] = $this->load->controller('common/footer');
420
 
421
		$this->response->setOutput($this->load->view('user/user_form', $data));
422
	}
423
 
424
	protected function validateForm() {
425
		if (!$this->user->hasPermission('modify', 'user/user')) {
426
			$this->error['warning'] = $this->language->get('error_permission');
427
		}
428
 
429
		if ((utf8_strlen($this->request->post['username']) < 3) || (utf8_strlen($this->request->post['username']) > 20)) {
430
			$this->error['username'] = $this->language->get('error_username');
431
		}
432
 
433
		$user_info = $this->model_user_user->getUserByUsername($this->request->post['username']);
434
 
435
		if (!isset($this->request->get['user_id'])) {
436
			if ($user_info) {
437
				$this->error['warning'] = $this->language->get('error_exists_username');
438
			}
439
		} else {
440
			if ($user_info && ($this->request->get['user_id'] != $user_info['user_id'])) {
441
				$this->error['warning'] = $this->language->get('error_exists_username');
442
			}
443
		}
444
 
445
		if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
446
			$this->error['firstname'] = $this->language->get('error_firstname');
447
		}
448
 
449
		if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
450
			$this->error['lastname'] = $this->language->get('error_lastname');
451
		}
452
 
453
		if ((utf8_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
454
			$this->error['email'] = $this->language->get('error_email');
455
		}
456
 
457
		$user_info = $this->model_user_user->getUserByEmail($this->request->post['email']);
458
 
459
		if (!isset($this->request->get['user_id'])) {
460
			if ($user_info) {
461
				$this->error['warning'] = $this->language->get('error_exists_email');
462
			}
463
		} else {
464
			if ($user_info && ($this->request->get['user_id'] != $user_info['user_id'])) {
465
				$this->error['warning'] = $this->language->get('error_exists_email');
466
			}
467
		}
468
 
469
		if ($this->request->post['password'] || (!isset($this->request->get['user_id']))) {
470
			if ((utf8_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 4) || (utf8_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
471
				$this->error['password'] = $this->language->get('error_password');
472
			}
473
 
474
			if ($this->request->post['password'] != $this->request->post['confirm']) {
475
				$this->error['confirm'] = $this->language->get('error_confirm');
476
			}
477
		}
478
 
479
		return !$this->error;
480
	}
481
 
482
	protected function validateDelete() {
483
		if (!$this->user->hasPermission('modify', 'user/user')) {
484
			$this->error['warning'] = $this->language->get('error_permission');
485
		}
486
 
487
		foreach ($this->request->post['selected'] as $user_id) {
488
			if ($this->user->getId() == $user_id) {
489
				$this->error['warning'] = $this->language->get('error_account');
490
			}
491
		}
492
 
493
		return !$this->error;
494
	}
495
}