Minor cleanup
[civicrm-core.git] / CRM / Contribute / Form / ContributionBase.php
index 36a6913f853f61683cb8a27e1fc6ef1e93813f6a..cb9e64d40a1cb0a6270a01a0b8e0319749133dd5 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
 
 /**
- * This class generates form components for processing a ontribution
+ * This class generates form components for processing a contribution
  *
  */
 class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
   /**
-   * the id of the contribution page that we are proceessing
+   * the id of the contribution page that we are processsing
    *
    * @var int
    * @public
@@ -88,6 +88,11 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    */
   public $_membershipBlock = NULL;
 
+  /**
+   * Does this form support a separate membership payment
+   * @var bool
+   */
+  protected $_separateMembershipPayment;
   /**
    * the default values for the form
    *
@@ -110,7 +115,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * @var array
    * @public
    */
-  public $_fields;
+  public $_fields = array();
 
   /**
    * The billing location id for this contribiution page
@@ -187,6 +192,25 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
   public $_action;
 
+ /**
+   * Is honor block is enabled for this contribution?
+   *
+   * @var boolean
+   * @protected
+   */
+  public $_honor_block_is_active = FALSE;
+
+  /**
+   * Contribution mode e.g express for payment express, notify for off-site + notification back to CiviCRM
+   * @var string
+   */
+  public $_contributeMode;
+
+  /**
+   * contribution page supports memberships
+   * @var boolean
+   */
+  public $_useForMember;
   /**
    * Function to set variables up before form is built
    *
@@ -208,6 +232,17 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     // this was used prior to the cleverer this_>getContactID - unsure now
     $this->_userID = $session->get('userID');
 
+    //Check if honor block is enabled for current contribution
+    $ufJoinParams = array(
+      'module' => 'soft_credit',
+      'entity_table' => 'civicrm_contribution_page',
+      'entity_id' => $this->_id,
+    );
+    $ufJoin = new CRM_Core_DAO_UFJoin();
+    $ufJoin->copyValues($ufJoinParams);
+    $ufJoin->find(TRUE);
+    $this->_honor_block_is_active = $ufJoin->is_active;
+
     $this->_contactID = $this->_membershipContactID = $this->getContactID();
     $this->_mid = NULL;
     if ($this->_contactID) {
@@ -280,7 +315,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
       CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values);
 
       // check if form is active
-      if (!CRM_Utils_Array::value('is_active', $this->_values)) {
+      if (empty($this->_values['is_active'])) {
         // form is inactive, die a fatal death
         CRM_Core_Error::fatal(ts('The page you requested is currently unavailable.'));
       }
@@ -302,7 +337,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
       //FIXME: to support multiple payment processors
       if ($isMonetary &&
-        (!$isPayLater || CRM_Utils_Array::value('payment_processor', $this->_values))
+        (!$isPayLater || !empty($this->_values['payment_processor']))
       ) {
         $ppID = CRM_Utils_Array::value('payment_processor', $this->_values);
         if (!$ppID) {
@@ -353,7 +388,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
       // this avoids getting E_NOTICE errors in php
       $setNullFields = array(
         'amount_block_is_active',
-        'honor_block_is_active',
         'is_allow_other_amount',
         'footer_text',
       );
@@ -431,7 +465,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     }
 
     //set pledge block if block id is set
-    if (CRM_Utils_Array::value('pledge_block_id', $this->_values)) {
+    if (!empty($this->_values['pledge_block_id'])) {
       $this->assign('pledgeBlock', TRUE);
     }
 
@@ -512,7 +546,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    */
   function assignToTemplate() {
     $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
-    if (CRM_Utils_Array::value('billing_middle_name', $this->_params)) {
+    if (!empty($this->_params['billing_middle_name'])) {
       $name .= " {$this->_params['billing_middle_name']}";
     }
     $name .= ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params);
@@ -527,9 +561,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     );
 
     $config = CRM_Core_Config::singleton();
