Merge pull request #5485 from eileenmcnaughton/4.6
[civicrm-core.git] / CRM / Contribute / Form / Contribution / Confirm.php
index 2926fc0b81a4b07030b7d94faaf5c455a8fb0c92..37126d5e14e07aed6282d8fe51f61af2fa0f4c51 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -39,7 +39,7 @@
 class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_ContributionBase {
 
   /**
-   * The id of the contact associated with this contribution
+   * The id of the contact associated with this contribution.
    *
    * @var int
    */
@@ -47,14 +47,14 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
 
 
   /**
-   * The id of the contribution object that is created when the form is submitted
+   * The id of the contribution object that is created when the form is submitted.
    *
    * @var int
    */
   public $_contributionID;
 
   /**
-   * Set the parameters to be passed to contribution create function
+   * Set the parameters to be passed to contribution create function.
    *
    * @param array $params
    * @param int $contactID
@@ -89,22 +89,13 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       'amount_level' => CRM_Utils_Array::value('amount_level', $params),
       'invoice_id' => $params['invoiceID'],
       'currency' => $params['currencyID'],
-      'source' =>
-        (!$online || !empty($params['source'])) ?
-          CRM_Utils_Array::value('source', $params) :
-          CRM_Utils_Array::value('description', $params),
+      'source' => (!$online || !empty($params['source'])) ? CRM_Utils_Array::value('source', $params) : CRM_Utils_Array::value('description', $params),
       'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
       //configure cancel reason, cancel date and thankyou date
       //from 'contribution' type profile if included
       'cancel_reason' => CRM_Utils_Array::value('cancel_reason', $params, 0),
-      'cancel_date' =>
-        isset($params['cancel_date']) ?
-          CRM_Utils_Date::format($params['cancel_date']) :
-          NULL,
-      'thankyou_date' =>
-        isset($params['thankyou_date']) ?
-          CRM_Utils_Date::format($params['thankyou_date']) :
-          NULL,
+      'cancel_date' => isset($params['cancel_date']) ? CRM_Utils_Date::format($params['cancel_date']) : NULL,
+      'thankyou_date' => isset($params['thankyou_date']) ? CRM_Utils_Date::format($params['thankyou_date']) : NULL,
       'campaign_id' => $campaignId,
       'is_test' => $isTest,
       'address_id' => $addressID,
@@ -157,7 +148,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Set variables up before form is built
+   * Set variables up before form is built.
    *
    * @return void
    */
@@ -178,7 +169,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
         $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
         $expressParams = $payment->getExpressCheckoutDetails($this->get('token'));
 
-        $this->_params['payer'] = $expressParams['payer'];
+        $this->_params['payer'] = CRM_Utils_Array::value('payer', $expressParams);
         $this->_params['payer_id'] = $expressParams['payer_id'];
         $this->_params['payer_status'] = $expressParams['payer_status'];
 
@@ -450,7 +441,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Build the form object
+   * Build the form object.
    *
    * @return void
    */
@@ -670,9 +661,10 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Set default values for the form. Note that in edit/view mode
-   * the default values are retrieved from the database
+   * Set default values for the form.
    *
+   * Note that in edit/view mode
+   * the default values are retrieved from the database
    *
    * @return void
    */
@@ -680,7 +672,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Process the form
+   * Process the form.
    *
    * @return void
    */
@@ -1067,10 +1059,13 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
+   * Wrangle financial type ID.
+   *
    * This wrangling of the financialType ID was happening in a shared function rather than in the form it relates to & hence has been moved to that form
    * Pledges are not relevant to the membership code so that portion will not go onto the membership form.
    *
-   * Comments from previous refactor indicate doubt as to what was going on
+   * Comments from previous refactor indicate doubt as to what was going on.
+   *
    * @param int $contributionTypeId
    *
    * @return null|string
@@ -1089,12 +1084,10 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Process the form
+   * Process the form.
    *
    * @param array $premiumParams
    * @param $contribution
-   *
-   * @return void
    */
   public function postProcessPremium($premiumParams, $contribution) {
     $hour = $minute = $second = 0;
@@ -1213,7 +1206,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Process the contribution
+   * Process the contribution.
    *
    * @param CRM_Core_Form $form
    * @param array $params
@@ -1229,7 +1222,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
    * @throws Exception
    * @return CRM_Contribute_DAO_Contribution
    */
-  static function processContribution(
+  public static function processContribution(
     &$form,
     $params,
     $result,
@@ -1416,7 +1409,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
             'actual_amount' => $scheduledAmount,
           );
 
-
           CRM_Pledge_BAO_PledgePayment::add($pledgePaymentParams);
         }
 
@@ -1505,7 +1497,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       CRM_Core_BAO_Note::add($noteParams, array());
     }
 
-
     if (isset($params['related_contact'])) {
       $contactID = $params['related_contact'];
     }
@@ -1537,7 +1528,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Create the recurring contribution record
+   * Create the recurring contribution record.
    *
    * @param CRM_Core_Form $form
    * @param array $params
@@ -1625,7 +1616,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Add on behalf of organization and it's location
+   * Add on behalf of organization and it's location.
    *
    * @param array $behalfOrganization
    *   array of organization info.
@@ -1637,8 +1628,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
    *   form values array.
    * @param array $params
    * @param null $fields
-   *
-   * @return void
    */
   public static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) {
     $isCurrentEmployer = FALSE;
@@ -1696,7 +1685,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
     // create relationship
     $relParams['contact_check'][$orgID] = 1;
     $cid = array('contact' => $contactID);
-    CRM_Contact_BAO_Relationship::createMultiple($relParams, $cid);
+    CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid);
 
     // if multiple match - send a duplicate alert
     if ($dupeIDs && (count($dupeIDs) > 1)) {
@@ -1737,24 +1726,24 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Function used to save pcp / soft credit entry
+   * Function used to save pcp / soft credit entry.
+   *
    * This is used by contribution and also event pcps
    *
    * @param array $params
    * @param object $contribution
    *   Contribution object.
-   *
    */
   public static function processPcpSoft(&$params, &$contribution) {
-    //add soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin.
+    // Add soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin.
     if (!empty($params['soft_credit_to'])) {
       $contributionSoftParams = array();
       foreach (array(
-                 'pcp_display_in_roll',
-                 'pcp_roll_nickname',
-                 'pcp_personal_note',
-                 'amount',
-               ) as $val) {
+          'pcp_display_in_roll',
+          'pcp_roll_nickname',
+          'pcp_personal_note',
+          'amount',
+        ) as $val) {
         if (!empty($params[$val])) {
           $contributionSoftParams[$val] = $params[$val];
         }
@@ -1768,13 +1757,78 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
 
       $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name');
 
-      CRM_Contribute_BAO_ContributionSoft::add($contributionSoftParams);
+      $contributionSoft = CRM_Contribute_BAO_ContributionSoft::add($contributionSoftParams);
+
+      //Send notification to owner for PCP
+      if ($contributionSoft->id && $contributionSoft->pcp_id) {
+        CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft);
+      }
     }
   }
 
   /**
-   * Function used to se pcp related defaults / params
-   * This is used by contribution and also event pcps
+   * Function used to send notification mail to pcp owner.
+   *
+   * This is used by contribution and also event PCPs.
+   *
+   * @param object $contribution
+   * @param object $contributionSoft
+   *   Contribution object.
+   */
+  public static function pcpNotifyOwner($contribution, $contributionSoft) {
+    $params = array('id' => $contributionSoft->pcp_id);
+    CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
+    $ownerNotifyID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCPBlock', $pcpInfo['pcp_block_id'], 'owner_notify_id');
+
+    if ($ownerNotifyID != CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'no_notifications', 'name') &&
+        (($ownerNotifyID == CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'owner_chooses', 'name') &&
+        CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $contributionSoft->pcp_id, 'is_notify')) ||
+        $ownerNotifyID == CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'all_owners', 'name'))) {
+      $pcpInfoURL = CRM_Utils_System::url('civicrm/pcp/info',
+        "reset=1&id={$contributionSoft->pcp_id}",
+        TRUE, NULL, FALSE, TRUE
+      );
+      // set email in the template here
+      // get the billing location type
+      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
+      $billingLocationTypeId = array_search('Billing', $locationTypes);
+
+      if ($billingLocationTypeId) {
+        list($donorName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contribution->contact_id, FALSE, $billingLocationTypeId);
+      }
+      // get primary location email if no email exist( for billing location).
+      if (!$email) {
+        list($donorName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contribution->contact_id);
+      }
+      list($ownerName, $ownerEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contributionSoft->contact_id);
+      $tplParams = array(
+        'page_title' => $pcpInfo['title'],
+        'receive_date' => $contribution->receive_date,
+        'total_amount' => $contributionSoft->amount,
+        'donors_display_name' => $donorName,
+        'donors_email' => $email,
+        'pcpInfoURL' => $pcpInfoURL,
+        'is_honor_roll_enabled' => $contributionSoft->pcp_display_in_roll,
+      );
+      $domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
+      $sendTemplateParams = array(
+        'groupName' => 'msg_tpl_workflow_contribution',
+        'valueName' => 'pcp_owner_notify',
+        'contactId' => $contributionSoft->contact_id,
+        'toEmail' => $ownerEmail,
+        'toName' => $ownerName,
+        'from' => "$domainValues[0] <$domainValues[1]>",
+        'tplParams' => $tplParams,
+        'PDFFilename' => 'receipt.pdf',
+      );
+      CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams);
+    }
+  }
+
+  /**
+   * Function used to se pcp related defaults / params.
+   *
+   * This is used by contribution and also event PCPs
    *
    * @param CRM_Core_Form $page
    *   Form object.
@@ -1807,6 +1861,8 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
+   * Process membership.
+   *
    * @param array $membershipParams
    * @param int $contactID
    * @param array $customFieldsFormatted
@@ -1854,10 +1910,11 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Are we going to do 2 financial transactions?
-   * ie the membership block supports a separate transactions AND the contribution form has been configured for a contribution
-   * transaction AND a membership transaction AND the payment processor supports double financial transactions (ie. NOT doTransferPayment style)
+   * Are we going to do 2 financial transactions.
    *
+   * Ie the membership block supports a separate transactions AND the contribution form has been configured for a
+   * contribution
+   * transaction AND a membership transaction AND the payment processor supports double financial transactions (ie. NOT doTransferPayment style)
    *
    * @param int $formID
    * @param bool $amountBlockActiveOnForm
@@ -1873,7 +1930,8 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * This function sets the fields
+   * This function sets the fields.
+   *
    * - $this->_params['amount_level']
    * - $this->_params['selectMembership']
    * And under certain circumstances sets
@@ -1902,7 +1960,9 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
           $this->_params['selectMembership'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue',
             $this->_params["price_{$priceField->id}"], 'membership_type_id');
         }
-      } // if separate payment we set contribution amount to be null, so that it will not show contribution amount same as membership amount.
+      }
+      // If separate payment we set contribution amount to be null, so that it will not show contribution amount same
+      // as membership amount.
       // @todo - this needs more documentation - it appears the setting to null is tied up with separate membership payments
       // but the circumstances are very confusing. Many of these conditions are repeated in the next conditional
       // so we should merge them together
@@ -1934,6 +1994,8 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
+   * Submit function.
+   *
    * @param array $params
    *
    * @throws CiviCRM_API3_Exception
@@ -1977,7 +2039,9 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
   }
 
   /**
-   * Helper function for static submit function - set relevant params - help us to build up an array that we can pass in
+   * Helper function for static submit function - set relevant params - help us to build up an array that we can pass
+   * in.
+   *
    * @param int $id
    * @param array $params
    *