CRM-15678 hookable invalid page handling
[civicrm-core.git] / CRM / Contribute / Form / ContributionBase.php
index d209eabff51328ba337f8686a0b752a87b09194b..31919c8314ace3f28778b55b2ee8c41447f6a38d 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  */
 
 /**
- * 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
    */
   public $_id;
 
   /**
-   * the mode that we are in
+   * The mode that we are in
    *
    * @var string
    * @protect
@@ -56,26 +55,23 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
   public $_mode;
 
   /**
-   * the contact id related to a membership
+   * The contact id related to a membership
    *
    * @var int
-   * @public
    */
   public $_membershipContactID;
 
   /**
-   * the values for the contribution db object
+   * The values for the contribution db object
    *
    * @var array
-   * @protected
    */
   public $_values;
 
   /**
-   * the paymentProcessor attributes for this page
+   * The paymentProcessor attributes for this page
    *
    * @var array
-   * @protected
    */
   public $_paymentProcessor;
   public $_paymentObject = NULL;
@@ -84,15 +80,18 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * The membership block for this page
    *
    * @var array
-   * @protected
    */
   public $_membershipBlock = NULL;
 
   /**
-   * the default values for the form
+   * Does this form support a separate membership payment
+   * @var bool
+   */
+  protected $_separateMembershipPayment;
+  /**
+   * The default values for the form
    *
    * @var array
-   * @protected
    */
   protected $_defaults;
 
@@ -100,7 +99,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * The params submitted by the form and computed by the app
    *
    * @var array
-   * @public
    */
   public $_params;
 
@@ -108,15 +106,13 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * The fields involved in this contribution page
    *
    * @var array
-   * @public
    */
-  public $_fields;
+  public $_fields = array();
 
   /**
    * The billing location id for this contribiution page
    *
    * @var int
-   * @protected
    */
   public $_bltID;
 
@@ -124,31 +120,27 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * Cache the amount to make things easier
    *
    * @var float
-   * @public
    */
   public $_amount;
 
   /**
-   * pcp id
+   * Pcp id
    *
    * @var integer
-   * @public
    */
   public $_pcpId;
 
   /**
-   * pcp block
+   * Pcp block
    *
    * @var array
-   * @public
    */
   public $_pcpBlock;
 
   /**
-   * pcp info
+   * Pcp info
    *
    * @var array
-   * @public
    */
   public $_pcpInfo;
 
@@ -162,10 +154,9 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
   protected $_userID;
 
   /**
-   * the Membership ID for membership renewal
+   * The Membership ID for membership renewal
    *
    * @var int
-   * @public
    */
   public $_membershipId;
 
@@ -173,7 +164,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * Price Set ID, if the new price set method is used
    *
    * @var int
-   * @protected
    */
   public $_priceSetId;
 
@@ -181,7 +171,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * Array of fields for the price set
    *
    * @var array
-   * @protected
    */
   public $_priceSet;
 
@@ -191,7 +180,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * Is honor block is enabled for this contribution?
    *
    * @var boolean
-   * @protected
    */
   public $_honor_block_is_active = FALSE;
 
@@ -202,19 +190,20 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
   public $_contributeMode;
 
   /**
-   * contribution page supports memberships
+   * Contribution page supports memberships
    * @var boolean
    */
   public $_useForMember;
 
   public $_isBillingAddressRequiredForPayLater;
   /**
-   * Function to set variables up before form is built
+   * Set variables up before form is built
    *
+   * @throws CRM_Contribution_Exception_InactiveContributionPageException
    * @return void
-   * @access public
    */
   public function preProcess() {
+
     $config = CRM_Core_Config::singleton();
     $session = CRM_Core_Session::singleton();
 
@@ -311,10 +300,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
       CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values);
 
-      // check if form is active
       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.'));
+        throw new CRM_Contribute_Exception_InactiveContributionPageException(ts('The page you requested is currently unavailable.'), $this->_id);
       }
 
       // also check for billing informatin
