Merge pull request #12220 from civicrm/5.2
[civicrm-core.git] / CRM / Utils / API / AbstractFieldCoder.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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
67 // Field should be skipped
68 if (in_array($fldName, $skipFields)) {
69 return TRUE;
70 }
71 // Field is multilingual and after cutting off _xx_YY should be skipped (CRM-7230)…
72 if ((preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $fldName) && in_array(substr($fldName, 0, -6), $skipFields))) {
73 return TRUE;
74 }
75 // Field can take multiple entries, eg. fieldName[1], fieldName[2], etc.
76 // We remove the index and check again if the fieldName in the list of skipped fields.
77 $matches = array();
78 if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
79 return TRUE;
80 }
81
82 return FALSE;
83 }
84
85 /**
86 * Going to filter the submitted values.
87 *
88 * @param array|string $values the field value from the API
89 */
90 public abstract function encodeInput(&$values);
91
92 /**
93 * Decode output.
94 *
95 * @param string $values
96 *
97 * @return mixed
98 */
99 public abstract function decodeOutput(&$values);
100
101 /**
102 * @inheritDoc
103 */
104 public function fromApiInput($apiRequest) {
105 $lowerAction = strtolower($apiRequest['action']);
106 if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create'))) {
107 // note: 'getsingle', 'replace', 'update', and chaining all build on top of 'get'/'create'
108 foreach ($apiRequest['params'] as $key => $value) {
109 // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
110 // and don't apply to other skippable fields
111 if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
112 $this->encodeInput($apiRequest['params'][$key]);
113 }
114 }
115 }
116 elseif ($apiRequest['version'] == 3 && $lowerAction == 'setvalue') {
117 if (isset($apiRequest['params']['field']) && isset($apiRequest['params']['value'])) {
118 if (!$this->isSkippedField($apiRequest['params']['field'])) {
119 $this->encodeInput($apiRequest['params']['value']);
120 }
121 }
122 }
123 return $apiRequest;
124 }
125
126 /**
127 * @inheritDoc
128 */
129 public function toApiOutput($apiRequest, $result) {
130 $lowerAction = strtolower($apiRequest['action']);
131 if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create', 'setvalue', 'getquick'))) {
132 foreach ($result as $key => $value) {
133 // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
134 // and don't apply to other skippable fields
135 if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
136 $this->decodeOutput($result[$key]);
137 }
138 }
139 }
140 // setvalue?
141 return $result;
142 }
143
144 /**
145 * @param $key
146 *
147 * @return bool
148 */
149 protected function isApiControlField($key) {
150 return (FALSE !== strpos($key, '.'));
151 }
152
153 }