Merge pull request #18852 from eileenmcnaughton/aip
[civicrm-core.git] / ext / financialacls / financialacls.php
index e6c39a0ab06890039330ed41417adcc8bf759428..c039ef1998f005ea1017a53063f79f69c24af753 100644 (file)
@@ -143,6 +143,133 @@ function financialacls_civicrm_themes(&$themes) {
   _financialacls_civix_civicrm_themes($themes);
 }
 
+/**
+ * Intervene to prevent deletion, where permissions block it.
+ *
+ * @param string $op
+ * @param string $objectName
+ * @param int|null $id
+ * @param array $params
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ */
+function financialacls_civicrm_pre($op, $objectName, $id, &$params) {
+  if ($objectName === 'LineItem' && !empty($params['check_permissions'])) {
+    if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
+      $operationMap = ['delete' => CRM_Core_Action::DELETE, 'edit' => CRM_Core_Action::UPDATE, 'create' => CRM_Core_Action::ADD];
+      CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types, $operationMap[$op]);
+      if (empty($params['financial_type_id'])) {
+        $params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_LineItem', $params['id'], 'financial_type_id');
+      }
+      if (!in_array($params['financial_type_id'], array_keys($types))) {
+        throw new API_Exception('You do not have permission to ' . $op . ' this line item');
+      }
+    }
+  }
+}
+
+/**
+ * Implements hook_civicrm_selectWhereClause().
+ *
+ * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_selectWhereClause
+ */
+function financialacls_civicrm_selectWhereClause($entity, &$clauses) {
+  if ($entity === 'LineItem') {
+    if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
+      $types = [];
+      CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types);
+      if ($types) {
+        $clauses['financial_type_id'] = 'IN (' . implode(',', array_keys($types)) . ')';
+      }
+      else {
+        $clauses['financial_type_id'] = '= 0';
+      }
+    }
+  }
+
+}
+
+/**
+ * Remove un.
+ *
+ * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildAmount
+ *
+ * @param string $component
+ * @param \CRM_Core_Form $form
+ * @param array $feeBlock
+ */
+function financialacls_civicrm_buildAmount($component, $form, &$feeBlock) {
+  if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
+    foreach ($feeBlock as $key => $value) {
+      foreach ($value['options'] as $k => $options) {
+        if (!CRM_Core_Permission::check('add contributions of type ' . CRM_Contribute_PseudoConstant::financialType($options['financial_type_id']))) {
+          unset($feeBlock[$key]['options'][$k]);
+        }
+      }
+      if (empty($feeBlock[$key]['options'])) {
+        unset($feeBlock[$key]);
+      }
+    }
+  }
+}
+
+/**
+ * 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. ---
 
 /**