Skip to content

Commit

Permalink
Use entity form to render fields, including the new cancel_reason field
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Apr 25, 2019
1 parent dfcc1e0 commit 7bbf629
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
73 changes: 62 additions & 11 deletions CRM/Contribute/Form/CancelSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,25 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib

protected $_mode = NULL;

protected $_selfService = FALSE;
/**
* Should custom data be suppressed on this form.
*
* We override to suppress custom data because historically it has not been
* shown on this form & we don't want to expose it as a by-product of
* other change without establishing that it would be good on this form.
*
* @return bool
*/
protected function isSuppressCustomData() {
return TRUE;
}

/**
* Is the from being accessed by a front end user to update their own recurring.
*
* @var bool
*/
protected $selfService;

/**
* Set variables up before form is built.
Expand Down Expand Up @@ -94,14 +112,6 @@ public function preProcess() {
CRM_Core_Error::fatal('Required information missing.');
}

if (!CRM_Core_Permission::check('edit contributions')) {
if ($this->_subscriptionDetails->contact_id != $this->getContactID()) {
CRM_Core_Error::statusBounce(ts('You do not have permission to cancel this recurring contribution.'));
}
$this->_selfService = TRUE;
}
$this->assign('self_service', $this->_selfService);

// handle context redirection
CRM_Contribute_BAO_ContributionRecur::setSubscriptionContext();

Expand All @@ -114,10 +124,32 @@ public function preProcess() {
}
}

/**
* Set entity fields for this cancellation.
*/
public function setEntityFields() {
$this->entityFields = [
'cancel_reason' => ['name' => 'cancel_reason'],
];
if (!$this->isSelfService()) {
$this->entityFields['send_cancel_request'] = [
'title' => ts('Send cancellation request to %1 ?', [1 => $this->_paymentProcessorObj->_processorName]),
'name' => 'send_cancel_request',
'not-auto-addable' => TRUE,
];
$this->entityFields['is_notify'] = [
'title' => ts('Notify Contributor?'),
'name' => 'is_notify',
'not-auto-addable' => TRUE,
];
}
}

/**
* Build the form object.
*/
public function buildQuickForm() {
$this->buildQuickEntityForm();
// Determine if we can cancel recurring contribution via API with this processor
$cancelSupported = $this->_paymentProcessorObj->supports('CancelRecurring');
if ($cancelSupported) {
Expand Down Expand Up @@ -145,7 +177,7 @@ public function buildQuickForm() {
}

$type = 'next';
if ($this->_selfService) {
if ($this->isSelfService()) {
$type = 'submit';
}

Expand Down Expand Up @@ -184,7 +216,7 @@ public function postProcess() {
$cancelSubscription = TRUE;
$params = $this->controller->exportValues($this->_name);

if ($this->_selfService) {
if ($this->isSelfService()) {
// for self service force sending-request & notify
if ($this->_paymentProcessorObj->supports('cancelRecurring')) {
$params['send_cancel_request'] = 1;
Expand Down Expand Up @@ -308,4 +340,23 @@ public function postProcess() {
}
}

/**
* Is this being used by a front end user to update their own recurring.
*
* @return bool
*/
protected function isSelfService() {
if (!is_null($this->selfService)) {
return $this->selfService;
}
$this->selfService = FALSE;
if (!CRM_Core_Permission::check('edit contributions')) {
if ($this->_subscriptionDetails->contact_id != $this->getContactID()) {
CRM_Core_Error::statusBounce(ts('You do not have permission to cancel this recurring contribution.'));
}
$this->selfService = TRUE;
}
return $this->selfService;
}

}
9 changes: 9 additions & 0 deletions CRM/Contribute/Form/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ public function getDefaultContext() {
return 'create';
}

/**
* Get the entity id being edited.
*
* @return int|null
*/
public function getEntityId() {
return $this->contributionRecurID;
}

/**
* Set variables up before form is built.
*/
Expand Down
12 changes: 12 additions & 0 deletions CRM/Core/Form/EntityFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ public function getEntityId() {
return $this->_id;
}

/**
* Should custom data be suppressed on this form.
*
* @return bool
*/
protected function isSuppressCustomData() {
return FALSE;
}

/**
* Get the entity subtype ID being edited
*
Expand All @@ -105,6 +114,9 @@ public function getEntitySubTypeId($subTypeId) {
* If the custom data is in the submitted data (eg. added via ajax loaded form) add to form.
*/
public function addCustomDataToForm() {
if ($this->isSuppressCustomData()) {
return TRUE;
}
$customisableEntities = CRM_Core_SelectValues::customGroupExtends();
if (isset($customisableEntities[$this->getDefaultEntity()])) {
CRM_Custom_Form_CustomData::addToForm($this);
Expand Down
14 changes: 1 addition & 13 deletions templates/CRM/Contribute/Form/CancelSubscription.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,5 @@
</div>
{/if}
</div>
{if !$self_service}
<table class="form-layout">
<tr>
<td class="label">{$form.send_cancel_request.label}</td>
<td class="html-adjust">{$form.send_cancel_request.html}</td>
</tr>
<tr>
<td class="label">{$form.is_notify.label}</td>
<td class="html-adjust">{$form.is_notify.html}</td>
</tr>
</table>
{/if}
<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
{include file="CRM/Core/Form/EntityForm.tpl"}
</div>

0 comments on commit 7bbf629

Please sign in to comment.