Commit | Line | Data |
---|---|---|
0e4d4398 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
0e4d4398 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
0e4d4398 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
0e4d4398 TO |
27 | |
28 | /** | |
dfd70ed4 TO |
29 | * Implement the "reload" option. This option can be used with "create" to force |
30 | * the API to reload a clean copy of the entity before returning the result. | |
0e4d4398 TO |
31 | * |
32 | * @code | |
33 | * $clean = civicrm_api('myentity', 'create', array( | |
34 | * 'options' => array( | |
35 | * 'reload' => 1 | |
36 | * ), | |
37 | * )); | |
38 | * @endcode | |
39 | * | |
40 | * @package CRM | |
6b83d5bd | 41 | * @copyright CiviCRM LLC (c) 2004-2019 |
0e4d4398 TO |
42 | */ |
43 | ||
44 | require_once 'api/Wrapper.php'; | |
5bc392e6 EM |
45 | |
46 | /** | |
47 | * Class CRM_Utils_API_ReloadOption | |
48 | */ | |
0e4d4398 TO |
49 | class CRM_Utils_API_ReloadOption implements API_Wrapper { |
50 | ||
51 | /** | |
ac830223 | 52 | * @var CRM_Utils_API_ReloadOption |
0e4d4398 | 53 | */ |
ac830223 TO |
54 | private static $_singleton = NULL; |
55 | ||
56 | /** | |
57 | * @return CRM_Utils_API_ReloadOption | |
58 | */ | |
59 | public static function singleton() { | |
60 | if (self::$_singleton === NULL) { | |
61 | self::$_singleton = new CRM_Utils_API_ReloadOption(); | |
62 | } | |
63 | return self::$_singleton; | |
64 | } | |
0e4d4398 TO |
65 | |
66 | /** | |
da2e61bf | 67 | * @inheritDoc |
0e4d4398 TO |
68 | */ |
69 | public function fromApiInput($apiRequest) { | |
0e4d4398 TO |
70 | return $apiRequest; |
71 | } | |
72 | ||
73 | /** | |
da2e61bf | 74 | * @inheritDoc |
0e4d4398 TO |
75 | */ |
76 | public function toApiOutput($apiRequest, $result) { | |
5dc318b1 | 77 | $reloadMode = NULL; |
e5254c8a | 78 | if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options']) && is_array($apiRequest['params']['options']) && isset($apiRequest['params']['options']['reload'])) { |
de6c59ca | 79 | if (empty($result['is_error'])) { |
5dc318b1 TO |
80 | $reloadMode = $apiRequest['params']['options']['reload']; |
81 | } | |
351b0f6b | 82 | $id = (!empty($apiRequest['params']['sequential'])) ? 0 : $result['id']; |
ac830223 TO |
83 | } |
84 | ||
85 | switch ($reloadMode) { | |
0e4d4398 TO |
86 | case NULL: |
87 | case '0': | |
88 | case 'null': | |
1788e919 | 89 | case '': |
0e4d4398 TO |
90 | return $result; |
91 | ||
92 | case '1': | |
93 | case 'default': | |
be2fb01f | 94 | $params = [ |
0e4d4398 | 95 | 'id' => $result['id'], |
be2fb01f | 96 | ]; |
0e4d4398 | 97 | $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params); |
ac830223 TO |
98 | if ($reloadResult['is_error']) { |
99 | throw new API_Exception($reloadResult['error_message']); | |
100 | } | |
351b0f6b | 101 | $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$result['id']]); |
0e4d4398 TO |
102 | return $result; |
103 | ||
104 | case 'selected': | |
be2fb01f | 105 | $params = [ |
351b0f6b | 106 | 'id' => $id, |
0e4d4398 | 107 | 'return' => $this->pickReturnFields($apiRequest), |
be2fb01f | 108 | ]; |
0e4d4398 | 109 | $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params); |
351b0f6b | 110 | $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$id]); |
0e4d4398 TO |
111 | return $result; |
112 | ||
113 | default: | |
1788e919 | 114 | throw new API_Exception("Unknown reload mode " . $reloadMode); |
0e4d4398 TO |
115 | } |
116 | } | |
117 | ||
118 | /** | |
fe482240 | 119 | * Identify the fields which should be returned. |
0e4d4398 TO |
120 | * |
121 | * @param $apiRequest | |
122 | * @return array | |
123 | */ | |
124 | public function pickReturnFields($apiRequest) { | |
be2fb01f | 125 | $fields = civicrm_api3($apiRequest['entity'], 'getfields', []); |
0e4d4398 TO |
126 | $returnKeys = array_intersect( |
127 | array_keys($apiRequest['params']), | |
128 | array_keys($fields['values']) | |
129 | ); | |
130 | return $returnKeys; | |
131 | } | |
96025800 | 132 | |
0e4d4398 | 133 | } |