Cleanup custom field handling... twice
[civicrm-core.git] / CRM / Utils / API / ReloadOption.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
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.
15 *
16 * @code
17 * $clean = civicrm_api('myentity', 'create', array(
18 * 'options' => array(
19 * 'reload' => 1
20 * ),
21 * ));
22 * @endcode
23 *
24 * @package CRM
25 * @copyright CiviCRM LLC https://civicrm.org/licensing
26 */
27
28 require_once 'api/Wrapper.php';
29
30 /**
31 * Class CRM_Utils_API_ReloadOption
32 */
33 class CRM_Utils_API_ReloadOption implements API_Wrapper {
34
35 /**
36 * @var CRM_Utils_API_ReloadOption
37 */
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 }
49
50 /**
51 * @inheritDoc
52 */
53 public function fromApiInput($apiRequest) {
54 return $apiRequest;
55 }
56
57 /**
58 * @inheritDoc
59 */
60 public function toApiOutput($apiRequest, $result) {
61 $reloadMode = NULL;
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'];
65 }
66 $id = (!empty($apiRequest['params']['sequential'])) ? 0 : $result['id'];
67 }
68
69 switch ($reloadMode) {
70 case NULL:
71 case '0':
72 case 'null':
73 case '':
74 return $result;
75
76 case '1':
77 case 'default':
78 $params = [
79 'id' => $result['id'],
80 ];
81 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
82 if ($reloadResult['is_error']) {
83 throw new API_Exception($reloadResult['error_message']);
84 }
85 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$result['id']]);
86 return $result;
87
88 case 'selected':
89 $params = [
90 'id' => $id,
91 'return' => $this->pickReturnFields($apiRequest),
92 ];
93 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
94 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$id]);
95 return $result;
96
97 default:
98 throw new API_Exception("Unknown reload mode " . $reloadMode);
99 }
100 }
101
102 /**
103 * Identify the fields which should be returned.
104 *
105 * @param $apiRequest
106 * @return array
107 */
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'])
113 );
114 return $returnKeys;
115 }
116
117 }