CRM-21771: error when viewing event registration with linked contribution
[civicrm-core.git] / CRM / Utils / Request.php
index 2748e07d7df8e2c16d361009ff96d2e1a6f7ae9e..ad6bf094e593f233cd3732b32a072ca3a282c715 100644 (file)
@@ -79,17 +79,15 @@ class CRM_Utils_Request {
    *   Default value of the variable if not present.
    * @param string $method
    *   Where to look for the variable - 'GET', 'POST' or 'REQUEST'.
+   * @param bool $isThrowException
+   *   Should a an exception be thrown rather than a fatal.
    *
    * @return mixed
    *   The value of the variable
+   *
+   * @throws \CRM_Core_Exception
    */
-  public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST') {
-
-    // hack to detect stuff not yet converted to new style
-    if (!is_string($type)) {
-      CRM_Core_Error::backtrace();
-      CRM_Core_Error::fatal(ts("Please convert retrieve call to use new function signature"));
-    }
+  public static function retrieve($name, $type, &$store = NULL, $abort = FALSE, $default = NULL, $method = 'REQUEST', $isThrowException = FALSE) {
 
     $value = NULL;
     switch ($method) {
@@ -117,6 +115,9 @@ class CRM_Utils_Request {
     }
 
     if (!isset($value) && $abort) {
+      if ($isThrowException) {
+        throw new CRM_Core_Exception(ts("Could not find valid value for %1", array(1 => $name)));
+      }
       CRM_Core_Error::fatal(ts("Could not find valid value for %1", array(1 => $name)));
     }
 
@@ -145,7 +146,7 @@ class CRM_Utils_Request {
    * @return mixed
    *    The value of the variable
    */
-  public static function getValue($name, $method) {
+  protected static function getValue($name, $method) {
     if (isset($method[$name])) {
       return $method[$name];
     }
@@ -165,6 +166,10 @@ class CRM_Utils_Request {
   }
 
   /**
+   * @deprecated
+   *
+   * We should use a function that checks url values.
+   *
    * This is a replacement for $_REQUEST which includes $_GET/$_POST
    * but excludes $_COOKIE / $_ENV / $_SERVER.
    *
@@ -186,4 +191,32 @@ class CRM_Utils_Request {
     return $result;
   }
 
+  /**
+   * Retrieve a variable from the http request.
+   *
+   * @param string $name
+   *   Name of the variable to be retrieved.
+   * @param string $type
+   *   Type of the variable (see CRM_Utils_Type for details).
+   *   Most common options are:
+   *   - 'Integer'
+   *   - 'Positive'
+   *   - 'CommaSeparatedIntegers'
+   *   - 'Boolean'
+   *   - 'String'
+   *
+   * @param mixed $defaultValue
+   *   Default value of the variable if not present.
+   * @param bool $isRequired
+   *   Is the variable required for this function to proceed without an exception.
+   * @param string $method
+   *   Where to look for the value - GET|POST|REQUEST
+   *
+   * @return mixed
+   */
+  public static function retrieveValue($name, $type, $defaultValue = NULL, $isRequired = FALSE, $method = 'REQUEST') {
+    $null = NULL;
+    return CRM_Utils_Request::retrieve((string) $name, (string) $type, $null, (bool) $isRequired, $defaultValue, $method, TRUE);
+  }
+
 }