Skip to content

Commit

Permalink
Merge pull request #32 from TheDMSGroup/ENG-235
Browse files Browse the repository at this point in the history
[ENG-235] AgeFromBirthdate actually calculating age, from birth date
  • Loading branch information
heathdutton authored Apr 17, 2018
2 parents 65a6420 + 2541186 commit a8f781d
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions Integration/AgeFromBirthdateIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ protected function getEnhancerFieldArray()
'label' => 'Age',
'type' => 'number',
],
'afb_dob' => [
'label' => 'Date of Birth',
'type' => 'date',
],
];
}

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit a8f781d

Please sign in to comment.