summaryrefslogtreecommitdiff
path: root/src/views/pages/users/index.php
blob: d801faf02cf5f3cf9f4fcfdb1a4e91da20b636ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php global $mm;

$logged_in_user = $mm->getAuthObj($mm->isLoggedIn());
if (!$logged_in_user->isUser()) {
	include(VIEWPATH.'/pages/users/401.php');
	exit();
}

function attrib($key, $name, $check=false) {
	return array('key'=>$key, 'name'=>$name, 'checkbox'=>$check);
}

function getSetConf($user, $key) {
	global $mm;
	$logged_in_user = $mm->getAuthObj($mm->isLoggedIn());
	$uid = $user->getUID();
	$post_key = $key."[$uid]";
	@$value = $_POST[$post_key];
	$editable = $user->canEdit();
	$edit = isset($_POST[$post_key]);
	
	switch ($key) {
	case 'auth_name':
		if ($editable && $edit) $user->setName($value);
		$value = $user->getName();
		break;
	case 'auth_user':
		$editable = $editable && $logged_in_user->isAdmin();
		if ($editable && $edit) $user->setUser($value=='true');
		$value = $user->isUser();
		break;
	case 'auth_admin':
		$editable = $editable && $logged_in_user->isAdmin();
		if ($editable && $edit) $user->setAdmin($value=='true');
		$value = $user->isAdmin();
		break;
	default: 
		if ($editable && $edit) $user->setConf($key, $value);
		$value = $user->getConf($key);
		break;
	}
	
	return array(
	             'value'=>$value,
	             'post_key'=>$post_key,
	             'editable'=>$editable);
}

$attribs = array(attrib('auth_user', 'Active', true),
                 attrib('lastname','Last'),
                 attrib('firstname','First'),
                 attrib('hsclass','Class of'),
                 attrib('phone','Phone number'),
                 attrib('email','Email'),
                 attrib('auth_name', 'Username'),
                 );

////////////////////////////////////////////////////////////////////////////////

$t = $mm->template();
$mm->header('Users');

$t->openTag('form', array('action'=>$mm->baseUrl().'users',
                          'method'=>'post'));

$t->openTag('table');

$t->openTag('tr');
foreach ($attribs as $attrib) {
	$t->tag('th', array(), $attrib['name']);
}
$t->tag('th');
$t->closeTag('tr');

$uids = $mm->listUsers();
foreach ($uids as $uid) {
	$user = $mm->getAuthObj($uid);
	$t->openTag('tr');
	
	foreach ($attribs as $attrib) {
		$props = getSetConf($user, $attrib['key']);
		
		$arr = array('name'=>$props['post_key']);
		if (!$props['editable']) {
			$arr['readonly'] = 'readonly';
			if ($attrib['checkbox']) $arr['disabled'] = $disabled;
		}
		if ($attrib['checkbox']) {
			if ($props['value'])
				$arr['checked'] = 'checked';
			$arr['value'] = 'true';
			$arr['type'] = 'checkbox';
		} else {
			$arr['value'] = $props['value'];
			$arr['type'] = 'text';
		}
		
		$t->openTag('td');
		$t->tag('input', $arr);
		$t->closeTag('td');
	}
	
	$t->openTag('td');
	$t->link($mm->baseUrl().'users/'.$user->getName(), 'More');
	$t->closeTag('td');
	
	$t->closeTag('tr');
}

$t->closeTag('table');

$t->tag('input', array('type'=>'submit',
                       'value'=>'Save/Update'));
$t->closeTag('form');

$mm->footer();