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) {
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;
72 // Field should be skipped
73 if (in_array($fldName, $skipFields)) {
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))) {
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.
83 if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
91 * Going to filter the submitted values.
93 * @param array|string $values the field value from the API
95 public abstract function encodeInput(&$values);
100 * @param string $values
104 public abstract function decodeOutput(&$values);
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]);
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']);
134 public function toApiOutput($apiRequest, $result) {
135 $lowerAction = strtolower($apiRequest['action']);
136 if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create', 'setvalue', 'getquick'))) {
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]);
154 protected function isApiControlField($key) {
155 return (FALSE !== strpos($key, '.'));