CRM-13163 - APIv3 (*.create) - Add support for options.reload=1
[civicrm-core.git] / CRM / Utils / API / ReloadOption.php
CommitLineData
0e4d4398
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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/**
29 * Implement the "api.reload" option. This option can be used with "create" to
30 * force the API to reload a clean copy of the entity before returning the result.
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
45require_once 'api/Wrapper.php';
46class CRM_Utils_API_ReloadOption implements API_Wrapper {
47
48 /**
49 * @var null|'null'|'default'|'selected'
50 */
51 private $reloadMode = NULL;
52
53 /**
54 * {@inheritDoc}
55 */
56 public function fromApiInput($apiRequest) {
57 if ($apiRequest['action'] === 'create' && isset($apiRequest['params'], $apiRequest['params']['options'], $apiRequest['params']['options']['reload'])) {
58 $this->reloadMode = $apiRequest['params']['options']['reload'];
59 }
60 return $apiRequest;
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 public function toApiOutput($apiRequest, $result) {
67 if ($result['is_error']) {
68 return $result;
69 }
70 switch ($this->reloadMode) {
71 case NULL:
72 case '0':
73 case 'null':
74 return $result;
75
76 case '1':
77 case 'default':
78 $params = array(
79 'id' => $result['id'],
80 );
81 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
82 $result['values'][$result['id']] = array_merge($result['values'][$result['id']], $reloadResult['values'][$result['id']]);
83 return $result;
84
85 case 'selected':
86 $params = array(
87 'id' => $result['id'],
88 'return' => $this->pickReturnFields($apiRequest),
89 );
90 $reloadResult = civicrm_api3($apiRequest['entity'], 'get', $params);
91 $result['values'][$result['id']] = array_merge($result['values'][$result['id']], $reloadResult['values'][$result['id']]);
92 return $result;
93
94 default:
95 throw new API_Exception("Unknown reload mode: " . var_export($this->reloadMode, TRUE));
96 }
97 }
98
99 /**
100 * Identify the fields which should be returned
101 *
102 * @param $apiRequest
103 * @return array
104 */
105 public function pickReturnFields($apiRequest) {
106 $fields = civicrm_api3($apiRequest['entity'], 'getfields', array());
107 $returnKeys = array_intersect(
108 array_keys($apiRequest['params']),
109 array_keys($fields['values'])
110 );
111 return $returnKeys;
112 }
113}