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