From 59c798c98d2539714f46c30a2c3d81690f6d6051 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 20 Aug 2018 12:41:30 +1200 Subject: [PATCH] Convert membership date fields to datepicker & form to entity form --- CRM/Core/Form/EntityFormTrait.php | 15 ++++- CRM/Member/Form.php | 19 ++++++ CRM/Member/Form/Membership.php | 74 ++++++++++++++++-------- CRM/Member/Form/MembershipRenewal.php | 12 ++++ templates/CRM/Member/Form/Membership.tpl | 10 ++-- 5 files changed, 99 insertions(+), 31 deletions(-) diff --git a/CRM/Core/Form/EntityFormTrait.php b/CRM/Core/Form/EntityFormTrait.php index f0031490f3..13f7057431 100644 --- a/CRM/Core/Form/EntityFormTrait.php +++ b/CRM/Core/Form/EntityFormTrait.php @@ -79,7 +79,7 @@ trait CRM_Core_Form_EntityFormTrait { * Build the form object. */ public function buildQuickEntityForm() { - if ($this->_action & CRM_Core_Action::DELETE) { + if ($this->isDeleteContext()) { $this->buildDeleteForm(); return; } @@ -202,7 +202,7 @@ trait CRM_Core_Form_EntityFormTrait { protected function addEntityFieldsToTemplate() { foreach ($this->getEntityFields() as $fieldSpec) { if (empty($fieldSpec['not-auto-addable'])) { - $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array::value('required', $fieldSpec)); + $element = $this->addField($fieldSpec['name'], [], CRM_Utils_Array::value('required', $fieldSpec), FALSE); if (!empty($fieldSpec['is_freeze'])) { $element->freeze(); } @@ -210,4 +210,15 @@ trait CRM_Core_Form_EntityFormTrait { } } + /** + * Is the form being used in the context of a deletion. + * + * (For some reason rather than having separate forms Civi overloads one form). + * + * @return bool + */ + protected function isDeleteContext() { + return ($this->_action & CRM_Core_Action::DELETE); + } + } diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 2d2a757264..e30b24c1be 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -37,6 +37,7 @@ */ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { + use CRM_Core_Form_EntityFormTrait; /** * The id of the object being edited / created * @@ -98,6 +99,24 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { */ protected $_params = array(); + /** + * Fields for the entity to be assigned to the template. + * + * Fields may have keys + * - name (required to show in tpl from the array) + * - description (optional, will appear below the field) + * - not-auto-addable - this class will not attempt to add the field using addField. + * (this will be automatically set if the field does not have html in it's metadata + * or is not a core field on the form's entity). + * - help (option) add help to the field - e.g ['id' => 'id-source', 'file' => 'CRM/Contact/Form/Contact']] + * - template - use a field specific template to render this field + * - required + * - is_freeze (field should be frozen). + * + * @var array + */ + protected $entityFields = []; + public function preProcess() { // Check for edit permission. if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 9c89a53038..8c3e4f8ad5 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -112,14 +112,54 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { ); /** - * Get the entity id being edited. + * Set entity fields to be assigned to the form. + */ + protected function setEntityFields() { + $this->entityFields = [ + 'join_date' => [ + 'name' => 'join_date', + 'description' => ts('Member Since'), + ], + 'start_date' => [ + 'name' => 'start_date', + 'description' => ts('Start Date'), + ], + 'end_date' => [ + 'name' => 'end_date', + 'description' => ts('End Date'), + ], + ]; + } + + /** + * Set the delete message. * - * @return int|null + * We do this from the constructor in order to do a translation. */ - public function getEntityId() { - return $this->_id; + public function setDeleteMessage() { + $this->deleteMessage = '' + . ts("WARNING: Deleting this membership will also delete any related payment (contribution) records." . ts("This action cannot be undone.") + . '

' + . ts("Consider modifying the membership status instead if you want to maintain an audit trail and avoid losing payment data. You can set the status to Cancelled by editing the membership and clicking the Status Override checkbox.") + . '

' + . ts("Click 'Delete' if you want to continue.") . '

'); } + /** + * Overriding this entity trait function as not yet tested. + * + * We continue to rely on legacy handling. + */ + public function addCustomDataToForm() {} + + + /** + * Overriding this entity trait function as not yet tested. + * + * We continue to rely on legacy handling. + */ + public function addFormButtons() {} + /** * Get selected membership type from the form values. * @@ -182,6 +222,9 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { // This string makes up part of the class names, differentiating them (not sure why) from the membership fields. $this->assign('formClass', 'membership'); parent::preProcess(); + if ($this->isUpdateToExistingRecurringMembership()) { + $this->entityFields['end_date']['is_freeze'] = TRUE; + } // get price set id. $this->_priceSetId = CRM_Utils_Array::value('priceSetId', $_GET); $this->set('priceSetId', $this->_priceSetId); @@ -372,16 +415,9 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { // $defaults['credit_card_exp_date'] = array( 'Y' => '2012', 'M' => '05' ); } - $dates = array('join_date', 'start_date', 'end_date'); - foreach ($dates as $key) { - if (!empty($defaults[$key])) { - list($defaults[$key]) = CRM_Utils_Date::setDateDefaults(CRM_Utils_Array::value($key, $defaults)); - } - } - //setting default join date if there is no join date if (empty($defaults['join_date'])) { - $defaults['join_date'] = $now; + $defaults['join_date'] = date('Y-m-d H:i:s'); } if (!empty($defaults['membership_end_date'])) { @@ -396,6 +432,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { */ public function buildQuickForm() { + $this->buildQuickEntityForm(); $this->assign('currency', CRM_Core_Config::singleton()->defaultCurrencySymbol); $isUpdateToExistingRecurringMembership = $this->isUpdateToExistingRecurringMembership(); // build price set form. @@ -562,19 +599,10 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $sel->freeze(); } - $this->applyFilter('__ALL__', 'trim'); - if ($this->_action & CRM_Core_Action::ADD) { $this->add('text', 'num_terms', ts('Number of Terms'), array('size' => 6)); } - $this->addDate('join_date', ts('Member Since'), FALSE, array('formatType' => 'activityDate')); - $this->addDate('start_date', ts('Start Date'), FALSE, array('formatType' => 'activityDate')); - $endDate = $this->addDate('end_date', ts('End Date'), FALSE, array('formatType' => 'activityDate')); - if ($endDate && $isUpdateToExistingRecurringMembership) { - $endDate->freeze(); - } - $this->add('text', 'source', ts('Source'), CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'source') ); @@ -1934,9 +1962,9 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { protected function isUpdateToExistingRecurringMembership() { $isRecur = FALSE; if ($this->_action & CRM_Core_Action::UPDATE - && CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_id, + && CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->getEntityId(), 'contribution_recur_id') - && !CRM_Member_BAO_Membership::isSubscriptionCancelled($this->_id)) { + && !CRM_Member_BAO_Membership::isSubscriptionCancelled($this->getEntityId())) { $isRecur = TRUE; } diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index d5eb08968a..d5f0cd1007 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -119,6 +119,18 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { 'receive_date' => array('default' => 'now'), ); + /** + * Set entity fields to be assigned to the form. + */ + protected function setEntityFields() {} + + /** + * Set the delete message. + * + * We do this from the constructor in order to do a translation. + */ + public function setDeleteMessage() {} + /** * Pre-process form. * diff --git a/templates/CRM/Member/Form/Membership.tpl b/templates/CRM/Member/Form/Membership.tpl index cdda8dd679..68c5b56d27 100644 --- a/templates/CRM/Member/Form/Membership.tpl +++ b/templates/CRM/Member/Form/Membership.tpl @@ -79,9 +79,7 @@ {if $action eq 8}
  - {ts}WARNING: Deleting this membership will also delete any related payment (contribution) records.{/ts} {ts}This action cannot be undone.{/ts} -

{ts}Consider modifying the membership status instead if you want to maintain an audit trail and avoid losing payment data. You can set the status to Cancelled by editing the membership and clicking the Status Override checkbox.{/ts}

-

{ts}Click 'Delete' if you want to continue.{/ts}

+ {$deleteMessage}
{else} @@ -133,10 +131,10 @@ {include file="CRM/Campaign/Form/addCampaignToComponent.tpl" campaignTrClass="crm-membership-form-block-campaign_id"} - - @@ -148,7 +146,7 @@ {help id="override_end_date"} -- 2.25.1
{$form.join_date.label}{include file="CRM/common/jcalendar.tpl" elementName=join_date} +
{$form.join_date.label}{$form.join_date.html}
{ts}When did this contact first become a member?{/ts}
{$form.start_date.label}{include file="CRM/common/jcalendar.tpl" elementName=start_date} +
{$form.start_date.label}{$form.start_date.html}
{ts}First day of current continuous membership period. Start Date will be automatically set based on Membership Type if you don't select a date.{/ts}
{$form.end_date.label} - {include file="CRM/common/jcalendar.tpl" elementName=end_date} + {$form.end_date.html}
{ts}Latest membership period expiration date. End Date will be automatically set based on Membership Type if you don't select a date.{/ts}