Merge branch 'JohnFF-patch-1'
[civicrm-core.git] / CRM / Contribute / Form / SoftCredit.php
index db9170ba9f387370291f83d81e4c0586c4e86a6a..b8145f7898ea4cbb1cb80bfb88c582052f73e04b 100644 (file)
@@ -41,6 +41,8 @@ class CRM_Contribute_Form_SoftCredit {
   /**
    * Function to set variables up before form is built
    *
+   * @param $form
+   *
    * @return void
    * @access static
    */
@@ -74,24 +76,24 @@ class CRM_Contribute_Form_SoftCredit {
    * @return void
    */
   static function buildQuickForm(&$form) {
-    if ($form->_mode == 'live' && $form->_honor_block_is_active) {
+    if ($form->_mode == 'live' && !empty($form->_honor_block_is_active)) {
       $ufJoinDAO = new CRM_Core_DAO_UFJoin();
       $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) {
-            $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value, $extraOption);
+          foreach ($jsonData['soft_credit_types'] as $value) {
+            $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value);
           }
-          $form->addGroup($honorTypes, 'soft_credit_type_id', NULL);
+          $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE);
         }
       }
         return $form;
@@ -104,7 +106,7 @@ class CRM_Contribute_Form_SoftCredit {
     if ($form->getAction() & CRM_Core_Action::UPDATE) {
       $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE);
     }
-    elseif ($form->_pledgeID) {
+    elseif (!empty($form->_pledgeID)) {
       //Check and select most recent completed contrubtion and use it to retrieve
       //soft-credit information to use as default for current pledge payment, CRM-13981
       $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($form->_pledgeID);
@@ -153,25 +155,24 @@ class CRM_Contribute_Form_SoftCredit {
       CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"),
       array('id' => 'sct_default_id')
     );
-
-    // Tell tpl to hide soft credit field if contribution is linked directly to a PCP Page
-    if (!empty($form->_values['pcp_made_through_id'])) {
-      $form->assign('pcpLinked', 1);
-    }
   }
 
   /**
    * Function used to set defaults for soft credit block
    */
   static function setDefaultValues(&$defaults, &$form) {
+    //Used to hide/unhide PCP and/or Soft-credit Panes
+    $noPCP = $noSoftCredit = TRUE;
     if (!empty($form->_softCreditInfo['soft_credit'])) {
+      $noSoftCredit = FALSE;
       foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) {
         $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a');
         $defaults["soft_credit_contact_id[$key]"] = $value['contact_id'];
         $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
       }
     }
-    elseif (!empty($form->_softCreditInfo['pcp_id'])) {
+    if (!empty($form->_softCreditInfo['pcp_id'])) {
+      $noPCP = FALSE;
       $pcpInfo = $form->_softCreditInfo;
       $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo);
       $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
@@ -182,14 +183,20 @@ class CRM_Contribute_Form_SoftCredit {
       $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo);
       $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo);
     }
+
+    $form->assign('noSoftCredit', $noSoftCredit);
+    $form->assign('noPCP', $noPCP);
   }
 
   /**
    * global form rule
    *
-   * @param array $fields  the input form values
+   * @param array $fields the input form values
+   *
+   * @param $errors
+   * @param $self
    *
-   * @return true if no errors, else array of errors
+   * @return array of errors
    * @access public
    * @static
    */