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