manual merge of fixes for CRM-13981
[civicrm-core.git] / CRM / Contribute / Form / SoftCredit.php
index bb1686d410d347519054efe5f363f036ca2bcb35..eab4cb5e263cde7fc2a2dec0d74a337a84b5ab33 100644 (file)
@@ -47,6 +47,29 @@ class CRM_Contribute_Form_SoftCredit {
    * @return void
    */
   static function buildQuickForm(&$form) {
+    if ($form->_mode == 'live' && !empty($form->_values['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);
+        if ($jsonData) {
+          $form->assign('honor_block_title', $jsonData->soft_credit->honor_block_title);
+          $form->assign('honor_block_text', $jsonData->soft_credit->honor_block_text);
+
+          $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);
+          }
+          $form->addGroup($honorTypes, 'soft_credit_type_id', NULL);
+        }
+      }
+        return $form;
+    }
+
     $prefix = 'soft_credit_';
     // by default generate 5 blocks
     $item_count = 6;
@@ -55,6 +78,22 @@ class CRM_Contribute_Form_SoftCredit {
     $showCreateNew = TRUE;
     if ($form->_action & CRM_Core_Action::UPDATE) {
       $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE);
+    }
+    elseif ($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);
+      foreach ($pledgePayments as $id => $record) {
+        if ($record['contribution_id']) {
+          $softCredits = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($record['contribution_id'], TRUE);
+          if ($record['status'] == 'Completed' && count($softCredits) > 0) {
+            $form->_softCreditInfo = $softCredits;
+          }
+        }
+      }
+    }
+
+    if (property_exists($form, "_softCreditInfo")) {
       if (!empty($form->_softCreditInfo['soft_credit'])) {
         $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']);
         $showSoftCreditRow++;
@@ -66,6 +105,13 @@ class CRM_Contribute_Form_SoftCredit {
       CRM_Contact_Form_NewContact::buildQuickForm($form, $rowNumber, NULL, FALSE, $prefix);
 
       $form->addMoney("{$prefix}amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
+
+      $form->add('select', "{$prefix}type[{$rowNumber}]",
+        ts( 'Soft Credit Type' ),
+        array(
+          '' => ts('- select -')) +
+          CRM_Core_OptionGroup::values("{$prefix}type", FALSE)
+      );
       if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) {
         $form->add('hidden', "{$prefix}id[{$rowNumber}]",
           $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']);
@@ -90,9 +136,13 @@ class CRM_Contribute_Form_SoftCredit {
     $form->assign('showSoftCreditRow', $showSoftCreditRow);
     $form->assign('rowCount', $item_count);
     $form->assign('showCreateNew', $showCreateNew);
+    $form->addElement('hidden', 'sct_default_id',
+      CRM_Core_OptionGroup::getDefaultValue("{$prefix}type"),
+      array('id' => 'sct_default_id')
+    );
 
     // Tell tpl to hide soft credit field if contribution is linked directly to a PCP Page
-    if (CRM_Utils_Array::value('pcp_made_through_id', $form->_values)) {
+    if (!empty($form->_values['pcp_made_through_id'])) {
       $form->assign('pcpLinked', 1);
     }
   }
@@ -105,10 +155,10 @@ class CRM_Contribute_Form_SoftCredit {
       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_select_id[$key]"] = $value['contact_id'];
+        $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
       }
     }
-
-    elseif (CRM_Utils_Array::value('pcp_id', $form->_softCreditInfo)) {
+    elseif (!empty($form->_softCreditInfo['pcp_id'])) {
       $pcpInfo = $form->_softCreditInfo;
       $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo);
       $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
@@ -130,13 +180,12 @@ class CRM_Contribute_Form_SoftCredit {
    * @access public
    * @static
    */
-  static function formRule($fields) {
+  static function formRule($fields, $errors, $self) {
     $errors = array();
 
     // if honor roll fields are populated but no PCP is selected
-    if (!CRM_Utils_Array::value('pcp_made_through_id', $fields)) {
-      if (CRM_Utils_Array::value('pcp_display_in_roll', $fields) ||
-        CRM_Utils_Array::value('pcp_roll_nickname', $fields) ||
+    if (empty($fields['pcp_made_through_id'])) {
+      if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) ||
         CRM_Utils_Array::value('pcp_personal_note', $fields)
       ) {
         $errors['pcp_made_through'] = ts('Please select a Personal Campaign Page, OR uncheck Display in Honor Roll and clear both the Honor Roll Name and the Personal Note field.');
@@ -150,7 +199,7 @@ class CRM_Contribute_Form_SoftCredit {
           if ($repeat[$fields['soft_credit_contact_select_id'][$key]] > 1) {
             $errors["soft_credit_contact_select_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.');
           }
-          if ($fields['soft_credit_amount'][$key]
+          if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key]
             && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount']))) {
             $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.');
           }