-
Notifications
You must be signed in to change notification settings - Fork 0
/
memberdb.module
228 lines (202 loc) · 8.01 KB
/
memberdb.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file
*/
/**
* Implementation of hook_menu()
*/
function memberdb_menu() {
$items = array();
$items['admin/settings/memberdb'] = array(
'title' => 'MemberDB',
'description' => 'Configure settings for the MemberDB module suite.',
'page callback' => 'drupal_get_form',
'page arguments' => array('memberdb_admin_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer memberdb'),
'file' => 'memberdb.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_perm()
*/
function memberdb_perm() {
return array(
'view memberdb information',
'administer memberdb',
);
}
/**
* Implements hook_user().
*/
function memberdb_user($op, &$edit, &$account, $category = NULL) {
global $user;
if ($op == 'view') {
if (is_array($account->content['summary'])) {
$account->content['summary']['#title'] = t('Site');
$account->content['summary']['member_for']['#title'] = t('Active for');
}
// Admin or looking at self
if (user_access('view memberdb information') || $user->uid == $account->uid) {
if (variable_get('memberdb_organisation', 0)) {
$account->content['memberdb'] = array(
'#type' => 'user_profile_category',
'#attributes' => array('class' => 'user-memberdb'),
'#weight' => 5,
'#title' => t('MemberDB'),
'membership' => array(
'#title' => t('Membership Status'),
'#type' => 'user_profile_item',
'#value' => memberdb_get_user_membership($account),
),
'council' => array(
'#title' => t('Council Status'),
'#type' => 'user_profile_item',
'#value' => memberdb_get_user_council($account),
'#weight' => 1,
),
);
}
else {
if (user_access('administer memberdb')) {
drupal_set_message(t('MemberDB is not configured. Please select an organisation on the !link.', array('!link' => l('MemberDB settings page', 'admin/settings/memberdb'))), 'warning');
}
}
}
}
}
/**
* Helper - check if user is current member.
*/
function memberdb_user_is_member($account) {
$organisation = variable_get('memberdb_organisation', 0);
$type = variable_get('memberdb_type', 0);
db_set_active('memberdb');
$result = db_result(db_query("SELECT m.id FROM members m LEFT JOIN org_members om ON (m.id = om.member_id) LEFT JOIN orgs o ON (o.id = om.org_id) LEFT JOIN member_types t ON (t.id = om.member_type_id) WHERE o.id = %d AND t.id = %d AND m.email = '%s' AND (om.expiry IS NULL OR om.expiry = '0000-00-00 00:00:00' OR om.expiry >= '%s')", $organisation, $type, $account->mail, date('Y-m-d H:i:s')));
db_set_active();
return $result;
}
/**
* Helper - fetch memberdb membership history.
*/
function memberdb_get_user_membership($account) {
$data = array();
$organisation = variable_get('memberdb_organisation', 0);
db_set_active('memberdb');
if (!empty($account->profile_memberdb_id)) {
$result = db_query("SELECT m.id, o.name AS org, t.type, t.description, UNIX_TIMESTAMP(om.start_date) AS start, UNIX_TIMESTAMP(om.expiry) AS expiry FROM members m LEFT JOIN org_members om ON (m.id = om.member_id) LEFT JOIN orgs o ON (o.id = om.org_id) LEFT JOIN member_types t ON (t.id = om.member_type_id) WHERE o.id = %d AND m.id = %d ORDER BY om.start_date ASC, om.expiry ASC", $organisation, $account->profile_memberdb_id);
}
else {
$result = db_query("SELECT m.id, o.name AS org, t.type, t.description, UNIX_TIMESTAMP(om.start_date) AS start, UNIX_TIMESTAMP(om.expiry) AS expiry FROM members m LEFT JOIN org_members om ON (m.id = om.member_id) LEFT JOIN orgs o ON (o.id = om.org_id) LEFT JOIN member_types t ON (t.id = om.member_type_id) WHERE o.id = %d AND m.email = '%s' ORDER BY om.start_date ASC, om.expiry ASC", $organisation, $account->mail);
}
while ($row = db_fetch_array($result)) {
$row['start'] = format_date($row['start'], 'small');
$row['expiry'] = ($row['expiry'] == 0) ? t('Will not expire') : format_date($row['expiry'], 'small');
$data[] = $row;
}
db_set_active();
if (empty($data)) {
return t('Has never applied for @organisation membership.', array('@organisation' => memberdb_get_organisation_by_id($organisation)));
}
$headers = array(
t('Member ID'), t('Organisation'), t('Status'), t('Description'), t('Started'), t('Expired'),
);
return theme('table', $headers, array_reverse($data, TRUE));
}
/**
* Helper - fetch memberdb council history.
*/
function memberdb_get_user_council($account) {
$data = array();
$organisation = variable_get('memberdb_organisation', 0);
db_set_active('memberdb');
if (!empty($account->profile_memberdb_id)) {
$result = db_query("SELECT m.id, o.name AS org, p.title AS position, p.description, UNIX_TIMESTAMP(ph.start_datetime) AS start, UNIX_TIMESTAMP(ph.end_datetime) AS expiry FROM positions_held ph INNER JOIN members m ON (m.id = ph.member_id) LEFT JOIN orgs o ON (o.id = ph.org_id) LEFT JOIN positions p ON (p.id = ph.position_id) WHERE o.id = %d AND m.id = %d ORDER BY ph.start_datetime ASC, ph.end_datetime ASC", $organisation, $account->profile_memberdb_id);
}
else {
$result = db_query("SELECT m.id, o.name AS org, p.title AS position, p.description, UNIX_TIMESTAMP(ph.start_datetime) AS start, UNIX_TIMESTAMP(ph.end_datetime) AS expiry FROM positions_held ph INNER JOIN members m ON (m.id = ph.member_id) LEFT JOIN orgs o ON (o.id = ph.org_id) LEFT JOIN positions p ON (p.id = ph.position_id) WHERE o.id = %d AND m.email = '%s' ORDER BY ph.start_datetime ASC ASC, ph.end_datetime ASC", $organisation, $account->mail);
}
while ($row = db_fetch_array($result)) {
$row['start'] = format_date($row['start'], 'small');
$row['expiry'] = ($row['expiry'] == 0) ? t('Current') : format_date($row['expiry'], 'small');
$data[] = $row;
}
db_set_active();
if (empty($data)) {
return t('Has never been on the @organisation council.', array('@organisation' => memberdb_get_organisation_by_id($organisation)));
}
$headers = array(
t('Member ID'), t('Organisation'), t('Position'), t('Description'), t('Started'), t('Expired'),
);
return theme('table', $headers, array_reverse($data, TRUE));
}
/**
* Helper - Return an option list of defined orgs.
*
* @param $option
* Boolean: return an option list for use in forms.
*/
function memberdb_get_organisations($option = FALSE) {
$options = array(0 => t('<none>'));
db_set_active('memberdb');
$result = db_query("SELECT id, name, UNIX_TIMESTAMP(founded) AS founded FROM orgs ORDER BY name ASC");
while($row = db_fetch_object($result)) {
if ($option) {
$options[$row->id] = t('@name (Founded in @founded)', array('@name' => $row->name, '@founded' => format_date($row->founded, 'custom', 'M Y')));
}
else {
$options[$row->id] = check_plain($row->name);
}
}
db_set_active();
return $options;
}
/**
* Fetch an organisation name from a cached list.
*/
function memberdb_get_organisation_by_id($id = 0) {
static $orgs;
if (empty($orgs)) {
$orgs = memberdb_get_organisations();
}
if (isset($orgs[$id])) {
return $orgs[$id];
}
return NULL;
}
/**
* Helper - Return an option list of defined member types.
*
* @param $option
* Boolean: return an option list for use in forms.
*/
function memberdb_get_types($option = FALSE) {
$options = array(0 => t('<none>'));
db_set_active('memberdb');
$result = db_query("SELECT id, type, description FROM member_types ORDER BY type ASC");
while($row = db_fetch_object($result)) {
if ($option) {
$options[$row->id] = t('@name (@description)', array('@name' => $row->type, '@description' => $row->description));
}
else {
$options[$row->id] = check_plain($row->type);
}
}
db_set_active();
return $options;
}
/**
* Fetch a member type name from a cached list.
*/
function memberdb_get_type_by_id($id = 0) {
static $types;
if (empty($types)) {
$types = memberdb_get_types();
}
if (isset($types[$id])) {
return $types[$id];
}
return NULL;
}