CRM-13681 : fix for event registration as well as contribution, also consists fixes...
[civicrm-core.git] / CRM / Contribute / Form / ContributionBase.php
index 2118a03de561bcc858af5f3f33a414f03347928d..e92bdf45c1cd341035654df73d3788d05a488ab5 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -200,17 +200,11 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     // current contribution page id
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
     if (!$this->_id) {
-      $pastContributionID = $session->get('pastContributionID');
-      if (!$pastContributionID) {
-        CRM_Core_Error::fatal(ts('We can\'t load the requested web page due to an incomplete link. This can be caused by using your browser\'s Back button or by using an incomplete or invalid link.'));
-      }
-      else {
-        CRM_Core_Error::fatal(ts('An error occurred during form submission. This page requires form data to be submitted for processing and no form data was submitted or processed. We are sorry for any inconvience. Please click <a href=\'%1\'>here</a> to visit the contribution page and re-start the contribution process.', array(1 => CRM_Utils_System::url('civicrm/contribute/transact', 'reset=1&id=' . $pastContributionID))));
-      }
-    }
-    else {
-      $session->set('pastContributionID', $this->_id);
+      // seems like the session is corrupted and/or we lost the id trail
+      // lets just bump this to a regular session error and redirect user to main page
+      $this->controller->invalidKeyRedirect();
     }
+
     // this was used prior to the cleverer this_>getContactID - unsure now
     $this->_userID = $session->get('userID');
 
@@ -225,13 +219,25 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
         if ($membership->find(TRUE)) {
           $this->_defaultMemTypeId = $membership->membership_type_id;
           if ($membership->contact_id != $this->_contactID) {
+            $validMembership = FALSE;
             $employers = CRM_Contact_BAO_Relationship::getPermissionedEmployer($this->_userID);
-            if (array_key_exists($membership->contact_id, $employers)) {
+            if (!empty($employers) && array_key_exists($membership->contact_id, $employers)) {
               $this->_membershipContactID = $membership->contact_id;
               $this->assign('membershipContactID', $this->_membershipContactID);
               $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']);
+              $validMembership = TRUE;
+            } else {
+              $membershipType = new CRM_Member_BAO_MembershipType();
+              $membershipType->id = $membership->membership_type_id;
+              if ($membershipType->find(TRUE)) {
+                $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, $membershipType->relationship_type_id);
+                if (array_key_exists($membership->contact_id, $permContacts)) {
+                  $this->_membershipContactID = $membership->contact_id;
+                  $validMembership = TRUE;
+                }
+              }
             }
-            else {
+            if (!$validMembership) {
               CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert');
             }
           }
@@ -278,7 +284,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
       // also check for billing informatin
       // get the billing location type
-      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
+      $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
       // CRM-8108 remove ts around Billing location type
       //$this->_bltID = array_search( ts('Billing'),  $locationTypes );
       $this->_bltID = array_search('Billing', $locationTypes);
@@ -553,8 +559,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
             $this->_params[$v] = $frequencyUnits[$this->_params[$v]];
           }
         }
-        if ($v == "amount") {
-          $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], ' ');
+        if ($v == "amount" && $this->_params[$v] === 0) {
+          $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], NULL, NULL, TRUE);
         }
         $this->assign($v, $this->_params[$v]);
       }