/
view_2_families__4_contact_list.class.php
497 lines (454 loc) · 16.6 KB
/
view_2_families__4_contact_list.class.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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php
class View_Families__Contact_List extends View
{
static function getMenuPermissionLevel()
{
return PERM_RUNREPORT;
}
function processView()
{
$GLOBALS['system']->includeDBClass('family');
$GLOBALS['system']->includeDBClass('person');
$GLOBALS['system']->includeDBClass('person_group');
}
function getTitle()
{
return _('Contact List');
}
function printView()
{
if (!empty($_REQUEST['go'])) {
if (!empty($_REQUEST['groupid'])) {
?>
<div class="pull-right">
<a class="clickable back"><i class="icon-wrench"></i>Adjust configuration</a><br />
<i class="icon-download"></i> Download as
<a href="<?php echo build_url(Array('call' => 'contact_list', 'format' => 'html', 'view' => NULL)); ?>">HTML</a> |
<a href="<?php echo build_url(Array('call' => 'contact_list', 'format' => 'docx', 'view' => NULL)); ?>">DOCX</a>
</div>
<?php
$this->printResults();
return;
} else {
print_message(_("You must choose an opt-in group"), 'error');
}
}
$this->printForm();
}
function printForm()
{
$dummy_person = new Person();
$dummy_person->fields['congregationid']['allow_multiple'] = true;
$dummy_person->fields['age_bracketid']['allow_multiple'] = true;
$dummy_person->fields['age_bracketid']['allow_empty'] = true;
?>
<form method="get">
<input type="hidden" name="view" value="<?php echo ents($_REQUEST['view']); ?>" />
<table>
<tr>
<th><?php echo _('Opt-in group');?></th>
<td>
<?php echo _('Only show families that have a member in the group'); ?> <br />
<?php Person_Group::printChooser('groupid', 0); ?></td>
</tr>
<tr>
<th><?php echo _('Congregation');?></th>
<td><?php echo _('Only include opted-in persons from');?><br />
<?php $dummy_person->printFieldInterface('congregationid'); ?></td>
</tr>
<tr>
<th><?php echo _('Age brackets');?></th>
<td><?php echo _('Only show contact details for persons who are');?><br />
<?php $dummy_person->printFieldInterface('age_bracketid'); ?>
</td>
</tr>
<tr>
<th><?php echo _('Other family members');?></th>
<td><?php echo _('When a family has other members not in the opt-in group above:');?><br />
<label class="radio">
<input type="radio" name="all_member_details" value="-1" checked="checked" id="all_member_details_0" />
<?php echo _('Do not show them at all');?>
</label>
<label class="radio">
<input type="radio" name="all_member_details" value="0" checked="checked" id="all_member_details_0" />
<?php echo _('Show their names but no contact details');?>
</label>
<label class="radio">
<input type="radio" name="all_member_details" value="1" id="all_member_details_1" />
<?php echo _('Show their contact details just like the opted-in persons');?>
</label>
</tr>
<tr>
<th><?php echo _('Details to show');?></th>
<td>
<label class="checkbox">
<?php
print_widget('include_address', Array('type' => 'checkbox'), array_get($_REQUEST, 'include_address', TRUE));
echo _('Home address');
?>
</label>
<label class="checkbox">
<?php
print_widget('include_home_tel', Array('type' => 'checkbox'), array_get($_REQUEST, 'include_home_tel', TRUE));
echo _('Home phone');
?>
</label>
<label class="checkbox">
<?php
print_widget('include_congregation', Array('type' => 'checkbox'), array_get($_REQUEST, 'include_congregation', TRUE));
echo _('Congregation');
?>
</label>
<?php
if ($GLOBALS['system']->featureEnabled('PHOTOS')) {
?>
<label class="checkbox">
<?php
print_widget('include_photos', Array('type' => 'checkbox'), array_get($_REQUEST, 'include_photos', TRUE));
echo _('Family photos');
?>
</label>
<?php
}
?>
</td>
</tr>
<tr>
<th></th>
<td>
<input class="btn" type="submit" name="go" value="Show results" />
</td>
</tr>
</table>
</form>
<?php
}
function printResults($dataURLs=FALSE)
{
?>
<table class="contact-list">
<?php
foreach ($this->getData() as $family) {
?>
<tr>
<?php
if (!empty($_REQUEST['include_photos'])) {
$rowSpan = count($family['optins']) + 2;
if (!empty($_REQUEST['include_home_tel']) && $family['home_tel']) $rowSpan++;
if (!empty($_REQUEST['include_address']) && $family['address_street']) $rowSpan++;
if (count($family['all']) > count($family['optins'])) $rowSpan++;
if (($family['have_photo']) || count($family['optins']) == 1) {
if ($dataURLs) {
$src = Photo_Handler::getDataURL('family', $family['familyid']);
} else {
$src = '?call=photo&familyid='.$family['familyid'];
}
} else {
$src = BASE_URL.'resources/img/unknown_family.gif';
}
?>
<td rowspan="<?php echo $rowSpan; ?>" style="padding: 5px">
<img src="<?php echo $src; ?>" />
</td>
<?php
}
?>
<td colspan="4" style="height: 1px">
<h2 style="margin: 5px 0px 0px 0px"><?php echo $family['family_name']; ?></h2>
</td>
</tr>
<?php
if (count($family['all']) > count($family['optins'])) {
?>
<tr style="height: 1px">
<td colspan="4"><i><?php echo ents($family['all_names']); ?></td>
</tr>
<?php
}
if (!empty($_REQUEST['include_home_tel']) && $family['home_tel']) {
echo '<tr style="height: 1px"><td colspan="4">';
echo ents($family['home_tel']);
echo '</td></tr>';
}
if (!empty($_REQUEST['include_address']) && $family['address_street']) {
echo '<tr style="height: 1px"><td colspan="4">'.str_replace("\n", ', ', ents($family['address_street'])).', ';
echo ents($family['address_suburb'].' '.$family['address_state'].' '.$family['address_postcode']);
echo '</td></tr>';
}
foreach ($family['optins'] as $adult) {
?>
<tr style="height: 1px">
<td style="padding-right: 1ex"><?php echo ents($adult['name']); ?></td>
<td style="padding-right: 1ex">
<?php
if (!empty($_REQUEST['include_congregation'])) echo ents($adult['congname']);
?>
</td>
<td style="padding-right: 1ex"><?php echo ents($adult['mobile_tel']); ?></td>
<td><?php echo ents($adult['email']); ?></td>
</tr>
<?php
}
if (!empty($_REQUEST['include_photos'])) {
// to take up extra vertical space
?>
<tr>
<td colspan="4"> </td>
</tr>
<?php
}
}
?>
</table>
<?php
}
private function getData()
{
$dummy_person = new Person();
$dummy_family = new Family();
$db = $GLOBALS['db'];
$groupid = (int)$_REQUEST['groupid'];
$all_member_details = array_get($_REQUEST, 'all_member_details', 0);
if (empty($groupid)) return;
$sql = '
select family.id as familyid, family.family_name, family.home_tel,
person.id, person.first_name, person.last_name, person.mobile_tel, person.email, person.age_bracketid,
congregation.long_name as congname,
address_street, address_suburb, address_state, address_postcode,
IF (fp.familyid IS NULL, 0, 1) as have_photo,
IF (signup.groupid IS NULL, 0, 1) as signed_up,
IF (pp.personid IS NULL, 0, 1) as have_person_photo
from family
join person on family.id = person.familyid
join age_bracket ab ON ab.id = person.age_bracketid
left join congregation on person.congregationid = congregation.id
left join family_photo fp ON fp.familyid = family.id
left join person_group_membership signup ON signup.personid = person.id AND signup.groupid = '.(int)$groupid.'
left join person_photo pp ON pp.personid = person.id
where person.status <> "archived"
and family.id in
(select familyid
from person join person_group_membership pgm on person.id = pgm.personid
where pgm.groupid = '.(int)$groupid;
if (!empty($_REQUEST['congregationid'])) {
$sql .= '
AND person.congregationid in ('.implode(',', array_map(Array($db, 'quote'), $_REQUEST['congregationid'])).')';
}
$sql .= ')
order by family_name asc, familyid, ab.rank asc, gender desc
';
$res = $db->queryAll($sql, null, null, true, true, true);
if (empty($res)) {
?><p><i><?php echo _('No families to show');?></i></p><?php
return Array();
}
$GLOBALS['system']->includeDBClass('family');
$GLOBALS['system']->includeDBClass('person');
$families = Array();
foreach ($res as $familyid => $family_members) {
$family = Array(
'familyid' => $familyid,
'optins' => Array(),
'all' => Array(),
);
$adults_use_full = FALSE;
$all_use_full = FALSE;
foreach ($family_members as $member) {
$member['name'] = $member['first_name'];
// show full details if
// - (they are signed up, or all-member-details is 1)
// - AND (their age bracket is correct) OR all age brackets are in
if (
($member['signed_up'] || $all_member_details == 1)
&& (empty($_REQUEST['age_bracketid']) || in_array($member['age_bracketid'], $_REQUEST['age_bracketid']))
) {
$member['mobile_tel'] = $dummy_person->getFormattedValue('mobile_tel', $member['mobile_tel']);
$family['optins'][] = $member;
if ($member['last_name'] != $member['family_name']) {
$adults_use_full = true;
}
}
if ($member['signed_up'] || $all_member_details != -1) {
$family['all'][] = $member;
}
if ($member['last_name'] != $member['family_name']) {
$all_use_full = true;
}
}
if ($adults_use_full) {
foreach ($family['optins'] as &$adult) {
$adult['name'] .= ' '.$adult['last_name'];
}
unset($adult);
}
if ($all_use_full) {
foreach ($family['all'] as &$member) {
$member['name'] .= ' '.$member['last_name'];
}
unset($member);
}
$family['all_names'] = Array();
foreach ($family['all'] as $member) {
$family['all_names'][] = $member['name'];
}
$last = '';
if (count($family['all_names']) > 1) $last = array_pop($family['all_names']);
$family['all_names'] = implode(', ', $family['all_names']);
if ($last) $family['all_names'] .= ' & '.$last;
$first_member = reset($family_members);
foreach (Array('have_photo', 'family_name', 'home_tel', 'address_street', 'address_suburb', 'address_state', 'address_postcode') as $ffield) {
$family[$ffield] = $first_member[$ffield];
}
$family['home_tel'] = $dummy_family->getFormattedValue('home_tel', $family['home_tel']);
$families[] = $family;
}
return $families;
}
public function printDOCX()
{
require_once 'include/odf_tools.class.php';
require_once 'vendor/autoload.php';
\PhpOffice\PhpWord\Settings::setTempDir(sys_get_temp_dir());
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(TRUE);
require_once 'view_9_documents.class.php';
$width = 14173; // 25cm in twips
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('FAMILY-HEADER', array());
$phpWord->addParagraphStyle('FAMILY-SUB-HEADER', array());
$phpWord->addFontStyle('FAMILY-NAME', array('bold' => true, 'size' => 15));
$phpWord->addFontStyle('FAMILY-MEMBERS', array('italic' => true));
$phpWord->addFontStyle('HOME-PHONE', array());
$phpWord->addFontStyle('ADDRESS', array());
$phpWord->addFontStyle('PERSON-NAME', array('bold' => true));
$phpWord->addTableStyle('FAMILY-LIST', array('width' => $width, 'borderSize' => 0, 'cellMargin' => 80,'borderColor' => 'CCCCCC'));
$section = $phpWord->addSection(array('breakType' => 'continuous'));
/////////////////////////////////////////////////////////////////////////////////
// STEP ONE - BUILD A TEMPORARY DOCX FILE WITH THE TABLE OF CONTACT DATA:
$table = $section->addTable('FAMILY-LIST');
$gridspan = 3;
if (!empty($_REQUEST['include_congregation'])) $gridspan++;
$extraWideCellProps = $wideCellProps = array('gridSpan' => $gridspan, 'valign' => 'top');
if (!empty($_REQUEST['include_photos'])) {
$extraWideCellProps['gridSpan']++;
}
$narrowCellProps = array('valign' => 'top', 'vMerge' => 'restart');
$mergeProps = array('vMerge' => 'continue');
$imageStyle = Array('width' => 100);
$cleanup = Array();
foreach ($this->getData() as $family) {
$table->addRow();
$table->addCell(NULL, $extraWideCellProps)
->addText($family['family_name'], 'FAMILY-NAME', 'FAMILY-HEADER');
$table->addRow();
$rowOpen = TRUE;
if (!empty($_REQUEST['include_photos'])) {
// Add photo cell but stay on the same row.
$cell = $table->addCell(NULL, $narrowCellProps);
if ($family['have_photo']) {
$tempfile = str_replace('.tmp', '', tempnam(sys_get_temp_dir(), 'contactlistphoto')).'.jpg';
$cleanup[] = $tempfile;
file_put_contents($tempfile, Photo_Handler::getPhotoData('family', $family['familyid']));
$cell->addImage($tempfile, $imageStyle);
} else if (count($family['optins']) == 1) {
if ($family['optins'][0]['have_person_photo']) {
$tempfile = str_replace('.tmp', '', tempnam(sys_get_temp_dir(), 'contactlistphoto')).'.jpeg';
$cleanup[] = $tempfile;
file_put_contents($tempfile, Photo_Handler::getPhotoData('person', $family['optins'][0]['id']));
$cell->addImage($tempfile, $imageStyle);
} else {
$cell->addImage(JETHRO_ROOT.'/resources/img/unknown.jpg', $imageStyle);
}
} else {
$cell->addImage(JETHRO_ROOT.'/resources/img/unknown_family.jpg', $imageStyle);
}
}
if (count($family['all']) > count($family['optins'])) {
$table->addCell(NULL, $wideCellProps)
->addText($family['all_names'], 'FAMILY-MEMBERS', 'FAMILY-SUB-HEADER');
$rowOpen = FALSE;
}
if (!empty($_REQUEST['include_address']) && $family['address_street']) {
if (!$rowOpen) {
$table->addRow();
if (!empty($_REQUEST['include_photos'])) {
$table->addCell(NULL, $mergeProps);
}
}
$cell = $table->addCell(NULL, $wideCellProps);
// save vertical space by putting address on one line
$text = str_replace("\n", ', ', $family['address_street']).', ';
$text .= $family['address_suburb'].' '.$family['address_state'].' '.$family['address_postcode'];
$cell->addText($text, 'ADDRESS');
$rowOpen = FALSE;
}
if (!empty($_REQUEST['include_home_tel']) && $family['home_tel']) {
if (!$rowOpen) {
$table->addRow();
if (!empty($_REQUEST['include_photos'])) {
$table->addCell(NULL, $mergeProps);
}
}
$table->addCell(NULL, $wideCellProps)
->addText($family['home_tel'], 'HOME PHONE');
$rowOpen = FALSE;
}
foreach ($family['optins'] as $member) {
if (!$rowOpen) {
$table->addRow();
if (!empty($_REQUEST['include_photos'])) {
$table->addCell(NULL, $mergeProps);
}
}
$table->addCell($width*0.25, $narrowCellProps)->addText($member['name'], 'PERSON-NAME');
if (!empty($_REQUEST['include_congregation'])) {
$table->addCell($width*0.25, $narrowCellProps)->addText($member['congname']);
}
//$nbsp = html_entity_decode(' ');
$table->addCell($width*0.25, $narrowCellProps)->addText($member['mobile_tel']);
$table->addCell($width*0.2, $narrowCellProps)->addText($member['email']);
$rowOpen = FALSE;
}
}
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$tempname = tempnam(sys_get_temp_dir(), 'contactlist');
$objWriter->save($tempname);
foreach ($cleanup as $file) {
unlink($file);
}
//readfile($tempname); exit; // TEMP
/////////////////////////////////////////////////////////////////////////////////
// STEP 2 - MERGE THE CONTACT LIST DATA FROM THE TEMPORARY FILE INTO A TEMPLATE
// EITHER THE BUILT-IN ONE OR A CUSTOM ONE
// In the original file:
// - in word/media folder
// rename xyz.jpg to jethroimage-xyz.jpg
// - in word/_rels/document.xml.rels
// change ID="xyz" to ID="jethroimage-xyz"
// change Target="media/xyz.jpg" to Target="media/jethroimage-xyz.jpg"
// - in word/document.xml
// change r:id="xyz" to r:id="jethroimage-xyz"
// Then copy the document content into the new word/document.xml
// Then add the images from word/media to the new zip
// Then add the relationships from word/_rels/document.xml.rels to new zip's rels file.
//
$templateFilename = Documents_Manager::getRootPath().'/Templates/contact_list_template.docx';
if (!file_exists($templateFilename)) {
// no custom template found - use the built-in template
$templateFilename = JETHRO_ROOT.'/resources/contact_list_template.docx';
}
if (file_exists($templateFilename)) {
require_once 'include/odf_tools.class.php';
$outname = tempnam(sys_get_temp_dir(), 'contactlist').'.docx';
copy($templateFilename, $outname);
ODF_Tools::insertFileIntoFile($tempname, $outname, '%CONTACT_LIST%');
$replacements = Array('SYSTEM_NAME' => SYSTEM_NAME);
ODF_Tools::replaceKeywords($outname, $replacements);
readfile($outname);
unlink($outname);
} else {
// Couldn't find any template (!) - dump the temporary raw file.
readfile($tempname);
}
unlink($tempname);
}
}