-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathRenderer.php
447 lines (409 loc) · 15.4 KB
/
Renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
/**
* Customize QF output to meet our specific requirements
*/
class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty {
/**
* We only need one instance of this object. So we use the singleton
* pattern and cache the instance in this variable
*
* @var object
*/
static private $_singleton = NULL;
/**
* The converter from array size to css class.
*
* @var array
*/
public static $_sizeMapper = [
2 => 'two',
4 => 'four',
6 => 'six',
8 => 'eight',
12 => 'twelve',
20 => 'medium',
30 => 'big',
45 => 'huge',
];
/**
* Constructor.
*/
public function __construct() {
$template = CRM_Core_Smarty::singleton();
parent::__construct($template);
}
/**
* Static instance provider.
*
* Method providing static instance of as in Singleton pattern.
*/
public static function &singleton() {
if (!isset(self::$_singleton)) {
self::$_singleton = new CRM_Core_Form_Renderer();
}
return self::$_singleton;
}
/**
* Creates an array representing an element containing.
* the key for storing this. We allow the parent to do most of the
* work, but then we add some CiviCRM specific enhancements to
* make the html compliant with our css etc
*
*
* @param HTML_QuickForm_element $element
* @param bool $required
* Whether an element is required.
* @param string $error
* Error associated with the element.
*
* @return array
*/
public function _elementToArray(&$element, $required, $error) {
self::updateAttributes($element, $required, $error);
$el = parent::_elementToArray($element, $required, $error);
$el['textLabel'] = $element->_label ?? NULL;
// add label html
if (!empty($el['label'])) {
$id = $element->getAttribute('id');
if (!empty($id)) {
$el['label'] = '<label for="' . $id . '">' . $el['label'] . '</label>';
}
else {
$el['label'] = "<label>{$el['label']}</label>";
}
}
// Display-only (frozen) elements
if (!empty($el['frozen'])) {
if ($element->getAttribute('data-api-entity') && $element->getAttribute('data-entity-value')) {
$this->renderFrozenEntityRef($el, $element);
}
elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('data-select-params')) {
$this->renderFrozenSelect2($el, $element);
}
elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('data-crm-datepicker')) {
$this->renderFrozenDatepicker($el, $element);
}
elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('formatType')) {
[$date, $time] = CRM_Utils_Date::setDateDefaults($element->getValue(), $element->getAttribute('formatType'), $element->getAttribute('format'), $element->getAttribute('timeformat'));
$date .= ($element->getAttribute('timeformat')) ? " $time" : '';
$el['html'] = $date . '<input type="hidden" value="' . $element->getValue() . '" name="' . $element->getAttribute('name') . '">';
}
// Render html for wysiwyg textareas
if ($el['type'] == 'textarea' && isset($element->_attributes['class']) && str_contains($element->_attributes['class'], 'wysiwyg')) {
$el['html'] = '<span class="crm-frozen-field">' . $el['value'] . '</span>';
}
else {
$el['html'] = '<span class="crm-frozen-field">' . $el['html'] . '</span>';
}
}
// Active form elements
else {
$typesToShowEditLink = ['select', 'group'];
$hasEditPath = NULL !== $element->getAttribute('data-option-edit-path');
if (in_array($element->getType(), $typesToShowEditLink) && $hasEditPath) {
$this->addOptionsEditLink($el, $element);
}
if ($element->getAttribute('allowClear')) {
$this->appendUnselectButton($el, $element);
}
}
return $el;
}
/**
* Update the attributes of this element and add a few CiviCRM
* based attributes so we can style this form element better
*
*
* @param HTML_QuickForm_element $element
* @param bool $required
* Whether an element is required.
* @param string $error
* Error associated with the element.
*
*/
public static function updateAttributes(&$element, $required, $error) {
// lets create an id for all input elements, so we can generate nice label tags
// to make it nice and clean, we'll just use the elementName if it is non null
$attributes = [];
if (!$element->getAttribute('id')) {
$name = $element->getAttribute('name');
if ($name) {
$attributes['id'] = str_replace([']', '['],
['', '_'],
$name
);
}
}
$class = $element->getAttribute('class') ?? '';
$type = $element->getType();
if (!$class) {
if ($type == 'text' || $type == 'password') {
$size = $element->getAttribute('size');
if (!empty($size)) {
$class = self::$_sizeMapper[$size] ?? '';
}
}
}
// When select2 is an <input> it requires comma-separated values instead of an array
if (in_array($type, ['text', 'hidden']) && str_contains($class, 'crm-select2') && is_array($element->getValue())) {
$element->setValue(implode(',', $element->getValue()));
}
if ($type == 'select' && $element->getAttribute('multiple')) {
$type = 'multiselect';
}
// Add widget-specific class
if (!$class || strpos($class, 'crm-form-') === FALSE) {
$class = ($class ? "$class " : '') . 'crm-form-' . $type;
}
elseif (strpos($class, 'crm-form-entityref') !== FALSE) {
self::preProcessEntityRef($element);
}
elseif (str_contains($class, 'crm-form-autocomplete')) {
self::preProcessAutocomplete($element);
}
elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) {
self::preprocessContactReference($element);
}
// Hack to support html5 fields (number, url, etc)
else {
foreach (CRM_Core_Form::$html5Types as $type) {
if (strpos($class, "crm-form-$type") !== FALSE) {
$element->setAttribute('type', $type);
// Also add the "base" class for consistent styling
$class .= ' crm-form-text';
break;
}
}
}
if ($required) {
$class .= ' required';
}
if ($error) {
$class .= ' error';
}
$attributes['class'] = $class;
$element->updateAttributes($attributes);
}
/**
* Process an template sourced in a string with Smarty
*
* This overrides the quick form function which has not been updated in a while.
*
* The function is called when render the code to mark a field as 'required'
*
* The notes on the quick form function seem to refer to older smarty - ie:
* Smarty has no core function to render a template given as a string.
* So we use the smarty eval plugin function to do this.
*
* @param string $tplSource The template source
*/
public function _tplFetch($tplSource) {
// Smarty3 does not have this function defined so the parent fails.
// Adding this is preparatory to smarty 3....
if (!function_exists('smarty_function_eval') && (!defined('SMARTY_DIR') || !file_exists(SMARTY_DIR . '/plugins/function.eval.php'))) {
$smarty = $this->_tpl;
$smarty->assign('var', $tplSource);
return $smarty->fetch("eval:$tplSource");
}
// This part is what the parent does & is suitable to Smarty 2.
if (!function_exists('smarty_function_eval')) {
require SMARTY_DIR . '/plugins/function.eval.php';
}
return smarty_function_eval(['var' => $tplSource], $this->_tpl);
}
/**
* @param HTML_QuickForm_element $field
*/
private static function preProcessAutocomplete($field) {
$val = $field->getValue();
if (is_array($val)) {
$field->setValue(implode(',', $val));
}
}
/**
* Convert IDs to values and format for display.
*
* @param HTML_QuickForm_element $field
*/
public static function preProcessEntityRef($field) {
$val = $field->getValue();
// Temporarily convert string values to an array
if (!is_array($val)) {
// Try to auto-detect method of serialization
$val = strpos(($val ?? ''), ',') ? explode(',', str_replace(', ', ',', ($val ?? ''))) : (array) CRM_Utils_Array::explodePadded($val);
}
if ($val) {
$entity = $field->getAttribute('data-api-entity');
// Get api params, ensure it is an array
$params = $field->getAttribute('data-api-params');
$params = $params ? json_decode($params, TRUE) : [];
$result = civicrm_api3($entity, 'getlist', ['id' => $val] + $params);
// Purify label output of entityreference fields
if (!empty($result['values'])) {
foreach ($result['values'] as &$res) {
if (!empty($res['label'])) {
$res['label'] = CRM_Utils_String::purifyHTML($res['label']);
}
}
}
if ($field->isFrozen()) {
// Prevent js from treating frozen entityRef as a "live" field
$field->removeAttribute('class');
}
if (!empty($result['values'])) {
$field->setAttribute('data-entity-value', json_encode($result['values']));
}
// CRM-15803 - Remove invalid values
$val = array_intersect($val, CRM_Utils_Array::collect('id', $result['values']));
}
// Convert array values back to a string
$field->setValue(implode(',', $val));
}
/**
* Render datepicker as text.
*
* @param array $el
* @param HTML_QuickForm_element $field
*/
public function renderFrozenDatepicker(&$el, $field) {
$settings = json_decode($field->getAttribute('data-crm-datepicker'), TRUE);
$settings += ['date' => TRUE, 'time' => TRUE];
$val = $field->getValue();
if ($val) {
$dateFormat = NULL;
if (!$settings['time']) {
$val = substr($val, 0, 10);
}
elseif (!$settings['date']) {
$dateFormat = Civi::settings()->get('dateformatTime');
}
$val = CRM_Utils_Date::customFormat($val, $dateFormat);
}
$el['html'] = $val . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
}
/**
* Render select2 as text.
*
* @param array $el
* @param HTML_QuickForm_element $field
*/
public function renderFrozenSelect2(&$el, $field) {
$params = json_decode($field->getAttribute('data-select-params'), TRUE);
$val = $field->getValue();
if ($val && !empty($params['data'])) {
$display = [];
foreach (explode(',', $val) as $item) {
$match = CRM_Utils_Array::findInTree($item, $params['data']);
if (isset($match['text']) && strlen($match['text'])) {
$display[] = CRM_Utils_String::purifyHTML($match['text']);
}
}
$el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
}
}
/**
* Render entity references as text.
* If user has permission, format as link (for now limited to contacts).
*
* @param array $el
* @param HTML_QuickForm_element $field
*/
public function renderFrozenEntityRef(&$el, $field) {
$entity = $field->getAttribute('data-api-entity');
$vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
$display = [];
// Custom fields of type contactRef store their data in a slightly different format
if ($field->getAttribute('data-crm-custom') && $entity == 'Contact') {
$vals = [['id' => $vals['id'], 'label' => $vals['text']]];
}
foreach ($vals as $val) {
// Format contact as link
if ($entity == 'Contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) {
$url = CRM_Utils_System::url("civicrm/contact/view", ['reset' => 1, 'cid' => $val['id']]);
$val['label'] = '<a class="view-contact no-popup" href="' . $url . '" title="' . ts('View Contact', ['escape' => 'htmlattribute']) . '">' . CRM_Utils_String::purifyHTML($val['label']) . '</a>';
}
$display[] = $val['label'];
}
$el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
}
/**
* Pre-fill contact name for a custom field of type ContactReference
*
* Todo: Migrate contact reference fields to use EntityRef
*
* @param HTML_QuickForm_element $field
*/
public static function preprocessContactReference($field) {
$val = $field->getValue();
$multiple = $field->getAttribute('multiple');
$data = [];
if ($val) {
$list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'contact_reference_options'
), '1');
$return = array_unique(array_merge(['sort_name'], $list));
$cids = is_array($val) ? $val : explode(',', $val);
foreach ($cids as $cid) {
$contact = civicrm_api('contact', 'getsingle', ['id' => $cid, 'return' => $return, 'version' => 3]);
if (!empty($contact['id'])) {
$view = [];
foreach ($return as $fld) {
if (!empty($contact[$fld])) {
$view[] = $contact[$fld];
}
}
$data[] = [
'id' => $contact['id'],
'text' => implode(' :: ', $view),
];
}
}
}
if ($data) {
$field->setAttribute('data-entity-value', json_encode($multiple ? $data : $data[0]));
$field->setValue(implode(',', $cids));
}
}
/**
* @param array $el
* @param HTML_QuickForm_element $field
*/
public function addOptionsEditLink(&$el, $field) {
if (CRM_Core_Permission::check('administer CiviCRM')) {
// NOTE: $path is used on the client-side to know which option lists need rebuilding,
// that's why we need that bit of data both in the link and in the form element
$path = $field->getAttribute('data-option-edit-path');
// NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ?
$url = CRM_Utils_System::url($path, 'reset=1');
$icon = CRM_Core_Page::crmIcon('fa-wrench', ts('Edit %1 Options', [1 => $field->getLabel() ?: ts('Field')]));
$el['html'] .= <<<HEREDOC
<a href="$url" class="crm-option-edit-link medium-popup crm-hover-button" target="_blank" data-option-edit-path="$path">$icon</a>
HEREDOC;
}
}
/**
* @param array $el
* @param HTML_QuickForm_element $field
*/
public function appendUnselectButton(&$el, $field) {
// Initially hide if not needed
// Note: visibility:hidden prevents layout jumping around unlike display:none
$display = $field->getValue() !== NULL ? '' : ' style="visibility:hidden;"';
$el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear', ['escape' => 'htmlattribute']) . '"><i class="crm-i fa-times" aria-hidden="true"></i></a>';
}
}