/[drupal]/contributions/modules/autoassignrole/autoassignrole-admin.inc
ViewVC logotype

Contents of /contributions/modules/autoassignrole/autoassignrole-admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Fri Sep 25 16:08:43 2009 UTC (2 months ago) by cyberswat
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +285 -75 lines
File MIME type: text/x-php
Updating head with latest from d6
1 <?php
2 // $Id: autoassignrole-admin.inc,v 1.1.2.15 2009/09/23 13:55:01 cyberswat Exp $
3
4 /**
5 * @file
6 *
7 * The autoassignrole-admin.inc file
8 *
9 * Contains the admin form and associated functionality for the autoassignrole
10 * module
11 */
12
13 function autoassignrole_admin_form() {
14 $form['autoassignrole_settings_auto'] = array(
15 '#type' => 'fieldset',
16 '#title' => t('Automatic Role Assignment'),
17 '#description' => t('Automatically assigned roles will be attached to
18 accounts created through the administrative interface as well as normal
19 new user registration according to the following settings.'),
20 '#collapsible' => TRUE,
21 '#collapsed' => TRUE,
22 );
23 $form['autoassignrole_settings_auto']['auto_active'] = array(
24 '#type' => 'radios',
25 '#title' => t('Automatic role assignment'),
26 '#default_value' => _autoassignrole_get_settings('auto_active'),
27 '#description' => t('Automatic role assignment occurs when the user first
28 logins to the account. This happens without the users knowledge. Set to
29 Enabled to allow this functionality or Disabled to not allow.'),
30 '#options' => array(1 => t('Enabled'), 0 => t('Disabled')),
31 );
32 $form['autoassignrole_settings_auto']['auto_admin_active'] = array(
33 '#type' => 'radios',
34 '#title' => t('Automatic role assignment for admin created accounts'),
35 '#default_value' => _autoassignrole_get_settings('auto_admin_active'),
36 '#description' => t('Automatically assign roles if the user account is being
37 created by an administrator.'),
38 '#options' => array(1 => t('Enabled'), 0 => t('Disabled')),
39 );
40 $roles = user_roles(TRUE);
41 unset($roles[DRUPAL_AUTHENTICATED_RID]);
42 if ($roles) {
43 $form['autoassignrole_settings_auto']['auto_roles'] = array(
44 '#type' => 'checkboxes',
45 '#title' => t('Roles'),
46 '#default_value' => _autoassignrole_get_settings('auto_roles'),
47 '#description' => t('Check the specific Roles the user will automatically
48 be assigned to when created by an administrator or through the new user
49 registration process. The Authenticated User role is automatically
50 assigned by Drupal core and can not be edited.'),
51 '#options' => $roles,
52 );
53 }
54
55 $form['autoassignrole_settings_page'] = array(
56 '#type' => 'fieldset',
57 '#title' => t('Assign from Path'),
58 '#description' => t('Assign user roles based on the path the user
59 is creating the account from.'),
60 '#collapsible' => TRUE,
61 '#collapsed' => TRUE,
62 );
63 $default = '';
64 if ($nid = _autoassignrole_get_settings('node_user_register')) {
65 $node = db_fetch_object(db_query("SELECT nid, title FROM {node} WHERE nid = %d", $nid));
66 $default = check_plain($node->title) ." [nid:$node->nid]";
67 }
68 $form['autoassignrole_settings_page']['node_user_register'] = array(
69 '#type' => 'textfield',
70 '#title' => t('User Register Node'),
71 '#description' => t('This is a node that you want to use to replace the default user/register landing page. A common use would be to populate this node with a description and links to each of the other paths you define below. '),
72 '#maxlength' => 60,
73 '#autocomplete_path' => 'admin/user/autoassignrole/autocomplete/node',
74 '#default_value' => $default,
75 '#weight' => -1,
76 );
77 $result = db_query("SELECT rid, display, path, title, description, format, weight, menu, registration FROM {autoassignrole_page}");
78 $defaults = array();
79 while ($setting = db_fetch_object($result)) {
80 $defaults[$setting->rid] = array(
81 'display' => $setting->display,
82 'path' => $setting->path,
83 'title' => $setting->title,
84 'description' => $setting->description,
85 'format' => $setting->format,
86 'weight' => $setting->weight,
87 'menu' => $setting->menu,
88 'registration' => $setting->registration,
89 );
90 }
91 foreach ($roles as $k => $v) {
92 $active = (isset($defaults[$k]['display']) ? 1 : 0);
93 $form['autoassignrole_settings_page'][$k] = array(
94 '#type' => 'fieldset',
95 '#title' => $v,
96 '#collapsible' => TRUE,
97 '#collapsed' => ($active ? FALSE : TRUE),
98 );
99 $form['autoassignrole_settings_page'][$k]["path_active_$k"] = array(
100 '#type' => 'radios',
101 '#title' => check_plain($v) .' '. t('status'),
102 '#default_value' => $active,
103 '#description' => t('Enable or disable this role for path based role assignment'),
104 '#options' => array(1 => t('Enabled'), 0 => t('Disabled')),
105 );
106 $form['autoassignrole_settings_page'][$k]["path_registration_$k"] = array(
107 '#type' => 'radios',
108 '#title' => t('Default Registration Page'),
109 '#default_value' => ($defaults[$k]['registration'] ? 1 : 0),
110 '#description' => t('Should this path replace the default user/register path?'),
111 '#options' => array(1 => t('Yes'), 0 => t('No')),
112 );
113 $form['autoassignrole_settings_page'][$k]["path_display_$k"] = array(
114 '#type' => 'radios',
115 '#title' => t('Display Method'),
116 '#default_value' => (isset($defaults[$k]['display']) ? $defaults[$k]['display'] : 0),
117 '#description' => t('Menu Items are displayed as normal menu items. Tabs on registration pages display on the standard user/* pages. Pages with no navigation do not display in the menu and rely on you to provide a method for end users to reach them. For example, if you set this path as the default registration page and select Pages with no navigation, the default login block will have a link to your registration page but it will not show up in any menu.'),
118 '#options' => array(0 => t('Menu Items'), 1 => t('Tabs on registration page'), 2 => 'Pages with no navigation'),
119 );
120 $form['autoassignrole_settings_page'][$k]["path_menu_$k"] = array(
121 '#type' => 'select',
122 '#title' => t('Menu'),
123 '#default_value' => $defaults[$k]['menu'],
124 '#description' => t('If this path is going to be displayed as a menu item which parent menu should it appear under? This setting will be ignored for all Display Methods except Menu Items.'),
125 '#options' => menu_get_menus(),
126 );
127 $form['autoassignrole_settings_page'][$k]["path_weight_$k"] = array(
128 '#type' => 'weight',
129 '#title' => t('Weight'),
130 '#default_value' => (isset($defaults[$k]['weight']) ? $defaults[$k]['weight'] : '0'),
131 '#delta' => 10,
132 '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
133 );
134 $form['autoassignrole_settings_page'][$k]["path_$k"] = array(
135 '#type' => 'textfield',
136 '#title' => t('Path'),
137 '#description' => t('The Drupal path for this role. This is a relative path based on your Drupal installation.'),
138 '#default_value' => (isset($defaults[$k]['path']) ? $defaults[$k]['path'] : ''),
139 '#size' => 60,
140 '#maxlength' => 128,
141 '#required' => FALSE,
142 );
143 $form['autoassignrole_settings_page'][$k]["path_title_$k"] = array(
144 '#type' => 'textfield',
145 '#title' => t('Title'),
146 '#description' => t('The title that will be displayed to users in menus, if applicable, and on the path landing page.'),
147 '#default_value' => (isset($defaults[$k]['title']) ? $defaults[$k]['title'] : ''),
148 '#size' => 60,
149 '#maxlength' => 128,
150 '#required' => FALSE,
151 );
152 $form['autoassignrole_settings_page'][$k]["path_description_$k"] = array(
153 '#type' => 'textarea',
154 '#title' => t('Description'),
155 '#description' => t('The description displayed to the end user when they are
156 selecting their role during registration. This description is filtered
157 in a similar fashion to node body content.'),
158 '#default_value' => (isset($defaults[$k]['description']) ? $defaults[$k]['description'] : ''),
159 '#required' => FALSE,
160 );
161 $format = (isset($defaults[$k]['format']) ? $defaults[$k]['format'] : FILTER_FORMAT_DEFAULT);
162 $form['autoassignrole_settings_page'][$k]["format"] = filter_form($format);
163 foreach ($form['autoassignrole_settings_page'][$k]["format"] as $key => $value) {
164 if (is_numeric($key) && isset($value['#parents'][0])) {
165 $form['autoassignrole_settings_page'][$k]["format"][$key]['#parents'][0] = "path_format_$k";
166 }
167 }
168 }
169
170 $form['autoassignrole_settings_user'] = array(
171 '#type' => 'fieldset',
172 '#title' => t('Allow User to Choose'),
173 '#collapsible' => TRUE,
174 '#collapsed' => TRUE,
175 );
176 $form['autoassignrole_settings_user']['user_active'] = array(
177 '#type' => 'radios',
178 '#title' => t('User Role Assignment'),
179 '#default_value' => _autoassignrole_get_settings('user_active'),
180 '#description' => t('The end user will be allowed to select the following
181 roles when they log in.'),
182 '#options' => array(1 => t('Enabled'), 0 => t('Disabled')),
183 );
184 if ($roles) {
185 $form['autoassignrole_settings_user']['user_roles'] = array(
186 '#type' => 'checkboxes',
187 '#title' => t('Roles'),
188 '#default_value' => _autoassignrole_get_settings('user_roles'),
189 '#options' => $roles,
190 );
191 }
192
193 $form['autoassignrole_settings_user']['selection']['user_multiple'] = array(
194 '#type' => 'radios',
195 '#title' => t('User Role Selection'),
196 '#default_value' => _autoassignrole_get_settings('user_multiple'),
197 '#description' => t('Should the end user be allowed to choose a single role
198 or can they choose multiple roles?'),
199 '#options' => array(0 => t('Single Role'), 1 => t('Multiple Roles')),
200 );
201 $form['autoassignrole_settings_user']['user_selection'] = array(
202 '#type' => 'radios',
203 '#title' => t('Selection Method'),
204 '#default_value' => _autoassignrole_get_settings('user_selection'),
205 '#description' => t('The type of form elements the end user will be presented with.'),
206 '#options' => array(0 => t('Radio Buttons'), 1 => t('Selection Box'), 2 => t('Check Boxes')),
207 );
208 $form['autoassignrole_settings_user']['user_required'] = array(
209 '#type' => 'radios',
210 '#title' => t('Required'),
211 '#default_value' => _autoassignrole_get_settings('user_required'),
212 '#description' => t('Should the end user be required to choose a role?'),
213 '#options' => array(0 => t('No'), 1 => t('Yes')),
214 );
215 $form['autoassignrole_settings_user']['user_sort'] = array(
216 '#type' => 'radios',
217 '#title' => t('Sorting'),
218 '#default_value' => _autoassignrole_get_settings('user_sort'),
219 '#description' => t('Default sort order of roles the user will see.'),
220 '#options' => array(
221 'SORT_ASC' => t('Ascending'),
222 'SORT_DESC' => t('Descending')
223 ),
224 );
225 $form['autoassignrole_settings_user']['user_fieldset_title'] = array(
226 '#type' => 'textfield',
227 '#title' => t('User Role Fieldset Title'),
228 '#description' => t('The title of the fieldset that contains role
229 options.'),
230 '#default_value' => _autoassignrole_get_settings('user_fieldset_title'),
231 '#size' => 60,
232 '#maxlength' => 128
233 );
234 $form['autoassignrole_settings_user']['user_title'] = array(
235 '#type' => 'textfield',
236 '#title' => t('User Role Title'),
237 '#description' => t('The title of the field that contains the role options
238 the end user sees during registration.'),
239 '#default_value' => _autoassignrole_get_settings('user_title'),
240 '#size' => 60,
241 '#maxlength' => 128,
242 '#required' => FALSE,
243 );
244 $form['autoassignrole_settings_user']['user_description'] = array(
245 '#type' => 'textarea',
246 '#title' => t('User Role Description'),
247 '#description' => t('The description displayed to the end user when they are
248 selecting their role during registration.'),
249 '#default_value' => _autoassignrole_get_settings('user_description'),
250 '#required' => FALSE,
251 );
252
253 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
254 return $form;
255 }
256 function autoassignrole_admin_form_validate($form_id, &$form_state) {
257
258 // path based role validation
259 $roles = user_roles(TRUE);
260 unset($roles[DRUPAL_AUTHENTICATED_RID]);
261 // Create a registration array and store all paths that are set to replace
262 // the default user/register page
263 $registration = array();
264 foreach ($roles as $rid => $role) {
265 if ($form_state['values']["path_active_$rid"]) {
266 $format = $form_state['values']["path_format_$rid"];
267 $path = $form_state['values']["path_$rid"];
268 $title = $form_state['values']["path_title_$rid"];
269 $description = $form_state['values']["path_description_$rid"];
270 $display = $form_state['values']["path_display_$rid"];
271 if ($form_state['values']["path_registration_$rid"]) {
272 $registration[] = $rid;
273 // If this is going to function as the default user registration page it
274 // can not have a Display Method of Tabs on registration page
275 if ($display == 1) {
276 form_set_error("path_display_$rid", t('If a path serves as the default user registration page it can not have a Display Method of Tabs on registration page'));
277 }
278 }
279
280 // Check that a display option is set
281 if ($display == '') {
282 form_set_error("path_display_$rid", t('Please select a valid display method for enabled URL role assignments'));
283 }
284
285 // check that path exists, is syntactically correct, and that it is not
286 // an existing Drupal path
287 if ($path) {
288 if (ereg("[^A-Za-z0-9/_-]+", $path) || strpos(trim($path), '/') === 0) {
289 form_set_error("path_$rid", '\''. check_plain($path) .'\' '. t('is not a valid path'));
290 }
291 else {
292 $menu_link = db_fetch_object(db_query("SELECT count(mlid) AS count FROM {menu_links} where link_path like '%s'", $path));
293 if (drupal_lookup_path('source', $path) || $menu_link->count > 0) {
294 // the menu item exists so need to check if the path has
295 // something other than autoassign_role_path registered
296 // otherwise throw an error
297 $menu_item = menu_get_item($path);
298 if (!isset($menu_item['page_callback']) && $menu_item['page_callback'] != 'autoassign_role_path') {
299 form_set_error("path_$rid", '\''. check_plain($path) .'\' '. t('is already in use as a path'));
300 }
301 }
302
303 // if the menu items are being displayed as tabs on user/* the path can
304 // not be constructed in a way that makes the tab a child of an existing
305 // tab. register/* and password/* will fail
306 if (($display == 1) && (strpos(trim($path), 'register') === 0 || strpos(trim($path), 'password') === 0)) {
307 form_set_error("path_$rid", '\''. check_plain($path) .'\' '. t('can only be used as a path if the Display Method is not Tabs on registration page.'));
308 }
309 }
310 }
311 else {
312 form_set_error("path_$rid", t('Please enter a valid path for enabled URL role assignments'));
313 }
314
315 // If the display option is a menu item or links we need a title to display
316 if (($display == '0' || $display == '1')) {
317 $title = ereg_replace('[:space:]', '', $title);
318 if (!strlen($title)) {
319 form_set_error("path_title_$rid", t('Please enter a valid title for enabled URL role assignments'));
320 }
321 }
322 }
323 }
324
325 // If the registration array has more than one item in it throw an error
326 if (count($registration) > 1) {
327 form_set_error(NULL, t('Only one path can can serve as the default user register page'));
328 }
329 if (count($registration) > 0 && !empty($form_state['values']['node_user_register'])) {
330 form_set_error('node_user_register', t('You can not replace the user register page with a node if a path has been designated as the user register page.'));
331 }
332 // User chooses their own role validation
333 if ($form_state['values']['user_active'] == 1) {
334 if ($form_state['values']['user_multiple'] == 1 &&
335 $form_state['values']['user_selection'] == 0) {
336 form_set_error('user_selection', t('If a user can select multiple roles they can not use Radio Buttons as the selection method.'));
337 }
338 if (strlen(trim($form_state['values']['user_title'])) == 0) {
339 form_set_error('user_title', t('Enter the title of the form fields the user will be presented with.'));
340 }
341 }
342 }
343 function autoassignrole_admin_form_submit($form_id, &$form_state) {
344 $sql = "UPDATE {autoassignrole} set value = '%s' where arid = '%s'";
345 db_query($sql, $form_state['values']['auto_active'], 'auto_active');
346 db_query($sql, serialize($form_state['values']['auto_roles']), 'auto_roles');
347 db_query($sql, $form_state['values']['user_active'], 'user_active');
348 db_query($sql, serialize($form_state['values']['user_roles']), 'user_roles');
349 db_query($sql, $form_state['values']['user_multiple'], 'user_multiple');
350 db_query($sql, $form_state['values']['user_title'], 'user_title');
351 db_query($sql, $form_state['values']['user_fieldset_title'], 'user_fieldset_title');
352 db_query($sql, $form_state['values']['user_required'], 'user_required');
353 db_query($sql, $form_state['values']['user_sort'], 'user_sort');
354 db_query($sql, $form_state['values']['user_description'], 'user_description');
355 db_query($sql, $form_state['values']['user_selection'], 'user_selection');
356 db_query($sql, $form_state['values']['auto_admin_active'], 'auto_admin_active');
357 preg_match('/\[nid:(\d+)\]/', $form_state['values']['node_user_register'], $matches);
358 db_query($sql, $matches[1], 'node_user_register');
359
360
361
362 // Role assignment based on path
363 db_query("truncate {autoassignrole_page}");
364 $roles = user_roles(TRUE);
365 unset($roles[DRUPAL_AUTHENTICATED_RID]);
366 foreach ($roles as $k => $v) {
367 if ($form_state['values']["path_active_$k"]) {
368 $path = $form_state['values']["path_$k"];
369 $title = $form_state['values']["path_title_$k"];
370 $description = $form_state['values']["path_description_$k"];
371 $display = $form_state['values']["path_display_$k"];
372 $format = $form_state['values']["path_format_$k"];
373 $weight = $form_state['values']["path_weight_$k"];
374 $menu = $form_state['values']["path_menu_$k"];
375 $registration = $form_state['values']["path_registration_$k"];
376 db_query("INSERT INTO {autoassignrole_page} (rid, display, path, title, description, format, weight, menu, registration) VALUES(%d, '%s', '%s', '%s', '%s', %d, %d, '%s', %d)", $k, $display, $path, $title, $description, $format, $weight, $menu, $registration);
377 }
378 }
379 // rebuild the menu
380 menu_rebuild();
381 drupal_set_message(t('The changes have been saved.'));
382 }
383
384
385 /**
386 * Menu callback; Retrieve a JSON object containing autocomplete suggestions for existing nodes.
387 */
388 function autoassignrole_autocomplete_node($string = '') {
389 $matches = array();
390 if ($string) {
391 $result = db_query_range("SELECT nid, title FROM {node} WHERE LOWER(title) LIKE LOWER('%s%%') OR nid = '%d'", $string, $string, 0, 10);
392 while ($node= db_fetch_object($result)) {
393 // Add a class wrapper for a few required CSS overrides.
394 $matches[check_plain($node->title) ." [nid:$node->nid]"] = "<div class=\"reference-autocomplete\">". check_plain($node->title) ." [nid:$node->nid]</div>";
395 }
396 }
397
398 drupal_json($matches);
399 }

  ViewVC Help
Powered by ViewVC 1.1.2