-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnyintergroup-pdf.php
221 lines (181 loc) · 7.36 KB
/
nyintergroup-pdf.php
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
<?php
/**
* Plugin Name: NY Intergroup PDF Generator
*/
//generates form from shortcode
include('form.php');
//generates pdf with ajax hook
include('pdf.php');
//for guide page
function format_time($string) {
if ($string == '12:00') return '12N';
if ($string == '23:59') return '12M';
list($hours, $minutes) = explode(':', $string);
$hours -= 0;
if ($hours == 0) return '12:' . $minutes . 'a';
if ($hours < 12) return $hours . ':' . $minutes . 'a';
if ($hours > 12) $hours -= 12;
return $hours . ':' . $minutes;
}
//need this for formatting the meeting types
function decode_types($type) {
global $tsml_programs, $tsml_program;
if (!array_key_exists($type, $tsml_programs[$tsml_program]['types'])) return '';
return $tsml_programs[$tsml_program]['types'][$type];
}
//pdf function to get data and attach it to the regions array
function attachPdfMeetingData($regions) {
//symbols used in the book, in the order in which they're applied
$symbols = array(
'*', '^', '#', '!', '+', '@', '%',
'**', '^^', '##', '!!', '++', '@@', '%%',
'***', '^^^', '###', '!!!', '+++', '@@@', '%%%',
);
//going to be checking this over and over
$count_symbols = count($symbols);
//get all the sub-regions and their children
$sub_region_ids = get_terms(array(
'taxonomy' => 'tsml_region',
'exclude' => array_keys($regions),
'fields' => 'ids',
));
$sub_sub_regions = array();
foreach ($sub_region_ids as $sub_region_id) {
$sub_sub_region_ids = get_terms(array(
'taxonomy' => 'tsml_region',
'parent' => $sub_region_id,
'fields' => 'ids',
));
foreach ($sub_sub_region_ids as $sub_sub_region_id) {
$sub_sub_regions[$sub_sub_region_id] = $sub_region_id;
}
}
//build an array of table rows for each region all in one shot, to preserve memory
$rows = array();
$meetings = tsml_get_meetings();
foreach ($meetings as $meeting) {
//we group meetings by group-at-location
$key = @$meeting['group_id'] . '-' . @$meeting['location_id'];
//replace with parent category
if (array_key_exists($meeting['region_id'], $sub_sub_regions)) {
$meeting['region_id'] = $sub_sub_regions[$meeting['region_id']];
}
//make sure array region exists
if (!array_key_exists($meeting['region_id'], $rows)) {
$rows[$meeting['region_id']] = array();
}
//attach meeting to region
if (!array_key_exists($key, $rows[$meeting['region_id']])) {
$parts = explode(', ', $meeting['formatted_address']);
$rows[$meeting['region_id']][$key] = array(
'group' => @$meeting['group'],
'location' => @$meeting['location'],
'address' => $parts[0],
'postal_code' => substr($parts[2], 3),
'notes' => @$meeting['location_notes'],
'last_contact' => empty($meeting['last_contact']) ? null : date('n/j/y', strtotime($meeting['last_contact'])),
'wheelchair' => false,
'spanish' => true,
'days' => array(
0 => array(),
1 => array(),
2 => array(),
3 => array(),
4 => array(),
5 => array(),
6 => array(),
),
'footnotes' => array(),
'types' => array(), //for indexes
);
}
//for indexes
$rows[$meeting['region_id']][$key]['types'] = array_merge($rows[$meeting['region_id']][$key]['types'], $meeting['types']);
//at least one meeting tagged wheelchair-accessible
if (($index = array_search('X', $meeting['types'])) !== false) {
$rows[$meeting['region_id']][$key]['wheelchair'] = true;
unset($meeting['types'][$index]);
}
//at least one meeting *not* tagged spanish means row is not "spanish"
if (!in_array('S', $meeting['types'])) $rows[$meeting['region_id']][$key]['spanish'] = false;
//insert into day
$time = '';
if (($index = array_search('D', $meeting['types'])) !== false) {
$time .= 'OD-'; //open discussion meeting (comes before open because all ODs are open)
unset($meeting['types'][$index]);
} elseif (($index = array_search('O', $meeting['types'])) !== false) {
$time .= 'O-'; //open meeting
unset($meeting['types'][$index]);
} elseif (($index = array_search('BE', $meeting['types'])) !== false) {
$time .= 'B-'; //beginners meeting
unset($meeting['types'][$index]);
} elseif (($index = array_search('B', $meeting['types'])) !== false) {
$time .= 'BB-'; //big book meeting
unset($meeting['types'][$index]);
} elseif (($index = array_search('ST', $meeting['types'])) !== false) {
$time .= 'S-'; //step meeting
unset($meeting['types'][$index]);
} elseif (($index = array_search('TR', $meeting['types'])) !== false) {
$time .= 'T-'; //tradition meeting
unset($meeting['types'][$index]);
} elseif (($index = array_search('C', $meeting['types'])) !== false) {
$time .= 'C-'; //closed meeting
unset($meeting['types'][$index]);
}
$time .= format_time($meeting['time']);
//per Janet, don't need Closed meeting type now because it's implied
if (($index = array_search('C', $meeting['types'])) !== false) {
unset($meeting['types'][$index]);
}
//append footnote to array
if (!empty($meeting['types']) || !empty($meeting['notes'])) {
//decide what this meeting's footnote should be
$footnote = array_map('decode_types', $meeting['types']);
if (!empty($meeting['notes'])) $footnote[] = $meeting['notes'];
$footnote = implode(', ', $footnote);
//add footnote if not full
$count_footnotes = count($rows[$meeting['region_id']][$key]['footnotes']);
//if (!is_array($rows[$meeting['region_id']][$key]['footnotes'])) dd($meeting);
if (array_key_exists($footnote, $rows[$meeting['region_id']][$key]['footnotes'])) {
$index = array_search($footnote, $rows[$meeting['region_id']][$key]['footnotes']);
$time = $symbols[$index] . $time;
} elseif ($count_footnotes < $count_symbols) {
$rows[$meeting['region_id']][$key]['footnotes'][$footnote] = $symbols[$count_footnotes];
$time = $symbols[$count_footnotes] . $time;
}
}
//add meeting to row->day array
$rows[$meeting['region_id']][$key]['days'][$meeting['day']][] = $time;
}
//add children from the database to the main regions array
$categories = get_categories('taxonomy=tsml_region');
foreach ($categories as $category) {
$category->name = html_entity_decode($category->name);
if (array_key_exists($category->term_id, $rows)) {
usort($rows[$category->term_id], function($a, $b) {
if ($a['group'] == $b['group']) return strcmp($a['location'], $b['location']);
return strcmp($a['group'], $b['group']);
});
}
//check if this is a sub_region
if (array_key_exists($category->parent, $regions)) {
//this region has a parent, so make sure that parent has an array for sub_regions
if (!isset($regions[$category->parent]['sub_regions'])) $regions[$category->parent]['sub_regions'] = array();
//skip if there aren't any rows for this sub_region
if (!array_key_exists($category->term_id, $rows)) continue;
//attach the sub_region
$regions[$category->parent]['sub_regions'][$category->name] = $rows[$category->term_id];
} elseif (array_key_exists($category->term_id, $regions)) {
//this is a main region
$regions[$category->term_id]['name'] = $category->name;
$regions[$category->term_id]['description'] = $category->description;
if (array_key_exists($category->term_id, $rows)) {
$regions[$category->term_id]['rows'] = $rows[$category->term_id];
}
} else {
//this isn't in the array -- no meetings are assigned
}
}
//dd($regions);
return $regions;
}