Skip to content

Commit

Permalink
Cleanup notices on contribution.userDashboard, switch to apiv4
Browse files Browse the repository at this point in the history
Escape option-possible values

Adjust test for change in order
  • Loading branch information
eileenmcnaughton committed Feb 7, 2023
1 parent f9322f5 commit b5900bc
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 210 deletions.
2 changes: 2 additions & 0 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1458,8 +1458,10 @@ public static function getCurrentandGoalAmount($pageID) {
* @todo - this is a confusing function called from one place. It has a test. It would be
* nice to deprecate it.
*
* @deprecated
*/
public static function getHonorContacts($honorId) {
CRM_Core_Error::deprecatedFunctionWarning('apiv4');
$params = [];
$honorDAO = new CRM_Contribute_DAO_ContributionSoft();
$honorDAO->contact_id = $honorId;
Expand Down
2 changes: 2 additions & 0 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ public static function getPaymentProcessorID($recurID) {
* @param array $ids
* (reference ) an array of recurring contribution ids.
*
* @deprecated use the api.
*
* @return array
* an array of recurring ids count
*/
Expand Down
155 changes: 91 additions & 64 deletions CRM/Contribute/Page/UserDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Contribution;
use Civi\Api4\ContributionRecur;

/**
*
* @package CRM
Expand All @@ -18,112 +21,84 @@ class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBo

/**
* called when action is browse.
*
* @throws \CRM_Core_Exception
*/
public function listContribution() {
$rows = civicrm_api3('Contribution', 'get', [
'options' => [
'limit' => 12,
'sort' => 'receive_date DESC',
],
'sequential' => 1,
'contact_id' => $this->_contactId,
'return' => [
'total_amount',
'contribution_recur_id',
'financial_type',
'receive_date',
'receipt_date',
'contribution_status',
'currency',
'amount_level',
'contact_id,',
'contribution_source',
],
])['values'];

// We want oldest first, just among the most recent contributions
$rows = array_reverse($rows);
public function listContribution(): void {
$contributions = $this->getContributions();

foreach ($rows as $index => &$row) {
foreach ($contributions as &$row) {
// This is required for tpl logic. We should move away from hard-code this to adding an array of actions to the row
// which the tpl can iterate through - this should allow us to cope with competing attempts to add new buttons
// and allow extensions to assign new ones through the pageRun hook
$row['balance_amount'] = CRM_Contribute_BAO_Contribution::getContributionBalance($row['contribution_id']);
$contributionStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $row['contribution_status_id']);

if (in_array($contributionStatus, ['Pending', 'Partially paid'])) {
// We could check for balance_amount > 0 here? It feels more correct but this seems to be working.
if (in_array($row['contribution_status_id:name'], ['Pending', 'Partially paid'], TRUE)) {
$row['buttons']['pay'] = [
'class' => 'button',
'label' => ts('Pay Now'),
'url' => CRM_Utils_System::url('civicrm/contribute/transact', [
'reset' => 1,
'id' => Civi::settings()->get('default_invoice_page'),
'ccid' => $row['contribution_id'],
'ccid' => $row['id'],
'cs' => $this->getUserChecksum(),
'cid' => $row['contact_id'],
]),
];
}
}
unset($row);

$this->assign('contribute_rows', $rows);
$this->assign('contribute_rows', $contributions);
$this->assign('contributionSummary', ['total_amount' => civicrm_api3('Contribution', 'getcount', ['contact_id' => $this->_contactId])]);

//add honor block
$params = CRM_Contribute_BAO_Contribution::getHonorContacts($this->_contactId);

if (!empty($params)) {
// assign vars to templates
$this->assign('honorRows', $params);
$this->assign('honor', TRUE);
}
$softCreditContributions = $this->getContributions(TRUE);
$this->assign('soft_credit_contributions', $softCreditContributions);

$recur = new CRM_Contribute_DAO_ContributionRecur();
$recur->contact_id = $this->_contactId;
$recur->is_test = 0;
$recur->find();

$recurStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
$recurringContributions = (array) ContributionRecur::get(FALSE)
->addWhere('contact_id', '=', $this->_contactId)
->addWhere('is_test', '=', 0)
->setSelect([
'*',
'contribution_status_id:label',
])->execute();

$recurRow = [];
$recurIDs = [];
while ($recur->fetch()) {
if (empty($recur->payment_processor_id)) {
foreach ($recurringContributions as $recur) {
if (empty($recur['payment_processor_id'])) {
// it's not clear why we continue here as any without a processor id would likely
// be imported from another system & still seem valid.
continue;
}

require_once 'api/v3/utils.php';
//@todo calling api functions directly is not supported
_civicrm_api3_object_to_array($recur, $values);

$values['recur_status'] = $recurStatus[$values['contribution_status_id']];
$recurRow[$values['id']] = $values;
// Cast to something Smarty-friendly.
$recur['recur_status'] = $recur['contribution_status_id:label'];
$recurRow[$recur['id']] = $recur;

$action = array_sum(array_keys(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $recur->contact_id)));
$action = array_sum(array_keys(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur['id'], (int) $recur['contact_id'])));

$details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recur->id, 'recur');
$details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recur['id'], 'recur');
$hideUpdate = $details->membership_id & $details->auto_renew;

if ($hideUpdate) {
$action -= CRM_Core_Action::UPDATE;
}

$recurRow[$values['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $this->_contactId),
$recurRow[$recur['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur['id'], (int) $this->_contactId),
$action, [
'cid' => $this->_contactId,
'crid' => $values['id'],
'crid' => $recur['id'],
'cxt' => 'contribution',
],
ts('more'),
FALSE,
'contribution.dashboard.recurring',
'Contribution',
$values['id']
$recur['id']
);

$recurIDs[] = $values['id'];
$recurIDs[] = $recur['id'];
}
if (is_array($recurIDs) && !empty($recurIDs)) {
$getCount = CRM_Contribute_BAO_ContributionRecur::getCount($recurIDs);
Expand All @@ -136,12 +111,7 @@ public function listContribution() {
}

$this->assign('recurRows', $recurRow);
if (!empty($recurRow)) {
$this->assign('recur', TRUE);
}
else {
$this->assign('recur', FALSE);
}

}

/**
Expand All @@ -163,11 +133,68 @@ public function isIncludeInvoiceLinks() {
/**
* the main function that is called when the page
* loads, it decides the which action has to be taken for the page.
*
* @throws \CRM_Core_Exception
*/
public function run() {
$this->assign('isIncludeInvoiceLinks', $this->isIncludeInvoiceLinks());
parent::preProcess();
$this->listContribution();
}

/**
* Get the contact's contributions.
*
* @param bool $isSoftCredit
* Return contributions for which the contact is the soft credit contact instead.
*
* @return array
* @throws \CRM_Core_Exception
*/
protected function getContributions(bool $isSoftCredit = FALSE): array {
$apiQuery = Contribution::get(FALSE)
->addOrderBy('receive_date', 'DESC')
->setLimit(12)
->setSelect([
'total_amount',
'contribution_recur_id',
'receive_date',
'receipt_date',
'cancel_date',
'amount_level',
'contact_id',
'contact_id.display_name',
'contribution_status_id:name',
'contribution_status_id:label',
'financial_type_id:label',
'currency',
'amount_level',
'contact_id,',
'source',
'balance_amount',
'id',
]);

if ($isSoftCredit) {
$apiQuery->addJoin('ContributionSoft AS contribution_soft', 'INNER');
$apiQuery->addWhere('contribution_soft.contact_id', '=', $this->_contactId);
$apiQuery->addSelect('contribution_soft.soft_credit_type_id:label');
}
else {
$apiQuery->addWhere('contact_id', '=', $this->_contactId);
}
$contributions = (array) $apiQuery->execute();
foreach ($contributions as $index => $contribution) {
// QuickForm can't cope with the colons & dots ... cast to a legacy or simplified key.
$contributions[$index]['financial_type'] = $contribution['financial_type_id:label'];
$contributions[$index]['contribution_status'] = $contribution['contribution_status_id:label'];
$contributions[$index]['contribution_status_name'] = $contribution['contribution_status_id:name'];
$contributions[$index]['display_name'] = $contribution['contact_id.display_name'];
$contributions[$index]['soft_credit_type'] = $contribution['contribution_soft.soft_credit_type_id:label'] ?? NULL;
// Add in the api-v3 style naming just in case any extensions are still looking for it.
$contributions[$index]['contribution_id'] = $contribution['id'];
}
return $contributions;
}

}
93 changes: 44 additions & 49 deletions templates/CRM/Contribute/Page/UserDashboard.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
</tr>

{foreach from=$contribute_rows item=row}
<tr id='rowid{$row.contribution_id}'
<tr id='rowid{$row.id}'
class="{cycle values="odd-row,even-row"}{if !empty($row.cancel_date)} disabled{/if}">
<td>{$row.total_amount|crmMoney:$row.currency} {if !empty($row.amount_level) && !is_array($row.amount_level)} - {$row.amount_level} {/if}
<td>{$row.total_amount|crmMoney:$row.currency} {if !empty($row.amount_level) && !is_array($row.amount_level)} - {$row.amount_level|escape|smarty:nodefaults} {/if}
{if !empty($row.contribution_recur_id)}
<br/>
{ts}(Recurring Contribution){/ts}
{/if}
</td>
<td>{$row.financial_type}</td>
<td>{$row.financial_type|escape|smarty:nodefaults}</td>
<td>{$row.receive_date|truncate:10:''|crmDate}</td>
<td>{$row.receipt_date|truncate:10:''|crmDate}</td>
<td>{$row.balance_amount|crmMoney:$row.currency}</td>
<td>{$row.contribution_status}</td>
<td>{$row.contribution_status|escape|smarty:nodefaults}</td>
{if $isIncludeInvoiceLinks}
<td>
{* @todo Instead of this tpl handling assign actions as an array attached the row, iterate through - will better accomodate extension overrides and competition for scarce real estate on this page*}
{assign var='id' value=$row.contribution_id}
{assign var='id' value=$row.id}
{assign var='contact_id' value=$row.contact_id}
{assign var='urlParams' value="reset=1&id=$id&cid=$contact_id"}
{if call_user_func(array('CRM_Core_Permission','check'), 'view my invoices') OR call_user_func(array('CRM_Core_Permission','check'), 'access CiviContribute')}
Expand Down Expand Up @@ -80,42 +80,38 @@
</div>
{/if}


{if !empty($honor)}
{if $honorRows}
{strip}
<div class="help">
{ts}Contributions made in your honor{/ts}:
</div>
<table class="selector">
<tr class="columnheader">
<th>{ts}Contributor{/ts}</th>
<th>{ts}Amount{/ts}</th>
<th>{ts}Type{/ts}</th>
<th>{ts}Financial Type{/ts}</th>
<th>{ts}Received date{/ts}</th>
<th>{ts}Receipt Sent{/ts}</th>
<th>{ts}Status{/ts}</th>
{if !empty($soft_credit_contributions)}
{strip}
<div class="help">
{ts}Contributions made in your honor{/ts}:
</div>
<table class="selector">
<tr class="columnheader">
<th>{ts}Contributor{/ts}</th>
<th>{ts}Amount{/ts}</th>
<th>{ts}Type{/ts}</th>
<th>{ts}Financial Type{/ts}</th>
<th>{ts}Received date{/ts}</th>
<th>{ts}Receipt Sent{/ts}</th>
<th>{ts}Status{/ts}</th>
</tr>
{foreach from=$soft_credit_contributions item=row}
<tr id='rowid{$row.contact_id}' class="{cycle values="odd-row,even-row"}">
<td><a href="{crmURL p="civicrm/contact/view" q="reset=1&cid=`$row.contact_id`"}"
id="view_contact">{$row.display_name|escape|smarty:nodefaults}</a></td>
<td>{$row.total_amount|crmMoney:$row.currency}</td>
<td>{$row.soft_credit_type|escape|smarty:nodefaults}</td>
<td>{$row.financial_type|escape|smarty:nodefaults}</td>
<td>{$row.receive_date|truncate:10:''|crmDate}</td>
<td>{$row.receipt_date|truncate:10:''|crmDate}</td>
<td>{$row.contribution_status|escape|smarty:nodefaults}</td>
</tr>
{foreach from=$honorRows item=row}
<tr id='rowid{$row.honorId}' class="{cycle values="odd-row,even-row"}">
<td><a href="{crmURL p="civicrm/contact/view" q="reset=1&cid=`$row.honorId`"}"
id="view_contact">{$row.display_name}</a></td>
<td>{$row.amount}</td>
<td>{$row.honor_type}</td>
<td>{$row.type}</td>
<td>{$row.receive_date|truncate:10:''|crmDate}</td>
<td>{$row.receipt_date|truncate:10:''|crmDate}</td>
<td>{$row.contribution_status}</td>
</tr>
{/foreach}
</table>
{/strip}
{/if}
{/foreach}
</table>
{/strip}
{/if}

{if !empty($recur)}
{if $recurRows}
{if !empty($recurRows)}
{strip}
<div><label>{ts}Recurring Contribution(s){/ts}</label></div>
<table class="selector">
Expand All @@ -126,24 +122,23 @@
<th>{ts}Created{/ts}</th>
<th></th>
</tr>
{foreach from=$recurRows item=row key=id}
{foreach from=$recurRows item=row}
<tr class="{cycle values="odd-row,even-row"}">
<td><label>{$recurRows.$id.amount|crmMoney}</label>
every {$recurRows.$id.frequency_interval} {$recurRows.$id.frequency_unit}
for {$recurRows.$id.installments} installments
<td><label>{$row.amount|crmMoney}</label>
every {$row.frequency_interval} {$row.frequency_unit} for {$row.installments} installments
</td>
<td>{$recurRows.$id.recur_status}</td>
<td>{if $recurRows.$id.completed}<a href="{$recurRows.$id.link}">{$recurRows.$id.completed}
/{$recurRows.$id.installments}</a>
{else}0/{$recurRows.$id.installments} {/if}</td>
<td>{$recurRows.$id.create_date|crmDate}</td>
<td>{$recurRows.$id.action|replace:'xx':$recurRows.id}</td>
<td>{$row.recur_status|escape|smarty:nodefaults}</td>
<td>{if $row.completed}<a href="{$row.link}">{$row.completed}
/{$row.installments}</a>
{else}0/{$row.installments} {/if}</td>
<td>{$row.create_date|crmDate}</td>
<td>{$row.action|replace:'xx':$row.id}</td>
</tr>
{/foreach}
</table>
{/strip}
{/if}
{/if}

</div>
{crmRegion name="crm-contribute-userdashboard-post"}
{/crmRegion}
Loading

0 comments on commit b5900bc

Please sign in to comment.