3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * Base class for writing API_Wrappers which generically manipulate the content
14 * of all fields (except for some black-listed skip-fields).
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 require_once 'api/Wrapper.php';
23 * Class CRM_Utils_API_AbstractFieldCoder.
25 abstract class CRM_Utils_API_AbstractFieldCoder
implements API_Wrapper
{
30 * @return array<string>
33 public function getSkipFields() {
40 * @param string $fldName
43 * TRUE if encoding should be skipped for this field
45 public function isSkippedField($fldName) {
46 $skipFields = $this->getSkipFields();
47 if ($skipFields === NULL) {
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;
56 // Field should be skipped
57 if (in_array($fldName, $skipFields)) {
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))) {
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.
67 if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
75 * Going to filter the submitted values.
77 * @param array|string $values the field value from the API
79 abstract public function encodeInput(&$values);
84 * @param string $values
88 abstract public function decodeOutput(&$values);
93 public function fromApiInput($apiRequest) {
94 $lowerAction = strtolower($apiRequest['action']);
95 if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create'])) {
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]);
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']);
118 public function toApiOutput($apiRequest, $result) {
119 $lowerAction = strtolower($apiRequest['action']);
120 if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create', 'setvalue', 'getquick'])) {
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]);
138 protected function isApiControlField($key) {
139 return (FALSE !== strpos($key, '.'));