Commit 700cf121 authored by mattwire's avatar mattwire

Initial commit

parents
This diff is collapsed.
This diff is collapsed.
<?php
/**
* https://civicrm.org/licensing
*/
/**
* Class CRM_Mjwshared_Utils
*/
class CRM_Mjwshared_Utils {
/**********************
* MJW_Utils: 20190822
*********************/
/**
* Return the field ID for $fieldName custom field
*
* @param $fieldName
* @param $fieldGroup
* @param bool $fullString
*
* @return mixed
* @throws \CiviCRM_API3_Exception
*/
public static function getCustomByName($fieldName, $fieldGroup, $fullString = TRUE) {
if (!isset(Civi::$statics[__CLASS__][$fieldGroup][$fieldName])) {
$field = civicrm_api3('CustomField', 'get', array(
'custom_group_id' => $fieldGroup,
'name' => $fieldName,
));
if (!empty($field['id'])) {
Civi::$statics[__CLASS__][$fieldGroup][$fieldName]['id'] = $field['id'];
Civi::$statics[__CLASS__][$fieldGroup][$fieldName]['string'] = 'custom_' . $field['id'];
}
}
if ($fullString) {
return Civi::$statics[__CLASS__][$fieldGroup][$fieldName]['string'];
}
return Civi::$statics[__CLASS__][$fieldGroup][$fieldName]['id'];
}
}
This diff is collapsed.
# mjwshared
This extension does nothing by itself but is required by a number of other extensions developed by MJW Consulting.
The extension is licensed under [AGPL-3.0](LICENSE.txt).
## Requirements
* PHP v7.x+
* CiviCRM 5.16+
## Installation
See: https://docs.civicrm.org/sysadmin/en/latest/customize/extensions/#installing-a-new-extension
<?php
/**
* https://civicrm.org/licensing
*/
/**********************
* MJW_Payment_Api: 20190901
*********************/
/**
* @todo mjwpayment.get_contribution is a replacement for Contribution.get
* mjwpayment.get_payment is a replacement for Payment.get
* which support querying by contribution/payment trxn_id per https://github.com/civicrm/civicrm-core/pull/14748
* - These API functions should be REMOVED once core has the above PR merged and we increment the min version for the extension.
* - The change is small, but to re-implement them here we have to copy quite a lot over.
*/
/**
* Adjust Metadata for Get action.
*
* The metadata is used for setting defaults, documentation & validation.
*
* @param array $params
* Array of parameters determined by getfields.
*/
function _civicrm_api3_mjwpayment_get_contribution_spec(&$params) {
$params['contribution_test'] = [
'api.default' => 0,
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => 'Get Test Contributions?',
'api.aliases' => ['is_test'],
];
$params['financial_type_id']['api.aliases'] = ['contribution_type_id'];
$params['payment_instrument_id']['api.aliases'] = ['contribution_payment_instrument', 'payment_instrument'];
$params['contact_id'] = CRM_Utils_Array::value('contribution_contact_id', $params);
$params['contact_id']['api.aliases'] = ['contribution_contact_id'];
unset($params['contribution_contact_id']);
}
/**
* Retrieve a set of contributions.
*
* @param array $params
* Input parameters.
*
* @return array
* Array of contributions, if error an array with an error id and error message
*/
function civicrm_api3_mjwpayment_get_contribution($params) {
$contributionResult = civicrm_api3('Contribution', 'get', $params);
$foundContributions = CRM_Utils_Array::value('values', $contributionResult, []);
$contributions = [];
// If we have a trxn_id check payments for that transaction ID and also return any contributions associated with those payments
// An additional array property "payment_trxn_id" will be available containing all found trxn_ids (eg. if you did ['LIKE' => 'test124%'])
if (!empty($params['trxn_id'])) {
$payments = civicrm_api3('Payment', 'get', $params);
if (!empty($payments['count'])) {
foreach ($payments['values'] as $paymentID => $paymentValues) {
if (empty($contributions[$paymentValues['contribution_id']])) {
// Get the details of each additional contribution found via a payment
$contributions[$paymentValues['contribution_id']] = CRM_Contribute_BAO_Contribution::getValuesWithMappings(['id' => $paymentValues['contribution_id']]);
}
$contributions[$paymentValues['contribution_id']]['payment_trxn_id'][] = $paymentValues['trxn_id'];
}
}
}
foreach ($contributions as $id => $contribution) {
$softContribution = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($id, TRUE);
$contributions[$id] = array_merge($contributions[$id], $softContribution);
// format soft credit for backward compatibility
_civicrm_api3_mjwpayment_format_soft_credit($contributions[$id]);
_civicrm_api3_mjwpayment_contribution_add_supported_fields($contributions[$id]);
}
foreach($foundContributions as $id => $detail) {
if (isset($contributions[$id])) {
$foundContributions[$id] = $contributions[$id];
}
}
return civicrm_api3_create_success($contributions, $params, 'Contribution', 'get');
}
/**
* This function is used to format the soft credit for backward compatibility.
*
* As of v4.4 we support multiple soft credit, so now contribution returns array with 'soft_credit' as key
* but we still return first soft credit as a part of contribution array
*
* @param $contribution
*/
function _civicrm_api3_mjwpayment_format_soft_credit(&$contribution) {
if (!empty($contribution['soft_credit'])) {
$contribution['soft_credit_to'] = $contribution['soft_credit'][1]['contact_id'];
$contribution['soft_credit_id'] = $contribution['soft_credit'][1]['soft_credit_id'];
}
}
/**
* Support for supported output variables.
*
* @param $contribution
*/
function _civicrm_api3_mjwpayment_contribution_add_supported_fields(&$contribution) {
// These are output fields that are supported in our test contract.
// Arguably we should also do the same with 'campaign_id' &
// 'source' - which are also fields being rendered with unique names.
// That seems more consistent with other api where we output the actual field names.
$outputAliases = [
'contribution_check_number' => 'check_number',
'contribution_address_id' => 'address_id',
'payment_instrument_id' => 'instrument_id',
'contribution_cancel_date' => 'cancel_date',
];
foreach ($outputAliases as $returnName => $copyTo) {
if (array_key_exists($returnName, $contribution)) {
$contribution[$copyTo] = $contribution[$returnName];
}
}
}
/**
* Adjust Metadata for Get action.
*
* The metadata is used for setting defaults, documentation & validation.
*
* @param array $params
* Array of parameters determined by getfields.
*/
function _civicrm_api3_mjwpayment_get_payment_spec(&$params) {
$params = [
'contribution_id' => [
'title' => 'Contribution ID',
'type' => CRM_Utils_Type::T_INT,
],
'entity_table' => [
'title' => 'Entity Table',
'api.default' => 'civicrm_contribution',
],
'entity_id' => [
'title' => 'Entity ID',
'type' => CRM_Utils_Type::T_INT,
'api.aliases' => ['contribution_id'],
],
'trxn_id' => [
'title' => 'Transaction ID',
'type' => CRM_Utils_Type::T_STRING,
],
];
}
/**
* Retrieve a set of financial transactions which are payments.
*
* @param array $params
* Input parameters.
*
* @return array
* Array of financial transactions which are payments, if error an array with an error id and error message
* @throws \CiviCRM_API3_Exception
*/
function civicrm_api3_mjwpayment_get_payment($params) {
$financialTrxn = [];
$limit = '';
if (isset($params['options']) && CRM_Utils_Array::value('limit', $params['options'])) {
$limit = CRM_Utils_Array::value('limit', $params['options']);
}
$params['options']['limit'] = 0;
$ftParams['is_payment'] = 1;
if ($limit) {
$ftParams['options']['limit'] = $limit;
}
if (!empty($params['trxn_id'])) {
$ftParams['trxn_id'] = $params['trxn_id'];
$financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
if (!empty($financialTrxn['count'])) {
$financialTrxnIDs = CRM_Utils_Array::collect('id', CRM_Utils_Array::value('values', $financialTrxn));
$params['financial_trxn_id'] = ['IN' => $financialTrxnIDs];
$eft = civicrm_api3('EntityFinancialTrxn', 'get', $params);
foreach ($eft['values'] as $eftID => $eftValues) {
$financialTrxn['values'][$eftValues['financial_trxn_id']]['contribution_id'] = $eftValues['entity_id'];
}
}
}
else {
$eft = civicrm_api3('EntityFinancialTrxn', 'get', $params);
if (!empty($eft['values'])) {
$eftIds = [];
foreach ($eft['values'] as $efts) {
if (empty($efts['financial_trxn_id'])) {
continue;
}
$eftIds[] = $efts['financial_trxn_id'];
$map[$efts['financial_trxn_id']] = $efts['entity_id'];
}
if (!empty($eftIds)) {
$ftParams['id'] = ['IN' => $eftIds];
$financialTrxn = civicrm_api3('FinancialTrxn', 'get', $ftParams);
foreach ($financialTrxn['values'] as &$values) {
$values['contribution_id'] = $map[$values['id']];
}
}
}
}
return civicrm_api3_create_success(CRM_Utils_Array::value('values', $financialTrxn, []), $params, 'Payment', 'get');
}
<?xml version="1.0"?>
<extension key="mjwshared" type="module">
<file>mjwshared</file>
<name>MJWShared</name>
<description>Extension containing shared code used by MJW extensions (normally copied into extension rather than linked from this one)</description>
<license>AGPL-3.0</license>
<maintainer>
<author>Matthew Wire (MJW Consulting)</author>
<email>mjw@mjwconsult.co.uk</email>
</maintainer>
<urls>
<url desc="Main Extension Page">https://github.com/mattwire/civicrm-mjwshared</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2019-09-03</releaseDate>
<version>0.1</version>
<develStage>beta</develStage>
<compatibility>
<ver>5.13</ver>
</compatibility>
<civix>
<namespace>CRM/Mjwshared</namespace>
</civix>
</extension>
This diff is collapsed.
<?php
require_once 'mjwshared.civix.php';
use CRM_Mjwshared_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function mjwshared_civicrm_config(&$config) {
_mjwshared_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function mjwshared_civicrm_xmlMenu(&$files) {
_mjwshared_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function mjwshared_civicrm_install() {
_mjwshared_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function mjwshared_civicrm_postInstall() {
_mjwshared_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function mjwshared_civicrm_uninstall() {
_mjwshared_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function mjwshared_civicrm_enable() {
_mjwshared_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function mjwshared_civicrm_disable() {
_mjwshared_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function mjwshared_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _mjwshared_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function mjwshared_civicrm_managed(&$entities) {
_mjwshared_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types.
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function mjwshared_civicrm_caseTypes(&$caseTypes) {
_mjwshared_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
*/
function mjwshared_civicrm_angularModules(&$angularModules) {
_mjwshared_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function mjwshared_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_mjwshared_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implements hook_civicrm_entityTypes().
*
* Declare entity types provided by this module.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
*/
function mjwshared_civicrm_entityTypes(&$entityTypes) {
_mjwshared_civix_civicrm_entityTypes($entityTypes);
}
/**
* Implements hook_civicrm_navigationMenu().
*\/
function mjwshared_civicrm_navigationMenu(&$menu) {
$item = [
'label' => E::ts('Haystack theme settings'),
'name' => 'Haystack theme settings',
'url' => 'civicrm/admin/haystack/settings',
'permission' => 'administer CiviCRM',
'operator' => NULL,
'separator' => NULL,
];
_mjwshared_civix_insert_navigation_menu($menu, 'Administer/Customize Data and Screens', $item);
_mjwshared_civix_navigationMenu($menu);
}
*/
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment