CRM-13163 - CRM_Utils_API_ReloadOption - Switch to stateless operation
authorTim Otten <totten@civicrm.org>
Thu, 8 Aug 2013 04:28:21 +0000 (21:28 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 8 Aug 2013 04:29:52 +0000 (21:29 -0700)
----------------------------------------
* CRM-13163: hrjob: Display/edit dates with jQuery date picker
  http://issues.civicrm.org/jira/browse/CRM-13163

CRM/Utils/API/ReloadOption.php
api/api.php

index 45677c6fb8ee860a7ec789207c4ce6b81daf41f0..0c6600ad65b3b77be07a2f4896e9bbb43daede9e 100644 (file)
@@ -46,17 +46,24 @@ require_once 'api/Wrapper.php';
 class CRM_Utils_API_ReloadOption implements API_Wrapper {
 
   /**
-   * @var null|'null'|'default'|'selected'
+   * @var CRM_Utils_API_ReloadOption
    */
-  private $reloadMode = NULL;
+  private static $_singleton = NULL;
+
+  /**
+   * @return CRM_Utils_API_ReloadOption
+   */
+  public static function singleton() {
+    if (self::$_singleton === NULL) {
+      self::$_singleton = new CRM_Utils_API_ReloadOption();
+    }
+    return self::$_singleton;
+  }
 
   /**
    * {@inheritDoc}
    */
   public function fromApiInput($apiRequest) {
-    if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options'], $apiRequest['params']['options']['reload'])) {
-      $this->reloadMode = $apiRequest['params']['options']['reload'];
-    }
     return $apiRequest;
   }
 
@@ -67,7 +74,15 @@ class CRM_Utils_API_ReloadOption implements API_Wrapper {
     if ($result['is_error']) {
       return $result;
     }
-    switch ($this->reloadMode) {
+
+    if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options'], $apiRequest['params']['options']['reload'])) {
+      $reloadMode = $apiRequest['params']['options']['reload'];
+    }
+    else {
+      $reloadMode = NULL;
+    }
+
+    switch ($reloadMode) {
       case NULL:
       case '0':
       case 'null':
@@ -79,6 +94,9 @@ class CRM_Utils_API_ReloadOption implements API_Wrapper {
           'id' => $result['id'],
         );
         $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
+        if ($reloadResult['is_error']) {
+          throw new API_Exception($reloadResult['error_message']);
+        }
         $result['values'][$result['id']] = array_merge($result['values'][$result['id']], $reloadResult['values'][$result['id']]);
         return $result;
 
@@ -92,7 +110,7 @@ class CRM_Utils_API_ReloadOption implements API_Wrapper {
         return $result;
 
       default:
-        throw new API_Exception("Unknown reload mode: " . var_export($this->reloadMode, TRUE));
+        throw new API_Exception("Unknown reload mode");
     }
   }
 
index d339644e98bc69445b322a484cf3b1b488a7dfc8..2564b1898b9593ce151184fece57739bcc471b9f 100644 (file)
@@ -22,7 +22,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
   $apiWrappers = array(
     CRM_Utils_API_HTMLInputCoder::singleton(),
     CRM_Utils_API_NullOutputCoder::singleton(),
-    new CRM_Utils_API_ReloadOption(),
+    CRM_Utils_API_ReloadOption::singleton(),
   );
   try {
     require_once ('api/v3/utils.php');