Merge pull request #15666 from revati90/shared_address
[civicrm-core.git] / CRM / Utils / API / AbstractFieldCoder.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 * Base class for writing API_Wrappers which generically manipulate the content
30 * of all fields (except for some black-listed skip-fields).
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2020
34 */
35
36 require_once 'api/Wrapper.php';
37
38 /**
39 * Class CRM_Utils_API_AbstractFieldCoder.
40 */
41 abstract class CRM_Utils_API_AbstractFieldCoder implements API_Wrapper {
42
43 /**
44 * Get skipped fields.
45 *
46 * @return array<string>
47 * List of field names
48 */
49 public function getSkipFields() {
50 return NULL;
51 }
52
53 /**
54 * Is field skipped.
55 *
56 * @param string $fldName
57 *
58 * @return bool
59 * TRUE if encoding should be skipped for this field
60 */
61 public function isSkippedField($fldName) {
62 $skipFields = $this->getSkipFields();
63 if ($skipFields === NULL) {
64 return FALSE;
65 }
66 // Strip extra numbers from custom fields e.g. custom_32_1 should be custom_32
67 if (strpos($fldName, 'custom_') === 0) {
68 list($fldName, $customId) = explode('_', $fldName);
69 $fldName .= '_' . $customId;
70 }
71
72 // Field should be skipped
73 if (in_array($fldName, $skipFields)) {
74 return TRUE;
75 }
76 // Field is multilingual and after cutting off _xx_YY should be skipped (CRM-7230)…
77 if ((preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $fldName) && in_array(substr($fldName, 0, -6), $skipFields))) {
78 return TRUE;
79 }
80 // Field can take multiple entries, eg. fieldName[1], fieldName[2], etc.
81 // We remove the index and check again if the fieldName in the list of skipped fields.
82 $matches = [];
83 if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
84 return TRUE;
85 }
86
87 return FALSE;
88 }
89
90 /**
91 * Going to filter the submitted values.
92 *
93 * @param array|string $values the field value from the API
94 */
95 abstract public function encodeInput(&$values);
96
97 /**
98 * Decode output.
99 *
100 * @param string $values
101 *
102 * @return mixed
103 */
104 abstract public function decodeOutput(&$values);
105
106 /**
107 * @inheritDoc
108 */
109 public function fromApiInput($apiRequest) {
110 $lowerAction = strtolower($apiRequest['action']);
111 if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create'])) {
112 // note: 'getsingle', 'replace', 'update', and chaining all build on top of 'get'/'create'
113 foreach ($apiRequest['params'] as $key => $value) {
114 // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
115 // and don't apply to other skippable fields
116 if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
117 $this->encodeInput($apiRequest['params'][$key]);
118 }
119 }
120 }
121 elseif ($apiRequest['version'] == 3 && $lowerAction == 'setvalue') {
122 if (isset($apiRequest['params']['field']) && isset($apiRequest['params']['value'])) {
123 if (!$this->isSkippedField($apiRequest['params']['field'])) {
124 $this->encodeInput($apiRequest['params']['value']);
125 }
126 }
127 }
128 return $apiRequest;
129 }
130
131 /**
132 * @inheritDoc
133 */
134 public function toApiOutput($apiRequest, $result) {
135 $lowerAction = strtolower($apiRequest['action']);
136 if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create', 'setvalue', 'getquick'])) {
137 foreach ($result as $key => $value) {
138 // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
139 // and don't apply to other skippable fields
140 if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
141 $this->decodeOutput($result[$key]);
142 }
143 }
144 }
145 // setvalue?
146 return $result;
147 }
148
149 /**
150 * @param $key
151 *
152 * @return bool
153 */
154 protected function isApiControlField($key) {
155 return (FALSE !== strpos($key, '.'));
156 }
157
158 }