-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathFunctions.php
1621 lines (1424 loc) · 56.4 KB
/
Functions.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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use ChurchCRM\Authentication\AuthenticationManager;
use ChurchCRM\dto\Cart;
use ChurchCRM\dto\SystemConfig;
use ChurchCRM\Service\PersonService;
use ChurchCRM\Service\SystemService;
use ChurchCRM\Utils\InputUtils;
use ChurchCRM\Utils\LoggerUtils;
$personService = new PersonService();
$systemService = new SystemService();
$_SESSION['sSoftwareInstalledVersion'] = SystemService::getInstalledVersion();
// Basic security checks:
if (empty($bSuppressSessionTests)) { // This is used for the login page only.
AuthenticationManager::ensureAuthentication();
}
// If magic_quotes off and array
function addslashes_deep($value)
{
return is_array($value) ?
array_map('addslashes_deep', $value) :
addslashes($value);
}
// If Magic Quotes is turned off, do the same thing manually..
if (!isset($_SESSION['bHasMagicQuotes'])) {
foreach ($_REQUEST as $value) {
$value = addslashes_deep($value);
}
}
// Constants
$aPropTypes = [
1 => gettext('True / False'),
2 => gettext('Date'),
3 => gettext('Text Field (50 char)'),
4 => gettext('Text Field (100 char)'),
5 => gettext('Text Field (long)'),
6 => gettext('Year'),
7 => gettext('Season'),
8 => gettext('Number'),
9 => gettext('Person from Group'),
10 => gettext('Money'),
11 => gettext('Phone Number'),
12 => gettext('Custom Drop-Down List'),
];
$sGlobalMessageClass = 'success';
if (isset($_GET['Registered'])) {
$sGlobalMessage = gettext('Thank you for registering your ChurchCRM installation.');
}
if (isset($_GET['PDFEmailed'])) {
if ($_GET['PDFEmailed'] == 1) {
$sGlobalMessage = gettext('PDF successfully emailed to family members.');
} else {
$sGlobalMessage = gettext('Failed to email PDF to family members.');
}
}
// Are they adding an entire group to the cart?
if (isset($_GET['AddGroupToPeopleCart'])) {
AddGroupToPeopleCart(InputUtils::legacyFilterInput($_GET['AddGroupToPeopleCart'], 'int'));
$sGlobalMessage = gettext('Group successfully added to the Cart.');
}
// Are they removing an entire group from the Cart?
if (isset($_GET['RemoveGroupFromPeopleCart'])) {
RemoveGroupFromPeopleCart(InputUtils::legacyFilterInput($_GET['RemoveGroupFromPeopleCart'], 'int'));
$sGlobalMessage = gettext('Group successfully removed from the Cart.');
}
if (isset($_GET['ProfileImageDeleted'])) {
$sGlobalMessage = gettext('Profile Image successfully removed.');
}
if (isset($_GET['ProfileImageUploaded'])) {
$sGlobalMessage = gettext('Profile Image successfully updated.');
}
if (isset($_GET['ProfileImageUploadedError'])) {
$sGlobalMessage = gettext('Profile Image upload Error.');
$sGlobalMessageClass = 'danger';
}
// Are they removing a person from the Cart?
if (isset($_GET['RemoveFromPeopleCart'])) {
RemoveFromPeopleCart(InputUtils::legacyFilterInput($_GET['RemoveFromPeopleCart'], 'int'));
$sGlobalMessage = gettext('Selected record successfully removed from the Cart.');
}
if (isset($_POST['BulkAddToCart'])) {
$aItemsToProcess = explode(',', $_POST['BulkAddToCart']);
if (isset($_POST['AndToCartSubmit'])) {
if (isset($_SESSION['aPeopleCart'])) {
$_SESSION['aPeopleCart'] = array_intersect($_SESSION['aPeopleCart'], $aItemsToProcess);
}
} elseif (isset($_POST['NotToCartSubmit'])) {
if (isset($_SESSION['aPeopleCart'])) {
$_SESSION['aPeopleCart'] = array_diff($_SESSION['aPeopleCart'], $aItemsToProcess);
}
} else {
for ($iCount = 0; $iCount < count($aItemsToProcess); $iCount++) {
Cart::addPerson(str_replace(',', '', $aItemsToProcess[$iCount]));
}
$sGlobalMessage = $iCount . ' ' . gettext('item(s) added to the Cart.');
}
}
//
// Some very basic functions that all scripts use
//
// Returns the current fiscal year
function CurrentFY(): int
{
$yearNow = (int) date('Y');
$monthNow = (int) date('m');
$FYID = $yearNow - 1996;
if ($monthNow >= SystemConfig::getValue('iFYMonth') && SystemConfig::getValue('iFYMonth') > 1) {
$FYID += 1;
}
return $FYID;
}
// PrintFYIDSelect: make a fiscal year selection menu.
function PrintFYIDSelect(string $selectName, int $iFYID = null): void
{
echo sprintf('<select class="form-control" name="%s">', $selectName);
$hasSelected = false;
$selectableOptions = [];
for ($fy = 1; $fy < CurrentFY() + 2; $fy++) {
$selectedTag = '';
if ($iFYID === $fy) {
$hasSelected = true;
$selectedTag = ' selected';
}
$selectableOptions[] = sprintf('<option value="%s"', $fy) . $selectedTag . '>' . MakeFYString((int) $fy) . '</option>';
}
$selectableOptions = [
'<option disabled value="0"' . (!$hasSelected ? ' selected' : '') . '>' . gettext('Select Fiscal Year') . '</option>',
...$selectableOptions
];
echo implode('', $selectableOptions);
echo '</select>';
}
// Formats a fiscal year string
function MakeFYString(int $iFYID): string
{
if (SystemConfig::getValue('iFYMonth') == 1) {
return (string) (1996 + $iFYID);
} else {
return 1995 + $iFYID . '/' . mb_substr(1996 + $iFYID, 2, 2);
}
}
// Runs an SQL query. Returns the result resource.
// By default stop on error, unless a second (optional) argument is passed as false.
function RunQuery(string $sSQL, bool $bStopOnError = true)
{
global $cnInfoCentral;
mysqli_query($cnInfoCentral, "SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))");
if ($result = mysqli_query($cnInfoCentral, $sSQL)) {
return $result;
} elseif ($bStopOnError) {
LoggerUtils::getAppLogger()->error(gettext('Cannot execute query.') . " " . $sSQL . " -|- " . mysqli_error($cnInfoCentral));
if (SystemConfig::getValue('sLogLevel') == "100") { // debug level
throw new Exception(gettext('Cannot execute query.') . "<p>$sSQL<p>" . mysqli_error($cnInfoCentral));
} else {
throw new Exception('Database error or invalid data, change sLogLevel to debug to see more.');
}
} else {
return false;
}
}
//
// Adds a volunteer opportunity assignment to a person
//
function AddVolunteerOpportunity(string $iPersonID, string $iVolID)
{
$sSQL = 'INSERT INTO person2volunteeropp_p2vo (p2vo_per_ID, p2vo_vol_ID) VALUES (' . $iPersonID . ', ' . $iVolID . ')';
return RunQuery($sSQL, false);
}
function RemoveVolunteerOpportunity(string $iPersonID, string $iVolID): void
{
$sSQL = 'DELETE FROM person2volunteeropp_p2vo WHERE p2vo_per_ID = ' . $iPersonID . ' AND p2vo_vol_ID = ' . $iVolID;
RunQuery($sSQL);
}
function convertCartToString(array $aCartArray): string
{
// Implode the array
$sCartString = implode(',', $aCartArray);
// Make sure the comma is chopped off the end
if (mb_substr($sCartString, strlen($sCartString) - 1, 1) == ',') {
$sCartString = mb_substr($sCartString, 0, strlen($sCartString) - 1);
}
// Make sure there are no duplicate commas
$sCartString = str_replace(',,', '', $sCartString);
return $sCartString;
}
/*
* Returns the proper information to use for a field.
* Person info overrides Family info if they are different.
* If using family info and bFormat set, generate HTML tags for text color red.
* If neither family nor person info is available, return an empty string.
*/
function SelectWhichInfo(string $sPersonInfo = null, string $sFamilyInfo = null, bool $bFormat = false): string
{
$sPersonInfo ??= '';
$sFamilyInfo ??= '';
$finalData = '';
$isFamily = false;
if (SystemConfig::getValue('bShowFamilyData')) {
if ($sPersonInfo !== '') {
$finalData = $sPersonInfo;
} elseif ($sFamilyInfo !== '') {
$isFamily = true;
$finalData = $sFamilyInfo;
}
} elseif ($sPersonInfo != '') {
$finalData = $sPersonInfo;
}
if ($bFormat && $isFamily) {
$finalData = $finalData . "<i class='fa fa-fw fa-tree'></i>";
}
return $finalData;
}
//
// Returns the correct address to use via the sReturnAddress arguments.
// Function value returns 0 if no info was given, 1 if person info was used, and 2 if family info was used.
// We do address lines 1 and 2 in together because separately we might end up with half family address and half person address!
//
function SelectWhichAddress(&$sReturnAddress1, &$sReturnAddress2, $sPersonAddress1, $sPersonAddress2, ?string $sFamilyAddress1, ?string $sFamilyAddress2, bool $bFormat = false): int
{
if (SystemConfig::getValue('bShowFamilyData')) {
if ($bFormat) {
$sFamilyInfoBegin = "<span style='color: red;'>";
$sFamilyInfoEnd = '</span>';
}
if ($sPersonAddress1 || $sPersonAddress2) {
$sReturnAddress1 = $sPersonAddress1;
$sReturnAddress2 = $sPersonAddress2;
return 1;
} elseif ($sFamilyAddress1 || $sFamilyAddress2) {
if ($bFormat) {
if ($sFamilyAddress1) {
$sReturnAddress1 = $sFamilyInfoBegin . $sFamilyAddress1 . $sFamilyInfoEnd;
} else {
$sReturnAddress1 = '';
}
if ($sFamilyAddress2) {
$sReturnAddress2 = $sFamilyInfoBegin . $sFamilyAddress2 . $sFamilyInfoEnd;
} else {
$sReturnAddress2 = '';
}
return 2;
} else {
$sReturnAddress1 = $sFamilyAddress1;
$sReturnAddress2 = $sFamilyAddress2;
return 2;
}
} else {
$sReturnAddress1 = '';
$sReturnAddress2 = '';
return 0;
}
} else {
if ($sPersonAddress1 || $sPersonAddress2) {
$sReturnAddress1 = $sPersonAddress1;
$sReturnAddress2 = $sPersonAddress2;
return 1;
} else {
$sReturnAddress1 = '';
$sReturnAddress2 = '';
return 0;
}
}
}
function ChopLastCharacter(string $sText): string
{
return mb_substr($sText, 0, strlen($sText) - 1);
}
function change_date_for_place_holder(string $string = null): string
{
$string ??= '';
$timestamp = strtotime($string);
if ($timestamp !== false) {
return date(SystemConfig::getValue("sDatePickerFormat"), $timestamp);
}
return '';
}
function FormatDateOutput(): string
{
$fmt = SystemConfig::getValue("sDateFormatLong");
$fmt = str_replace("/", " ", $fmt);
$fmt = str_replace("-", " ", $fmt);
$fmt = str_replace("d", "%d", $fmt);
$fmt = str_replace("m", "%B", $fmt);
return str_replace("Y", "%Y", $fmt);
}
// Reinstated by Todd Pillars for Event Listing
// Takes MYSQL DateTime
// bWithtime 1 to be displayed
function FormatDate($dDate, bool $bWithTime = false): string
{
if ($dDate == '' || $dDate == '0000-00-00 00:00:00' || $dDate == '0000-00-00') {
return '';
}
if (strlen($dDate) === 10) { // If only a date was passed append time
$dDate = $dDate . ' 12:00:00';
} // Use noon to avoid a shift in daylight time causing
// a date change.
if (strlen($dDate) !== 19) {
return '';
}
// Verify it is a valid date
$sScanString = mb_substr($dDate, 0, 10);
[$iYear, $iMonth, $iDay] = sscanf($sScanString, '%04d-%02d-%02d');
if (!checkdate($iMonth, $iDay, $iYear)) {
return 'Unknown';
}
// PHP date() function is not used because it is only robust for dates between
// 1970 and 2038. This is a problem on systems that are limited to 32 bit integers.
// To handle a much wider range of dates use MySQL date functions.
$sSQL = "SELECT DATE_FORMAT('$dDate', '%b') as mn, "
. "DAYOFMONTH('$dDate') as dm, YEAR('$dDate') as y, "
. "DATE_FORMAT('$dDate', '%k') as h, "
. "DATE_FORMAT('$dDate', ':%i') as m";
extract(mysqli_fetch_array(RunQuery($sSQL)));
if ($h > 11) {
$sAMPM = gettext('pm');
if ($h > 12) {
$h = $h - 12;
}
} else {
$sAMPM = gettext('am');
if ($h == 0) {
$h = 12;
}
}
$fmt = FormatDateOutput();
$localValue = SystemConfig::getValue("sLanguage");
setlocale(LC_ALL, $localValue, $localValue . '.UTF-8', $localValue . '.utf8');
if ($bWithTime) {
return utf8_encode(strftime("$fmt %H:%M $sAMPM", strtotime($dDate)));
} else {
return utf8_encode(strftime("$fmt", strtotime($dDate)));
}
}
function AlternateRowStyle(string $sCurrentStyle): string
{
if ($sCurrentStyle === 'RowColorA') {
return 'RowColorB';
} else {
return 'RowColorA';
}
}
function ConvertToBoolean(string $sInput): bool
{
if (empty($sInput)) {
return false;
} else {
if (is_numeric($sInput)) {
if ($sInput == 1) {
return true;
} else {
return false;
}
} else {
$sInput = strtolower($sInput);
if (in_array($sInput, ['true', 'yes', 'si'])) {
return true;
} else {
return false;
}
}
}
}
function ConvertFromBoolean($sInput): int
{
if ($sInput) {
return 1;
} else {
return 0;
}
}
//
// Collapses a formatted phone number as long as the Country is known
// Eg. for United States: 555-555-1212 Ext. 123 ==> 5555551212e123
//
// Need to add other countries besides the US...
//
function CollapsePhoneNumber($sPhoneNumber, $sPhoneCountry)
{
switch ($sPhoneCountry) {
case 'United States':
$sCollapsedPhoneNumber = '';
$bHasExtension = false;
// Loop through the input string
for ($iCount = 0; $iCount <= strlen($sPhoneNumber); $iCount++) {
// Take one character...
$sThisCharacter = mb_substr($sPhoneNumber, $iCount, 1);
// Is it a number?
if (ord($sThisCharacter) >= 48 && ord($sThisCharacter) <= 57) {
// Yes, add it to the returned value.
$sCollapsedPhoneNumber .= $sThisCharacter;
} elseif (!$bHasExtension && ($sThisCharacter == 'e' || $sThisCharacter == 'E')) {
// Is the user trying to add an extension?
// Yes, add the extension identifier 'e' to the stored string.
$sCollapsedPhoneNumber .= 'e';
// From now on, ignore other non-digits and process normally
$bHasExtension = true;
}
}
break;
default:
$sCollapsedPhoneNumber = $sPhoneNumber;
break;
}
return $sCollapsedPhoneNumber;
}
//
// Expands a collapsed phone number into the proper format for a known country.
//
// If, during expansion, an unknown format is found, the original will be returned
// and the boolean flag $bWeird will be set. Unfortunately, because PHP does not
// allow for pass-by-reference in conjunction with a variable-length argument list,
// a dummy variable will have to be passed even if this functionality is unneeded.
//
// Need to add other countries besides the US...
//
function ExpandPhoneNumber(string $sPhoneNumber = null, string $sPhoneCountry = null, &$bWeird): string
{
$sPhoneNumber ??= '';
$sPhoneCountry ??= '';
$bWeird = false;
$length = strlen($sPhoneNumber);
switch ($sPhoneCountry) {
case 'United States' || 'Canada':
if ($length === 0) {
return '';
} elseif (mb_substr($sPhoneNumber, 7, 1) === 'e') {
// 7 digit phone # with extension
return mb_substr($sPhoneNumber, 0, 3) . '-' . mb_substr($sPhoneNumber, 3, 4) . ' Ext.' . mb_substr($sPhoneNumber, 8, 6);
} elseif (mb_substr($sPhoneNumber, 10, 1) === 'e') {
// 10 digit phone # with extension
return mb_substr($sPhoneNumber, 0, 3) . '-' . mb_substr($sPhoneNumber, 3, 3) . '-' . mb_substr($sPhoneNumber, 6, 4) . ' Ext.' . mb_substr($sPhoneNumber, 11, 6);
} elseif ($length === 7) {
return mb_substr($sPhoneNumber, 0, 3) . '-' . mb_substr($sPhoneNumber, 3, 4);
} elseif ($length === 10) {
return mb_substr($sPhoneNumber, 0, 3) . '-' . mb_substr($sPhoneNumber, 3, 3) . '-' . mb_substr($sPhoneNumber, 6, 4);
} else {
// Otherwise, there is something weird stored, so just leave it untouched and set the flag
$bWeird = true;
return $sPhoneNumber;
}
// If the country is unknown, we don't know how to format it, so leave it untouched
default:
return $sPhoneNumber;
}
}
// Returns a string of a person's full name, formatted as specified by $Style
// $Style = 0 : "Title FirstName MiddleName LastName, Suffix"
// $Style = 1 : "Title FirstName MiddleInitial. LastName, Suffix"
// $Style = 2 : "LastName, Title FirstName MiddleName, Suffix"
// $Style = 3 : "LastName, Title FirstName MiddleInitial., Suffix"
//
function FormatFullName(?string $Title, ?string $FirstName, ?string $MiddleName, ?string $LastName, ?string $Suffix, $Style): string
{
$nameString = '';
switch ($Style) {
case 0:
if ($Title) {
$nameString .= $Title . ' ';
}
$nameString .= $FirstName;
if ($MiddleName) {
$nameString .= ' ' . $MiddleName;
}
if ($LastName) {
$nameString .= ' ' . $LastName;
}
if ($Suffix) {
$nameString .= ', ' . $Suffix;
}
break;
case 1:
if ($Title) {
$nameString .= $Title . ' ';
}
$nameString .= $FirstName;
if ($MiddleName) {
$nameString .= ' ' . mb_strtoupper(mb_substr($MiddleName, 0, 1)) . '.';
}
if ($LastName) {
$nameString .= ' ' . $LastName;
}
if ($Suffix) {
$nameString .= ', ' . $Suffix;
}
break;
case 2:
if ($LastName) {
$nameString .= $LastName . ', ';
}
if ($Title) {
$nameString .= $Title . ' ';
}
$nameString .= $FirstName;
if ($MiddleName) {
$nameString .= ' ' . $MiddleName;
}
if ($Suffix) {
$nameString .= ', ' . $Suffix;
}
break;
case 3:
if ($LastName) {
$nameString .= $LastName . ', ';
}
if ($Title) {
$nameString .= $Title . ' ';
}
$nameString .= $FirstName;
if ($MiddleName) {
$nameString .= ' ' . mb_strtoupper(mb_substr($MiddleName, 0, 1)) . '.';
}
if ($Suffix) {
$nameString .= ', ' . $Suffix;
}
break;
}
return $nameString;
}
// Generate a nicely formatted string for "FamilyName - Address / City, State" with available data
function FormatAddressLine(?string $Address, ?string $City, ?string $State): string
{
$sText = '';
if ($Address != '' || $City != '' || $State != '') {
$sText = ' - ';
}
$sText .= $Address;
if ($Address != '' && ($City != '' || $State != '')) {
$sText .= ' / ';
}
$sText .= $City;
if ($City != '' && $State != '') {
$sText .= ', ';
}
return $sText . $State;
}
//
// Formats the data for a custom field for display-only uses
//
function displayCustomField($type, ?string $data, $special)
{
global $cnInfoCentral;
switch ($type) {
// Handler for boolean fields
case 1:
if ($data == 'true') {
return gettext('Yes');
} elseif ($data == 'false') {
return gettext('No');
}
break;
// Handler for date fields
case 2:
return FormatDate($data);
// Handler for text fields, years, seasons, numbers, money
case 3:
case 4:
case 6:
case 8:
case 10:
return $data;
// Handler for extended text fields (MySQL type TEXT, Max length: 2^16-1)
case 5:
/*if (strlen($data) > 100) {
return mb_substr($data, 0, 100) . "...";
}else{
return $data;
}
*/
return $data;
// Handler for season. Capitalize the word for nicer display.
case 7:
return ucfirst($data);
// Handler for "person from group"
case 9:
if ($data > 0) {
$sSQL = 'SELECT per_FirstName, per_LastName FROM person_per WHERE per_ID =' . $data;
$rsTemp = RunQuery($sSQL);
extract(mysqli_fetch_array($rsTemp));
return $per_FirstName . ' ' . $per_LastName;
} else {
return '';
}
// Handler for phone numbers
case 11:
return ExpandPhoneNumber($data, $special, $dummy);
// Handler for custom lists
case 12:
if ($data > 0) {
$sSQL = "SELECT lst_OptionName FROM list_lst WHERE lst_ID = $special AND lst_OptionID = $data";
$rsTemp = RunQuery($sSQL);
extract(mysqli_fetch_array($rsTemp));
return $lst_OptionName;
} else {
return '';
}
// Otherwise, display error for debugging.
default:
return gettext('Invalid Editor ID!');
}
}
//
// Generates an HTML form <input> line for a custom field
//
function formCustomField($type, string $fieldname, $data, ?string $special, bool $bFirstPassFlag): void
{
global $cnInfoCentral;
switch ($type) {
// Handler for boolean fields
case 1:
echo '<div class="form-group">' .
'<div class="radio"><label><input type="radio" Name="' . $fieldname . '" value="true"' . ($data == 'true' ? 'checked' : '') . '>' . gettext('Yes') . '</label></div>' .
'<div class="radio"><label><input type="radio" Name="' . $fieldname . '" value="false"' . ($data == 'false' ? 'checked' : '') . '>' . gettext('No') . '</label></div>' .
'<div class="radio"><label><input type="radio" Name="' . $fieldname . '" value=""' . (strlen($data) === 0 ? 'checked' : '') . '>' . gettext('Unknown') . '</label></div>' .
'</div>';
break;
// Handler for date fields
case 2:
// code rajouté par Philippe Logel
echo '<div class="input-group">' .
'<div class="input-group-addon">' .
'<i class="fa fa-calendar"></i>' .
'</div>' .
'<input class="form-control date-picker" type="text" id="' . $fieldname . '" Name="' . $fieldname . '" value="' . change_date_for_place_holder($data) . '" placeholder="' . SystemConfig::getValue("sDatePickerPlaceHolder") . '"> ' .
'</div>';
break;
// Handler for 50 character max. text fields
case 3:
echo '<input class="form-control" type="text" Name="' . $fieldname . '" maxlength="50" size="50" value="' . htmlentities(stripslashes($data), ENT_NOQUOTES, 'UTF-8') . '">';
break;
// Handler for 100 character max. text fields
case 4:
echo '<textarea class="form-control" Name="' . $fieldname . '" cols="40" rows="2" onKeyPress="LimitTextSize(this, 100)">' . htmlentities(stripslashes($data), ENT_NOQUOTES, 'UTF-8') . '</textarea>';
break;
// Handler for extended text fields (MySQL type TEXT, Max length: 2^16-1)
case 5:
echo '<textarea class="form-control" Name="' . $fieldname . '" cols="60" rows="4" onKeyPress="LimitTextSize(this, 65535)">' . htmlentities(stripslashes($data), ENT_NOQUOTES, 'UTF-8') . '</textarea>';
break;
// Handler for 4-digit year
case 6:
echo '<input class="form-control" type="text" Name="' . $fieldname . '" maxlength="4" size="6" value="' . $data . '">';
break;
// Handler for season (drop-down selection)
case 7:
echo "<select name=\"$fieldname\" class=\"form-control\" >";
echo ' <option value="none">' . gettext('Select Season') . '</option>';
echo ' <option value="winter"';
if ($data == 'winter') {
echo ' selected';
}
echo '>' . gettext('Winter') . '</option>';
echo ' <option value="spring"';
if ($data == 'spring') {
echo ' selected';
}
echo '>' . gettext('Spring') . '</option>';
echo ' <option value="summer"';
if ($data == 'summer') {
echo 'selected';
}
echo '>' . gettext('Summer') . '</option>';
echo ' <option value="fall"';
if ($data == 'fall') {
echo ' selected';
}
echo '>' . gettext('Fall') . '</option>';
echo '</select>';
break;
// Handler for integer numbers
case 8:
echo '<input class="form-control" type="text" Name="' . $fieldname . '" maxlength="11" size="15" value="' . $data . '">';
break;
// Handler for "person from group"
case 9:
// ... Get First/Last name of everyone in the group, plus their person ID ...
// In this case, prop_Special is used to store the Group ID for this selection box
// This allows the group special-property designer to allow selection from a specific group
$sSQL = 'SELECT person_per.per_ID, person_per.per_FirstName, person_per.per_LastName
FROM person2group2role_p2g2r
LEFT JOIN person_per ON person2group2role_p2g2r.p2g2r_per_ID = person_per.per_ID
WHERE p2g2r_grp_ID = ' . $special . ' ORDER BY per_FirstName';
$rsGroupPeople = RunQuery($sSQL);
echo '<select name="' . $fieldname . '" class="form-control" >';
echo '<option value="0"';
if ($data <= 0) {
echo ' selected';
}
echo '>' . gettext('Unassigned') . '</option>';
echo '<option value="" disabled>-----------------------</option>';
while ($aRow = mysqli_fetch_array($rsGroupPeople)) {
extract($aRow);
echo '<option value="' . $per_ID . '"';
if ($data == $per_ID) {
echo ' selected';
}
echo '>' . $per_FirstName . ' ' . $per_LastName . '</option>';
}
echo '</select>';
break;
// Handler for money amounts
case 10:
echo '<input class="form-control" type="text" Name="' . $fieldname . '" maxlength="13" size="16" value="' . $data . '">';
break;
// Handler for phone numbers
case 11:
// This is silly. Perhaps ExpandPhoneNumber before this function is called!
// this business of overloading the special field is really troublesome when trying to follow the code.
if ($bFirstPassFlag) {
// in this case, $special is the phone country
$data = ExpandPhoneNumber($data, $special, $bNoFormat_Phone);
}
if (isset($_POST[$fieldname . 'noformat'])) {
$bNoFormat_Phone = true;
}
echo '<div class="input-group">';
echo '<div class="input-group-addon">';
echo '<i class="fa fa-phone"></i>';
echo '</div>';
echo '<input class="form-control" type="text" Name="' . $fieldname . '" maxlength="30" size="30" value="' . htmlentities(stripslashes($data), ENT_NOQUOTES, 'UTF-8') . '" data-inputmask=\'"mask": "' . SystemConfig::getValue('sPhoneFormat') . '"\' data-mask>';
echo '<br><input type="checkbox" name="' . $fieldname . 'noformat" value="1"';
if ($bNoFormat_Phone) {
echo ' checked';
}
echo '>' . gettext('Do not auto-format');
echo '</div>';
break;
// Handler for custom lists
case 12:
$sSQL = "SELECT * FROM list_lst WHERE lst_ID = $special ORDER BY lst_OptionSequence";
$rsListOptions = RunQuery($sSQL);
echo '<select class="form-control" name="' . $fieldname . '">';
echo '<option value="0" selected>' . gettext('Unassigned') . '</option>';
echo '<option value="" disabled>-----------------------</option>';
while ($aRow = mysqli_fetch_array($rsListOptions)) {
extract($aRow);
echo '<option value="' . $lst_OptionID . '"';
if ($data == $lst_OptionID) {
echo ' selected';
}
echo '>' . $lst_OptionName . '</option>';
}
echo '</select>';
break;
// Otherwise, display error for debugging.
default:
echo '<b>' . gettext('Error: Invalid Editor ID!') . '</b>';
break;
}
}
function assembleYearMonthDay($sYear, $sMonth, $sDay, $pasfut = 'future')
{
// This function takes a year, month and day from parseAndValidateDate. On success this
// function returns a string in the form "YYYY-MM-DD". It returns FALSE on failure.
// The year can be either 2 digit or 4 digit. If a 2 digit year is passed the $passfut
// indicates whether to return a 4 digit year in the past or the future. The parameter
// $passfut is not needed for the current year. If unspecified it assumes the two digit year
// is either this year or one of the next 99 years.
// Parse the year
// Take a 2 or 4 digit year and return a 4 digit year. Use $pasfut to determine if
// two digit year maps to past or future 4 digit year.
if (strlen($sYear) === 2) {
$thisYear = date('Y');
$twoDigit = mb_substr($thisYear, 2, 2);
if ($sYear == $twoDigit) {
// Assume 2 digit year is this year
$sYear = mb_substr($thisYear, 0, 4);
} elseif ($pasfut == 'future') {
// Assume 2 digit year is in next 99 years
if ($sYear > $twoDigit) {
$sYear = mb_substr($thisYear, 0, 2) . $sYear;
} else {
$sNextCentury = $thisYear + 100;
$sYear = mb_substr($sNextCentury, 0, 2) . $sYear;
}
} else {
// Assume 2 digit year was is last 99 years
if ($sYear < $twoDigit) {
$sYear = mb_substr($thisYear, 0, 2) . $sYear;
} else {
$sLastCentury = $thisYear - 100;
$sYear = mb_substr($sLastCentury, 0, 2) . $sYear;
}
}
}
// If the $sYear is not YYYY, return false.
if (strlen($sYear) !== 4) {
return false;
}
// Parse the Month
// Take a one or two character month and return a two character month
if (strlen($sMonth) === 1) {
$sMonth = '0' . $sMonth;
}
// If the $sMonth is not MM, return false.
if (strlen($sMonth) !== 2) {
return false;
}
// Parse the Day
// Take a one or two character day and return a two character day
if (strlen($sDay) === 1) {
$sDay = '0' . $sDay;
}
// If the $sDay is not DD, return false.
if (strlen($sDay) !== 2) {
return false;
}
$sScanString = $sYear . '-' . $sMonth . '-' . $sDay;
[$iYear, $iMonth, $iDay] = sscanf($sScanString, '%04d-%02d-%02d');
if (checkdate($iMonth, $iDay, $iYear)) {
return $sScanString;
} else {
return false;
}
}
function parseAndValidateDate($data, $locale = 'US', $pasfut = 'future')
{
// This function was written because I had no luck finding a PHP
// function that would reliably parse a human entered date string for
// dates before 1/1/1970 or after 1/19/2038 on any Operating System.
//
// This function has hooks for US English M/D/Y format as well as D/M/Y. The
// default is M/D/Y for date. To change to D/M/Y use anything but "US" for
// $locale.
//
// Y-M-D is allowed if the delimiter is "-" instead of "/"
//
// In order to help this function guess a two digit year a "past" or "future" flag is
// passed to this function. If no flag is passed the function assumes that two digit
// years are in the future (or the current year).
//
// Month and day may be either 1 character or two characters (leading zeroes are not
// necessary)
// Determine if the delimiter is "-" or "/". The delimiter must appear
// twice or a FALSE will be returned.
if (mb_substr_count($data, '-') === 2) {
// Assume format is Y-M-D
$iFirstDelimiter = strpos($data, '-');
$iSecondDelimiter = strpos($data, '-', $iFirstDelimiter + 1);
// Parse the year.
$sYear = mb_substr($data, 0, $iFirstDelimiter);
// Parse the month
$sMonth = mb_substr($data, $iFirstDelimiter + 1, $iSecondDelimiter - $iFirstDelimiter - 1);
// Parse the day
$sDay = mb_substr($data, $iSecondDelimiter + 1);
// Put into YYYY-MM-DD form
return assembleYearMonthDay($sYear, $sMonth, $sDay, $pasfut);
} elseif ((mb_substr_count($data, '/') == 2) && ($locale == 'US')) {
// Assume format is M/D/Y
$iFirstDelimiter = strpos($data, '/');
$iSecondDelimiter = strpos($data, '/', $iFirstDelimiter + 1);
// Parse the month
$sMonth = mb_substr($data, 0, $iFirstDelimiter);
// Parse the day
$sDay = mb_substr($data, $iFirstDelimiter + 1, $iSecondDelimiter - $iFirstDelimiter - 1);
// Parse the year
$sYear = mb_substr($data, $iSecondDelimiter + 1);
// Put into YYYY-MM-DD form
return assembleYearMonthDay($sYear, $sMonth, $sDay, $pasfut);
} elseif (mb_substr_count($data, '/') == 2) {