Get current users roles in welcome_get_message rather than welcome_user_login
[project/welcome.git] / welcome.module
1 <?php
2 /**
3 * @file
4 * Module file for welcome
5 */
6
7 /**
8 * Implements hook_menu().
9 */
10 function welcome_menu() {
11 $items['admin/config/people/welcome'] = array(
12 'title' => 'Welcome message configure',
13 'page callback' => 'drupal_get_form',
14 'page arguments' => array('welcome_admin_settings'),
15 'access arguments' => array('administer users'),
16 'type' => MENU_NORMAL_ITEM,
17 'file' => 'welcome.admin.inc',
18 );
19
20 return $items;
21 }
22
23 /**
24 * Implements hook_user_login().
25 */
26 function welcome_user_login(&$edit, $account) {
27 $message = welcome_get_message($account);
28 $message_replaced = token_replace($message, array('user' => $account));
29 drupal_set_message(t($message_replaced));
30 }
31
32 /**
33 * Implements hook_theme().
34 */
35 function welcome_theme() {
36 return array(
37 'welcome_admin_settings' => array(
38 'render element' => 'form',
39 'file' => 'welcome.admin.inc',
40 ),
41 );
42 }
43
44 /**
45 * Get message for current user.
46 */
47 function welcome_get_message($account) {
48 $roles = $account->roles;
49 $rid = array_keys($roles);
50
51 $query = db_select('welcome_message', 'wm')
52 ->fields('wm', array('rid', 'message', 'weight'))
53 ->condition('rid', $rid, 'IN')
54 ->orderBy('weight', 'ASC')
55 ->range(0, 1);
56
57 $result = $query->execute();
58 $message = $result->fetch();
59
60 return $message->message;
61 }
62
63 /**
64 * Implements hook_help().
65 */
66 function welcome_help($path, $arg) {
67 switch ($path) {
68 case 'admin/config/people/welcome':
69 $output = '<p>' . t('<strong>Welcome message per role.</strong> Drag to order. If a user has more than one role, highest weighted role will be used.') . '</p>';
70 return $output;
71 }
72 }