1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Implementations of administration functions for the acl module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implementation of acl_edit_form().
|
10
|
*/
|
11
|
function _acl_edit_form($acl_id, $label = NULL, $new_acl = FALSE) {
|
12
|
$users = array();
|
13
|
if (!$new_acl) {
|
14
|
// Ensure the ACL in question even exists.
|
15
|
if (!($record = db_query("SELECT name, number FROM {acl} WHERE acl_id = :acl_id", array(
|
16
|
'acl_id' => $acl_id,
|
17
|
))->fetchAssoc())) {
|
18
|
return array();
|
19
|
}
|
20
|
$result = db_query("SELECT u.uid, u.name FROM {users} u LEFT JOIN {acl_user} aclu ON aclu.uid = u.uid WHERE acl_id = :acl_id", array(
|
21
|
'acl_id' => $acl_id));
|
22
|
foreach ($result as $user) {
|
23
|
$users[$user->uid] = $user->name;
|
24
|
}
|
25
|
}
|
26
|
if (!isset($label)) {
|
27
|
$label = (isset($record['name']) ? $record['name'] : (isset($record['number']) ? $record['number'] : $acl_id));
|
28
|
}
|
29
|
|
30
|
$form = array(
|
31
|
'#type' => 'fieldset',
|
32
|
'#collapsible' => TRUE,
|
33
|
'#title' => $label,
|
34
|
'#tree' => TRUE,
|
35
|
);
|
36
|
|
37
|
$form['acl_id'] = array(
|
38
|
'#type' => 'value',
|
39
|
'#value' => $acl_id,
|
40
|
);
|
41
|
|
42
|
$form['deletions'] = array(
|
43
|
'#type' => 'checkboxes',
|
44
|
'#options' => array(),
|
45
|
); // placeholder
|
46
|
$form['delete_button'] = array(
|
47
|
'#type' => 'button',
|
48
|
'#name' => 'acl_' . $acl_id,
|
49
|
'#value' => t('Remove Checked'),
|
50
|
'#submit' => FALSE,
|
51
|
);
|
52
|
|
53
|
$form['add'] = array(
|
54
|
'#type' => 'textfield',
|
55
|
'#title' => t('Add user'),
|
56
|
'#maxlength' => 60,
|
57
|
'#size' => 40,
|
58
|
'#autocomplete_path' => 'user/autocomplete',
|
59
|
);
|
60
|
$form['add_button'] = array(
|
61
|
'#type' => 'button',
|
62
|
'#name' => 'acl_' . $acl_id,
|
63
|
'#value' => t('Add User'),
|
64
|
'#submit' => FALSE,
|
65
|
);
|
66
|
|
67
|
$form['user_list'] = array(
|
68
|
'#type' => 'hidden',
|
69
|
'#default_value' => serialize($users),
|
70
|
);
|
71
|
|
72
|
$form['#after_build'] = array('_acl_edit_form_after_build');
|
73
|
|
74
|
return $form;
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Process a form that had our buttons on it.
|
79
|
*/
|
80
|
function _acl_edit_form_after_build($form, &$form_state) {
|
81
|
// We can't use the form values because it's the entire structure
|
82
|
// and we have no clue where our values actually are. That's
|
83
|
// ok tho cause #value still works for us.
|
84
|
$user_list = unserialize($form['user_list']['#value']);
|
85
|
$button_name = 'acl_' . $form['acl_id']['#value'];
|
86
|
|
87
|
if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] == $form['delete_button']['#value']) {
|
88
|
$deletions = $form['deletions']['#value'];
|
89
|
foreach ($deletions as $uid) {
|
90
|
unset($user_list[$uid]);
|
91
|
unset($form['deletions']['#value'][$uid]);
|
92
|
}
|
93
|
}
|
94
|
elseif (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] == $form['add_button']['#value'] && !empty($form['add']['#value'])) {
|
95
|
$user = db_query("SELECT uid, name FROM {users} WHERE name = :name", array(
|
96
|
'name' => $form['add']['#value'],
|
97
|
))->fetchObject();
|
98
|
if (!$user || !$user->uid) {
|
99
|
form_error($form['add'], t("Invalid user specified."));
|
100
|
}
|
101
|
else {
|
102
|
$user_list[$user->uid] = $user->name;
|
103
|
$form['add']['#value'] = NULL;
|
104
|
}
|
105
|
}
|
106
|
|
107
|
if (count($user_list) != 0) {
|
108
|
$form['deletions']['#type'] = 'checkboxes';
|
109
|
$form['deletions']['#title'] = t("Current users");
|
110
|
$form['deletions']['#options'] = $user_list;
|
111
|
$form['deletions']['#value'] = array(); // don't carry value through.
|
112
|
$form['deletions'] = form_builder(!empty($form['#post']) ? $form['#post']['form_id'] : 'acl_form', $form['deletions'], $form_state);
|
113
|
}
|
114
|
else {
|
115
|
$form['delete_button']['#type'] = 'value';
|
116
|
}
|
117
|
$form['user_list']['#value'] = serialize($user_list);
|
118
|
|
119
|
return $form;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Write the results of a form.
|
124
|
*
|
125
|
* The module that embedded our form must call this function!
|
126
|
*/
|
127
|
function acl_save_form($form, $priority = NULL) {
|
128
|
$users = unserialize($form['user_list']);
|
129
|
db_delete('acl_user')
|
130
|
->condition('acl_id', $form['acl_id'])
|
131
|
->execute();
|
132
|
foreach ($users as $uid => $name) {
|
133
|
db_insert('acl_user')
|
134
|
->fields(array(
|
135
|
'acl_id' => $form['acl_id'],
|
136
|
'uid' => $uid,
|
137
|
))
|
138
|
->execute();
|
139
|
}
|
140
|
if (isset($priority)) {
|
141
|
db_update('acl_node')
|
142
|
->fields(array(
|
143
|
'priority' => $priority,
|
144
|
))
|
145
|
->condition('acl_id', $form['acl_id'])
|
146
|
->execute();
|
147
|
}
|
148
|
}
|
149
|
|