From 3d927ee7efceef0c549bff376650610cd0db887b Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 13 Feb 2017 15:09:23 +1300 Subject: [PATCH] CRM-20012 alternate fix to setting null dates This is part of the work started in CRM-19490 to switch profile dates to use the date picker, as a solution to multiple problems --- CRM/Batch/Form/Entry.php | 19 +----- CRM/Contribute/Form/Task/Batch.php | 61 +++++++------------ CRM/Core/BAO/UFGroup.php | 62 +------------------- templates/CRM/Batch/Form/Entry.tpl | 8 +-- templates/CRM/Contribute/Form/Task/Batch.tpl | 6 +- templates/CRM/UF/Form/Fields.tpl | 2 - tests/phpunit/CRM/Batch/Form/EntryTest.php | 22 +++---- 7 files changed, 35 insertions(+), 145 deletions(-) diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index b5900b044412..d96b337de2a7 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -380,13 +380,12 @@ public function setDefaultValues() { // for add mode set smart defaults if ($this->_action & CRM_Core_Action::ADD) { - list($currentDate, $currentTime) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime'); + $currentDate = date('Y-m-d H-i-s'); $completeStatus = CRM_Contribute_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); $specialFields = array( 'join_date' => date('Y-m-d'), 'receive_date' => $currentDate, - 'receive_date_time' => $currentTime, 'contribution_status_id' => $completeStatus, ); @@ -447,12 +446,6 @@ public function postProcess() { * @return bool */ private function processContribution(&$params) { - $dates = array( - 'receive_date', - 'receipt_date', - 'thankyou_date', - 'cancel_date', - ); // get the price set associated with offline contribution record. $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name'); @@ -504,12 +497,6 @@ private function processContribution(&$params) { 'Contribution' ); - foreach ($dates as $val) { - if (!empty($value[$val])) { - $value[$val] = CRM_Utils_Date::processDate($value[$val], $value[$val . '_time'], TRUE); - } - } - if (!empty($value['send_receipt'])) { $value['receipt_date'] = date('Y-m-d His'); } @@ -715,10 +702,6 @@ private function processMembership(&$params) { } } - if (!empty($value['receive_date'])) { - $value['receive_date'] = CRM_Utils_Date::processDate($value['receive_date'], $value['receive_date_time'], TRUE); - } - $params['actualBatchTotal'] += $value['total_amount']; unset($value['financial_type']); diff --git a/CRM/Contribute/Form/Task/Batch.php b/CRM/Contribute/Form/Task/Batch.php index f1b47a4375eb..3964d6cbdcb1 100644 --- a/CRM/Contribute/Form/Task/Batch.php +++ b/CRM/Contribute/Form/Task/Batch.php @@ -138,6 +138,11 @@ public function buildQuickForm() { ); } + // It is possible to have fields that are required in CiviCRM not be required in the + // profile. Overriding that here. Perhaps a better approach would be to + // make them required in the schema & read that up through getFields functionality. + $requiredFields = array('receive_date'); + //fix for CRM-2752 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution'); foreach ($this->_contributionIds as $contributionId) { @@ -160,6 +165,9 @@ public function buildQuickForm() { } else { // handle non custom fields + if (in_array($field['name'], $requiredFields)) { + $field['is_required'] = TRUE; + } CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId); } } @@ -198,58 +206,29 @@ public function setDefaultValues() { */ public function postProcess() { $params = $this->exportValues(); - $dates = array( - 'receive_date', - 'receipt_date', - 'thankyou_date', - 'cancel_date', - ); if (isset($params['field'])) { - foreach ($params['field'] as $key => $value) { + foreach ($params['field'] as $contributionID => $value) { - $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, - $key, - 'Contribution' - ); - - $ids['contribution'] = $key; - foreach ($dates as $val) { - if (isset($value[$val])) { - $value[$val] = CRM_Utils_Date::processDate($value[$val]); - } - } + $value['id'] = $contributionID; if (!empty($value['financial_type'])) { $value['financial_type_id'] = $value['financial_type']; } - if (!empty($value['payment_instrument'])) { - $value['payment_instrument_id'] = $value['payment_instrument']; - } - - if (!empty($value['contribution_source'])) { - $value['source'] = $value['contribution_source']; - } - - unset($value['financial_type']); - unset($value['contribution_source']); - $contribution = CRM_Contribute_BAO_Contribution::add($value, $ids); + $value['options'] = array( + 'reload' => 1, + ); + $contribution = civicrm_api3('Contribution', 'create', $value); + $contribution = $contribution['values'][$contributionID]; + // @todo add check as to whether the status is updated. if (!empty($value['contribution_status_id'])) { - CRM_Contribute_BAO_Contribution::transitionComponentWithReturnMessage($contribution->id, + // @todo - use completeorder api or make api call do this. + CRM_Contribute_BAO_Contribution::transitionComponentWithReturnMessage($contribution['id'], $value['contribution_status_id'], - CRM_Utils_Array::value("field[{$key}][contribution_status_id]", - $this->_defaultValues - ), - $contribution->receive_date + CRM_Utils_Array::value("field[{$contributionID}][contribution_status_id]", $this->_defaultValues), + $contribution['receive_date'] ); } - - // add custom field values - if (!empty($value['custom']) && - is_array($value['custom']) - ) { - CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_contribution', $contribution->id); - } } CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success'); } diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 3af88e2ffd46..ee4d36a87fbf 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -519,9 +519,6 @@ protected static function formatUFField( ); $formattedField = CRM_Utils_Date::addDateMetadataToField($fieldMetaData, $formattedField); - if (in_array($name, array_keys(self::getNonUpgradedDateFields()))) { - $formattedField['is_legacy_date'] = 1; - } //adding custom field property if (substr($field->field_name, 0, 6) == 'custom' || @@ -1890,7 +1887,6 @@ public static function buildProfile( $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options', TRUE, NULL, TRUE ); - $legacyHandledDateFields = self::getNonUpgradedDateFields(); if (substr($fieldName, 0, 14) === 'state_province') { $form->addChainSelect($name, array('label' => $title, 'required' => $required)); @@ -1953,9 +1949,6 @@ public static function buildProfile( } } } - elseif (isset($legacyHandledDateFields[$fieldName])) { - $form->addDate($name, $title, $required, array('formatType' => $legacyHandledDateFields[$fieldName])); - } elseif (CRM_Utils_Array::value('name', $field) == 'membership_type') { list($orgInfo, $types) = CRM_Member_BAO_MembershipType::getMembershipTypeInfo(); $sel = &$form->addElement('hierselect', $name, $title); @@ -2317,23 +2310,6 @@ public static function buildProfile( } } - /** - * Get fields that have not been upgraded to use datepicker. - * - * Fields that use the old code have jcalendar in the tpl and - * the form uses a customised format. We are moving towards datepicker - * which among other things passes dates back and forth using a standardised - * format. Remove fields from here as they are tested and converted. - */ - static public function getNonUpgradedDateFields() { - return array( - 'receive_date' => 'activityDateTime', - 'receipt_date' => 'activityDateTime', - 'thankyou_date' => 'activityDateTime', - 'cancel_date' => 'activityDateTime', - ); - } - /** * Set profile defaults. * @@ -2439,34 +2415,6 @@ public static function setProfileDefaults( } break; - case 'Select Date': - if (!in_array($name, array_keys(self::getNonUpgradedDateFields()))) { - $defaults[$fldName] = $details[$name]; - } - else { - // Do legacy handling. - // CRM-6681, set defult values according to date and time format (if any). - $dateFormat = NULL; - if (!empty($customFields[$customFieldId]['date_format'])) { - $dateFormat = $customFields[$customFieldId]['date_format']; - } - - if (empty($customFields[$customFieldId]['time_format'])) { - list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], NULL, - $dateFormat - ); - } - else { - $timeElement = $fldName . '_time'; - if (substr($fldName, -1) == ']') { - $timeElement = substr($fldName, 0, -1) . '_time]'; - } - list($defaults[$fldName], $defaults[$timeElement]) = CRM_Utils_Date::setDateDefaults($details[$name], - NULL, $dateFormat, $customFields[$customFieldId]['time_format']); - } - } - break; - default: $defaults[$fldName] = $details[$name]; break; @@ -3280,18 +3228,10 @@ public static function setComponentDefaults(&$fields, $componentId, $component, } $formattedGroupTree = array(); - // @todo - as we put these on datepicker they need to be removed from here. - $dateTimeFields = array_keys(self::getNonUpgradedDateFields()); foreach ($fields as $name => $field) { $fldName = $isStandalone ? $name : "field[$componentId][$name]"; - if (in_array($name, $dateTimeFields)) { - $timefldName = $isStandalone ? "{$name}_time" : "field[$componentId][{$name}_time]"; - if (!empty($values[$name])) { - list($defaults[$fldName], $defaults[$timefldName]) = CRM_Utils_Date::setDateDefaults($values[$name]); - } - } - elseif (array_key_exists($name, $values)) { + if (array_key_exists($name, $values)) { $defaults[$fldName] = $values[$name]; } elseif ($name == 'participant_note') { diff --git a/templates/CRM/Batch/Form/Entry.tpl b/templates/CRM/Batch/Form/Entry.tpl index 6be276ec9e95..5a954c6ef12d 100644 --- a/templates/CRM/Batch/Form/Entry.tpl +++ b/templates/CRM/Batch/Form/Entry.tpl @@ -93,13 +93,7 @@ {/if} {foreach from=$fields item=field key=fieldName} {assign var=n value=$field.name} - {if in_array( $n, array( 'thankyou_date', 'cancel_date', 'receipt_date', 'receive_date') ) } -
- - {include file="CRM/common/jcalendar.tpl" elementName=$n elementIndex=$rowNumber batchUpdate=1} - -
- {elseif $n eq 'soft_credit'} + {if $n eq 'soft_credit'}
{$form.soft_credit_contact_id.$rowNumber.html|crmAddClass:big} {$form.soft_credit_amount.$rowNumber.label} {$form.soft_credit_amount.$rowNumber.html|crmAddClass:eight} diff --git a/templates/CRM/Contribute/Form/Task/Batch.tpl b/templates/CRM/Contribute/Form/Task/Batch.tpl index 1dbac47c61ac..b351c52c5fca 100644 --- a/templates/CRM/Contribute/Form/Task/Batch.tpl +++ b/templates/CRM/Contribute/Form/Task/Batch.tpl @@ -48,11 +48,7 @@ {foreach from=$fields item=field key=fieldName} {assign var=n value=$field.name} - {if ( $n eq 'thankyou_date' ) or ( $n eq 'cancel_date' ) or ( $n eq 'receipt_date' ) or ( $n eq 'receive_date' )} - {include file="CRM/common/jcalendar.tpl" elementName=$n elementIndex=$cid batchUpdate=1} - {else} - {$form.field.$cid.$n.html} - {/if} + {$form.field.$cid.$n.html} {/foreach} {/foreach} diff --git a/templates/CRM/UF/Form/Fields.tpl b/templates/CRM/UF/Form/Fields.tpl index e16d7305f2f5..7e1b872d07eb 100644 --- a/templates/CRM/UF/Form/Fields.tpl +++ b/templates/CRM/UF/Form/Fields.tpl @@ -97,8 +97,6 @@ value="{$form.$profileFieldName.value}" id="{$form.$profileFieldName.name}" > - {elseif $field.is_legacy_date} - {include file="CRM/common/jcalendar.tpl" elementName=$profileFieldName} {elseif $profileFieldName|substr:0:5 eq 'phone'} {assign var="phone_ext_field" value=$profileFieldName|replace:'phone':'phone_ext'} {if $prefix}{$form.$prefix.$profileFieldName.html}{else}{$form.$profileFieldName.html}{/if} diff --git a/tests/phpunit/CRM/Batch/Form/EntryTest.php b/tests/phpunit/CRM/Batch/Form/EntryTest.php index ef40bb28e613..97a08f692754 100644 --- a/tests/phpunit/CRM/Batch/Form/EntryTest.php +++ b/tests/phpunit/CRM/Batch/Form/EntryTest.php @@ -234,9 +234,9 @@ public function testMembershipRenewalDates() { // explicitly specify start and end dates $params['field'][2]['membership_type'] = array(0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2); - $params['field'][2]['membership_start_date'] = "04/01/2016"; - $params['field'][2]['membership_end_date'] = "03/31/2017"; - $params['field'][2]['receive_date'] = "04/01/2016"; + $params['field'][2]['membership_start_date'] = "2016-04-01"; + $params['field'][2]['membership_end_date'] = "2017-03-31"; + $params['field'][2]['receive_date'] = "2016-04-01"; $this->assertTrue($form->testProcessMembership($params)); $result = $this->callAPISuccess('membership', 'get', array()); @@ -268,13 +268,13 @@ public function getMembershipData() { 'field' => array( 1 => array( 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID), - 'join_date' => '07/22/2013', + 'join_date' => '2013-07-22', 'membership_start_date' => NULL, 'membership_end_date' => NULL, 'membership_source' => NULL, 'financial_type' => 2, 'total_amount' => 1, - 'receive_date' => '07/24/2013', + 'receive_date' => '2013-07-24', 'receive_date_time' => NULL, 'payment_instrument' => 1, 'trxn_id' => 'TX101', @@ -283,13 +283,13 @@ public function getMembershipData() { ), 2 => array( 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID), - 'join_date' => '07/03/2013', - 'membership_start_date' => '02/03/2013', + 'join_date' => '2013-07-03', + 'membership_start_date' => '2013-02-03', 'membership_end_date' => NULL, 'membership_source' => NULL, 'financial_type' => 2, 'total_amount' => 1, - 'receive_date' => '07/17/2013', + 'receive_date' => '2013-07-17', 'receive_date_time' => NULL, 'payment_instrument' => NULL, 'trxn_id' => 'TX102', @@ -305,7 +305,7 @@ public function getMembershipData() { 'membership_source' => NULL, 'financial_type' => 2, 'total_amount' => 1, - 'receive_date' => '07/17/2013', + 'receive_date' => '2013-07-17', 'receive_date_time' => NULL, 'payment_instrument' => NULL, 'trxn_id' => 'TX103', @@ -335,7 +335,7 @@ public function getContributionData() { 1 => array( 'financial_type' => 1, 'total_amount' => 15, - 'receive_date' => '07/24/2013', + 'receive_date' => '2013-07-24', 'receive_date_time' => NULL, 'payment_instrument' => 1, 'check_number' => NULL, @@ -344,7 +344,7 @@ public function getContributionData() { 2 => array( 'financial_type' => 1, 'total_amount' => 15, - 'receive_date' => '07/24/2013', + 'receive_date' => '2013-07-24', 'receive_date_time' => NULL, 'payment_instrument' => 1, 'check_number' => NULL,