Convert membership date fields to datepicker & form to entity form
authoreileen <emcnaughton@wikimedia.org>
Mon, 20 Aug 2018 00:41:30 +0000 (12:41 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 20 Aug 2018 05:30:15 +0000 (17:30 +1200)
CRM/Core/Form/EntityFormTrait.php
CRM/Member/Form.php
CRM/Member/Form/Membership.php
CRM/Member/Form/MembershipRenewal.php
templates/CRM/Member/Form/Membership.tpl

index f0031490f3ed674c330a1fe953cae2f594b6775e..13f70574316fec26e0c370c1bdb17baf8c1a8e3c 100644 (file)
@@ -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);
+  }
+
 }
index 2d2a757264135909b5722b58ae41c452a82d441d..e30b24c1be47d17991ed53320dccefe5000143d3 100644 (file)
@@ -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)) {
index 9c89a5303816ed620c4881ef78b460288819a2b8..8c3e4f8ad5c956b1f804ef63b91ce9354a61c82d 100644 (file)
@@ -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 = '<span class="font-red bold">'
+      . ts("WARNING: Deleting this membership will also delete any related payment (contribution) records." .  ts("This action cannot be undone.")
+        . '</span><p>'
+        . 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.")
+          . '</p><p>'
+        . ts("Click 'Delete' if you want to continue.") . '</p>');
   }
 
+  /**
+   * 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;
     }
index d5eb08968a686c0ae65cb3bfb70c8abcbdc1b320..d5f0cd1007e57aebf48221706121550fa2d69703 100644 (file)
@@ -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.
    *
index cdda8dd679693da987994c812ea695fdf7c8b1af..68c5b56d2745ab27ce9631961d26dc81bc576974 100644 (file)
@@ -79,9 +79,7 @@
     {if $action eq 8}
     <div class="messages status no-popup">
       <div class="icon inform-icon"></div>&nbsp;
-      <span class="font-red bold">{ts}WARNING: Deleting this membership will also delete any related payment (contribution) records.{/ts} {ts}This action cannot be undone.{/ts}</span>
-      <p>{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}</p>
-      <p>{ts}Click 'Delete' if you want to continue.{/ts}</p>
+      {$deleteMessage}
     </div>
     {else}
       <table class="form-layout-compressed">
         {include file="CRM/Campaign/Form/addCampaignToComponent.tpl"
         campaignTrClass="crm-membership-form-block-campaign_id"}
 
-        <tr class="crm-membership-form-block-join_date"><td class="label">{$form.join_date.label}</td><td>{include file="CRM/common/jcalendar.tpl" elementName=join_date}
+        <tr class="crm-membership-form-block-join_date"><td class="label">{$form.join_date.label}</td><td>{$form.join_date.html}
           <br />
           <span class="description">{ts}When did this contact first become a member?{/ts}</span></td></tr>
-        <tr class="crm-membership-form-block-start_date"><td class="label">{$form.start_date.label}</td><td>{include file="CRM/common/jcalendar.tpl" elementName=start_date}
+        <tr class="crm-membership-form-block-start_date"><td class="label">{$form.start_date.label}</td><td>{$form.start_date.html}
           <br />
           <span class="description">{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}</span></td></tr>
         <tr class="crm-membership-form-block-end_date"><td class="label">{$form.end_date.label}</td>
               {help id="override_end_date"}
           </td>
           <td id="end-date-editable">
-            {include file="CRM/common/jcalendar.tpl" elementName=end_date}
+            {$form.end_date.html}
             <br />
             <span class="description">{ts}Latest membership period expiration date. End Date will be automatically set based on Membership Type if you don't select a date.{/ts}</span>
           </td>