3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Base class for writing API_Wrappers which generically manipulate the content
30 * of all fields (except for some black-listed skip-fields).
33 * @copyright CiviCRM LLC (c) 2004-2018
36 require_once 'api/Wrapper.php';
39 * Class CRM_Utils_API_AbstractFieldCoder.
41 abstract class CRM_Utils_API_AbstractFieldCoder
implements API_Wrapper
{
46 * @return array<string>
49 public function getSkipFields() {
56 * @param string $fldName
59 * TRUE if encoding should be skipped for this field
61 public function isSkippedField($fldName) {
62 $skipFields = $this->getSkipFields();
63 if ($skipFields === NULL) {
67 // Field should be skipped
68 if (in_array($fldName, $skipFields)) {
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))) {
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.
78 if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
86 * Going to filter the submitted values.
88 * @param array|string $values the field value from the API
90 public abstract function encodeInput(&$values);
95 * @param string $values
99 public abstract function decodeOutput(&$values);
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]);
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']);
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]);
149 protected function isApiControlField($key) {
150 return (FALSE !== strpos($key, '.'));