Get current users roles in welcome_get_message rather than welcome_user_login
[project/welcome.git] / welcome.admin.inc
1 <?php
2 /**
3 * @file
4 * Admin page callbacks for the welcome module.
5 */
6
7 /**
8 * Implements hook_form().
9 * Admin form to configurable welcome message
10 */
11 function welcome_admin_settings($form) {
12
13 $token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
14
15 $form['#tree'] = TRUE;
16
17 // Get saved messages.
18 $query = db_select('welcome_message', 'wm')
19 ->fields('wm', array('rid', 'message', 'weight'))
20 ->orderBy('weight');
21 $result = $query->execute();
22
23 foreach ($result as $record) {
24 $role = user_role_load($record->rid);
25
26 $form['welcome_message'][$record->rid]['msg'] = array(
27 '#type' => 'textarea',
28 '#title' => t('Welcome message for @role', array('@role' => $role->name)),
29 '#description' => t('Edit the welcome message.') . ' ' . $token_help,
30 '#rows' => 5,
31 '#default_value' => $record->message,
32 );
33
34 $form['welcome_message'][$record->rid]['weight'] = array(
35 '#type' => 'weight',
36 '#delta' => 10,
37 '#default_value' => $record->weight,
38 );
39
40 $form['welcome_message'][$record->rid]['current'] = array(
41 '#type' => 'value',
42 '#value' => $record->rid,
43 );
44 }
45
46 // Add fields for any roles not already included
47 $roles = user_roles(TRUE);
48 foreach($roles as $rid => $role) {
49 if(isset($form['welcome_message'])) {
50 $form_keys = $form['welcome_message'];
51 }
52 else{
53 $form_keys = array();
54 }
55
56 if (!array_key_exists($rid, $form_keys)) {
57 $form['welcome_message'][$rid]['msg'] = array(
58 '#type' => 'textarea',
59 '#title' => t('Welcome message for @role', array('@role' => $role)),
60 '#rows' => 5,
61 '#default_value' => "",
62 );
63
64 $form['welcome_message'][$rid]['weight'] = array(
65 '#type' => 'weight',
66 '#delta' => 10,
67 '#default_value' => 10,
68 );
69 }
70 }
71
72 // Submit button
73 $form['actions'] = array('#type' => 'actions');
74 $form['actions']['submit'] = array(
75 '#type' => 'submit',
76 '#value' => t('Save Changes'),
77 );
78
79 return $form;
80 }
81
82 /**
83 * Implements hook_submit().
84 */
85 function welcome_admin_settings_submit($form, &$form_state) {
86 foreach ($form_state['values']['welcome_message'] as $id => $data) {
87 if (is_array($data) && isset($data['weight'])) {
88 $table = 'welcome_message';
89 $record = array('rid' => $id, 'message' => $data['msg'], 'weight' => $data['weight']);
90 if (empty($data['current']) || !isset($data['current'])) {
91 drupal_write_record($table, $record);
92 }
93 else {
94 drupal_write_record($table, $record, array('rid'));
95 }
96 }
97 drupal_set_message(t('Saved welcome messages'));
98 }
99 }
100
101 /**
102 * Returns HTML for the welcome admin form.
103 */
104 function theme_welcome_admin_settings($variables) {
105 $form = $variables['form'];
106 $rows = array();
107 foreach(element_children($form['welcome_message']) as $id) {
108 $form['welcome_message'][$id]['weight']['#attributes']['class'] = array('text-welcome-order-weight');
109 $rows[] = array(
110 'data' => array(
111 drupal_render($form['welcome_message'][$id]['msg']),
112 drupal_render($form['welcome_message'][$id]['weight']),
113 ),
114 'class' => array('draggable'),
115 );
116 }
117 $header = array(t('Message'), t('Weight'));
118 $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'text-welcome-order')));
119 $output .= drupal_render_children($form);
120
121 drupal_add_tabledrag('text-welcome-order', 'order', 'sibling', 'text-welcome-order-weight');
122
123 return $output;
124 }