Skip to content

Commit

Permalink
Remove inheritance of MembershipConfig form from MembershipStatus form.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eileenmcnaughton committed May 23, 2018
1 parent ab943c0 commit 8391751
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 29 deletions.
5 changes: 4 additions & 1 deletion CRM/Member/DAO/MembershipStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Member/MembershipStatus.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:dfe977e53a6b66703ab4eca3560048e6)
* (GenCodeChecksum:bf2b321dfd9ef10c9315a6bf2f3da06e)
*/

/**
Expand Down Expand Up @@ -186,6 +186,9 @@ public static function &fields() {
'entity' => 'MembershipStatus',
'bao' => 'CRM_Member_BAO_MembershipStatus',
'localizable' => 1,
'html' => [
'type' => 'Text',
],
],
'start_event' => [
'name' => 'start_event',
Expand Down
5 changes: 0 additions & 5 deletions CRM/Member/Form/MembershipConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 58 additions & 17 deletions CRM/Member/Form/MembershipStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -51,15 +52,65 @@ public function getDefaultContext() {
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...).")
],
];
}

/**
* 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'])) {
Expand All @@ -70,28 +121,22 @@ public function setDefaultValues() {

/**
* 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')
);
$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')
);
Expand All @@ -118,9 +163,6 @@ public function buildQuickForm() {

/**
* Process the form submission.
*
*
* @return void
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
Expand All @@ -133,7 +175,6 @@ public function postProcess() {
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);
Expand All @@ -142,7 +183,7 @@ public function postProcess() {
$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) {
Expand All @@ -155,7 +196,7 @@ public function postProcess() {
$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');
Expand Down
12 changes: 6 additions & 6 deletions templates/CRM/Member/Form/MembershipStatus.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
<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">
Expand Down
4 changes: 4 additions & 0 deletions xml/schema/Member/MembershipStatus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
</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>
Expand Down

0 comments on commit 8391751

Please sign in to comment.