Remove inheritance of MembershipConfig form from MembershipStatus form.
authoreileen <emcnaughton@wikimedia.org>
Wed, 23 May 2018 03:23:40 +0000 (15:23 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 27 May 2018 21:47:31 +0000 (09:47 +1200)
This is part of trying to move towards an entity form (ie. support custom data) for MembershipType.

Currently the 2 forms share a parent class - but really the add buttons is the only shared functionality.

This change uses the EntityTrait to add the buttons as well. There is some minor tidy up in there
but I tried to keep it very limited.

I switched only one field over to being added via addField, the parent mechanism so far

CRM/Core/Form/EntityFormTrait.php
CRM/Member/DAO/MembershipStatus.php
CRM/Member/Form/MembershipConfig.php
CRM/Member/Form/MembershipStatus.php
templates/CRM/Member/Form/MembershipStatus.tpl
xml/schema/Member/MembershipStatus.xml

index c102c94e7f67d2de8e3b87857471ec739bf9ba64..76b1a2cd69c91dca32923c7e235dc0157ac20c23 100644 (file)
@@ -161,7 +161,7 @@ trait CRM_Core_Form_EntityFormTrait {
   protected function addEntityFieldsToTemplate() {
     foreach ($this->getEntityFields() as $fieldSpec) {
       if (empty($fieldSpec['not-auto-addable'])) {
-        $this->addField($fieldSpec['name']);
+        $this->addField($fieldSpec['name'], [], CRM_Utils_Array::value('required', $fieldSpec));
       }
     }
   }
index bf4302c3eb36732e7cbcb98f2871876643683829..e38b1559339c5d47050f169c7a6355e7785ffe28 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Member/MembershipStatus.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:dfe977e53a6b66703ab4eca3560048e6)
+ * (GenCodeChecksum:ea7435287a239f4851dd354150980069)
  */
 
 /**
@@ -186,6 +186,9 @@ class CRM_Member_DAO_MembershipStatus extends CRM_Core_DAO {
           'entity' => 'MembershipStatus',
           'bao' => 'CRM_Member_BAO_MembershipStatus',
           'localizable' => 1,
+          'html' => [
+            'type' => 'Text',
+          ],
         ],
         'start_event' => [
           'name' => 'start_event',
@@ -292,12 +295,15 @@ class CRM_Member_DAO_MembershipStatus extends CRM_Core_DAO {
         'is_admin' => [
           'name' => 'is_admin',
           'type' => CRM_Utils_Type::T_BOOLEAN,
-          'title' => ts('Admin Assigned Only?'),
+          'title' => ts('Administrator Only?'),
           'description' => 'Is this status for admin/manual assignment only.',
           'table_name' => 'civicrm_membership_status',
           'entity' => 'MembershipStatus',
           'bao' => 'CRM_Member_BAO_MembershipStatus',
           'localizable' => 0,
+          'html' => [
+            'type' => 'CheckBox',
+          ],
         ],
         'weight' => [
           'name' => 'weight',
@@ -317,6 +323,9 @@ class CRM_Member_DAO_MembershipStatus extends CRM_Core_DAO {
           'entity' => 'MembershipStatus',
           'bao' => 'CRM_Member_BAO_MembershipStatus',
           'localizable' => 0,
+          'html' => [
+            'type' => 'CheckBox',
+          ],
         ],
         'is_active' => [
           'name' => 'is_active',
@@ -328,6 +337,9 @@ class CRM_Member_DAO_MembershipStatus extends CRM_Core_DAO {
           'entity' => 'MembershipStatus',
           'bao' => 'CRM_Member_BAO_MembershipStatus',
           'localizable' => 0,
+          'html' => [
+            'type' => 'CheckBox',
+          ],
         ],
         'is_reserved' => [
           'name' => 'is_reserved',
@@ -338,6 +350,9 @@ class CRM_Member_DAO_MembershipStatus extends CRM_Core_DAO {
           'entity' => 'MembershipStatus',
           'bao' => 'CRM_Member_BAO_MembershipStatus',
           'localizable' => 0,
+          'html' => [
+            'type' => 'CheckBox',
+          ],
         ],
       ];
       CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
index a18034dc84433e6515eec357e1c7f4a0cd5a85a4..357a74bf0fc673eaa1ab5462bdefc76bbe7e1886 100644 (file)
@@ -53,11 +53,6 @@ class CRM_Member_Form_MembershipConfig extends CRM_Core_Form {
    */
   protected $_BAOName;
 
-  public function preProcess() {
-    $this->_id = $this->get('id');
-    $this->_BAOName = $this->get('BAOName');
-  }
-
   /**
    * Set default values for the form. MobileProvider that in edit/view mode
    * the default values are retrieved from the database
index 7600d881818a5a5e1c0f77af79589ab413670bec..5a47ed7e973467681f2d41261b2ad0d386fdadff 100644 (file)
@@ -34,8 +34,9 @@
 /**
  * This class generates form components for Membership Type
  */
-class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig {
+class CRM_Member_Form_MembershipStatus extends CRM_Core_Form {
 
+  use CRM_Core_Form_EntityFormTrait;
 
   /**
    * Explicitly declare the entity api name.
@@ -51,15 +52,70 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
     return 'create';
   }
 
+  /**
+   * 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 (optional) 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
+   * @var array
+   */
+  protected $entityFields = [];
+
+  /**
+   * Set entity fields to be assigned to the form.
+   */
+  protected function setEntityFields() {
+    $this->entityFields = [
+      'label' => [
+        'name' => 'label',
+        'description' => ts("Display name for this Membership status (e.g. New, Current, Grace, Expired...)."),
+        'required' => TRUE,
+      ],
+      'is_admin' => [
+        'name' => 'is_admin',
+        'description' => ts("Check this box if this status is for use by administrative staff only. If checked, this status is never automatically assigned by CiviMember. It is assigned to a contact's Membership by checking the <strong>Status Override</strong> flag when adding or editing the Membership record. Start and End Event settings are ignored for Administrator statuses. EXAMPLE: This setting can be useful for special case statuses like 'Non-expiring', 'Barred' or 'Expelled', etc."),
+      ],
+    ];
+  }
+
+  /**
+   * Set the delete message.
+   *
+   * We do this from the constructor in order to do a translation.
+   */
+  public function setDeleteMessage() {
+    $this->deleteMessage = ts('WARNING: Deleting this option will result in the loss of all membership records of this status.') . ' ' . ts('This may mean the loss of a substantial amount of data, and the action cannot be undone.') . ' ' . ts('Do you want to continue?');
+  }
+
+  public function preProcess() {
+    $this->_id = $this->get('id');
+    $this->_BAOName = 'CRM_Member_BAO_MembershipStatus';
+  }
+
   /**
    * Set default values for the form. MobileProvider that in edit/view mode
    * the default values are retrieved from the database
    *
-   *
-   * @return void
+   * @return array
    */
   public function setDefaultValues() {
-    $defaults = parent::setDefaultValues();
+    $defaults = array();
+
+    if ($this->getEntityId()) {
+      $params = array('id' => $this->getEntityId());
+      $baoName = $this->_BAOName;
+      $baoName::retrieve($params, $defaults);
+    }
+
+    if ($this->_action & CRM_Core_Action::ADD) {
+      $defaults['is_active'] = 1;
+    }
 
     //finding default weight to be put
     if (empty($defaults['weight'])) {
@@ -70,18 +126,15 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
 
   /**
    * Build the form object.
-   *
-   * @return void
    */
   public function buildQuickForm() {
+    self::buildQuickEntityForm();
     parent::buildQuickForm();
 
     if ($this->_action & CRM_Core_Action::DELETE) {
       return;
     }
 
-    $this->applyFilter('__ALL__', 'trim');
-
     if ($this->_id) {
       $name = $this->add('text', 'name', ts('Name'),
         CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'name')
@@ -89,9 +142,6 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
       $name->freeze();
       $this->assign('id', $this->_id);
     }
-    $this->add('text', 'label', ts('Label'),
-      CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'label'), TRUE
-    );
     $this->addRule('label', ts('A membership status with this label already exists. Please select another label.'),
       'objectExists', array('CRM_Member_DAO_MembershipStatus', $this->_id, 'name')
     );
@@ -107,7 +157,6 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
       CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'end_event_adjust_interval')
     );
     $this->add('checkbox', 'is_current_member', ts('Current Membership?'));
-    $this->add('checkbox', 'is_admin', ts('Administrator Only?'));
 
     $this->add('text', 'weight', ts('Order'),
       CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipStatus', 'weight')
@@ -118,9 +167,6 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
 
   /**
    * Process the form submission.
-   *
-   *
-   * @return void
    */
   public function postProcess() {
     if ($this->_action & CRM_Core_Action::DELETE) {
@@ -133,7 +179,6 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
       CRM_Core_Session::setStatus(ts('Selected membership status has been deleted.'), ts('Record Deleted'), 'success');
     }
     else {
-      $params = $ids = array();
       // store the submitted values in an array
       $params = $this->exportValues();
       $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
@@ -142,7 +187,7 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
       $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
 
       if ($this->_action & CRM_Core_Action::UPDATE) {
-        $ids['membershipStatus'] = $this->_id;
+        $params['id'] = $this->getEntityId();
       }
       $oldWeight = NULL;
       if ($this->_id) {
@@ -155,7 +200,7 @@ class CRM_Member_Form_MembershipStatus extends CRM_Member_Form_MembershipConfig
         $params['name'] = $params['label'];
       }
 
-      $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params, $ids);
+      $membershipStatus = CRM_Member_BAO_MembershipStatus::add($params);
       CRM_Core_Session::setStatus(ts('The membership status \'%1\' has been saved.',
         array(1 => $membershipStatus->label)
       ), ts('Saved'), 'success');
index d6bdbe176e74a2357fea228139c03c68c2d10064..37f8147d05bfa105f076077dee02acb5f337b9e1 100644 (file)
 <div class="crm-block crm-form-block crm-membership-status-form-block" id=membership_status>
 <fieldset><legend>{if $action eq 1}{ts}New Membership Status{/ts}{elseif $action eq 2}{ts}Edit Membership Status{/ts}{else}{ts}Delete Membership Status{/ts}{/if}</legend>
  <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
-   {if $action eq 8}
-      <div class="messages status no-popup">
-         <div class="icon inform-icon"></div>
-          {ts}WARNING: Deleting this option will result in the loss of all membership records of this status.{/ts} {ts}This may mean the loss of a substantial amount of data, and the action cannot be undone.{/ts} {ts}Do you want to continue?{/ts}
-      </div>
-   {else}
+  {if $action eq 8}
+    <div class="messages status no-popup">
+      <div class="icon inform-icon"></div>
+      {$deleteMessage|escape}
+    </div>
+  {else}
     <table class="form-layout-compressed">
       {if $action eq 2}
       <tr class="crm-membership-status-form-block-name">
index b25d44f45327e224e65451bad4eb66fce7d35246..5ef3ce5004cc32d1c25820015fdc32620380dd5d 100644 (file)
   </field>
   <field>
     <name>label</name>
+    <title>Label</title>
     <type>varchar</type>
     <length>128</length>
     <localizable>true</localizable>
+    <html>
+      <type>Text</type>
+    </html>
     <comment>Label for Membership Status</comment>
     <add>3.2</add>
   </field>
   </field>
   <field>
     <name>is_admin</name>
-    <title>Admin Assigned Only?</title>
+    <title>Administrator Only?</title>
     <type>boolean</type>
     <comment>Is this status for admin/manual assignment only.</comment>
+    <html>
+      <type>CheckBox</type>
+    </html>
     <add>1.5</add>
   </field>
   <field>
     <name>is_default</name>
     <title>Default Status?</title>
     <type>boolean</type>
+    <html>
+      <type>CheckBox</type>
+    </html>
     <comment>Assign this status to a membership record if no other status match is found.</comment>
     <add>1.5</add>
   </field>
     <title>Is Active</title>
     <type>boolean</type>
     <default>1</default>
+    <html>
+      <type>CheckBox</type>
+    </html>
     <comment>Is this membership_status enabled.</comment>
     <add>1.5</add>
   </field>
     <title>Is Reserved</title>
     <type>boolean</type>
     <default>0</default>
+    <html>
+      <type>CheckBox</type>
+    </html>
     <comment>Is this membership_status reserved.</comment>
     <add>2.1</add>
   </field>