Merge pull request #12583 from omarabuhussein/dev/core#288
[civicrm-core.git] / CRM / Utils / API / AbstractFieldCoder.php
CommitLineData
a9396478
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
a9396478 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
a9396478
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 */
a9396478
TO
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
6b83d5bd 33 * @copyright CiviCRM LLC (c) 2004-2019
a9396478
TO
34 */
35
36require_once 'api/Wrapper.php';
5bc392e6
EM
37
38/**
3d469574 39 * Class CRM_Utils_API_AbstractFieldCoder.
5bc392e6 40 */
a9396478
TO
41abstract class CRM_Utils_API_AbstractFieldCoder implements API_Wrapper {
42
43 /**
3d469574 44 * Get skipped fields.
45 *
46 * @return array<string>
47 * List of field names
a9396478
TO
48 */
49 public function getSkipFields() {
50 return NULL;
51 }
52
53 /**
3d469574 54 * Is field skipped.
55 *
a9396478 56 * @param string $fldName
e8e8f3ad 57 *
a6c01b45
CW
58 * @return bool
59 * TRUE if encoding should be skipped for this field
a9396478
TO
60 */
61 public function isSkippedField($fldName) {
62 $skipFields = $this->getSkipFields();
63 if ($skipFields === NULL) {
64 return FALSE;
65 }
589f0d9a
CW
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 }
a9396478
TO
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 = array();
83 if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
84 return TRUE;
85 }
86
87 return FALSE;
88 }
89
90 /**
3d469574 91 * Going to filter the submitted values.
a9396478
TO
92 *
93 * @param array|string $values the field value from the API
94 */
95 public abstract function encodeInput(&$values);
96
5bc392e6 97 /**
3d469574 98 * Decode output.
99 *
100 * @param string $values
5bc392e6
EM
101 *
102 * @return mixed
103 */
a9396478
TO
104 public abstract function decodeOutput(&$values);
105
106 /**
da2e61bf 107 * @inheritDoc
a9396478
TO
108 */
109 public function fromApiInput($apiRequest) {
110 $lowerAction = strtolower($apiRequest['action']);
111 if ($apiRequest['version'] == 3 && in_array($lowerAction, array('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 /**
da2e61bf 132 * @inheritDoc
a9396478
TO
133 */
134 public function toApiOutput($apiRequest, $result) {
135 $lowerAction = strtolower($apiRequest['action']);
91f70e70 136 if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create', 'setvalue', 'getquick'))) {
a9396478
TO
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 /**
77b97be7
EM
150 * @param $key
151 *
a9396478
TO
152 * @return bool
153 */
154 protected function isApiControlField($key) {
155 return (FALSE !== strpos($key, '.'));
156 }
96025800 157
a9396478 158}