forked from vejlebib/ding_staff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding_staff.module
146 lines (130 loc) · 3.86 KB
/
ding_staff.module
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* @file
* Code for the Ding staff feature.
*/
include_once 'ding_staff.features.inc';
/**
* Implements hook_ctools_plugin_directory().
*
* It simply tells panels where to find the .inc files that define various
* args, contexts, content_types. In this case the subdirectories of
* ctools_plugin_example/panels are used.
*/
function ding_staff_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
/**
* Implements of hook_rules_action_info().
*/
function ding_staff_rules_action_info() {
return array(
'ding_staff_add_role_staff' => array(
'label' => t('Add staff role to user'),
'group' => 'user',
'parameter' => array(
'profile_user' => array(
'type' => 'user',
'label' => t('User to add staff role to'),
),
),
),
);
}
/**
* Action to add the staff role to a user.
*
* Since we cannot know which role ID the staff role has been assigned,
* this function is used instead of the "add user role" action
*
* @param object $profile_user
* User object.
*/
function ding_staff_add_role_staff($profile_user) {
// Get the uid from the object.
if (isset($profile_user->uid)) {
$uid = $profile_user->uid;
}
else {
return;
}
// Make sure we have a user record.
if ($uid) {
// Define "staff" as the role to add.
$role_name = "staff";
$user = user_load($uid);
// If the user doesn't already have the role, add the role to that user.
$key = array_search($role_name, $user->roles);
if ($key == FALSE) {
// Get the rid from the roles table.
$roles = user_roles(TRUE);
$rid = array_search($role_name, $roles);
if ($rid != FALSE) {
$new_role[$rid] = $role_name;
// Add new role to existing roles.
$all_roles = $user->roles + $new_role;
user_save($user, array('roles' => $all_roles));
drupal_set_message(t('@username has been assigned the staff role.', array('@username' => $profile_user->name)), 'status');
}
}
}
}
/**
* Implements hook_views_default_views_alter().
*
* Since we cannot know which role ID the staff role has been assigned,
* we overwrite the rids in the staff view
*/
function ding_staff_views_default_views_alter(&$views) {
if (!empty($views['ding_staff'])) {
$roles = user_roles(TRUE);
$rid = array_search("staff", $roles);
if ($rid) {
$rids = array();
$rids[$rid] = $rid;
// Replace existing rid defined in the view with the actual rid of role
// "staff".
$views['ding_staff']->display['ding_staff_list_all']->display_options['filters']['rid']['value'] = $rids;
$views['ding_staff']->display['ding_staff_library_departments']->display_options['filters']['rid']['value'] = $rids;
}
}
}
/**
* Implements hook_og_context_negotiation_info().
*/
function ding_staff_og_context_negotiation_info() {
$providers = array();
$providers['ding-staff'] = array(
'name' => t('Ding Staff'),
'description' => t("Determine context user node."),
'callback' => 'ding_staff_og_context_handler',
);
return $providers;
}
/**
* Callback for OG context negotiation that tries to find library context.
*
*
* @return array
* Node ids.
*/
function ding_staff_og_context_handler() {
$nids = array();
$item = menu_get_item();
if (isset($item['page_arguments'][0]->roles) && isset($item['page_arguments'][0]->roles[16])) {
// Look up staff profile and check if there is a connection to a library
// group.
$profile = profile2_load_by_user($item['page_arguments'][0], 'ding_staff_profile');
if (isset($profile) && isset($profile->og_group_ref[LANGUAGE_NONE])) {
$values = $profile->og_group_ref[LANGUAGE_NONE];
foreach ($values as $value) {
$nids[] = $value['target_id'];
}
}
}
return array(
'node' => $nids,
);
}