);
// 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
/**
* 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
'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' => [
'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' => [
'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'),
'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' => [
'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'),
'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',
'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' => [
'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'),
'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'),
}
}
+/**
+ * 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. ---
/**
--- /dev/null
+<?php
+
+namespace Civi\Financialacls;
+
+// 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));
+ }
+
+}