com.megaphonetech.ftoverride/ftoverride.php

383 lines
11 KiB
PHP
Raw Normal View History

2018-10-23 22:16:53 +00:00
<?php
require_once 'ftoverride.civix.php';
use CRM_Ftoverride_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function ftoverride_civicrm_config(&$config) {
_ftoverride_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function ftoverride_civicrm_xmlMenu(&$files) {
_ftoverride_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function ftoverride_civicrm_install() {
_ftoverride_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function ftoverride_civicrm_postInstall() {
_ftoverride_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function ftoverride_civicrm_uninstall() {
_ftoverride_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function ftoverride_civicrm_enable() {
_ftoverride_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function ftoverride_civicrm_disable() {
_ftoverride_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function ftoverride_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _ftoverride_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 ftoverride_civicrm_managed(&$entities) {
_ftoverride_civix_civicrm_managed($entities);
$entities[] = [
'module' => 'com.megaphonetech.ftoverride',
'name' => 'contributionCustomfield',
'update' => 'never',
'entity' => 'OptionValue',
'params' => [
'label' => ts('ContributionPage'),
'name' => 'civicrm_contribution_page',
'value' => 'ContributionPage',
'option_group_id' => 'cg_extend_objects',
'options' => ['match' => ['option_group_id', 'name']],
'is_active' => 1,
'version' => 3,
],
];
$entities[] = [
'module' => 'com.megaphonetech.ftoverride',
'name' => 'ft_override_designation_og',
'entity' => 'OptionGroup',
'params' => [
'version' => 3,
'name' => 'ft_override_designation',
'label' => ts('Designation'),
],
];
$entities[] = [
'module' => 'com.megaphonetech.ftoverride',
'name' => 'ft_override',
'entity' => 'CustomGroup',
'update' => 'never',
'params' => [
'version' => 3,
'name' => 'ft_override',
'title' => ts('Details'),
'extends' => 'ContributionPage',
'style' => 'Inline',
'is_active' => TRUE,
],
];
$entities[] = [
'module' => 'com.megaphonetech.ftoverride',
'name' => 'ft_override_designation',
'entity' => 'CustomField',
'update' => 'never',
'params' => [
'version' => 3,
'name' => 'ft_override_designation',
'label' => ts('Designation'),
'data_type' => 'String',
'html_type' => 'Multi-Select',
'is_active' => TRUE,
'text_length' => 255,
'custom_group_id' => 'ft_override',
'option_group_id' => 'ft_override_designation'
],
];
}
/**
* 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 ftoverride_civicrm_caseTypes(&$caseTypes) {
_ftoverride_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 ftoverride_civicrm_angularModules(&$angularModules) {
_ftoverride_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function ftoverride_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_ftoverride_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 ftoverride_civicrm_entityTypes(&$entityTypes) {
_ftoverride_civix_civicrm_entityTypes($entityTypes);
}
/**
* Implements hook_civicrm_buildForm().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
*/
function ftoverride_civicrm_buildForm($formName, &$form) {
if ('CRM_Contribute_Form_ContributionPage_Settings' == $formName) {
2018-10-29 02:31:34 +00:00
$showElement = TRUE;
if ($form->getVar('_id')) {
$snippet = CRM_Utils_Array::value('snippet', $_GET);
if (empty($snippet)) {
$showElement = FALSE;
}
}
2018-11-01 16:46:25 +00:00
2018-10-29 02:31:34 +00:00
$form->assign('showElement', $showElement);
2018-11-01 16:46:25 +00:00
$financialTypes = ftoverride_get_financialType($form->getVar('_action'));
2018-10-23 22:16:53 +00:00
$form->add(
'select',
'designation',
ts('Designation'),
$financialTypes,
FALSE,
[
'class' => 'crm-select2',
'multiple' => 'multiple',
'data-option-edit-path' => 'civicrm/admin/financial/financialType',
]
);
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Contribute/Form/ContributionPage/common.tpl',
));
if ($form->getVar('_id')) {
try {
$designations = ftoverride_get_designation($form->getVar('_id'));
if (empty($designations)) {
return;
}
$form->setDefaults(['designation' => $designations]);
}
catch (Exception $e) {
// Ignore
}
}
}
2018-10-29 03:01:40 +00:00
if ('CRM_Contribute_Form_Contribution_Confirm' == $formName && $form->_flagSubmitted) {
$submitValues = $form->_params;
if (!empty($submitValues['designation'])) {
2018-10-31 18:41:41 +00:00
$form->assign('contribution_designation', $submitValues['designation']);
2018-11-01 17:21:40 +00:00
if ($submitValues['designation'] == 'other_financial_type') {
$form->_params['contribution_note'] = $submitValues['designation_note'];
}
2018-10-29 03:01:40 +00:00
}
}
2018-10-23 22:16:53 +00:00
if ('CRM_Contribute_Form_Contribution_Main' == $formName) {
try {
$designations = ftoverride_get_designation($form->_id);
if (empty($designations)) {
return;
}
2018-11-01 16:46:25 +00:00
$financialTypes = ftoverride_get_financialType($form->getVar('_action'));
2018-10-23 22:16:53 +00:00
$designations = array_flip($designations);
$financialTypes = array_intersect_key($financialTypes, $designations);
$form->add(
'select',
'designation',
ts('Designation'),
['' => ts('- select -')] + $financialTypes,
FALSE,
['class' => 'crm-select2']
);
2018-11-01 17:21:40 +00:00
$form->add(
'text',
'designation_note',
''
);
2018-10-23 22:16:53 +00:00
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Contribute/Form/ContributionMain/common.tpl',
));
}
catch (Exception $e) {
// Ignore
}
}
}
/**
* Implements hook_civicrm_buildForm().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
*/
function ftoverride_civicrm_pre($op, $objectName, $id, &$params) {
2018-10-29 03:01:40 +00:00
if ($op == 'create' && $objectName == 'Contribution' && !empty($params['contribution_page_id'])) {
$designation = CRM_Core_Smarty::singleton()->get_template_vars('contribution_designation');
if (!empty($designation)) {
2018-11-01 17:21:40 +00:00
if ($designation == 'other_financial_type') {
// Ignore
}
else {
$params['financial_type_id'] = $designation;
}
2018-10-29 03:01:40 +00:00
}
}
2018-10-23 22:16:53 +00:00
if (in_array($op, ['create', 'edit'])
&& $objectName == 'ContributionPage'
2018-10-29 02:31:34 +00:00
&& isset($params['designation'])
2018-10-23 22:16:53 +00:00
) {
if ($id) {
$params['id'] = $id;
}
$customFieldId = civicrm_api3('CustomField', 'getvalue', [
'return' => "id",
'custom_group_id' => "ft_override",
'name' => "ft_override_designation",
]);
$params["custom_{$customFieldId}"] = $params['designation'];
unset($params['designation']);
$contributionPage = civicrm_api3('ContributionPage', 'create', $params);
$params = ['id' => $contributionPage['id']];
}
}
2018-11-01 17:29:22 +00:00
/**
* Implements hook_civicrm_validateForm().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_validateForm
*/
function ftoverride_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ('CRM_Contribute_Form_Contribution_Main' == $formName) {
if (!empty($fields['designation'])
&& $fields['designation'] == 'other_financial_type'
&& empty($fields['designation_note'])
) {
$errors['designation_note'] = ts('Please provide other information about designation.');
}
}
}
2018-10-23 22:16:53 +00:00
/**
* Implements hook_civicrm_buildAmount().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildAmount
*/
function ftoverride_civicrm_buildAmount($pageType, &$form, &$amount) {
if (!empty($amount) && $form->_flagSubmitted) {
$submitValues = $form->_submitValues;
2018-11-01 17:21:40 +00:00
if (!empty($submitValues['designation']) && $submitValues['designation'] != 'other_financial_type') {
2018-10-23 22:16:53 +00:00
foreach ($amount as &$priceFields) {
foreach ($priceFields['options'] as &$options) {
$options['financial_type_id'] = $submitValues['designation'];
}
}
}
}
}
function ftoverride_get_designation($pageId) {
$customFieldId = civicrm_api3('CustomField', 'getvalue', [
'return' => "id",
'custom_group_id' => "ft_override",
'name' => "ft_override_designation",
]);
$designations = civicrm_api3('ContributionPage', 'getvalue', [
'return' => "custom_{$customFieldId}",
'id' => $pageId,
]);
return $designations;
}
2018-11-01 16:46:25 +00:00
function ftoverride_get_financialType($action) {
$financialTypes = [];
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $action);
2018-11-01 17:21:40 +00:00
if (empty($financialTypes)) {
return $financialTypes;
}
2018-11-01 16:46:25 +00:00
$result = civicrm_api3('FinancialType', 'get', [
'return' => ["description"],
'description' => ['!=' => ""],
'id' => ['IN' => array_keys($financialTypes)],
]);
$descFinancialType = [];
if (!empty($result['values'])) {
$descFinancialType = array_column($result['values'], 'description', 'id');
}
2018-11-01 17:21:40 +00:00
return $descFinancialType + $financialTypes + ['other_financial_type' => ts('Other')];
2018-11-01 16:46:25 +00:00
}