Merge pull request #19017 from eileenmcnaughton/remove_recur
[civicrm-core.git] / CRM / Utils / API / AbstractFieldCoder.php
CommitLineData
a9396478
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
a9396478 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
a9396478 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
a9396478
TO
11
12/**
13 * Base class for writing API_Wrappers which generically manipulate the content
14 * of all fields (except for some black-listed skip-fields).
15 *
16 * @package CRM
ca5cec67 17 * @copyright CiviCRM LLC https://civicrm.org/licensing
a9396478
TO
18 */
19
20require_once 'api/Wrapper.php';
5bc392e6
EM
21
22/**
3d469574 23 * Class CRM_Utils_API_AbstractFieldCoder.
5bc392e6 24 */
a9396478
TO
25abstract class CRM_Utils_API_AbstractFieldCoder implements API_Wrapper {
26
27 /**
3d469574 28 * Get skipped fields.
29 *
30 * @return array<string>
31 * List of field names
a9396478
TO
32 */
33 public function getSkipFields() {
34 return NULL;
35 }
36
37 /**
3d469574 38 * Is field skipped.
39 *
a9396478 40 * @param string $fldName
e8e8f3ad 41 *
a6c01b45
CW
42 * @return bool
43 * TRUE if encoding should be skipped for this field
a9396478
TO
44 */
45 public function isSkippedField($fldName) {
46 $skipFields = $this->getSkipFields();
47 if ($skipFields === NULL) {
48 return FALSE;
49 }
589f0d9a
CW
50 // Strip extra numbers from custom fields e.g. custom_32_1 should be custom_32
51 if (strpos($fldName, 'custom_') === 0) {
52 list($fldName, $customId) = explode('_', $fldName);
53 $fldName .= '_' . $customId;
54 }
a9396478
TO
55
56 // Field should be skipped
57 if (in_array($fldName, $skipFields)) {
58 return TRUE;
59 }
60 // Field is multilingual and after cutting off _xx_YY should be skipped (CRM-7230)…
61 if ((preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $fldName) && in_array(substr($fldName, 0, -6), $skipFields))) {
62 return TRUE;
63 }
64 // Field can take multiple entries, eg. fieldName[1], fieldName[2], etc.
65 // We remove the index and check again if the fieldName in the list of skipped fields.
be2fb01f 66 $matches = [];
a9396478
TO
67 if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
68 return TRUE;
69 }
70
71 return FALSE;
72 }
73
74 /**
3d469574 75 * Going to filter the submitted values.
a9396478
TO
76 *
77 * @param array|string $values the field value from the API
78 */
6714d8d2 79 abstract public function encodeInput(&$values);
a9396478 80
5bc392e6 81 /**
3d469574 82 * Decode output.
83 *
84 * @param string $values
5bc392e6
EM
85 *
86 * @return mixed
87 */
6714d8d2 88 abstract public function decodeOutput(&$values);
a9396478
TO
89
90 /**
da2e61bf 91 * @inheritDoc
a9396478
TO
92 */
93 public function fromApiInput($apiRequest) {
94 $lowerAction = strtolower($apiRequest['action']);
be2fb01f 95 if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create'])) {
a9396478
TO
96 // note: 'getsingle', 'replace', 'update', and chaining all build on top of 'get'/'create'
97 foreach ($apiRequest['params'] as $key => $value) {
98 // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
99 // and don't apply to other skippable fields
100 if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
101 $this->encodeInput($apiRequest['params'][$key]);
102 }
103 }
104 }
105 elseif ($apiRequest['version'] == 3 && $lowerAction == 'setvalue') {
106 if (isset($apiRequest['params']['field']) && isset($apiRequest['params']['value'])) {
107 if (!$this->isSkippedField($apiRequest['params']['field'])) {
108 $this->encodeInput($apiRequest['params']['value']);
109 }
110 }
111 }
112 return $apiRequest;
113 }
114
115 /**
da2e61bf 116 * @inheritDoc
a9396478
TO
117 */
118 public function toApiOutput($apiRequest, $result) {
119 $lowerAction = strtolower($apiRequest['action']);
be2fb01f 120 if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create', 'setvalue', 'getquick'])) {
a9396478
TO
121 foreach ($result as $key => $value) {
122 // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
123 // and don't apply to other skippable fields
124 if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
125 $this->decodeOutput($result[$key]);
126 }
127 }
128 }
129 // setvalue?
130 return $result;
131 }
132
133 /**
77b97be7
EM
134 * @param $key
135 *
a9396478
TO
136 * @return bool
137 */
138 protected function isApiControlField($key) {
139 return (FALSE !== strpos($key, '.'));
140 }
96025800 141
a9396478 142}