-    if (isset($this->_values['is_recur']) &&
-      CRM_Utils_Array::value('is_recur', $this->_paymentProcessor)
-    ) {
+    if (isset($this->_values['is_recur']) && !empty($this->_paymentProcessor['is_recur'])) {
       $this->assign('is_recur_enabled', 1);
       $vars = array_merge($vars, array(
         'is_recur', 'frequency_interval', 'frequency_unit',
@@ -586,7 +618,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
     $this->assign('address', CRM_Utils_Address::format($addressFields));
 
-    if (CRM_Utils_Array::value('hidden_onbehalf_profile', $this->_params)) {
+    if (!empty($this->_params['hidden_onbehalf_profile'])) {
       $this->assign('onBehalfName', $this->_params['organization_name']);
       $locTypeId = array_keys($this->_params['onbehalf_location']['email']);
       $this->assign('onBehalfEmail', $this->_params['onbehalf_location']['email'][$locTypeId[0]]['email']);
@@ -597,7 +629,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     if ($this->_amount > 0.0) {
       $assignCCInfo = TRUE;
     }
-    elseif (CRM_Utils_array::value('selectMembership', $this->_params)) {
+    elseif (!empty($this->_params['selectMembership'])) {
       $memFee = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'minimum_fee');
       if ($memFee > 0.0) {
         $assignCCInfo = TRUE;
@@ -637,12 +669,16 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
   /**
    * Function to add the custom fields
    *
-   * @return None
+   * @param $id
+   * @param $name
+   * @param bool $viewOnly
+   * @param null $profileContactType
+   * @param null $fieldTypes
+   *
+   * @return void
    * @access public
    */
-  function buildCustom($id, $name, $viewOnly = FALSE, $onBehalf = FALSE, $fieldTypes = NULL) {
-    $stateCountryMap = array();
-
+  function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
     if ($id) {
       $contactID = $this->getContactID();
 
@@ -678,7 +714,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
       if ($fields) {
         // unset any email-* fields since we already collect it, CRM-2888
         foreach (array_keys($fields) as $fieldName) {
-          if (substr($fieldName, 0, 6) == 'email-') {
+          if (substr($fieldName, 0, 6) == 'email-' && $profileContactType != 'honor') {
             unset($fields[$fieldName]);
           }
         }
@@ -701,24 +737,23 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
             continue;
           }
 
-          list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
-          if ($prefixName == 'state_province' || $prefixName == 'country' || $prefixName == 'county') {
-            if (!array_key_exists($index, $stateCountryMap)) {
-              $stateCountryMap[$index] = array();
+          if ($profileContactType) {
+            //Since we are showing honoree name separately so we are removing it from honoree profile just for display
+            $honoreeNamefields = array('prefix_id', 'first_name', 'last_name', 'suffix_id', 'organization_name', 'household_name');
+            if ($profileContactType == 'honor' && in_array($field['name'], $honoreeNamefields)) {
+              unset($fields[$field['name']]);
+              continue;
             }
-            $stateCountryMap[$index][$prefixName] = $key;
-          }
-
-          if ($onBehalf) {
             if (!empty($fieldTypes) && in_array($field['field_type'], $fieldTypes)) {
               CRM_Core_BAO_UFGroup::buildProfile(
                 $this,
                 $field,
                 CRM_Profile_Form::MODE_CREATE,
                 $contactID,
-                TRUE
+                TRUE,
+                $profileContactType
               );
-              $this->_fields['onbehalf'][$key] = $field;
+              $this->_fields[$profileContactType][$key] = $field;
             }
             else {
               unset($fields[$key]);
@@ -742,8 +777,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
         $this->assign($name, $fields);
 
-        CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
-
         if ($addCaptcha && !$viewOnly) {
           $captcha = CRM_Utils_ReCAPTCHA::singleton();
           $captcha->add($this);
@@ -753,6 +786,12 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     }
   }
 
+  /**
+   * Check template file exists
+   * @param null $suffix
+   *
+   * @return null|string
+   */
   function checkTemplateFileExists($suffix = NULL) {
     if ($this->_id) {
       $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
@@ -764,11 +803,30 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     return NULL;
   }
 
+  /**
+   * Use the form name to create the tpl file name
+   *
+   * @return string
+   * @access public
+   */
+  /**
+   * @return string
+   */
   function getTemplateFileName() {
     $fileName = $this->checkTemplateFileExists();
     return $fileName ? $fileName : parent::getTemplateFileName();
   }
 
+  /**
+   * Default extra tpl file basically just replaces .tpl with .extra.tpl
+   * i.e. we dont override
+   *
+   * @return string
+   * @access public
+   */
+  /**
+   * @return string
+   */
   function overrideExtraTemplateFileName() {
     $fileName = $this->checkTemplateFileExists('extra.');
     return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
@@ -779,7 +837,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   public function authenticatePledgeUser() {
     //get the userChecksum and contact id