From d48efe005f8649d881731175dc202c5febe9f67e Mon Sep 17 00:00:00 2001 From: cykonetic Date: Mon, 16 Apr 2018 15:02:57 -0400 Subject: [PATCH 1/2] AgeFromBirthdate actually calculating age, from birthdate --- Integration/AgeFromBirthdateIntegration.php | 26 ++++++++------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Integration/AgeFromBirthdateIntegration.php b/Integration/AgeFromBirthdateIntegration.php index 27373c8..a855917 100644 --- a/Integration/AgeFromBirthdateIntegration.php +++ b/Integration/AgeFromBirthdateIntegration.php @@ -53,10 +53,6 @@ protected function getEnhancerFieldArray() 'label' => 'Age', 'type' => 'number', ], - 'afb_dob' => [ - 'label' => 'Date of Birth', - 'type' => 'date', - ], ]; } @@ -93,18 +89,16 @@ public function appendToForm(&$builder, $data, $formArea) */ public function doEnhancement(Lead &$lead) { - if (!empty($lead)) { - // Field name can be dynamic, with the field name picked up through the config - // see the random plugin. - $dob = $lead->getFieldValue('afb_dob'); - if (isset($dob)) { - $today = new DateTime(); - $age = $today->diff($dob)->format('%y'); - if ($lead->getFieldValue('afb_age') !== $age) { - $lead->addUpdatedField('afb_age', $age, $lead->getFieldValue('afb_age')); - $this->saveLead($lead); - } - } + $year = intval($lead->getFieldValue('dob_year')); + $month = intval($lead->getFieldValue('dob_month')); + $day = intval($lead->getFieldValue('dob_day')); + + if ($year && $month && $day) { + $birthdate = sprintf('%04d-%02d-%02d 00:00:00', $year, $month, $day); + $dob = new DateTime($birthdate); + $today = new DateTime(); + $lead->addUpdatedField('afb_age', $today->diff($dob)->y, $lead->getFieldValue('afb_age')); + $this->saveLead($lead); } } } From 25411860024dd904047aaf3bda51a2780d592464 Mon Sep 17 00:00:00 2001 From: cykonetic Date: Mon, 16 Apr 2018 15:29:14 -0400 Subject: [PATCH 2/2] php-cs-fixer'd --- Integration/AgeFromBirthdateIntegration.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Integration/AgeFromBirthdateIntegration.php b/Integration/AgeFromBirthdateIntegration.php index a855917..0138275 100644 --- a/Integration/AgeFromBirthdateIntegration.php +++ b/Integration/AgeFromBirthdateIntegration.php @@ -89,14 +89,14 @@ public function appendToForm(&$builder, $data, $formArea) */ public function doEnhancement(Lead &$lead) { - $year = intval($lead->getFieldValue('dob_year')); - $month = intval($lead->getFieldValue('dob_month')); - $day = intval($lead->getFieldValue('dob_day')); + $year = intval($lead->getFieldValue('dob_year')); + $month = intval($lead->getFieldValue('dob_month')); + $day = intval($lead->getFieldValue('dob_day')); if ($year && $month && $day) { $birthdate = sprintf('%04d-%02d-%02d 00:00:00', $year, $month, $day); - $dob = new DateTime($birthdate); - $today = new DateTime(); + $dob = new DateTime($birthdate); + $today = new DateTime(); $lead->addUpdatedField('afb_age', $today->diff($dob)->y, $lead->getFieldValue('afb_age')); $this->saveLead($lead); }