CRM-13148 Support use of cid=0 and cid=NNN for online contribution pages.
authorDave Greenberg <dave@civicrm.org>
Sat, 3 Aug 2013 18:28:01 +0000 (11:28 -0700)
committerDave Greenberg <dave@civicrm.org>
Sat, 3 Aug 2013 18:28:01 +0000 (11:28 -0700)
----------------------------------------
* CRM-13148:
  http://issues.civicrm.org/jira/browse/CRM-13148

CRM/Contribute/Form/Contribution/Confirm.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php
CRM/Core/Form.php
CRM/Event/Form/Registration.php

index a7a20bbbd393781b23edf1f7d2b24987653ab85a..1c8d3a901cee314f76455d5d14c200a5ee401745 100644 (file)
@@ -596,7 +596,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
    */
   public function postProcess() {
     $config = CRM_Core_Config::singleton();
-    $contactID = $this->_userID;
+    $contactID = $this->getContactID();
 
     // add a description field at the very beginning
     $this->_params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']);
@@ -751,7 +751,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       }
     }
 
-    if (!isset($contactID)) {
+    if (empty($contactID)) {
       $dupeParams = $params;
       if (CRM_Utils_Array::value('onbehalf', $dupeParams)) {
         unset($dupeParams['onbehalf']);
index 50d0875aae56cc9aec558b5a17fa3dd445625963..cb0ded878ad8717f9ccf6007adcc3996958478d5 100644 (file)
@@ -79,18 +79,10 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     $this->assign('isConfirmEnabled', CRM_Utils_Array::value('is_confirm_enabled', $this->_values));
 
     // make sure we have right permission to edit this user
-    $csContactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, $this->_userID);
+    $csContactID = $this->getContactID();
     $reset       = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
     $mainDisplay = CRM_Utils_Request::retrieve('_qf_Main_display', 'Boolean', CRM_Core_DAO::$_nullObject);
 
-    if ($csContactID != $this->_userID) {
-      if (CRM_Contact_BAO_Contact_Permission::validateChecksumContact($csContactID, $this)) {
-        $session = CRM_Core_Session::singleton();
-        $session->set('userID', $csContactID);
-        $this->_userID = $csContactID;
-      }
-    }
-
     if ($reset) {
       $this->assign('reset', $reset);
     }
@@ -174,9 +166,9 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
   function setDefaultValues() {
     // check if the user is registered and we have a contact ID
-    $contactID = $this->_userID;
+    $contactID = $this->getContactID();
 
-    if ($contactID) {
+    if (!empty($contactID)) {
       $fields = array();
       $removeCustomFieldTypes = array('Contribution', 'Membership');
       $contribFields = CRM_Contribute_BAO_Contribution::getContributionFields();
@@ -1095,6 +1087,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
     // get the submitted form values.
     $params = $this->controller->exportValues($this->_name);
+
     if (CRM_Utils_Array::value('priceSetId', $params)) {
       $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
       $formValue = array();
index df47a2b74b252b66ca87de4e128662cfec894c3c..c45e1d5a984944174213df2da7046558feae6492 100644 (file)
@@ -626,7 +626,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     $stateCountryMap = array();
 
     if ($id) {
-      $contactID = $this->_userID;
+      $contactID = $this->getContactID();
 
       // we don't allow conflicting fields to be
       // configured via profile - CRM 2100
index d2ec736e677292ac9ddc17adcd91c3a80be20ddf..f0d60b7c763bbbee570dfbc52488f414bfb90009 100644 (file)
@@ -1294,5 +1294,49 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       }
     }
   }
+
+/**
+ * Get contact if for a form object. Prioritise
+ *  - cid in URL if 0 (on behalf on someoneelse)
+ *      (@todo consider setting a variable if onbehalf for clarity of downstream 'if's
+ *  - logged in user id if it matches the one in the cid in the URL
+ *  - contact id validated from a checksum from a checksum
+ *  - cid from the url if the caller has ACL permission to view
+ *  - fallback is logged in user (or ? NULL if no logged in user) (@todo wouldn't 0 be more intuitive?)
+ *
+ * @return Ambigous <mixed, NULL, value, unknown, array, number>|unknown
+ */
+  function getContactID() {
+    $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
+
+    // force to ignore the authenticated user
+    if ($tempID === '0') {
+      return $tempID;
+    }
+
+    // check if the user is logged in and has a contact ID
+    $session = CRM_Core_Session::singleton();
+    $userID = $session->get('userID');
+
+    if ($tempID == $userID) {
+      return $userID;
+    }
+
+    //check if this is a checksum authentication
+    $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
+    if ($userChecksum) {
+      //check for anonymous user.
+      $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum);
+      if ($validUser) {
+        return $tempID;
+      }
+    }
+    // check if user has permission, CRM-12062
+    else if ($tempID && CRM_Contact_BAO_Contact_Permission::allow($tempID)) {
+      return $tempID;
+    }
+
+    return $userID;
+  }
 }
 
index a6f08e5d72eebff4d174e1ab9b2c5b46c8a4a42c..12a1337976e7fad8aa55448c50e7d6a75dd0e8ad 100644 (file)
@@ -646,7 +646,7 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
           //have been skip the additional participant.
           if ($button == 'skip') {
             $field['is_required'] = FALSE;
-          }          
+          }
           // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
           elseif ($field['add_captcha']  && !$contactID) {
             // only add captcha for first page
@@ -1165,42 +1165,12 @@ WHERE  v.option_group_id = g.id
     return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
   }
 
-  function getContactID() {
-    $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
-
-    // force to ignore the authenticated user
-    if ($tempID === '0') {
-      return $tempID;
-    }
-
-    // check if the user is logged in and has a contact ID
-    $session = CRM_Core_Session::singleton();
-    $userID = $session->get('userID');
-
-    if ($tempID == $userID) {
-      return $userID;
-    }
-
-    //check if this is a checksum authentication
-    $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
-    if ($userChecksum) {
-      //check for anonymous user.
-      $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum);
-      if ($validUser) {
-        return $tempID;
-      }
-    }
-    // check if user has permission, CRM-12062
-    else if ($tempID && CRM_Contact_BAO_Contact_Permission::allow($tempID)) {
-      return $tempID;
-    }
-
-    return $userID;
-  }
-
-  /* Validate price set submitted params for price option limit,
+  /**
+   * Validate price set submitted params for price option limit,
    * as well as user should select at least one price field option.
-   *
+   * @param unknown_type $form
+   * @param unknown_type $params
+   * @return multitype:|Ambigous <multitype:, string, string>
    */
   static function validatePriceSet(&$form, $params) {
     $errors = array();