@@ -532,22 +519,20 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
   }
 
   /**
-   * set the default values
+   * Set the default values
    *
    * @return void
-   * @access public
    */
-  function setDefaultValues() {
+  public function setDefaultValues() {
     return $this->_defaults;
   }
 
   /**
-   * assign the minimal set of variables to the template
+   * Assign the minimal set of variables to the template
    *
    * @return void
-   * @access public
    */
-  function assignToTemplate() {
+  public function assignToTemplate() {
     $name = CRM_Utils_Array::value('billing_first_name', $this->_params);
     if (!empty($this->_params['billing_middle_name'])) {
       $name .= " {$this->_params['billing_middle_name']}";
@@ -643,7 +628,6 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
       if ($this->_paymentProcessor &&
         $this->_paymentProcessor['payment_type'] & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
       ) {
-        $this->assign('payment_type', $this->_paymentProcessor['payment_type']);
         $this->assign('account_holder', $this->_params['account_holder']);
         $this->assign('bank_identification_number', $this->_params['bank_identification_number']);
         $this->assign('bank_name', $this->_params['bank_name']);
@@ -670,20 +654,17 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
   }
 
   /**
-   * Function to add the custom fields
+   * Add the custom fields
    *
-   * @param $id
-   * @param $name
+   * @param int $id
+   * @param string $name
    * @param bool $viewOnly
    * @param null $profileContactType
    * @param null $fieldTypes
    *
    * @return void
-   * @access public
    */
-  function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
-    $stateCountryMap = array();
-
+  public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType = NULL, $fieldTypes = NULL) {
     if ($id) {
       $contactID = $this->getContactID();
 
@@ -742,33 +723,6 @@ 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();
-            }
-            $stateCountryMap[$index][$prefixName] = $key;
-
-            if ($prefixName == "state_province") {
-              if ($profileContactType == 'onbehalf') {
-                //CRM-11881: Bypass required-ness check for state/province on Contribution Confirm page
-                //as already done during Contribution registration via onBehalf's quickForm
-                $field['is_required'] = FALSE;
-              }
-              else {
-                if (count($this->_submitValues)) {
-                  $locationTypeId = $field['location_type_id'];
-                  if (array_key_exists("country-{$locationTypeId}", $fields) &&
-                  array_key_exists("state_province-{$locationTypeId}", $fields) &&
-                    !empty($this->_submitValues["country-{$locationTypeId}"])) {
-                    $field['is_required'] =
-                      CRM_Core_Payment_Form::checkRequiredStateProvince($this, "country-{$locationTypeId}");
-                  }
-                }
-              }
-            }
-          }
-
           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');
@@ -809,8 +763,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);
@@ -826,7 +778,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    *
    * @return null|string
    */
-  function checkTemplateFileExists($suffix = NULL) {
+  public function checkTemplateFileExists($suffix = NULL) {
     if ($this->_id) {
       $templateFile = "CRM/Contribute/Form/Contribution/{$this->_id}/{$this->_name}.{$suffix}tpl";
       $template = CRM_Core_Form::getTemplate();
@@ -841,12 +793,11 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * Use the form name to create the tpl file name
    *
    * @return string
-   * @access public
    */
   /**
    * @return string
    */
-  function getTemplateFileName() {
+  public function getTemplateFileName() {
     $fileName = $this->checkTemplateFileExists();
     return $fileName ? $fileName : parent::getTemplateFileName();
   }
@@ -856,20 +807,18 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    * i.e. we dont override
    *
    * @return string
-   * @access public
    */
   /**
    * @return string
    */
-  function overrideExtraTemplateFileName() {
+  public function overrideExtraTemplateFileName() {
     $fileName = $this->checkTemplateFileExists('extra.');
     return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
   }
 
   /**
-   * Function to authenticate pledge user during online payment.
+   * Authenticate pledge user during online payment.
    *
-   * @access public
    *
    * @return void
    */
@@ -940,4 +889,3 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     }
   }
 }
-