summaryrefslogtreecommitdiffstats
path: root/welcome.module
blob: 7e77c79be3d56f7650a106d8e0ef18b4c730483b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
 * @file
 * Module file for welcome
 */

/**
 * Implements hook_menu().
 */
function welcome_menu() {
  $items['admin/config/people/welcome'] = array(
    'title' => 'Welcome message configure',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('welcome_admin_settings'),
    'access arguments' => array('administer users'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'welcome.admin.inc',
  );

  return $items;
}

/**
 * Implements hook_user_login().
 */
function welcome_user_login(&$edit, $account) {
  $message = welcome_get_message($account);
  $message_replaced = token_replace($message, array('user' => $account));
  drupal_set_message(t($message_replaced));
}

/**
 * Implements hook_theme().
 */
function welcome_theme() {
  return array(
    'welcome_admin_settings' => array(
      'render element' => 'form',
      'file' => 'welcome.admin.inc',
    ),
  );
}

/**
 * Get message for current user.
 */
function welcome_get_message($account) {
  $roles = $account->roles;
  $rid = array_keys($roles);

  $query = db_select('welcome_message', 'wm')
    ->fields('wm', array('rid', 'message', 'weight'))
    ->condition('rid', $rid, 'IN')
    ->orderBy('weight', 'ASC')
    ->range(0, 1);

  $result = $query->execute();
  $message = $result->fetch();

  return $message->message;
}

/**
 * Implements hook_help().
 */
function welcome_help($path, $arg) {
  switch ($path) {
    case 'admin/config/people/welcome':
      $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>';
      return $output;
  }
}