3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Implement the "reload" option. This option can be used with "create" to force
14 * the API to reload a clean copy of the entity before returning the result.
17 * $clean = civicrm_api('myentity', 'create', array(
25 * @copyright CiviCRM LLC https://civicrm.org/licensing
28 require_once 'api/Wrapper.php';
31 * Class CRM_Utils_API_ReloadOption
33 class CRM_Utils_API_ReloadOption
implements API_Wrapper
{
36 * @var CRM_Utils_API_ReloadOption
38 private static $_singleton = NULL;
41 * @return CRM_Utils_API_ReloadOption
43 public static function singleton() {
44 if (self
::$_singleton === NULL) {
45 self
::$_singleton = new CRM_Utils_API_ReloadOption();
47 return self
::$_singleton;
53 public function fromApiInput($apiRequest) {
60 public function toApiOutput($apiRequest, $result) {
62 if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options']) && is_array($apiRequest['params']['options']) && isset($apiRequest['params']['options']['reload'])) {
63 if (empty($result['is_error'])) {
64 $reloadMode = $apiRequest['params']['options']['reload'];
66 $id = (!empty($apiRequest['params']['sequential'])) ?
0 : $result['id'];
69 switch ($reloadMode) {
79 'id' => $result['id'],
81 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
82 if ($reloadResult['is_error']) {
83 throw new API_Exception($reloadResult['error_message']);
85 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$result['id']]);
91 'return' => $this->pickReturnFields($apiRequest),
93 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
94 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$id]);
98 throw new API_Exception("Unknown reload mode " . $reloadMode);
103 * Identify the fields which should be returned.
108 public function pickReturnFields($apiRequest) {
109 $fields = civicrm_api3($apiRequest['entity'], 'getfields', []);
110 $returnKeys = array_intersect(
111 array_keys($apiRequest['params']),
112 array_keys($fields['values'])