Skip to content

Commit

Permalink
Move filtering of unpermitted options for reports/ search select to f…
Browse files Browse the repository at this point in the history
…inancial acl extension
  • Loading branch information
eileenmcnaughton committed Oct 23, 2020
1 parent e9eed3d commit e89fb39
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 11 deletions.
3 changes: 1 addition & 2 deletions CRM/Contribute/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,8 @@ public static function buildSearchForm(&$form) {
);

// CRM-13848
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::VIEW);
$form->addSelect('financial_type_id',
['entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search', 'options' => $financialTypes]
['entity' => 'contribution', 'multiple' => 'multiple', 'context' => 'search', 'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search')]
);

// use contribution_payment_instrument_id instead of payment_instrument_id
Expand Down
6 changes: 6 additions & 0 deletions CRM/Financial/BAO/FinancialType.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ public static function getAllEnabledAvailableFinancialTypes($action = CRM_Core_A
/**
* Get available Financial Types.
*
* This logic is being moved into the financialacls extension.
*
* Rather than call this function consider using
*
* $types = \CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search');
*
* @param array $financialTypes
* (reference ) an array of financial types
* @param int|string $action
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Bookkeeping.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function __construct() {
'title' => ts('Financial Type'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
],
],
'order_bys' => [
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function __construct() {
'financial_type_id' => [
'title' => ts('Financial Type'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
'type' => CRM_Utils_Type::T_INT,
],
'contribution_page_id' => [
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Lybunt.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function __construct() {
'title' => ts('Financial Type'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
],
'contribution_status_id' => [
'title' => ts('Contribution Status'),
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Recur.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function __construct() {
'financial_type_id' => [
'title' => ts('Financial Type'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
'type' => CRM_Utils_Type::T_INT,
],
'frequency_unit' => [
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function __construct() {
'title' => ts('Financial Type'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
),
'contribution_status_id' => array(
'title' => ts('Contribution Status'),
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/SoftCredit.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function __construct() {
'title' => ts('Financial Type'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
],
],
'grouping' => 'softcredit-fields',
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function __construct() {
'financial_type_id' => [
'title' => ts('Financial Type'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
'type' => CRM_Utils_Type::T_INT,
],
'contribution_page_id' => [
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Sybunt.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function __construct() {
'title' => ts('Financial Type'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
],
'contribution_status_id' => [
'title' => ts('Contribution Status'),
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/TopDonor.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function __construct() {
'title' => ts('Financial Type'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes(),
'options' => CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search'),
],
'contribution_status_id' => [
'title' => ts('Contribution Status'),
Expand Down
38 changes: 38 additions & 0 deletions ext/financialacls/financialacls.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,44 @@ function financialacls_civicrm_membershipTypeValues($form, &$membershipTypeValue
}
}

/**
* Remove unpermitted financial types from field Options in search context.
*
* Search context is described as
* 'search' => "search: searchable options are returned; labels are translated.",
* So this is appropriate to removing the options from search screens.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_fieldOptions
*
* @param string $entity
* @param string $field
* @param array $options
* @param array $params
*/
function financialacls_civicrm_fieldOptions($entity, $field, &$options, $params) {
if ($entity === 'Contribution' && $field === 'financial_type_id' && $params['context'] === 'search') {
$action = CRM_Core_Action::VIEW;
// At this stage we are only considering the view action. Code from
// CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes().
$actions = [
CRM_Core_Action::VIEW => 'view',
CRM_Core_Action::UPDATE => 'edit',
CRM_Core_Action::ADD => 'add',
CRM_Core_Action::DELETE => 'delete',
];
$cacheKey = 'available_types_' . $action;
if (!isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey])) {
foreach ($options as $finTypeId => $type) {
if (!CRM_Core_Permission::check($actions[$action] . ' contributions of type ' . $type)) {
unset($options[$finTypeId]);
}
}
\Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey] = $options;
}
$options = \Civi::$statics['CRM_Financial_BAO_FinancialType'][$cacheKey];
}
}

// --- Functions below this ship commented out. Uncomment as required. ---

/**
Expand Down
26 changes: 26 additions & 0 deletions ext/financialacls/tests/phpunit/Civi/Financialacls/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Civi\Financialacls;

use Civi\Api4\MembershipType;

// I fought the Autoloader and the autoloader won.
require_once 'BaseTestClass.php';

/**
* @group headless
*/
class OptionsTest extends BaseTestClass {

/**
* Test buildMembershipTypes.
*/
public function testBuildOptions() {
$this->setupLoggedInUserWithLimitedFinancialTypeAccess();
$options = \CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes();
$this->assertEquals(['Donation'], array_merge($options));
$builtOptions = \CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'search');
$this->assertEquals(['Donation'], array_merge($builtOptions));
}

}

0 comments on commit e89fb39

Please sign in to comment.