From ba60f73eb4bc61a4209b0598b6e821e36a6d48a1 Mon Sep 17 00:00:00 2001 From: monishdeb Date: Mon, 31 Mar 2014 20:06:10 +0530 Subject: [PATCH] CRM-13981 multilingual fix ---------------------------------------- * CRM-13981: http://issues.civicrm.org/jira/browse/CRM-13981 --- CRM/Contribute/BAO/ContributionPage.php | 67 +++++++++++++++++++ .../Form/ContributionPage/Settings.php | 19 ++---- CRM/Contribute/Form/SoftCredit.php | 9 +-- 3 files changed, 77 insertions(+), 18 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php index 002bca7d78..990033f509 100644 --- a/CRM/Contribute/BAO/ContributionPage.php +++ b/CRM/Contribute/BAO/ContributionPage.php @@ -794,5 +794,72 @@ LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm } return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); } + + /** + * Get or Set multilingually affected honor params for processing module_data or setting default values. + * + * @param Array|String $params: Array when we need to format it according to language state or String as a json encode + * @param Boolean $setDefault: If yes then returns array to used for setting default value afterward + * + * @return array|string + */ + public static function formatMultilingualHonorParams($params, $setDefault = FALSE) { + $config = CRM_Core_Config::singleton(); + $sctJson = $sctJsonDecode = NULL; + $domain = new CRM_Core_DAO_Domain(); + $domain->find(TRUE); + + //When we are fetching the honor params respecting both multi and single lingual state + //and setting it to default param of Contribution Page's Main and Setting form + if ($setDefault) { + $sctJsonDecode = json_decode($params); + $sctJsonDecode = (array) $sctJsonDecode->soft_credit; + if (!empty($sctJsonDecode[$config->lcMessages])) { + foreach ($sctJsonDecode[$config->lcMessages] as $column => $value) { + $sctJsonDecode[$column] = $value; + } + unset($sctJsonDecode[$config->lcMessages]); + } + return $sctJsonDecode; + } + + //check and handle multilingual honoree params + if (!$domain->locales) { + //if in singlelingual state simply return the array format + $sctJson = json_encode( + array( + 'soft_credit' => array( + 'soft_credit_types' => $params['soft_credit_types'], + 'honor_block_title' => $params['honor_block_title'], + 'honor_block_text' => $params['honor_block_text'] + ) + ) + ); + } + else { + //if in multilingual state then retrieve the module_data against this contribution and + //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information + $sctJson = array( + 'soft_credit' => array( + 'soft_credit_types' => $params['soft_credit_types'], + $config->lcMessages => array ( + 'honor_block_title' => $params['honor_block_title'], + 'honor_block_text' => $params['honor_block_text'] + ) + ) + ); + + $ufJoinDAO = new CRM_Core_DAO_UFJoin(); + $ufJoinDAO->module = 'soft_credit'; + $ufJoinDAO->entity_id = $params['id']; + $ufJoinDAO->find(TRUE); + $jsonData = json_decode($ufJoinDAO->module_data); + if ($jsonData) { + $sctJson['soft_credit'] = array_merge((array)$jsonData->soft_credit, $sctJson['soft_credit']); + } + $sctJson = json_encode($sctJson); + } + return $sctJson; + } } diff --git a/CRM/Contribute/Form/ContributionPage/Settings.php b/CRM/Contribute/Form/ContributionPage/Settings.php index cd0d4b299d..528d9066e8 100644 --- a/CRM/Contribute/Form/ContributionPage/Settings.php +++ b/CRM/Contribute/Form/ContributionPage/Settings.php @@ -81,12 +81,9 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ $ufJoinDAO->entity_id = $this->_id; if ($ufJoinDAO->find(TRUE)) { $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id; - $jsonData = json_decode($ufJoinDAO->module_data); - if ($jsonData) { - foreach ($jsonData->soft_credit as $index => $value){ - $defaults[$index] = $value; - } - } + $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($ufJoinDAO->module_data, TRUE); + $defaults = array_merge($defaults, $jsonData); + $defaults['honor_block_is_active'] = $ufJoinDAO->is_active; } else { $ufGroupDAO = new CRM_Core_DAO_UFGroup(); @@ -345,14 +342,7 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ $params['honor_block_text'] = NULL; } else { - $sctJSON = json_encode(array( - 'soft_credit' => array( - 'soft_credit_types' => $params['soft_credit_types'], - 'honor_block_title' => $params['honor_block_title'], - 'honor_block_text' => $params['honor_block_text'] - ) - ) - ); + $sctJSON = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($params); } $dao = CRM_Contribute_BAO_ContributionPage::create($params); @@ -379,6 +369,7 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ $ufJoinParam['weight'] = 1; if ($index == 'honor_block_is_active') { $ufJoinParam['is_active'] = 1; + $ufJoinParam['module'] = 'soft_credit'; $ufJoinParam['uf_group_id'] = $params['honoree_profile']; $ufJoinParam['module_data'] = $sctJSON; } diff --git a/CRM/Contribute/Form/SoftCredit.php b/CRM/Contribute/Form/SoftCredit.php index db9170ba9f..3abc2d38c3 100644 --- a/CRM/Contribute/Form/SoftCredit.php +++ b/CRM/Contribute/Form/SoftCredit.php @@ -79,16 +79,17 @@ class CRM_Contribute_Form_SoftCredit { $ufJoinDAO->module = 'soft_credit'; $ufJoinDAO->entity_id = $form->_id; if ($ufJoinDAO->find(TRUE)) { - $jsonData = json_decode($ufJoinDAO->module_data); + $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($ufJoinDAO->module_data, TRUE); if ($jsonData) { - $form->assign('honor_block_title', $jsonData->soft_credit->honor_block_title); - $form->assign('honor_block_text', $jsonData->soft_credit->honor_block_text); + foreach (array('honor_block_title', 'honor_block_text') as $name) { + $form->assign($name, $jsonData[$name]); + } $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); $extraOption = array('onclick' => "enableHonorType();"); // radio button for Honor Type - foreach ($jsonData->soft_credit->soft_credit_types as $value) { + foreach ($jsonData['soft_credit_types'] as $value) { $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value, $extraOption); } $form->addGroup($honorTypes, 'soft_credit_type_id', NULL); -- 2.25.1