From ee2e0c299e5878cafdf58bdb8c77838582961d55 Mon Sep 17 00:00:00 2001 From: varshith89 Date: Fri, 5 Apr 2019 09:16:05 +0000 Subject: [PATCH] BASW-394: Handle Custom Date Fields When Set Via Webform Conditionals --- includes/wf_crm_webform_postprocess.inc | 10 +++++++--- webform_civicrm.module | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/includes/wf_crm_webform_postprocess.inc b/includes/wf_crm_webform_postprocess.inc index 932648b66..0e078632d 100644 --- a/includes/wf_crm_webform_postprocess.inc +++ b/includes/wf_crm_webform_postprocess.inc @@ -109,7 +109,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { * @see webform_civicrm_webform_submission_presave * @param stdClass $submission */ - public function preSave(&$submission) { + public function preSave(&$submission, $formatDate = FALSE) { $this->submission = &$submission; $this->data = $this->settings['data']; // Check for existing submission @@ -122,7 +122,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { return; } - $this->fillDataFromSubmission(); + $this->fillDataFromSubmission($formatDate); // Create/update contacts foreach ($this->data['contact'] as $c => $contact) { @@ -2312,7 +2312,7 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { /** * Fill data array with submitted form values */ - private function fillDataFromSubmission() { + private function fillDataFromSubmission($formatDate = FALSE) { foreach ($this->enabled as $field_key => $fid) { $val = $this->submissionValue($fid); // If value is null then it was hidden by a webform conditional rule - skip it @@ -2376,6 +2376,10 @@ class wf_crm_webform_postprocess extends wf_crm_webform_base { $time = substr($time, -6); } $val .= $time; + if ($formatDate) { + $val = new DateTime($val); + $val = $val->format('YmdHis'); + } } // The admin can change a number field to use checkbox/radio/select/grid widget and we'll sum the result elseif ($field['type'] === 'number') { diff --git a/webform_civicrm.module b/webform_civicrm.module index b4de2bd7d..2ad379e6f 100644 --- a/webform_civicrm.module +++ b/webform_civicrm.module @@ -252,8 +252,26 @@ function webform_civicrm_webform_component_info() { function webform_civicrm_webform_submission_presave($node, &$submission) { if (!empty($node->webform_civicrm)) { module_load_include('inc', 'webform_civicrm', 'includes/wf_crm_webform_postprocess'); + + $formatDate = FALSE; + foreach ($node->webform['components'] as $component) { + if (strpos($component['form_key'], 'civicrm_') === 0 && strpos($component['form_key'], '_custom_')) { + // custom field + $form_key_items = explode('_', $component['form_key']); + $custom_field_id = array_pop($form_key_items); + civicrm_initialize(); + $result = civicrm_api3('CustomField', 'getSingle', [ + 'sequential' => 1, + 'id' => $custom_field_id, + ]); + if ($result['data_type'] == 'Date') { + $formatDate = TRUE; + } + } + } + $processor = wf_crm_webform_postprocess::singleton($node); - $processor->preSave($submission); + $processor->preSave($submission, $formatDate); } }