1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Administration menu callbacks for pm_block_user.module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Builds row of sending, receiving roles and actions that go with them.
|
10 |
|
|
*
|
11 |
|
|
* @param $details
|
12 |
|
|
* Details of the row: default values and the unique row number (delta).
|
13 |
|
|
* @param $blacklist
|
14 |
|
|
* When the functionality has been added, this will allow building actions
|
15 |
|
|
* based on a whitelist or blacklist. The current code only covers the use
|
16 |
|
|
* case of a blacklist, where blocking everyone is allowed by default and
|
17 |
|
|
* rules are exceptions to that. Conversely, a whitelist will disallow
|
18 |
|
|
* blocking by default and rules will configure roles that are allowed to
|
19 |
|
|
* block.
|
20 |
|
|
*
|
21 |
|
|
* @return
|
22 |
|
|
* Part of a form with controls for sending, receiving and actions.
|
23 |
|
|
*/
|
24 |
|
|
function _pm_block_user_actions_form($details, $blacklist = TRUE) {
|
25 |
|
|
$form = array(
|
26 |
|
|
'#tree' => TRUE,
|
27 |
|
|
);
|
28 |
|
|
$delta = $details['delta'];
|
29 |
|
|
// FALSE by default, or if the user has checked the 'Enabled' check box for
|
30 |
|
|
// this row.
|
31 |
|
|
$row_disabled = (isset($details['enabled']) ? !$details['enabled'] : FALSE);
|
32 |
|
|
|
33 |
|
|
$form['author'] = array(
|
34 |
|
|
'#type' => 'select',
|
35 |
|
|
'#options' => user_roles(TRUE),
|
36 |
|
|
'#default_value' => (isset($details['author']) ? $details['author'] : DRUPAL_AUTHENTICATED_RID),
|
37 |
|
|
'#disabled' => $row_disabled,
|
38 |
|
|
);
|
39 |
|
|
$form['recipient'] = array(
|
40 |
|
|
'#type' => 'select',
|
41 |
|
|
'#options' => user_roles(TRUE),
|
42 |
|
|
'#default_value' => (isset($details['recipient']) ? $details['recipient'] : DRUPAL_AUTHENTICATED_RID),
|
43 |
|
|
'#disabled' => $row_disabled,
|
44 |
|
|
);
|
45 |
|
|
|
46 |
|
|
// Provide different action radios if we're using a whitelist or a blacklist.
|
47 |
|
|
if ($blacklist) {
|
48 |
|
|
$options = array(
|
49 |
|
|
PM_BLOCK_USER_DISALLOW_BLOCKING => t('Disallow blocking author'),
|
50 |
|
|
PM_BLOCK_USER_DISALLOW_SENDING => t('Disallow sending message'),
|
51 |
|
|
);
|
52 |
|
|
$default_value = (isset($details['action']) ? $details['action'] : PM_BLOCK_USER_DISALLOW_BLOCKING);
|
53 |
|
|
}
|
54 |
|
|
else {
|
55 |
|
|
// TODO: add whitelist options/default_value here.
|
56 |
|
|
}
|
57 |
|
|
$form['action'] = array(
|
58 |
|
|
'#type' => 'radios',
|
59 |
|
|
'#options' => $options,
|
60 |
|
|
'#disabled' => $row_disabled,
|
61 |
|
|
'#default_value' => $default_value,
|
62 |
|
|
);
|
63 |
|
|
|
64 |
|
|
$form['enabled'] = array(
|
65 |
|
|
'#type' => 'checkbox',
|
66 |
|
|
'#default_value' => (isset($details['enabled']) ? $details['enabled'] : TRUE),
|
67 |
|
|
);
|
68 |
|
|
$form['remove'] = array(
|
69 |
|
|
'#type' => 'submit',
|
70 |
|
|
'#submit' => array('pm_block_user_remove_submit'),
|
71 |
|
|
'#value' => t('Remove_' . $delta),
|
72 |
|
|
'#attributes' => array('class' => array('remove-action')),
|
73 |
|
|
'#prefix' => '<div id="remove-rule-button">',
|
74 |
|
|
'#suffix' => '<label for="edit-remove">' . t('Remove rule') . '</label></div>',
|
75 |
|
|
'#ajax' => array(
|
76 |
|
|
'callback' => 'pm_block_user_js',
|
77 |
|
|
'wrapper' => 'block-actions',
|
78 |
|
|
),
|
79 |
|
|
);
|
80 |
|
|
|
81 |
|
|
return $form;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
|
|
* Takes an array of settings and filters out anything that's un-needed.
|
86 |
|
|
* Leaving only settings to be saved.
|
87 |
|
|
*
|
88 |
|
|
* @param $settings
|
89 |
|
|
* The array of settings to filter.
|
90 |
|
|
*
|
91 |
|
|
* @return
|
92 |
|
|
* Array of settings, ready to be stored in the database.
|
93 |
|
|
*
|
94 |
|
|
* @see pm_block_user_settings_submit()
|
95 |
|
|
*/
|
96 |
|
|
function _pm_block_user_settings_filter($settings) {
|
97 |
|
|
// Add-in the names of any settings to be saved into the array below.
|
98 |
|
|
$save_keys = array('author', 'recipient', 'action', 'enabled');
|
99 |
|
|
$matching = array();
|
100 |
|
|
// Run through each of the keys we want to save, creating a new array.
|
101 |
|
|
// It's not possible to simply check for unwanted values and unset() them as
|
102 |
|
|
// the array is multi-dimensional.
|
103 |
|
|
foreach ($save_keys as $save_key) {
|
104 |
|
|
if (isset($settings[$save_key])) {
|
105 |
|
|
$matching[$save_key] = $settings[$save_key];
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
if (count($matching) > 0) {
|
109 |
|
|
return $matching;
|
110 |
|
|
}
|
111 |
|
|
else {
|
112 |
|
|
return array_map('_pm_block_user_settings_filter', $settings);
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
/**
|
117 |
|
|
* Menu callback for AHAH handling.
|
118 |
|
|
*/
|
119 |
|
|
function pm_block_user_js($form, $form_state) {
|
120 |
|
|
return drupal_render($form['block_actions']);
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
/**
|
124 |
|
|
* Submit handler for 'More' button, adds a new action.
|
125 |
|
|
*
|
126 |
|
|
* @see pm_block_user_remove_submit()
|
127 |
|
|
*/
|
128 |
|
|
function pm_block_user_more_submit($form, &$form_state) {
|
129 |
|
|
unset($form_state['submit_handlers']);
|
130 |
|
|
form_execute_handlers('submit', $form, $form_state);
|
131 |
|
|
// Get the submitted actions, then put them into a special area of
|
132 |
|
|
// the $form_state.
|
133 |
|
|
$submitted_values = $form_state['values'];
|
134 |
|
|
// Add an empty action.
|
135 |
|
|
$submitted_values['block_actions'][] = array();
|
136 |
|
|
$form_state['pm_block_user'] = $submitted_values;
|
137 |
|
|
// Rebuild the form by passing our $form_state through the
|
138 |
|
|
// pm_block_user_settings() builder function.
|
139 |
|
|
$form_state['rebuild'] = TRUE;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Submit handler for 'Remove' button, removes an action.
|
144 |
|
|
*
|
145 |
|
|
* @see pm_block_user_more_submit()
|
146 |
|
|
*/
|
147 |
|
|
function pm_block_user_remove_submit($form, &$form_state) {
|
148 |
|
|
unset($form_state['submit_handlers']);
|
149 |
|
|
form_execute_handlers('submit', $form, $form_state);
|
150 |
|
|
$submitted_values = $form_state['values'];
|
151 |
|
|
// Remove the requested action.
|
152 |
|
|
$delta = $form_state['clicked_button']['#parents'][1];
|
153 |
|
|
unset($submitted_values['block_actions'][$delta]);
|
154 |
|
|
$form_state['pm_block_user'] = $submitted_values;
|
155 |
|
|
$form_state['rebuild'] = TRUE;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/**
|
159 |
|
|
* Menu callback for blocked user settings.
|
160 |
|
|
*/
|
161 |
|
|
function pm_block_user_settings($form, &$form_state) {
|
162 |
|
|
drupal_add_css(drupal_get_path('module', 'pm_block_user') . '/pm_block_user.css');
|
163 |
|
|
// Need to cache form for AHAH, so it can be rebuilt from cache later.
|
164 |
|
|
$form = array(
|
165 |
|
|
'#cache' => TRUE,
|
166 |
|
|
);
|
167 |
|
|
|
168 |
|
|
// Container for just the actions, used for AHAH.
|
169 |
|
|
$form['block_actions'] = array(
|
170 |
|
|
'#tree' => TRUE,
|
171 |
|
|
'#prefix' => '<div id="block-actions">',
|
172 |
|
|
'#suffix' => '</div>',
|
173 |
|
|
'#theme' => 'pm_block_user_actions',
|
174 |
|
|
);
|
175 |
|
|
|
176 |
|
|
// Should we populate the form with data from $form_state or the database?
|
177 |
|
|
if (!isset($form_state['pm_block_user']['block_actions'])) {
|
178 |
|
|
$block_actions = variable_get('pm_block_user_actions', array());
|
179 |
|
|
}
|
180 |
|
|
else {
|
181 |
|
|
$block_actions = $form_state['pm_block_user']['block_actions'];
|
182 |
|
|
}
|
183 |
|
|
// Work through each rule, adding it as a new element in
|
184 |
|
|
// $form['block_actions'] ready to be themed later.
|
185 |
|
|
foreach ($block_actions as $delta => $details) {
|
186 |
|
|
$details['delta'] = $delta;
|
187 |
|
|
$form['block_actions'][$delta] = _pm_block_user_actions_form($details);
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// The magic AHAH callback button that adds more rows.
|
191 |
|
|
$form['pm_block_actions_more'] = array(
|
192 |
|
|
'#type' => 'submit',
|
193 |
|
|
'#value' => t('More'),
|
194 |
|
|
'#weight' => 1,
|
195 |
|
|
'#prefix' => '<div id="add-rule-button">',
|
196 |
|
|
'#suffix' => '<label for="edit-pm-block-actions-more">' . t('Add new rule') . '</label></div>',
|
197 |
|
|
'#submit' => array('pm_block_user_more_submit'),
|
198 |
|
|
'#ajax' => array(
|
199 |
|
|
'callback' => 'pm_block_user_js',
|
200 |
|
|
'wrapper' => 'block-actions',
|
201 |
|
|
'method' => 'replace',
|
202 |
|
|
'effect' => 'fade',
|
203 |
|
|
),
|
204 |
|
|
);
|
205 |
|
|
|
206 |
|
|
$form['submit_form'] = array(
|
207 |
|
|
'#type' => 'submit',
|
208 |
|
|
'#weight' => 10,
|
209 |
|
|
'#value' => t('Save configuration'),
|
210 |
|
|
);
|
211 |
|
|
|
212 |
|
|
return $form;
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
/**
|
216 |
|
|
* Submit handler for admin form.
|
217 |
|
|
*/
|
218 |
|
|
function pm_block_user_settings_submit($form, &$form_state) {
|
219 |
|
|
// We don't want it to submit when we're adding/removing actions.
|
220 |
|
|
if ($form_state['clicked_button']['#id'] == 'edit-submit-form') {
|
221 |
|
|
// If the form's 'block_actions' aren't set, the user has deleted all the
|
222 |
|
|
// rows in the table, so we save an empty array to stop errors in the form
|
223 |
|
|
// builder.
|
224 |
|
|
if (isset($form_state['values']['block_actions'])) {
|
225 |
|
|
variable_set('pm_block_user_actions', _pm_block_user_settings_filter($form_state['values']['block_actions']));
|
226 |
|
|
}
|
227 |
|
|
else {
|
228 |
|
|
variable_set('pm_block_user_actions', array());
|
229 |
|
|
}
|
230 |
|
|
drupal_set_message(t('The configuration options have been saved.'));
|
231 |
|
|
}
|
232 |
|
|
} |