-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmod.module
460 lines (413 loc) · 15.4 KB
/
zmod.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
// $Id: zmod.module 10 2008-09-23 00:34:09Z pwolanin $
function zmod_block($op = 'list', $delta = '0', $edit = array()) {
global $user;
if ($op == 'list') {
$blocks = array();
$blocks['0']['info'] = t('User login/logout link');
return $blocks;
}
elseif ($op == 'view') {
$block = array();
switch ($delta) {
case '0':
if (!$user->uid) {
$block['subject'] = '';
$block['content'] = l(t('Login / Register'), 'user/login', array('attributes' => array('id' => 'toggle-login')));
}
else {
$block['subject'] = '';
$block['content'] = check_plain($user->name) .' - '. l(t('Log out'), 'logout');
}
break;
}
return $block;
}
}
function zmod_perm() {
return array('export membership list');
}
function zmod_menu() {
$items = array();
$items['member-list/view'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['member-list/stats'] = array(
'title' => 'Member stats',
'page callback' => 'zmod_member_stats',
'access arguments' => array('access user profiles'),
'type' => MENU_LOCAL_TASK,
);
$items['member-list/export'] = array(
'title' => 'Member list export',
'page callback' => 'drupal_get_form',
'page arguments' => array('zmod_list_export_form'),
'access arguments' => array('export membership list'),
'type' => MENU_LOCAL_TASK,
);
$items['member-list/mailmerge'] = array(
'title' => 'Member mailmerge export',
'page callback' => 'drupal_get_form',
'page arguments' => array('zmod_mailmerge_export_form'),
'access arguments' => array('export membership list'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function zmod_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'view':
if ($node->type == 'household') {
$result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n JOIN {content_type_member} ctm ON ctm.vid = n.vid WHERE ctm.field_household_nid = %d AND n.status = 1"), $node->nid);
$list = array();
while ($m = db_fetch_array($result)) {
$list[] = l($m['title'], 'node/' . $m['nid']);
}
$node->content['zmod_members_at_house'] = array(
'#value' => '<h3>Members at this address:</h3>' . theme('item_list', $list),
'#weight' => 100,
);
}
break;
case 'prepare':
break;
case 'presave':
if ($node->type == 'member') {
// Assign renewal date for updated member.
$curr = db_fetch_array(db_query("SELECT field_renewal_date_value, field_mem_value FROM {content_type_member} ctm INNER JOIN {node} n ON ctm.vid = n.vid WHERE n.nid = %d", $node->nid));
if (!$curr || $node->field_mem[0]['value'] > $curr['field_mem_value']) {
// Don't overwrite user input
if (empty($node->field_renewal_date[0]['value']) || ($curr && $node->field_renewal_date[0]['value'] == $curr['field_renewal_date_value'])) {
$node->field_renewal_date[0]['value'] = time();
}
}
}
break;
}
}
function zmod_form_alter(&$form, $form_state, $form_id) {
if ('member_node_form' == $form_id || 'household_node_form' == $form_id) {
// Get rid of "- Please choose -" option
array_shift($form['taxonomy']['9']['#options']);
}
elseif ('forum_node_form' == $form_id) {
// Avoid showing 'Please choose' if there is only one option.
$vid = variable_get('forum_nav_vocabulary', '');
if ($vid && isset($form['taxonomy'][$vid]['#options']['']) && count($form['taxonomy'][$vid]['#options']) == 2) {
unset($form['taxonomy'][$vid]['#options']['']);
}
}
elseif (isset($form['type']) && $form_id == $form['type']['#value'] .'_node_form') {
// node edit form
if (isset($form['event'])) {
$form['event']['#weight'] = -1;
}
}
$node_type_name = '';
// Avoid PHP notices by checking if '#node' exists.
if (!empty($form['#node']->type)) {
$node_type_name = $form['#node']->type;
}
switch ($form_id) {
// Alter all node edit forms
case $node_type_name .'_node_form':
// Add content type label/description to edit form
// as a new form variable called "formal-title"
if (arg(1) =='add' || arg(2) =='edit') {
// Get data on a content type
$node_type = node_get_types('type', $form['#node']);
// create new form element, and pass content type data
$form['formal-title'] = array(
'#type' => 'item',
'#weight' => -15,
'#title' => t('Content Type'),
'#value' => '<h3 class="node-type-name">'. check_plain($node_type->name) .'</h3>'. filter_xss_admin($node_type->description),
);
} // end if
break;
} // end switch
}
function zmod_init() {
global $user;
if (!$user->uid) {
$data =
"if (Drupal.jsEnabled) {
(function($) {
$(document).ready(function() {
$('#block-user-0').hide();
$('#toggle-login').click(function() {
$('#block-user-0').slideToggle('fast');
return false;
});
});
})(jQuery);
}";
drupal_add_js($data, 'inline');
}
}
function zmod_member_stats() {
$sql =
"SELECT ctm.field_mem_value as membership, COUNT(n.nid) AS num_members, COUNT(DISTINCT(ctm.field_household_nid)) AS num_households
FROM {node} n INNER JOIN {content_type_member} ctm ON n.vid = ctm.vid
WHERE (n.status <> 0) AND (n.type = 'member')
GROUP BY ctm.field_mem_value
ORDER BY ctm.field_mem_value DESC";
$header = array();
$rows = array();
$result = db_query($sql);
while ($row = db_fetch_array($result)) {
if (!$header) {
$header = array_keys($row);
}
$rows[] = $row;
}
$output = '<h3>Aggregate stats</h3>';
$output .= theme('table', $header, $rows);
$current = 'M-' . date('y');
$sql =
"SELECT substring(field_district_value, 1, 1) as municipality, COUNT(n.nid) AS num_members, COUNT(DISTINCT(ctm.field_household_nid)) AS num_households
FROM {node} n INNER JOIN {content_type_member} ctm ON n.vid = ctm.vid INNER JOIN content_type_household cth ON cth.nid = ctm.field_household_nid INNER JOIN {node} h_node ON h_node.vid = cth.vid
WHERE (n.status <> 0) AND cth.field_district_value IS NOT NULL AND ctm.field_mem_value = '%s'
GROUP BY substring(cth.field_district_value, 1, 1)";
$header = array();
$rows = array();
$result = db_query($sql, $current);
while ($row = db_fetch_array($result)) {
if (!$header) {
$header = array_keys($row);
}
$rows[] = $row;
}
$output .= '<h3>Current year paid full members by municipality</h3>';
$output .= theme('table', $header, $rows);
$sql =
"SELECT cth.field_district_value as district, COUNT(n.nid) AS num_members, COUNT(DISTINCT(ctm.field_household_nid)) AS num_households
FROM {node} n INNER JOIN {content_type_member} ctm ON n.vid = ctm.vid INNER JOIN content_type_household cth ON cth.nid = ctm.field_household_nid INNER JOIN {node} h_node ON h_node.vid = cth.vid
WHERE (n.status <> 0) AND ctm.field_mem_value = '%s'
GROUP BY cth.field_district_value
ORDER BY cth.field_district_value DESC";
$header = array();
$rows = array();
$result = db_query($sql, $current);
while ($row = db_fetch_array($result)) {
if (!$header) {
$header = array_keys($row);
}
$rows[] = $row;
}
$output .= '<h3>Current year paid full members by district</h3>';
$output .= theme('table', $header, $rows);
return $output;
}
function zmod_list_export_form() {
$cck_info = content_types('member');
$options = content_allowed_values($cck_info['fields']['field_mem']);
$form = array();
$form['list_format'] = array(
'#title' => t('List format'),
'#type' => 'radios',
'#options' => array('mailing' => t('Mailing list (grouped by houshold)'), 'meeting' => t('Meeting check-in (one name per line)')),
'#default_value' => 'mailing',
'#required' => TRUE,
);
$form['mem_types'] = array(
'#title' => t('Which members?'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => array('M-11', 'A-11', 'M-10', 'A-10', 'C', 'M-09', 'A-09'),
'#required' => TRUE,
);
$form['include_emo'] = array(
'#title' => t('Include email-only members?'),
'#type' => 'checkbox',
'#default_value' => FALSE,
);
$form['municipality'] = array(
'#title' => t('Municipality'),
'#type' => 'radios',
'#default_value' => '',
'#options' => array('' => t('All'), 'B-' => t('Boro only'), 'T-' => t('Township only')),
);
$form['separator'] = array(
'#title' => t('Line seperator'),
'#type' => 'radios',
'#default_value' => "w",
'#options' => array("w" => t('Windows'), "u" => t('Unix')),
);
$form['text'] = array(
'#value' => '<h3>Hit the button to export</h3>'
);
$form['export'] = array(
'#type' => 'submit',
'#value' => t('Export list'),
);
return $form;
}
function zmod_list_export_form_submit($form, &$form_state) {
if ($form_state['values']['list_format'] == 'mailing') {
$separator = $form_state['values']['values']['separator'] == 'u' ? "\n" : "\r\n";
$sql = "SELECT GROUP_CONCAT(CONCAT(n.title, ' (', ctm.field_mem_value, ')')
SEPARATOR \"". $separator ."\") AS names,";
}
else {
$sql = "SELECT ctm.field_last_name_value AS last_name, ctm.field_first_name_value AS first_name, ctm.field_mem_value AS membership, cth.field_district_value as district,";
}
$sql .= "
hn.title AS address, cth.field_city_value AS city,
cth.field_state_value AS state, cth.field_zip_value AS zip
FROM node n
INNER JOIN content_type_member ctm ON n.vid = ctm.vid
INNER JOIN content_type_household cth ON ctm.field_household_nid = cth.nid
INNER JOIN node hn ON hn.vid = cth.vid
WHERE ctm.field_mem_value IN (" . db_placeholders($form_state['values']['mem_types'], 'text') . ") AND n.status = 1";
$args = $form_state['values']['mem_types'];
if (!$form_state['values']['include_emo'] && $form_state['values']['list_format'] == 'mailing') {
$sql .= " AND ctm.field_email_only_value != 1";
}
if ($form_state['values']['municipality']) {
$sql .= " AND cth.field_district_value LIKE '%s%%'";
$args[] = $form_state['values']['municipality'];
}
if ($form_state['values']['list_format'] == 'mailing') {
$sql .= " GROUP BY cth.nid ORDER BY ctm.field_last_name_value";
}
else {
$sql .= " ORDER BY ctm.field_last_name_value, ctm.field_first_name_value";
}
$result = db_query($sql, $args);
$rows = array();
while ($row = db_fetch_array($result)) {
if (empty($rows)) {
// Header row.
$rows[] = array_keys($row);
}
$rows[] = $row;
}
zmod_member_export_csv($rows, 'pcdo_members_' . $form_state['values']['list_format'] . '_');
}
function zmod_member_export_csv($rows, $prefix = 'pcdo_members_') {
$filename = $prefix . format_date(time(), 'custom', 'Y-m-d');
$data = zmod_member_csv_put_formatted($rows);
header("Content-type: text/plain; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Cache-Control: no-cache");
header("Expires: 0");
print $data;
exit();
}
/**
* Transforms a nested array into a string in CSV format, where fields and
* records are character-delimited. This function is based on a custom version
* of the fputcsv() function, which can be found at this URL:
* http://php.net/fputcsv#57164
*
* @param $records
* A nested array of 'lines' and 'fields' to be transformed into CSV format.
* @param $field_delimiter
* The character to use as a field delimiter (defaults to comma ','
* character).
* @param $record_delimiter
* The character to use as a record delimiter (defaults to newline '\n'
* character).
* @param $enclosure
* The character to use as an enclosure (defaults to double-quote '"'
* character).
*
* @return
* A string containing the lines and fields in CSV format.
*/
function zmod_member_csv_put_formatted($records, $field_delimiter = ',', $record_delimiter = "\n", $enclosure = "\"") {
// Build the string
$csv_doc = '';
// for each array element, which represents a line in the CSV string...
foreach($records as $line) {
// No leading delimiter
$use_delimiter = FALSE;
foreach($line as $element) {
// Replaces a double quote with two double quotes
$element = str_replace("\"", "\"\"", $element);
// Adds a delimiter before each field (except the first)
if($use_delimiter) {
$csv_doc .= $field_delimiter;
}
// Encloses each field with $enclosure and adds it to the string
$csv_doc .= $enclosure . $element . $enclosure;
// Delimiters are used every time except the first.
$use_delimiter = TRUE;
}
// Append new line
$csv_doc .= $record_delimiter;
}
return $csv_doc;
}
function zmod_mailmerge_export_form() {
$cck_info = content_types('member');
$options = content_allowed_values($cck_info['fields']['field_mem']);
$form = array();
$form['mem_types'] = array(
'#title' => t('Which members?'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => array('M-11', 'A-11', 'M-10', 'A-10', 'M-09', 'A-09'),
'#required' => TRUE,
);
$form['include_emo'] = array(
'#title' => t('Include email-only members?'),
'#type' => 'checkbox',
'#default_value' => TRUE,
);
$form['municipality'] = array(
'#title' => t('Municipality'),
'#type' => 'radios',
'#default_value' => '',
'#options' => array('' => t('All'), 'B-' => t('Boro only'), 'T-' => t('Township only')),
);
$form['text'] = array(
'#value' => '<h3>Hit the button to export</h3>'
);
$form['export'] = array(
'#type' => 'submit',
'#value' => t('Export list'),
);
return $form;
}
function zmod_mailmerge_export_form_submit($form, &$form_state) {
$sql = "SELECT n.title AS name, ctm.field_household_nid AS hnid, ctm.field_email_value AS email, ctm.field_email_only_value AS email_only, ctm.field_mem_value AS membership,
hn.title AS address, cth.field_address_line2_value AS address_line2, cth.field_city_value AS city,
cth.field_state_value AS state, cth.field_zip_value AS zip
FROM node n
INNER JOIN content_type_member ctm ON n.vid = ctm.vid
INNER JOIN content_type_household cth ON ctm.field_household_nid = cth.nid
INNER JOIN node hn ON hn.vid = cth.vid
WHERE ctm.field_mem_value IN (" . db_placeholders($form_state['values']['mem_types'], 'text') . ") AND n.status = 1";
$args = $form_state['values']['mem_types'];
if (!$form_state['values']['include_emo']) {
$sql .= " AND ctm.field_email_only_value != 1";
}
if ($form_state['values']['municipality']) {
$sql .= " AND cth.field_district_value LIKE '%s%%'";
$args[] = $form_state['values']['municipality'];
}
$sql .= " ORDER BY ctm.field_last_name_value";
$result = db_query($sql, $args);
$rows = array();
// Header row. We know we have no more than 3 per houshold.
$header = array('address', 'address_line2', 'city', 'state', 'zip', 'name1', 'email1', 'email_only1', 'mem1', 'name2', 'email2', 'email_only2', 'mem2', 'name3', 'email3', 'email_only3', 'mem3');
$rows[] = $header;
$header_count = count($header);
while ($a = db_fetch_array($result)) {
$hnid = $a['hnid'];
if (!isset($rows[$hnid])) {
$rows[$hnid] = array($a['address'], $a['address_line2'], $a['city'], $a['state'], $a['zip']);
}
$rows[$hnid][] = $a['name'];
$rows[$hnid][] = $a['email'];
$rows[$hnid][] = $a['email_only'] != 1 ? '' : 'Yes';
$rows[$hnid][] = $a['membership'];
}
foreach ($rows as $idx => $row) {
$rows[$idx] = array_pad($row, $header_count, '');
}
zmod_member_export_csv($rows, 'pcdo_mailmerge_');
}