-
Notifications
You must be signed in to change notification settings - Fork 1
/
civicrm-contact-directory.php
625 lines (563 loc) · 22 KB
/
civicrm-contact-directory.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
<?php
/*
Plugin Name: CiviCRM Contact Directory
Plugin URI: https://git.aghstrategies.com/
Description: Creates a shortcode to make a Directory of CiviCRM Contacts
Version: 3.1
Author: AGH Strategies, LLC
Author URI: http://aghstrategies.com/
*/
/*
* Copyright 2013-2022 AGH Strategies, LLC (email : [email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
// Shortcode [civicrm_contact_directory]
add_shortcode('civicrm_contact_directory', 'civicrm_contact_directory_shortcode');
wp_enqueue_style('civicrm-contact-directory-css', plugins_url('civicrm-contact-directory.css', __FILE__));
/**
* Function to Create Shortcode for directory
*/
function civicrm_contact_directory_shortcode($atts) {
civicrm_initialize();
// Used to set the default for the display name field
$displayNameFilter = $proximityFilter = $locationDefault = $distanceDefault = $displayNameDefault = $specialtyFilterHTML = '';
// Compile filters (if any are sent)
$filters = $locations = $myLocation = [];
// Parameter for group of contacts to be displayed in the directory
if (!empty($atts['group'])) {
$groupToDisplay = $atts['group'];
}
// Message Template to be used on the single contact view
if (!empty($atts['singleview'])) {
$singleView = $atts['singleview'];
}
// Include the specialty filter?
if (!empty($atts['specialty'])) {
$specialtyFilter = $atts['specialty'];
}
if (!empty($atts['displaynamefilter'])) {
$displayNameFilter = $atts['displaynamefilter'];
}
// Filter by Display Name
if (!empty($atts['mainview'])) {
$mainView = $atts['mainview'];
}
else {
$mainView = NULL;
}
if (!empty($atts['proximityfilter'])) {
$proximityFilter = $atts['proximityfilter'];
}
if (!empty($atts['group_by'])) {
$filters['group_by'] = $atts['group_by'];
}
if (isset($specialtyFilter)) {
// Get Specialty Filter Options
$specialtyOptions = '';
$specialty = civicrm_contact_directory_get_field_options($specialtyFilter);
if (!empty($specialty)) {
foreach ($specialty as $id => $label) {
// If a search has been run using the specialty field default to the specialty selected
if (isset($_POST[$specialtyFilter]) && $_POST[$specialtyFilter] == $id) {
$specialtyOptions .= "<option selected='selected' value={$id}>{$label}</option>";
}
else {
$specialtyOptions .= "<option value={$id}>{$label}</option>";
}
}
// get specialty field label
try {
$specialtyLabel = civicrm_api3('Contact', 'getfield', [
'name' => $specialtyFilter,
'action' => "create",
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error while finding contacts %1', array(
'domain' => 'civicrm-contact-directory',
1 => $error,
)));
}
}
// Set sepcialty field label
$sLabel = 'Specialty';
if (isset($specialtyLabel['values']['label'])) {
$sLabel = $specialtyLabel['values']['label'];
}
$specialtyFilterHTML = '<label>' . $sLabel . '</label></br>
<select name="' . $specialtyFilter .'" id="' . $specialtyFilter . '" class="specialty">
<option value="">- Choose ' . $sLabel . ' - </option>' . $specialtyOptions . '
</select>
</br>';
}
if (isset($_POST['gg'])) {
if (isset($_POST['display_name'])) {
// Set the default for the display name and Send the display name filter
$displayNameDefault = $filters['display_name'] = $_POST['display_name'];
}
if (isset($_POST['distance'])) {
$distanceDefault = $filters['distance'] = $_POST['distance'];
}
if (isset($_POST['location'])) {
$locationDefault = $filters['location'] = $_POST['location'];
}
// Send the specialty filter
if (isset($_POST[$specialtyFilter])) {
$filters[$specialtyFilter] = $_POST[$specialtyFilter];
}
}
$searchForm = '<form class="civiDirectoryForm" method = "post"><h2>Search Filters</h2>';
if ($displayNameFilter == 1) {
$searchForm .= ' <label>Name</label></br>
<input class="displayName" type="text" size="50" name="display_name" value=' . $displayNameDefault . '>
</br>';
}
$searchForm .= $specialtyFilterHTML;
if ($proximityFilter == 1) {
$searchForm = $searchForm . ' <label>Proximity</label>
</br>
<span>With in</span>
<input size="7" type="text" name="distance" value=' . $distanceDefault . '>
<span>miles of</span>
<input size="20" type="text" name="location" value=' . $locationDefault . '>
</br>';
}
$searchForm .= '<input class="searchButton" type="submit" name="gg" value="Search"></form>';
if (isset($_GET['cid'])) {
// If a cid is in the url DO NOT show the search form
$searchForm = "";
$filters['contact_id'] = $_GET['cid'];
}
list($resultsDiv, $locations, $myLocation) = civicrm_contact_directory_results($filters, $groupToDisplay, $singleView, $specialtyFilter, $mainView);
//add mapping option, but not for a single card
if (!empty($atts['map'] && empty($filters['contact_id']))) {
// send results to js for mapping
wp_register_script('map_script', plugin_dir_url( __FILE__ ) . 'js/map.js', array('jquery', 'underscore'));
wp_localize_script('map_script', 'locations', $locations);
wp_localize_script('map_script', 'mylocation', $myLocation);
wp_enqueue_script('map_script', plugin_dir_url( __FILE__ ) . 'js/map.js' );
wp_enqueue_script('openLayers', 'https://cdnjs.cloudflare.com/ajax/libs/openlayers/2.13.1/OpenLayers.js');
$resultsDiv = '<div id="osm_map" style="width: 580px; height: 400px"></div>' . $resultsDiv;
}
if ($atts['search'] == 'no') {
return "$resultsDiv";
}
else {
return "$searchForm $resultsDiv";
}
}
/**
* Get results based on filters
* @param array $filters filters to search on
* @return string formatted html to be displayed
*/
function civicrm_contact_directory_results($filters, $groupToDisplay = NULL, $singleView = NULL, $specialtyFilter = NULL, $mainView) {
$oopsSomethingDidNotWork = [];
$locations = [];
$myLocation = civicrm_contact_directory_calculate_mylocation($filters);
civicrm_initialize();
// Do we need to do a proximity search
if (!empty($filters['distance']) && !empty($filters['location'])) {
// get lat/long of location (throw error if it cant be calculated)
$proximityFromLocationData = [
'street_address' => $filters['location'],
];
CRM_Core_BAO_Address::addGeocoderData($proximityFromLocationData);
if (!empty($proximityFromLocationData['geo_code_1']) && !empty($proximityFromLocationData['geo_code_2'])) {
// Get all contacts in proximity lat, long
$contactsInRange = civicrm_contact_directory_get_proximity($filters['distance'], $proximityFromLocationData['geo_code_1'], $proximityFromLocationData['geo_code_2']);
if (empty($contactsInRange)) {
$oopsSomethingDidNotWork['error_message'] = "No contacts found in range.";
}
}
else {
$oopsSomethingDidNotWork['error_message'] = "Could Not Calculate proximity based on the location entered.";
}
}
// Validate that both location and distance have been entered
if (!empty($filters['distance']) && empty($filters['location'])) {
$oopsSomethingDidNotWork['error_message'] = "To calculate proximity please enter an address.";
}
if (empty($filters['distance']) && !empty($filters['location'])) {
$oopsSomethingDidNotWork['error_message'] = "To calculate proximity please enter a number into the distance field.";
}
// Set empty results just in case
$formattedResults = '';
// Set the context as directory (as opposed to viewing a single contact)
$context = 'directory';
// Set the default return params
$returnParams = [
"display_name",
"street_address",
"supplemental_address_1",
"city",
"state_province_name",
"postal_code",
"phone",
"state_province_id",
"state_province",
"geo_code_1",
"geo_code_2",
];
// Set the default Search Params
$searchParams = [
// 'contact_sub_type' => ['IN' => ["Provider", "Residential"]],
'is_deleted' => 0,
'options' => [
'limit' => "",
'sort' => "display_name asc",
],
];
if ($groupToDisplay) {
$searchParams['group'] = $groupToDisplay;
}
if (!empty($contactsInRange)) {
$searchParams['id'] = ['IN' => $contactsInRange];
}
// Filter by Display NAme
if (!empty($filters['display_name'])) {
$searchParams['display_name'] = "%{$filters['display_name']}%";
}
// Filter by specialty
if (!empty($filters[$specialtyFilter]) && isset($specialtyFilter)) {
$searchParams[$specialtyFilter] = $filters[$specialtyFilter];
}
// Filter by contact id -> Used when showing a single card
if (!empty($filters['contact_id'])) {
$searchParams['contact_id'] = $filters['contact_id'];
// Add custom fields to the list of params to return
$customFieldsToInclude = civicrm_contact_directory_get_custom_fields();
foreach ($customFieldsToInclude as $fieldId => $values) {
$returnParams[] = 'custom_' . $fieldId;
}
$context = 'single';
}
$searchParams['return'] = $returnParams;
// Get Relevant Contacts
try {
$contacts = civicrm_api3('Contact', 'get', $searchParams);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error while finding contacts %1', array(
'domain' => 'civicrm-contact-directory',
1 => $error,
)));
}
if (!empty($contacts['values'])) {
//if we set a group_by filter, set that up here.
if (!empty($filters['group_by']) && $context != 'single') {
foreach ($contacts['values'] as $contactId => $contactDetails) {
$groupedContacts[$contactDetails[$filters['group_by']]][$contactId] = $contactDetails;
}
foreach ($groupedContacts as $header => $group) {
//specialty processing for state/province headers
if ($filters['group_by'] == 'state_province') {
$stateProvinceName = \Civi\Api4\StateProvince::get(FALSE)
->addSelect('name')
->addWhere('abbreviation', '=', $header)
->setLimit(1)
->execute();
$header = $stateProvinceName[0]['name'];
}
$formattedResults .= '<h2>' . $header . '</h2>';
foreach ($group as $groupedContactId => $groupedContactDetails) {
$formattedResults .= civicrm_contact_directory_format_contact($groupedContactDetails, $context, $singleView, $mainView, $locations);
}
}
}
else {
foreach ($contacts['values'] as $contactId => $contactDetails) {
$formattedResults .= civicrm_contact_directory_format_contact($contactDetails, $context, $singleView, $mainView, $locations);
}
}
}
else {
$oopsSomethingDidNotWork['error_message'] = "No Results Found.";
}
if (!empty($oopsSomethingDidNotWork['error_message'])) {
$formattedResults = "<div class='error'>{$oopsSomethingDidNotWork['error_message']}</div>";
}
$resultsDiv = "<div class='resultsDiv'>$formattedResults</div>";
return [$resultsDiv, $locations, $myLocation];
}
/**
* Formats Contacts to be displayed
* @param array $contactDetails array of information to display
* @param string $context single (to display just one contact) or directory (for the full listing)
* @return string formatted html to be displayed
*/
function civicrm_contact_directory_format_contact($contactDetails, $context, $singleView = NULL, $mainView = NULL, &$locations) {
$displayName = "<h3>{$contactDetails['display_name']}</h3>";
$singleViewDetails = '';
$locations[$contactDetails['id']] = civicrm_contact_directory_contact_location($contactDetails);
// Format Single card entries
if ($context == 'single' && $singleView) {
$additionalDetails = civicrm_contact_directory_message_template($singleView, $contactDetails['contact_id']);
try {
$msgTemplate = civicrm_api3('MessageTemplate', 'getsingle', ['id' => $singleView]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error while finding contacts %1', array(
'domain' => 'civicrm-contact-directory',
1 => $error,
)));
}
if (!empty(!empty($msgTemplate['msg_html']))) {
$mailing = new CRM_Mailing_BAO_Mailing();
$mailing->body_html = $msgTemplate['msg_html'];
$tokens = $mailing->getTokens();
$returnProperties = [];
$contactParams = ['contact_id' => $contactDetails['contact_id']];
foreach ($tokens['html']['contact'] as $name) {
$returnProperties[$name] = 1;
}
list($contact) = CRM_Utils_Token::getTokenDetails($contactParams,
$returnProperties,
FALSE, FALSE, NULL,
CRM_Utils_Token::flattenTokens($tokens),
// we should consider adding groupName and valueName here
'CRM_Core_BAO_MessageTemplate'
);
$singleViewDetails .= $additionalDetails;
}
$url = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$singleViewDetails .= "<div><a href='$url'>Back to directory listing</a></div>";
}
elseif ($singleView) {
$displayName = "<a href='{$_SERVER['REQUEST_URI']}?cid={$contactDetails['contact_id']}'>$displayName</a>";
}
if (!empty($contactDetails['city']) && !empty($contactDetails['state_province'])) {
$addressLine = "{$contactDetails['city']}, {$contactDetails['state_province']} {$contactDetails['postal_code']}";
}
else {
$addressLine = "{$contactDetails['city']} {$contactDetails['state_province']} {$contactDetails['postal_code']}";
}
if ($mainView == NULL) {
//Format Address line
return "<div class='civicontact'>
<div>{$displayName}</div>
<div>{$contactDetails['street_address']}</div>
<div>{$contactDetails['supplemental_address_1']}</div>
<div>{$addressLine}</div>
<div>{$contactDetails['phone']}</div>
$singleViewDetails
</div>";
}
else {
$contactInfo = civicrm_contact_directory_message_template($mainView, $contactDetails['contact_id']);
return "<div class='civicontact'>$contactInfo</div>";
}
}
function civicrm_contact_directory_message_template($templateId, $contactId) {
$result = '';
try {
$msgTemplate = civicrm_api3('MessageTemplate', 'getsingle', ['id' => $templateId]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error while finding contacts %1', array(
'domain' => 'civicrm-contact-directory',
1 => $error,
)));
}
if (!empty($msgTemplate['msg_html'])) {
$mailing = new CRM_Mailing_BAO_Mailing();
$mailing->body_html = $msgTemplate['msg_html'];
$tokens = $mailing->getTokens();
$returnProperties = [];
$contactParams = ['contact_id' => $contactId];
foreach ($tokens['html']['contact'] as $name) {
$returnProperties[$name] = 1;
}
list($contact) = CRM_Utils_Token::getTokenDetails($contactParams,
$returnProperties,
FALSE, FALSE, NULL,
CRM_Utils_Token::flattenTokens($tokens),
// we should consider adding groupName and valueName here
'CRM_Core_BAO_MessageTemplate'
);
$result .= CRM_Utils_Token::replaceContactTokens($msgTemplate['msg_html'], $contact, FALSE, $tokens['html'], FALSE, TRUE);
}
return $result;
}
/**
* Get the custom fields to display on cards
* @return array data about the relevant custom fields
*/
function civicrm_contact_directory_get_custom_fields() {
$fieldsToInclude = [];
try {
$customFields = civicrm_api3('CustomField', 'get', [
'custom_group_id' => ['IN' => ["Specialty", "Residential_Options"]],
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error while finding custom values %1', array(
'domain' => 'civicrm-contact-directory',
1 => $error,
)));
}
if (!empty($customFields['values'])) {
$fieldsToInclude = $customFields['values'];
}
return $fieldsToInclude;
}
/**
* Returns the field options for a given field keyed id => Label
* @param string $fieldName field name
* @return array options for the field
*/
function civicrm_contact_directory_get_field_options($fieldName) {
try {
$fieldOptions = civicrm_api3('Contact', 'getoptions', [
'field' => $fieldName,
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error while finding custom field options %1', array(
'domain' => 'civicrm-contact-directory',
1 => $error,
)));
}
return $fieldOptions['values'];
}
/**
* Get Contacts in range
* @param int $distance number of miles
* @param float $lat lattitude of location
* @param float $long longitude of location
* @return array array of contact ids of contacts in range
*/
function civicrm_contact_directory_get_proximity($distance, $lat, $long) {
$contactsInRange = [];
try {
$contactsInProximity = civicrm_api3('Contact', 'proximity', [
'latitude' => $lat,
'longitude' => $long,
'unit' => "miles",
'distance' => $distance,
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error while finding proximity %1', array(
'domain' => 'civicrm-contact-directory',
1 => $error,
)));
}
if (!empty($contactsInProximity['values'])) {
foreach ($contactsInProximity['values'] as $key => $contactDetails) {
$contactsInRange[] = $contactDetails['contact_id'];
}
}
return $contactsInRange;
}
function civicrm_contact_directory_calculate_mylocation($params) {
// If a location is entered try and map it
if (!empty($params['distance']) && !empty($params['location'])) {
$proximityFromLocationData = [
'street_address' => $params['location'],
'country_id' => 1228,
];
CRM_Core_BAO_Address::addGeocoderData($proximityFromLocationData);
// If the location has been mapped return coordinates
if (isset($proximityFromLocationData['geo_code_1'])
&& isset($proximityFromLocationData['geo_code_2'])
&& $proximityFromLocationData['geo_code_1'] != NULL
&& $proximityFromLocationData['geo_code_1'] != NULL) {
$myLocation = [
'lat' => $proximityFromLocationData['geo_code_1'],
'long' => $proximityFromLocationData['geo_code_2'],
'entered' => 'yes',
'is_error' => 0,
];
if (!is_numeric($params['distance'])) {
$myLocation['is_error'] = 1;
$myLocation['error_message'] = 'Distance must be a number';
}
}
// If the location cannot be mapped return error message
else {
$myLocation = [
'is_error' => 1,
'error_message' => "Could Not Calculate proximity based on the location entered.",
];
}
}
// Validate that both location and distance have been entered
elseif (!empty($params['distance']) && empty($params['location'])) {
$myLocation = [
'long' => -100.372127,
'lat' => 38.891033,
'entered' => 'no',
'is_error' => 1,
'error_message' => "To calculate proximity please enter a location to calculate the distance from."
];
}
elseif (empty($params['distance']) && !empty($params['location'])) {
$myLocation = [
'long' => -100.372127,
'lat' => 38.891033,
'entered' => 'no',
'is_error' => 1,
'error_message' => "To calculate proximity please enter a number into the distance field."
];
}
// IF no location is entered return coordinates of the center of the continental US to be used for centering the map.
elseif (empty($params['distance']) && empty($params['location'])) {
$myLocation = ['long' => -100.372127, 'lat' => 38.891033, 'entered' => 'no', 'is_error' => 0];
}
return $myLocation;
}
function civicrm_contact_directory_contact_location($contactDetails) {
$locationArray = [];
$locationData = [
'street_address' => $contactDetails['street_address'],
'city' => $contactDetails['city'],
'state_province_id' => $contactDetails['state_province_id'],
'postal_code' => $contactDetails['postal_code'],
'country_id' => 1228
];
//CRM_Core_BAO_Address::addGeocoderData($locationData);
$locationArray['lat'] = $contactDetails['geo_code_1'];
$locationArray['long'] = $contactDetails['geo_code_2'];
//this sets the content for the map popups
//@TODO this can definitely be refactored with what's above, but for now it's
//a copy/paste because I am running out of time for map popups
if (!empty($contactDetails['city']) && !empty($contactDetails['state_province'])) {
$addressLine = "{$contactDetails['city']}, {$contactDetails['state_province']} {$contactDetails['postal_code']}";
}
else {
$addressLine = "{$contactDetails['city']} {$contactDetails['state_province']} {$contactDetails['postal_code']}";
}
$displayName = "<h3>{$contactDetails['display_name']}</h3>";
$displayName = "<a href='{$_SERVER['REQUEST_URI']}?cid={$contactDetails['contact_id']}'>$displayName</a>";
$locationArray['text'] = "<div class='civicontact'>
<div>{$displayName}</div>
<div>{$contactDetails['street_address']}</div>
<div>{$contactDetails['supplemental_address_1']}</div>
<div>{$addressLine}</div>
<div>{$contactDetails['phone']}</div>
</div>";
//set the map pin image URL
$locationArray['logoLink'] = plugin_dir_url( __FILE__ ) . '/map_pin.png';
return $locationArray;
}