com.megaphonetech.ftoverride/ftoverride.php
2018-11-01 22:16:25 +05:30

352 lines
9.9 KiB
PHP

<?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) {
$showElement = TRUE;
if ($form->getVar('_id')) {
$snippet = CRM_Utils_Array::value('snippet', $_GET);
if (empty($snippet)) {
$showElement = FALSE;
}
}
$form->assign('showElement', $showElement);
$financialTypes = ftoverride_get_financialType($form->getVar('_action'));
$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
}
}
}
if ('CRM_Contribute_Form_Contribution_Confirm' == $formName && $form->_flagSubmitted) {
$submitValues = $form->_params;
if (!empty($submitValues['designation'])) {
$form->assign('contribution_designation', $submitValues['designation']);
}
}
if ('CRM_Contribute_Form_Contribution_Main' == $formName) {
try {
$designations = ftoverride_get_designation($form->_id);
if (empty($designations)) {
return;
}
$financialTypes = ftoverride_get_financialType($form->getVar('_action'));
$designations = array_flip($designations);
$financialTypes = array_intersect_key($financialTypes, $designations);
$form->add(
'select',
'designation',
ts('Designation'),
['' => ts('- select -')] + $financialTypes,
FALSE,
['class' => 'crm-select2']
);
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) {
if ($op == 'create' && $objectName == 'Contribution' && !empty($params['contribution_page_id'])) {
$designation = CRM_Core_Smarty::singleton()->get_template_vars('contribution_designation');
if (!empty($designation)) {
$params['financial_type_id'] = $designation;
CRM_Core_Smarty::singleton()->assign('contribution_designation', '');
}
}
if (in_array($op, ['create', 'edit'])
&& $objectName == 'ContributionPage'
&& isset($params['designation'])
) {
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']];
}
}
/**
* 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;
if (!empty($submitValues['designation'])) {
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;
}
function ftoverride_get_financialType($action) {
$financialTypes = [];
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $action);
$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');
}
return $descFinancialType + $financialTypes;
}