3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
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.
33 * $clean = civicrm_api('myentity', 'create', array(
41 * @copyright CiviCRM LLC (c) 2004-2015
44 require_once 'api/Wrapper.php';
47 * Class CRM_Utils_API_ReloadOption
49 class CRM_Utils_API_ReloadOption
implements API_Wrapper
{
52 * @var CRM_Utils_API_ReloadOption
54 private static $_singleton = NULL;
57 * @return CRM_Utils_API_ReloadOption
59 public static function singleton() {
60 if (self
::$_singleton === NULL) {
61 self
::$_singleton = new CRM_Utils_API_ReloadOption();
63 return self
::$_singleton;
69 public function fromApiInput($apiRequest) {
76 public function toApiOutput($apiRequest, $result) {
78 if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options']) && is_array($apiRequest['params']['options']) && isset($apiRequest['params']['options']['reload'])) {
79 if (!CRM_Utils_Array
::value('is_error', $result, FALSE)) {
80 $reloadMode = $apiRequest['params']['options']['reload'];
82 $id = (!empty($apiRequest['params']['sequential'])) ?
0 : $result['id'];
85 switch ($reloadMode) {
95 'id' => $result['id'],
97 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
98 if ($reloadResult['is_error']) {
99 throw new API_Exception($reloadResult['error_message']);
101 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$result['id']]);
107 'return' => $this->pickReturnFields($apiRequest),
109 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
110 $result['values'][$id] = array_merge($result['values'][$id], $reloadResult['values'][$id]);
114 throw new API_Exception("Unknown reload mode " . $reloadMode);
119 * Identify the fields which should be returned.
124 public function pickReturnFields($apiRequest) {
125 $fields = civicrm_api3($apiRequest['entity'], 'getfields', array());
126 $returnKeys = array_intersect(
127 array_keys($apiRequest['params']),
128 array_keys($fields['values'])