/[drupal]/contributions/modules/autoassignrole/autoassignrole.module
ViewVC logotype

Contents of /contributions/modules/autoassignrole/autoassignrole.module

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


Revision 1.17 - (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.16: +454 -103 lines
File MIME type: text/x-php
Updating head with latest from d6
1 <?php
2 // $Id: autoassignrole.module,v 1.9.2.27 2009/08/25 13:04:44 cyberswat Exp $
3
4 /**
5 * @file
6 *
7 * The main autoassignrole.module file
8 *
9 * Designate a role to assign all new users to in addition to providing a
10 * mechanism for new users to select a role or role for their account.
11 */
12
13 /**
14 * Implementation of hook_menu().
15 *
16 * @return array
17 */
18 function autoassignrole_menu() {
19 $items = array();
20 $items['admin/user/autoassignrole/autocomplete/node'] = array(
21 'title' => 'Node autocomplete',
22 'page callback' => 'autoassignrole_autocomplete_node',
23 'access callback' => 'user_access',
24 'access arguments' => array('administer autoassignrole'),
25 'type' => MENU_CALLBACK,
26 'file' => 'autoassignrole-admin.inc',
27 );
28 $items['admin/user/autoassignrole'] = array(
29 'title' => t('Auto assign role'),
30 'description' => t('Designate a role to assign all new users to.'),
31 'page callback' => 'drupal_get_form',
32 'page arguments' => array('autoassignrole_admin_form'),
33 'access arguments' => array('administer autoassignrole'),
34 'type' => MENU_NORMAL_ITEM,
35 'file' => 'autoassignrole-admin.inc',
36 );
37
38 // path based role assignments that are listed as a menu item
39 $result = db_query("SELECT rid, path, display, title, weight, menu FROM {autoassignrole_page}");
40 while ($r = db_fetch_object($result)) {
41 switch ($r->display) {
42 case 0:
43 $items[$r->path] = array(
44 'title' => check_plain($r->title),
45 'page arguments' => array($r->rid),
46 'page callback' => 'autoassignrole_path',
47 'access callback' => '_autoassignrole_path_access',
48 'file' => 'autoassignrole-path.inc',
49 'weight' => $r->weight,
50 'type' => MENU_NORMAL_ITEM,
51 'menu_name' => $r->menu,
52 );
53 $items[$r->path .'/register'] = array(
54 'title' => 'Create new account',
55 'page arguments' => array($r->rid),
56 'page callback' => 'autoassignrole_path',
57 'access callback' => '_autoassignrole_path_access',
58 'file' => 'autoassignrole-path.inc',
59 'type' => MENU_DEFAULT_LOCAL_TASK,
60 );
61 $items[$r->path .'/login'] = array(
62 'title' => 'Log in',
63 'access callback' => 'user_is_anonymous',
64 'type' => MENU_LOCAL_TASK,
65 'page callback' => '_autoassignrole_user_login',
66 'file' => 'autoassignrole-path.inc',
67 );
68
69 $items[$r->path .'/password'] = array(
70 'title' => 'Request new password',
71 'access callback' => 'user_is_anonymous',
72 'type' => MENU_LOCAL_TASK,
73 'page callback' => '_autoassignrole_user_password',
74 'file' => 'autoassignrole-path.inc',
75 );
76 break;
77 // tabs on user registration pages
78 case 1:
79 $items['user/'. $r->path] = array(
80 'title' => check_plain($r->title),
81 'page arguments' => array($r->rid),
82 'page callback' => 'autoassignrole_path',
83 'access callback' => '_autoassignrole_path_access',
84 'file' => 'autoassignrole-path.inc',
85 'type' => MENU_LOCAL_TASK,
86 'weight' => $r->weight
87 );
88 break;
89 // no menu items
90 case 2:
91 $items[$r->path] = array(
92 'title' => check_plain($r->title),
93 'page arguments' => array($r->rid),
94 'page callback' => 'autoassignrole_path',
95 'access callback' => '_autoassignrole_path_access',
96 'file' => 'autoassignrole-path.inc',
97 'weight' => $r->weight,
98 'type' => MENU_CALLBACK,
99 );
100 $items[$r->path .'/register'] = array(
101 'title' => 'Create new account',
102 'page arguments' => array($r->rid),
103 'page callback' => 'autoassignrole_path',
104 'access callback' => '_autoassignrole_path_access',
105 'file' => 'autoassignrole-path.inc',
106 'type' => MENU_DEFAULT_LOCAL_TASK,
107 );
108 $items[$r->path .'/login'] = array(
109 'title' => 'Log in',
110 'access callback' => 'user_is_anonymous',
111 'type' => MENU_LOCAL_TASK,
112 'page callback' => '_autoassignrole_user_login',
113 'file' => 'autoassignrole-path.inc',
114 );
115
116 $items[$r->path .'/password'] = array(
117 'title' => 'Request new password',
118 'access callback' => 'user_is_anonymous',
119 'type' => MENU_LOCAL_TASK,
120 'page callback' => '_autoassignrole_user_password',
121 'file' => 'autoassignrole-path.inc',
122 );
123 break;
124 }
125
126 }
127 return $items;
128 }
129
130 function _autoassignrole_path_access() {
131 global $user;
132 if (variable_get('user_register', 1) && $user->uid == 0) {
133 return TRUE;
134 }
135 if (arg(0) == 'admin' && arg(2) == 'menu-customize' && user_access('administer menu', $user)) {
136 return TRUE;
137 }
138
139 return FALSE;
140 }
141
142 /**
143 * Implementation of hook_perm().
144 * @return array
145 */
146 function autoassignrole_perm() {
147 return array('administer autoassignrole');
148 }
149
150 /**
151 * Implementation of hook_user().
152 */
153 function autoassignrole_user($op, &$edit, &$account, $category = NULL) {
154 switch ($op) {
155 case 'insert':
156 // If this is an administrator creating the account only use auto_assign if
157 // allowed by auto_admin_active
158 if (arg(0) == 'admin' && _autoassignrole_get_settings('auto_admin_active') == 0) {
159 return;
160 }
161 $rolenames = user_roles();
162 if (_autoassignrole_get_settings('user_active') == 1) {
163 $roles = array();
164 $user_roles = _autoassignrole_get_settings('user_roles');
165 if (is_array($edit['user_roles'])) {
166 foreach ($edit['user_roles'] as $k => $v) {
167 if ($v != 0 && in_array($k, $user_roles, TRUE)) {
168 $roles[$k] = $v;
169 }
170 }
171 $edit['roles'] = $roles;
172 }
173 elseif ($edit['user_roles'] != '') {
174 $edit['roles'] = array($edit['user_roles'] => $edit['user_roles']);
175 }
176 }
177 if ($rid = autoassignrole_get_active_path_rid()) {
178 $edit['roles'][$rid] = $rolenames[$rid];
179 }
180 if (_autoassignrole_get_settings('auto_active') == 1) {
181 $auto_roles = _autoassignrole_get_settings('auto_roles');
182 foreach ($auto_roles as $k => $v) {
183 $edit['roles'][$k] = $rolenames[$k];
184 }
185 }
186 $account = user_load(array("uid" => $account->uid));
187 break;
188 case 'register':
189 if (_autoassignrole_get_settings('user_active') == 1) {
190 $roles = user_roles(TRUE);
191 $user_roles = _autoassignrole_get_settings('user_roles');
192 $path_roles = db_query("SELECT rid from {autoassignrole_page}");
193 while ($path_role = db_fetch_object($path_roles)) {
194 unset($roles[$path_role->rid]);
195 }
196 foreach ($roles as $k => $r) {
197 if (!in_array($k, $user_roles, TRUE)) {
198 unset($roles[$k]);
199 }
200 }
201 if (count($roles)) {
202 $form['autoassignrole_user'] = array(
203 '#type' => 'fieldset',
204 '#title' => _autoassignrole_get_settings('user_fieldset_title'),
205 '#collapsible' => FALSE,
206 '#collapsed' => FALSE,
207 );
208 if (_autoassignrole_get_settings('user_sort') == "SORT_ASC") {
209 uasort($roles, '_autoassignrole_array_asc');
210 }
211 else {
212 uasort($roles, '_autoassignrole_array_desc');
213 }
214
215 if (!$edit || !array_key_exists('user_roles', $edit)) {
216 $edit['user_roles'] = array();
217 }
218 if (_autoassignrole_get_settings('user_selection') == 0) {
219 $form['autoassignrole_user']['user_roles'] = array(
220 '#type' => 'radios',
221 '#title' => t(_autoassignrole_get_settings('user_title')),
222 '#options' => $roles,
223 '#description' => t(_autoassignrole_get_settings('user_description')),
224 );
225 }
226 if (_autoassignrole_get_settings('user_selection') == 1) {
227 $form['autoassignrole_user']['user_roles'] = array(
228 '#type' => 'select',
229 '#title' => t(_autoassignrole_get_settings('user_title')),
230 '#default_value' => '',
231 '#description' => t(_autoassignrole_get_settings('user_description')),
232 );
233
234 if (_autoassignrole_get_settings('user_multiple')) {
235 $form['autoassignrole_user']['user_roles']['#multiple'] = TRUE;
236 unset($form['autoassignrole_user']['user_roles']['#default_value']);
237 }
238 else {
239 $roles[''] = '';
240 }
241 $form['autoassignrole_user']['user_roles']['#options'] = $roles;
242 }
243 if (_autoassignrole_get_settings('user_selection') == 2) {
244 $form['autoassignrole_user']['user_roles'] = array(
245 '#type' => 'checkboxes',
246 '#title' => t(_autoassignrole_get_settings('user_title')),
247 '#default_value' => $edit['user_roles'],
248 '#options' => $roles,
249 '#description' => _autoassignrole_get_settings('user_description'),
250 );
251 }
252 if (_autoassignrole_get_settings('user_required')) {
253 $form['autoassignrole_user']['user_roles']['#required'] = TRUE;
254 }
255 return $form;
256 }
257 }
258 break;
259 }
260 }
261
262 /**
263 * API function that returns an array of AAR's settings.
264 *
265 */
266 function _autoassignrole_get_settings($value) {
267 // if we are dealing with a variable in the format of path_field_rid get
268 // variables from {autoassignrole_page}
269 if (preg_match('/^path_(\D[^_]*)_(\d*)/i', $value, $matches)) {
270 $result = db_fetch_array(db_query("SELECT rid, display, path, title, description, format, weight FROM {autoassignrole_page} WHERE rid = %d", $matches[2]));
271 if (count($result) > 1 && $matches[1] == 'active') {
272 return 1;
273 }
274 elseif (count($result) == 1 && $matches[1] == 'active') {
275 return 0;
276 }
277 else {
278 if (isset($result[$matches[1]])) {
279 return $result[$matches[1]];
280 }
281 else {
282 return FALSE;
283 }
284 }
285 }
286
287 if (preg_match('/^path_(\d*)/i', $value, $matches)) {
288 $result = db_fetch_array(db_query("SELECT path FROM {autoassignrole_page} WHERE rid = %d", $matches[1]));
289 if (isset($result['path'])) {
290 return $result['path'];
291 }
292 else {
293 return FALSE;
294 }
295 }
296
297 // if we are not dealing with {autoassignrole_page} then get variables from
298 // {autoassignrole}
299 $settings = array();
300 $result = db_query("SELECT arid, value from {autoassignrole}");
301 while ($s = db_fetch_object($result)) {
302 $settings[$s->arid] = $s->value;
303 }
304
305 // break apart auto_roles[x] and user_roles[x]
306 if (preg_match('/^(\D[^_]*_\D[^_]*)\[(\d*)]/i', $value, $matches)) {
307 $values = unserialize($settings[$matches[1]]);
308 return $values[$matches[2]];
309 }
310
311 switch ($value) {
312 case 'auto_roles':
313 case 'user_roles':
314 // return all instances as an array
315 $roles = $settings[$value];
316 $roles = unserialize($roles);
317 if (!is_array($roles)) {
318 $roles = array();
319 }
320 foreach ($roles as $k => $r) {
321 if ($r == 0) {
322 unset($roles[$k]);
323 }
324 }
325 return $roles;
326 break;
327 default:
328 if (isset($settings[$value])) {
329 return $settings[$value];
330 }
331 else {
332 return FALSE;
333 }
334 break;
335 }
336 }
337
338 function _autoassignrole_path($rid, $value) {
339 static $pages = array();
340 if (count($pages) == 0) {
341 $result = db_query("SELECT rid, display, path, title, description from {autoassignrole_page}");
342 while ($s = db_fetch_object($result)) {
343 $pages[$s->rid] = array('path' => $s->path,
344 'display' => $s->display,
345 'title' => $s->title,
346 'description' => $s->description
347 );
348 }
349 return _autoassignrole_get_settings($rid, $value);
350 }
351 else {
352 if (isset($pages[$rid][$value])) {
353 $return = $pages[$rid][$value];
354 }
355 else {
356 $return = '';
357 }
358 return $return;
359 }
360 }
361
362 function _autoassignrole_user_input($args) {
363 switch ($args) {
364 case 'type':
365 if (_autoassignrole_get_settings('user_multiple') == 0) {
366 $type = 'radios';
367 }
368 else {
369 $type = 'checkboxes';
370 }
371 return $type;
372 break;
373 case 'required':
374 if (_autoassignrole_get_settings('user_required') == 0) {
375 $required = FALSE;
376 }
377 else {
378 $required = TRUE;
379 }
380 return $required;
381 break;
382 }
383 }
384
385 function _autoassignrole_array_intersect_key($isec, $keys) {
386 $argc = func_num_args();
387 if ($argc > 2) {
388 for ($i = 1; !empty($isec) && $i < $argc; $i++) {
389 $arr = func_get_arg($i);
390 foreach (array_keys($isec) as $key) {
391 if (!isset($arr[$key])) {
392 unset($isec[$key]);
393 }
394 }
395 }
396 return $isec;
397 }
398 else {
399 $res = array();
400 foreach (array_keys($isec) as $key) {
401 if (isset($keys[$key])) {
402 $res[$key] = t($isec[$key]);
403 }
404 }
405 return $res;
406 }
407 }
408
409 /**
410 * method to sort array in a descending fashion while preserving keys
411 * @param string $a a string to compare
412 * @param string $b a string to compare
413 * @return int
414 */
415 function _autoassignrole_array_desc($a, $b) {
416 if ($a == $b) {
417 return 0;
418 }
419 return ($a > $b) ? -1 : 1;
420 }
421
422 /**
423 * method to sort array in an ascending fashion while preserving keys
424 * @param string $a a string to compare
425 * @param string $b a string to compare
426 * @return int
427 */
428 function _autoassignrole_array_asc($a, $b) {
429 if ($a == $b) {
430 return 0;
431 }
432 return ($a < $b) ? -1 : 1;
433 }
434
435 /**
436 * Implementation of hook_form_alter().
437 *
438 * Integrate with content profile's registration integration
439 */
440 function autoassignrole_form_alter(&$form, $form_state, $form_id) {
441 if ($form_id == 'content_profile_admin_settings') {
442 $type = $form_state['type'];
443 $result = db_query("SELECT ar.path, ar.rid, r.name FROM {autoassignrole_page} ar INNER JOIN {role} r ON ar.rid = r.rid");
444 $options = array();
445 while ($r = db_fetch_object($result)) {
446 $options[$r->rid] = filter_xss_admin("($r->name) $r->path");
447 }
448 $form['registration']['autoassignrole_use'] = array(
449 '#type' => 'checkboxes',
450 '#title' => t('Use on Auto Assign Role paths'),
451 '#default_value' => content_profile_get_settings($type, 'autoassignrole_use'),
452 '#options' => $options,
453 '#description' => t('The Auto Assign Role module gives you the ability to assign paths a user can register from for a role. After associating a <a href="@aar">path with a role</a> your selection can associate this content type with a path.', array('@aar' => url('admin/user/autoassignrole'))),
454 '#disabled' => count($options) == 0,
455 '#weight' => 3,
456 );
457 array_unshift($form['#submit'], 'autoassignrole_content_profile_admin_form_submit');
458 }
459 elseif ($form_id == 'user_register' && module_exists('content_profile_registration')) {
460 if (implode('/', arg()) != 'user/register' && $rid = autoassignrole_get_active_path_rid()) {
461 // Set the content types which should be shown by the content_profile_registration module
462 $form['#content_profile_registration_use_types'] = array();
463 foreach (content_profile_get_types('names') as $type => $name) {
464 if (in_array($rid, content_profile_get_settings($type, 'autoassignrole_use'))) {
465 $form['#content_profile_registration_use_types'][$type] = $name;
466 }
467 }
468 }
469 }
470 }
471
472 function autoassignrole_content_profile_admin_form_submit($form, &$form_state) {
473 $form_state['values']['autoassignrole_use'] = array_keys(array_filter($form_state['values']['autoassignrole_use']));
474 }
475
476 /**
477 * Implementation of hook_content_profile_settings().
478 */
479 function autoassignrole_content_profile_settings() {
480 return array(
481 'autoassignrole_use' => array_keys(user_roles((TRUE))),
482 );
483 }
484
485 /**
486 * Returns the role id of the currently active AAR-path.
487 */
488 function autoassignrole_get_active_path_rid() {
489 $item = menu_get_item();
490 if ($item['page_callback'] == 'autoassignrole_path') {
491 return $item['page_arguments'][0];
492 }
493 return FALSE;
494 }
495
496 /**
497 * An API like call to return the roles a user has available or will be assigned
498 *
499 * @param $op
500 * string all, auto, path, user
501 * @param $path
502 * array Optional array of paths to restrict selection to when using an $op of path
503 * @return
504 * array An array of roles
505 */
506 function autoassignrole_get_roles($op = 'all', $path = array()) {
507 $roles = user_roles();
508 $aar_roles = array();
509
510 switch ($op) {
511 case 'all':
512 // Select all auto and user selectable roles
513 $sql = "SELECT value FROM {autoassignrole} WHERE arid = 'auto_roles' OR arid = 'user_roles'";
514 $result = db_fetch_object(db_query($sql));
515 $aar_roles = unserialize($result->value);
516
517 // Select all path assignable roles
518 $sql = "SELECT DISTINCT rid FROM {autoassignrole_page}";
519 $result = db_query($sql);
520 while ($row = db_fetch_object($result)) {
521 $aar_roles[$row->rid] = $row->rid;
522 }
523 break;
524 case 'auto':
525 // Select all auto roles
526 $sql = "SELECT value FROM {autoassignrole} WHERE arid = 'auto_roles'";
527 $result = db_fetch_object(db_query($sql));
528 $aar_roles = unserialize($result->value);
529 break;
530 case 'path':
531 // Select path based roles
532 if (count($path) == 0) {
533 $sql = "SELECT DISTINCT rid FROM {autoassignrole_page}";
534 $result = db_fetch_object(db_query($sql));
535 }
536 else {
537 $sql = "SELECT DISTINCT rid FROM {autoassignrole_page} WHERE path IN(%s)";
538 $result = db_fetch_object(db_query($sql, implode(',', $path)));
539 }
540 while ($row = db_fetch_object($result)) {
541 $aar_roles[$row->rid] = $row->rid;
542 }
543 break;
544 case 'user':
545 // Select all user selectable roles
546 $sql = "SELECT value FROM {autoassignrole} WHERE arid = 'user_roles'";
547 $result = db_fetch_object(db_query($sql));
548 $aar_roles = unserialize($result->value);
549 break;
550 }
551
552 // Use the results of user_roles() and unset anything not available from AAR
553 foreach ($roles as $key => $role) {
554 if (!array_key_exists($key, $aar_roles)) {
555 unset($roles[$key]);
556 }
557 }
558
559 return $roles;
560 }
561
562 /**
563 * Implementation of hook_menu_alter()
564 *
565 * Redirect the default user register page to a path that has been designated as
566 * the replacement registration path.
567 */
568 function autoassignrole_menu_alter(&$items) {
569 $row = db_fetch_object(db_query("SELECT rid, path, display, title, weight, menu, registration FROM {autoassignrole_page} WHERE registration = 1"));
570 if ($row->registration) {
571 $path = $row->path;
572 }
573 else {
574 $row = db_fetch_object(db_query("SELECT arid, value FROM {autoassignrole} WHERE arid = '%s'", 'node_user_register'));
575 if (!empty($row->value)) {
576 $path = 'node/'. $row->value;
577 }
578 }
579 if ($path) {
580 $items['user/register'] = array(
581 'page arguments' => array($path),
582 'page callback' => 'drupal_goto',
583 'access callback' => 'user_register_access',
584 'type' => MENU_LOCAL_TASK,
585 );
586 }
587 }

  ViewVC Help
Powered by ViewVC 1.1.2