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 f1105ed
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
78 changes: 66 additions & 12 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 @@ -91,33 +109,49 @@ public function preProcess() {
(!$this->_crid && !$this->_coid && !$this->_mid) ||
(!$this->_subscriptionDetails)
) {
CRM_Core_Error::fatal('Required information missing.');
CRM_Core_Error::statusBounce('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();

CRM_Utils_System::setTitle($this->_mid ? ts('Cancel Auto-renewal') : ts('Cancel Recurring Contribution'));
$this->assign('mode', $this->_mode);

if ($this->isSelfService()) {
unset($this->entityFields['send_cancel_request'], $this->entityFields['is_notify']);
}

if ($this->_subscriptionDetails->contact_id) {
list($this->_donorDisplayName, $this->_donorEmail)
= CRM_Contact_BAO_Contact::getContactDetails($this->_subscriptionDetails->contact_id);
}
}

/**
* Set entity fields for this cancellation.
*/
public function setEntityFields() {
$this->entityFields = [
'cancel_reason' => ['name' => 'cancel_reason'],
];
$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 +179,7 @@ public function buildQuickForm() {
}

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

Expand Down Expand Up @@ -184,7 +218,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 All @@ -209,6 +243,7 @@ public function postProcess() {
'id' => $this->_subscriptionDetails->recur_id,
'membership_id' => $this->_mid,
'processor_message' => $message,
'cancel_reason' => $params['cancel_reason'],
]);

$tplParams = [];
Expand Down Expand Up @@ -308,4 +343,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>
1 change: 1 addition & 0 deletions templates/CRM/Contribute/Page/ContributionRecur.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<tr><td class="label">{ts}Created Date{/ts}</td><td>{$recur.create_date|crmDate}</td></tr>
{if $recur.modified_date}<tr><td class="label">{ts}Modified Date{/ts}</td><td>{$recur.modified_date|crmDate}</td></tr>{/if}
{if $recur.cancel_date}<tr><td class="label">{ts}Cancelled Date{/ts}</td><td>{$recur.cancel_date|crmDate}</td></tr>{/if}
{if $recur.cancel_reason}<tr><td class="label">{ts}Cancel Reason{/ts}</td><td>{$recur.cancel_reason}</td></tr>{/if}
{if $recur.end_date}<tr><td class="label">{ts}End Date{/ts}</td><td>{$recur.end_date|crmDate}</td></tr>{/if}
{if $recur.processor_id}<tr><td class="label">{ts}Processor ID{/ts}</td><td>{$recur.processor_id}</td></tr>{/if}
<tr><td class="label">{ts}Transaction ID{/ts}</td><td>{$recur.trxn_id}</td></tr>
Expand Down

0 comments on commit f1105ed

Please sign in to comment.