Commit | Line | Data |
---|---|---|
0e4d4398 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
0e4d4398 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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 | |
41 | * @copyright CiviCRM LLC (c) 2004-2013 | |
42 | * $Id$ | |
43 | */ | |
44 | ||
45 | require_once 'api/Wrapper.php'; | |
46 | class CRM_Utils_API_ReloadOption implements API_Wrapper { | |
47 | ||
48 | /** | |
ac830223 | 49 | * @var CRM_Utils_API_ReloadOption |
0e4d4398 | 50 | */ |
ac830223 TO |
51 | private static $_singleton = NULL; |
52 | ||
53 | /** | |
54 | * @return CRM_Utils_API_ReloadOption | |
55 | */ | |
56 | public static function singleton() { | |
57 | if (self::$_singleton === NULL) { | |
58 | self::$_singleton = new CRM_Utils_API_ReloadOption(); | |
59 | } | |
60 | return self::$_singleton; | |
61 | } | |
0e4d4398 TO |
62 | |
63 | /** | |
64 | * {@inheritDoc} | |
65 | */ | |
66 | public function fromApiInput($apiRequest) { | |
0e4d4398 TO |
67 | return $apiRequest; |
68 | } | |
69 | ||
70 | /** | |
71 | * {@inheritDoc} | |
72 | */ | |
73 | public function toApiOutput($apiRequest, $result) { | |
5dc318b1 | 74 | $reloadMode = NULL; |
ac830223 | 75 | if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options'], $apiRequest['params']['options']['reload'])) { |
5dc318b1 TO |
76 | if (!CRM_Utils_Array::value('is_error', $result, FALSE)) { |
77 | $reloadMode = $apiRequest['params']['options']['reload']; | |
78 | } | |
ac830223 TO |
79 | } |
80 | ||
81 | switch ($reloadMode) { | |
0e4d4398 TO |
82 | case NULL: |
83 | case '0': | |
84 | case 'null': | |
85 | return $result; | |
86 | ||
87 | case '1': | |
88 | case 'default': | |
89 | $params = array( | |
90 | 'id' => $result['id'], | |
91 | ); | |
92 | $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params); | |
ac830223 TO |
93 | if ($reloadResult['is_error']) { |
94 | throw new API_Exception($reloadResult['error_message']); | |
95 | } | |
0e4d4398 TO |
96 | $result['values'][$result['id']] = array_merge($result['values'][$result['id']], $reloadResult['values'][$result['id']]); |
97 | return $result; | |
98 | ||
99 | case 'selected': | |
100 | $params = array( | |
101 | 'id' => $result['id'], | |
102 | 'return' => $this->pickReturnFields($apiRequest), | |
103 | ); | |
104 | $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params); | |
105 | $result['values'][$result['id']] = array_merge($result['values'][$result['id']], $reloadResult['values'][$result['id']]); | |
106 | return $result; | |
107 | ||
108 | default: | |
ac830223 | 109 | throw new API_Exception("Unknown reload mode"); |
0e4d4398 TO |
110 | } |
111 | } | |
112 | ||
113 | /** | |
114 | * Identify the fields which should be returned | |
115 | * | |
116 | * @param $apiRequest | |
117 | * @return array | |
118 | */ | |
119 | public function pickReturnFields($apiRequest) { | |
120 | $fields = civicrm_api3($apiRequest['entity'], 'getfields', array()); | |
121 | $returnKeys = array_intersect( | |
122 | array_keys($apiRequest['params']), | |
123 | array_keys($fields['values']) | |
124 | ); | |
125 | return $returnKeys; | |
126 | } | |
127 | } |