diff --git a/dsr_civicrm.module b/dsr_civicrm.module index f8e51fec5d53b1e91c235136aaba76e664786bbb..8bdfc299c8fc5742b19ed0b361bce7c6f900cdd3 100644 --- a/dsr_civicrm.module +++ b/dsr_civicrm.module @@ -307,6 +307,23 @@ function dsr_civicrm_filter_null_rec(&$array) { return $array; } +/** + * Convert civicrm date str to YYYY-mm-dd. + */ +function dsr_civicrm_convert_date($value) { + $date_str = preg_replace('/\D/', '', $value); + if (strlen($date_str) != 8) { + return NULL; + } + $year = substr($date_str, 0, 4); + $month = substr($date_str, 4, 2); + $day = substr($date_str, 6, 2); + if (checkdate($month, $day, $year)) { + return "{$year}-{$month}-{$day}"; + } + return NULL; +} + function dsr_civicrm_civicrm_map_civi_data_to_shareholder_data($objectName, &$objectRef, $shareholder) { if ($objectName === 'Email') { $shareholder_info = [ @@ -317,11 +334,8 @@ function dsr_civicrm_civicrm_map_civi_data_to_shareholder_data($objectName, &$ob $deceased_date = NULL; if ($objectRef->is_deceased) { $deceased_date = "1970-01-01"; - if ($objectRef->deceased_date && $objectRef->deceased_date !== 'null') { - $year = substr($objectRef->deceased_date, 0, 4); - $month = substr($objectRef->deceased_date, 4, 2); - $day = substr($objectRef->deceased_date, 6, 2); - $deceased_date = "{$year}-{$month}-{$day}"; + if ($civi_deceased_date = dsr_civicrm_convert_date($objectRef->deceased_date)) { + $deceased_date = $civi_deceased_date; } }