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