Merge pull request #18852 from eileenmcnaughton/aip
[civicrm-core.git] / ext / financialacls / financialacls.php
index 7c8e7e4eb46c56a0b315ec0f91572456bddaa8d4..c039ef1998f005ea1017a53063f79f69c24af753 100644 (file)
@@ -214,6 +214,62 @@ function financialacls_civicrm_buildAmount($component, $form, &$feeBlock) {
   }
 }
 
+/**
+ * Remove unpermitted membership types from selection availability..
+ *
+ * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_membershipTypeValues
+ *
+ * @param \CRM_Core_Form $form
+ * @param array $membershipTypeValues
+ */
+function financialacls_civicrm_membershipTypeValues($form, &$membershipTypeValues) {
+  $financialTypes = NULL;
+  $financialTypes = CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD);
+  foreach ($membershipTypeValues as $id => $type) {
+    if (!isset($financialTypes[$type['financial_type_id']])) {
+      unset($membershipTypeValues[$id]);
+    }
+  }
+}
+
+/**
+ * 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. ---
 
 /**