Merge pull request #19017 from eileenmcnaughton/remove_recur
[civicrm-core.git] / CRM / Utils / API / ReloadOption.php
CommitLineData
0e4d4398
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
0e4d4398 5 | |
bc77d7c0
TO
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 |
0e4d4398 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
0e4d4398
TO
11
12/**
dfd70ed4
TO
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.
0e4d4398 15 *
0b882a86 16 * ```
0e4d4398
TO
17 * $clean = civicrm_api('myentity', 'create', array(
18 * 'options' => array(
19 * 'reload' => 1
20 * ),
21 * ));
0b882a86 22 * ```
0e4d4398
TO
23 *
24 * @package CRM
ca5cec67 25 * @copyright CiviCRM LLC https://civicrm.org/licensing
0e4d4398
TO
26 */
27
28require_once 'api/Wrapper.php';
5bc392e6
EM
29
30/**
31 * Class CRM_Utils_API_ReloadOption
32 */
0e4d4398
TO
33class CRM_Utils_API_ReloadOption implements API_Wrapper {
34
35 /**
ac830223 36 * @var CRM_Utils_API_ReloadOption
0e4d4398 37 */
ac830223
TO
38 private static $_singleton = NULL;
39
40 /**
41 * @return CRM_Utils_API_ReloadOption
42 */
43 public static function singleton() {
44 if (self::$_singleton === NULL) {
45 self::$_singleton = new CRM_Utils_API_ReloadOption();
46 }
47 return self::$_singleton;
48 }
0e4d4398
TO
49
50 /**
da2e61bf 51 * @inheritDoc
0e4d4398
TO
52 */
53 public function fromApiInput($apiRequest) {
0e4d4398
TO
54 return $apiRequest;
55 }
56
57 /**
da2e61bf 58 * @inheritDoc
0e4d4398
TO
59 */
60 public function toApiOutput($apiRequest, $result) {
5dc318b1 61 $reloadMode = NULL;
e5254c8a 62 if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options']) && is_array($apiRequest['params']['options']) && isset($apiRequest['params']['options']['reload'])) {
de6c59ca 63 if (empty($result['is_error'])) {
5dc318b1
TO
64 $reloadMode = $apiRequest['params']['options']['reload'];
65 }
351b0f6b 66 $id = (!empty($apiRequest['params']['sequential'])) ? 0 : $result['id'];
ac830223
TO
67 }
68
69 switch ($reloadMode) {
0e4d4398
TO
70 case NULL:
71 case '0':
72 case 'null':
1788e919 73 case '':
0e4d4398
TO
74 return $result;
75
76 case '1':
77 case 'default':
be2fb01f 78 $params = [
0e4d4398 79 'id' => $result['id'],
be2fb01f 80 ];
0e4d4398 81 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
ac830223
TO
82 if ($reloadResult['is_error']) {
83 throw new API_Exception($reloadResult['error_message']);
84 }
351b0f6b 85 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$result['id']]);
0e4d4398
TO
86 return $result;
87
88 case 'selected':
be2fb01f 89 $params = [
351b0f6b 90 'id' => $id,
0e4d4398 91 'return' => $this->pickReturnFields($apiRequest),
be2fb01f 92 ];
0e4d4398 93 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
351b0f6b 94 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$id]);
0e4d4398
TO
95 return $result;
96
97 default:
1788e919 98 throw new API_Exception("Unknown reload mode " . $reloadMode);
0e4d4398
TO
99 }
100 }
101
102 /**
fe482240 103 * Identify the fields which should be returned.
0e4d4398
TO
104 *
105 * @param $apiRequest
106 * @return array
107 */
108 public function pickReturnFields($apiRequest) {
be2fb01f 109 $fields = civicrm_api3($apiRequest['entity'], 'getfields', []);
0e4d4398
TO
110 $returnKeys = array_intersect(
111 array_keys($apiRequest['params']),
112 array_keys($fields['values'])
113 );
114 return $returnKeys;
115 }
96025800 116
0e4d4398 117}