Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 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 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'HTML/QuickForm/Renderer/ArraySmarty.php'; | |
37 | ||
38 | /** | |
3c7fd868 | 39 | * Customize QF output to meet our specific requirements |
6a488035 TO |
40 | */ |
41 | class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { | |
42 | ||
43 | /** | |
44 | * We only need one instance of this object. So we use the singleton | |
45 | * pattern and cache the instance in this variable | |
46 | * | |
47 | * @var object | |
6a488035 TO |
48 | */ |
49 | static private $_singleton = NULL; | |
50 | ||
51 | /** | |
fe482240 | 52 | * The converter from array size to css class. |
6a488035 TO |
53 | * |
54 | * @var array | |
6a488035 | 55 | */ |
518fa0ee | 56 | public static $_sizeMapper = [ |
6a488035 TO |
57 | 2 => 'two', |
58 | 4 => 'four', | |
59 | 6 => 'six', | |
60 | 8 => 'eight', | |
61 | 12 => 'twelve', | |
62 | 20 => 'medium', | |
63 | 30 => 'big', | |
64 | 45 => 'huge', | |
be2fb01f | 65 | ]; |
6a488035 TO |
66 | |
67 | /** | |
fe482240 | 68 | * Constructor. |
95ea96be | 69 | */ |
317fceb4 | 70 | public function __construct() { |
6a488035 TO |
71 | $template = CRM_Core_Smarty::singleton(); |
72 | parent::__construct($template); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Static instance provider. | |
77 | * | |
78 | * Method providing static instance of as in Singleton pattern. | |
79 | */ | |
00be9182 | 80 | public static function &singleton() { |
6a488035 TO |
81 | if (!isset(self::$_singleton)) { |
82 | self::$_singleton = new CRM_Core_Form_Renderer(); | |
83 | } | |
84 | return self::$_singleton; | |
85 | } | |
86 | ||
87 | /** | |
fe482240 | 88 | * Creates an array representing an element containing. |
6a488035 TO |
89 | * the key for storing this. We allow the parent to do most of the |
90 | * work, but then we add some CiviCRM specific enhancements to | |
91 | * make the html compliant with our css etc | |
92 | * | |
6a488035 | 93 | * |
3c7fd868 CW |
94 | * @param HTML_QuickForm_element $element |
95 | * @param bool $required | |
96 | * Whether an element is required. | |
97 | * @param string $error | |
98 | * Error associated with the element. | |
6a488035 TO |
99 | * |
100 | * @return array | |
101 | */ | |
00be9182 | 102 | public function _elementToArray(&$element, $required, $error) { |
6a488035 TO |
103 | self::updateAttributes($element, $required, $error); |
104 | ||
105 | $el = parent::_elementToArray($element, $required, $error); | |
106 | ||
107 | // add label html | |
108 | if (!empty($el['label'])) { | |
109 | $id = $element->getAttribute('id'); | |
110 | if (!empty($id)) { | |
111 | $el['label'] = '<label for="' . $id . '">' . $el['label'] . '</label>'; | |
112 | } | |
113 | else { | |
114 | $el['label'] = "<label>{$el['label']}</label>"; | |
115 | } | |
116 | } | |
117 | ||
4a143c04 | 118 | // Display-only (frozen) elements |
704d3260 | 119 | if (!empty($el['frozen'])) { |
549be786 | 120 | if ($element->getAttribute('data-api-entity') && $element->getAttribute('data-entity-value')) { |
704d3260 CW |
121 | $this->renderFrozenEntityRef($el, $element); |
122 | } | |
b733747a CW |
123 | elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('data-select-params')) { |
124 | $this->renderFrozenSelect2($el, $element); | |
125 | } | |
09edd4bd CW |
126 | elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('data-crm-datepicker')) { |
127 | $this->renderFrozenDatepicker($el, $element); | |
128 | } | |
69b1c921 | 129 | elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('formatType')) { |
b691052b | 130 | list($date, $time) = CRM_Utils_Date::setDateDefaults($element->getValue(), $element->getAttribute('formatType'), $element->getAttribute('format'), $element->getAttribute('timeformat')); |
131 | $date .= ($element->getAttribute('timeformat')) ? " $time" : ''; | |
132 | $el['html'] = $date . '<input type="hidden" value="' . $element->getValue() . '" name="' . $element->getAttribute('name') . '">'; | |
657cae7c | 133 | } |
720cfc38 CW |
134 | // Render html for wysiwyg textareas |
135 | if ($el['type'] == 'textarea' && isset($element->_attributes['class']) && strstr($element->_attributes['class'], 'wysiwyg')) { | |
136 | $el['html'] = '<span class="crm-frozen-field">' . $el['value'] . '</span>'; | |
699728cc JM |
137 | } |
138 | else { | |
864005df JM |
139 | $el['html'] = '<span class="crm-frozen-field">' . $el['html'] . '</span>'; |
140 | } | |
704d3260 | 141 | } |
4a143c04 CW |
142 | // Active form elements |
143 | else { | |
be2fb01f | 144 | $typesToShowEditLink = ['select', 'group']; |
3ef93345 MD |
145 | $hasEditPath = NULL !== $element->getAttribute('data-option-edit-path'); |
146 | ||
147 | if (in_array($element->getType(), $typesToShowEditLink) && $hasEditPath) { | |
4a143c04 CW |
148 | $this->addOptionsEditLink($el, $element); |
149 | } | |
150 | ||
0c6fe5b5 | 151 | if ($element->getAttribute('allowClear')) { |
4a143c04 CW |
152 | $this->appendUnselectButton($el, $element); |
153 | } | |
154 | } | |
704d3260 | 155 | |
6a488035 TO |
156 | return $el; |
157 | } | |
158 | ||
159 | /** | |
160 | * Update the attributes of this element and add a few CiviCRM | |
161 | * based attributes so we can style this form element better | |
162 | * | |
6a488035 | 163 | * |
3c7fd868 CW |
164 | * @param HTML_QuickForm_element $element |
165 | * @param bool $required | |
166 | * Whether an element is required. | |
167 | * @param string $error | |
168 | * Error associated with the element. | |
6a488035 | 169 | * |
6a488035 | 170 | */ |
00be9182 | 171 | public static function updateAttributes(&$element, $required, $error) { |
6a488035 TO |
172 | // lets create an id for all input elements, so we can generate nice label tags |
173 | // to make it nice and clean, we'll just use the elementName if it is non null | |
be2fb01f | 174 | $attributes = []; |
6a488035 TO |
175 | if (!$element->getAttribute('id')) { |
176 | $name = $element->getAttribute('name'); | |
177 | if ($name) { | |
be2fb01f CW |
178 | $attributes['id'] = str_replace([']', '['], |
179 | ['', '_'], | |
6a488035 TO |
180 | $name |
181 | ); | |
182 | } | |
183 | } | |
184 | ||
185 | $class = $element->getAttribute('class'); | |
186 | $type = $element->getType(); | |
c5b53a6f | 187 | if (!$class) { |
e9bc5dcc | 188 | if ($type == 'text' || $type == 'password') { |
6a488035 TO |
189 | $size = $element->getAttribute('size'); |
190 | if (!empty($size)) { | |
dafd2d75 | 191 | $class = CRM_Utils_Array::value($size, self::$_sizeMapper); |
6a488035 TO |
192 | } |
193 | } | |
194 | } | |
195 | ||
4f95329c CW |
196 | if ($type == 'select' && $element->getAttribute('multiple')) { |
197 | $type = 'multiselect'; | |
198 | } | |
76ec9ca7 CW |
199 | // Add widget-specific class |
200 | if (!$class || strpos($class, 'crm-form-') === FALSE) { | |
201 | $class = ($class ? "$class " : '') . 'crm-form-' . $type; | |
202 | } | |
75f59466 CW |
203 | elseif (strpos($class, 'crm-form-entityref') !== FALSE) { |
204 | self::preProcessEntityRef($element); | |
205 | } | |
06508628 CW |
206 | elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) { |
207 | self::preprocessContactReference($element); | |
208 | } | |
d8f1758d CW |
209 | // Hack to support html5 fields (number, url, etc) |
210 | else { | |
211 | foreach (CRM_Core_Form::$html5Types as $type) { | |
212 | if (strpos($class, "crm-form-$type") !== FALSE) { | |
213 | $element->setAttribute('type', $type); | |
214 | // Also add the "base" class for consistent styling | |
215 | $class .= ' crm-form-text'; | |
216 | break; | |
217 | } | |
218 | } | |
092cb9c5 | 219 | } |
c5b53a6f | 220 | |
6a488035 TO |
221 | if ($required) { |
222 | $class .= ' required'; | |
223 | } | |
224 | ||
225 | if ($error) { | |
226 | $class .= ' error'; | |
227 | } | |
228 | ||
229 | $attributes['class'] = $class; | |
230 | $element->updateAttributes($attributes); | |
231 | } | |
704d3260 | 232 | |
75f59466 | 233 | /** |
fe482240 | 234 | * Convert IDs to values and format for display. |
06508628 | 235 | * |
3c7fd868 | 236 | * @param HTML_QuickForm_element $field |
75f59466 | 237 | */ |
00be9182 | 238 | public static function preProcessEntityRef($field) { |
75f59466 | 239 | $val = $field->getValue(); |
a8809160 CW |
240 | // Temporarily convert string values to an array |
241 | if (!is_array($val)) { | |
242 | // Try to auto-detect method of serialization | |
243 | $val = strpos($val, ',') ? explode(',', str_replace(', ', ',', $val)) : (array) CRM_Utils_Array::explodePadded($val); | |
75f59466 CW |
244 | } |
245 | if ($val) { | |
246 | $entity = $field->getAttribute('data-api-entity'); | |
e6d37e50 CW |
247 | // Get api params, ensure it is an array |
248 | $params = $field->getAttribute('data-api-params'); | |
be2fb01f CW |
249 | $params = $params ? json_decode($params, TRUE) : []; |
250 | $result = civicrm_api3($entity, 'getlist', ['id' => $val] + $params); | |
f39fa315 SL |
251 | // Purify label output of entityreference fields |
252 | if (!empty($result['values'])) { | |
253 | foreach ($result['values'] as &$res) { | |
254 | if (!empty($res['label'])) { | |
255 | $res['label'] = CRM_Utils_String::purifyHTML($res['label']); | |
256 | } | |
257 | } | |
258 | } | |
75f59466 | 259 | if ($field->isFrozen()) { |
a8809160 | 260 | // Prevent js from treating frozen entityRef as a "live" field |
75f59466 CW |
261 | $field->removeAttribute('class'); |
262 | } | |
263 | if (!empty($result['values'])) { | |
75f59466 CW |
264 | $field->setAttribute('data-entity-value', json_encode($result['values'])); |
265 | } | |
a8809160 CW |
266 | // CRM-15803 - Remove invalid values |
267 | $val = array_intersect($val, CRM_Utils_Array::collect('id', $result['values'])); | |
75f59466 | 268 | } |
a8809160 CW |
269 | // Convert array values back to a string |
270 | $field->setValue(implode(',', $val)); | |
75f59466 CW |
271 | } |
272 | ||
09edd4bd CW |
273 | /** |
274 | * Render datepicker as text. | |
275 | * | |
276 | * @param array $el | |
277 | * @param HTML_QuickForm_element $field | |
278 | */ | |
279 | public function renderFrozenDatepicker(&$el, $field) { | |
280 | $settings = json_decode($field->getAttribute('data-crm-datepicker'), TRUE); | |
281 | $settings += ['date' => TRUE, 'time' => TRUE]; | |
282 | $val = $field->getValue(); | |
283 | if ($val) { | |
284 | $dateFormat = NULL; | |
285 | if (!$settings['time']) { | |
286 | $val = substr($val, 0, 10); | |
287 | } | |
288 | elseif (!$settings['date']) { | |
289 | $dateFormat = Civi::settings()->get('dateformatTime'); | |
290 | } | |
291 | $val = CRM_Utils_Date::customFormat($val, $dateFormat); | |
292 | } | |
293 | $el['html'] = $val . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">'; | |
294 | } | |
295 | ||
b733747a CW |
296 | /** |
297 | * Render select2 as text. | |
298 | * | |
299 | * @param array $el | |
300 | * @param HTML_QuickForm_element $field | |
301 | */ | |
302 | public function renderFrozenSelect2(&$el, $field) { | |
303 | $params = json_decode($field->getAttribute('data-select-params'), TRUE); | |
304 | $val = $field->getValue(); | |
305 | if ($val && !empty($params['data'])) { | |
be2fb01f | 306 | $display = []; |
b733747a CW |
307 | foreach (explode(',', $val) as $item) { |
308 | $match = CRM_Utils_Array::findInTree($item, $params['data']); | |
309 | if (isset($match['text']) && strlen($match['text'])) { | |
eb6f5055 | 310 | $display[] = CRM_Utils_String::purifyHTML($match['text']); |
b733747a CW |
311 | } |
312 | } | |
313 | $el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">'; | |
314 | } | |
315 | } | |
316 | ||
704d3260 CW |
317 | /** |
318 | * Render entity references as text. | |
b56fec55 | 319 | * If user has permission, format as link (for now limited to contacts). |
3c7fd868 CW |
320 | * |
321 | * @param array $el | |
322 | * @param HTML_QuickForm_element $field | |
704d3260 | 323 | */ |
00be9182 | 324 | public function renderFrozenEntityRef(&$el, $field) { |
2229cf4f | 325 | $entity = $field->getAttribute('data-api-entity'); |
704d3260 | 326 | $vals = json_decode($field->getAttribute('data-entity-value'), TRUE); |
be2fb01f | 327 | $display = []; |
a9b69e9f CW |
328 | |
329 | // Custom fields of type contactRef store their data in a slightly different format | |
2229cf4f | 330 | if ($field->getAttribute('data-crm-custom') && $entity == 'Contact') { |
be2fb01f | 331 | $vals = [['id' => $vals['id'], 'label' => $vals['text']]]; |
a9b69e9f CW |
332 | } |
333 | ||
704d3260 CW |
334 | foreach ($vals as $val) { |
335 | // Format contact as link | |
2229cf4f | 336 | if ($entity == 'Contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) { |
be2fb01f | 337 | $url = CRM_Utils_System::url("civicrm/contact/view", ['reset' => 1, 'cid' => $val['id']]); |
41cb88ce | 338 | $val['label'] = '<a class="view-contact no-popup" href="' . $url . '" title="' . ts('View Contact') . '">' . CRM_Utils_String::purifyHTML($val['label']) . '</a>'; |
704d3260 | 339 | } |
549be786 | 340 | $display[] = $val['label']; |
704d3260 CW |
341 | } |
342 | ||
86bfa4f6 | 343 | $el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">'; |
704d3260 | 344 | } |
5fafc9b0 | 345 | |
06508628 CW |
346 | /** |
347 | * Pre-fill contact name for a custom field of type ContactReference | |
348 | * | |
349 | * Todo: Migrate contact reference fields to use EntityRef | |
350 | * | |
3c7fd868 | 351 | * @param HTML_QuickForm_element $field |
06508628 | 352 | */ |
00be9182 | 353 | public static function preprocessContactReference($field) { |
06508628 CW |
354 | $val = $field->getValue(); |
355 | if ($val && is_numeric($val)) { | |
356 | ||
357 | $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
358 | 'contact_reference_options' | |
359 | ), '1'); | |
360 | ||
be2fb01f | 361 | $return = array_unique(array_merge(['sort_name'], $list)); |
06508628 | 362 | |
be2fb01f | 363 | $contact = civicrm_api('contact', 'getsingle', ['id' => $val, 'return' => $return, 'version' => 3]); |
06508628 CW |
364 | |
365 | if (!empty($contact['id'])) { | |
be2fb01f | 366 | $view = []; |
06508628 CW |
367 | foreach ($return as $fld) { |
368 | if (!empty($contact[$fld])) { | |
369 | $view[] = $contact[$fld]; | |
370 | } | |
371 | } | |
be2fb01f | 372 | $field->setAttribute('data-entity-value', json_encode([ |
518fa0ee SL |
373 | 'id' => $contact['id'], |
374 | 'text' => implode(' :: ', $view), | |
375 | ])); | |
06508628 CW |
376 | } |
377 | } | |
378 | } | |
379 | ||
5fafc9b0 CW |
380 | /** |
381 | * @param array $el | |
382 | * @param HTML_QuickForm_element $field | |
383 | */ | |
00be9182 | 384 | public function addOptionsEditLink(&$el, $field) { |
5fafc9b0 | 385 | if (CRM_Core_Permission::check('administer CiviCRM')) { |
87831073 CW |
386 | // NOTE: $path is used on the client-side to know which option lists need rebuilding, |
387 | // that's why we need that bit of data both in the link and in the form element | |
388 | $path = $field->getAttribute('data-option-edit-path'); | |
389 | // NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ? | |
390 | $url = CRM_Utils_System::url($path, 'reset=1'); | |
972bd897 | 391 | $el['html'] .= ' <a href="' . $url . '" class="crm-option-edit-link medium-popup crm-hover-button" target="_blank" title="' . ts('Edit Options') . '" data-option-edit-path="' . $path . '"><i class="crm-i fa-wrench"></i></a>'; |
5fafc9b0 CW |
392 | } |
393 | } | |
4a143c04 CW |
394 | |
395 | /** | |
396 | * @param array $el | |
397 | * @param HTML_QuickForm_element $field | |
398 | */ | |
00be9182 | 399 | public function appendUnselectButton(&$el, $field) { |
4a143c04 CW |
400 | // Initially hide if not needed |
401 | // Note: visibility:hidden prevents layout jumping around unlike display:none | |
8a4f27dc | 402 | $display = $field->getValue() !== NULL ? '' : ' style="visibility:hidden;"'; |
80bc2820 | 403 | $el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear') . '"><i class="crm-i fa-times"></i></a>'; |
4a143c04 | 404 | } |
96025800 | 405 | |
6a488035 | 406 | } |