3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
23 * Customize QF output to meet our specific requirements
25 class CRM_Core_Form_Renderer
extends HTML_QuickForm_Renderer_ArraySmarty
{
28 * We only need one instance of this object. So we use the singleton
29 * pattern and cache the instance in this variable
33 static private $_singleton = NULL;
36 * The converter from array size to css class.
40 public static $_sizeMapper = [
54 public function __construct() {
55 $template = CRM_Core_Smarty
::singleton();
56 parent
::__construct($template);
60 * Static instance provider.
62 * Method providing static instance of as in Singleton pattern.
64 public static function &singleton() {
65 if (!isset(self
::$_singleton)) {
66 self
::$_singleton = new CRM_Core_Form_Renderer();
68 return self
::$_singleton;
72 * Creates an array representing an element containing.
73 * the key for storing this. We allow the parent to do most of the
74 * work, but then we add some CiviCRM specific enhancements to
75 * make the html compliant with our css etc
78 * @param HTML_QuickForm_element $element
79 * @param bool $required
80 * Whether an element is required.
81 * @param string $error
82 * Error associated with the element.
86 public function _elementToArray(&$element, $required, $error) {
87 self
::updateAttributes($element, $required, $error);
89 $el = parent
::_elementToArray($element, $required, $error);
92 if (!empty($el['label'])) {
93 $id = $element->getAttribute('id');
95 $el['label'] = '<label for="' . $id . '">' . $el['label'] . '</label>';
98 $el['label'] = "<label>{$el['label']}</label>";
102 // Display-only (frozen) elements
103 if (!empty($el['frozen'])) {
104 if ($element->getAttribute('data-api-entity') && $element->getAttribute('data-entity-value')) {
105 $this->renderFrozenEntityRef($el, $element);
107 elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('data-select-params')) {
108 $this->renderFrozenSelect2($el, $element);
110 elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('data-crm-datepicker')) {
111 $this->renderFrozenDatepicker($el, $element);
113 elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('formatType')) {
114 list($date, $time) = CRM_Utils_Date
::setDateDefaults($element->getValue(), $element->getAttribute('formatType'), $element->getAttribute('format'), $element->getAttribute('timeformat'));
115 $date .= ($element->getAttribute('timeformat')) ?
" $time" : '';
116 $el['html'] = $date . '<input type="hidden" value="' . $element->getValue() . '" name="' . $element->getAttribute('name') . '">';
118 // Render html for wysiwyg textareas
119 if ($el['type'] == 'textarea' && isset($element->_attributes
['class']) && strstr($element->_attributes
['class'], 'wysiwyg')) {
120 $el['html'] = '<span class="crm-frozen-field">' . $el['value'] . '</span>';
123 $el['html'] = '<span class="crm-frozen-field">' . $el['html'] . '</span>';
126 // Active form elements
128 $typesToShowEditLink = ['select', 'group'];
129 $hasEditPath = NULL !== $element->getAttribute('data-option-edit-path');
131 if (in_array($element->getType(), $typesToShowEditLink) && $hasEditPath) {
132 $this->addOptionsEditLink($el, $element);
135 if ($element->getAttribute('allowClear')) {
136 $this->appendUnselectButton($el, $element);
144 * Update the attributes of this element and add a few CiviCRM
145 * based attributes so we can style this form element better
148 * @param HTML_QuickForm_element $element
149 * @param bool $required
150 * Whether an element is required.
151 * @param string $error
152 * Error associated with the element.
155 public static function updateAttributes(&$element, $required, $error) {
156 // lets create an id for all input elements, so we can generate nice label tags
157 // to make it nice and clean, we'll just use the elementName if it is non null
159 if (!$element->getAttribute('id')) {
160 $name = $element->getAttribute('name');
162 $attributes['id'] = str_replace([']', '['],
169 $class = $element->getAttribute('class');
170 $type = $element->getType();
172 if ($type == 'text' ||
$type == 'password') {
173 $size = $element->getAttribute('size');
175 $class = self
::$_sizeMapper[$size] ??
NULL;
180 if ($type == 'select' && $element->getAttribute('multiple')) {
181 $type = 'multiselect';
183 // Add widget-specific class
184 if (!$class ||
strpos($class, 'crm-form-') === FALSE) {
185 $class = ($class ?
"$class " : '') . 'crm-form-' . $type;
187 elseif (strpos($class, 'crm-form-entityref') !== FALSE) {
188 self
::preProcessEntityRef($element);
190 elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) {
191 self
::preprocessContactReference($element);
193 // Hack to support html5 fields (number, url, etc)
195 foreach (CRM_Core_Form
::$html5Types as $type) {
196 if (strpos($class, "crm-form-$type") !== FALSE) {
197 $element->setAttribute('type', $type);
198 // Also add the "base" class for consistent styling
199 $class .= ' crm-form-text';
206 $class .= ' required';
213 $attributes['class'] = $class;
214 $element->updateAttributes($attributes);
218 * Convert IDs to values and format for display.
220 * @param HTML_QuickForm_element $field
222 public static function preProcessEntityRef($field) {
223 $val = $field->getValue();
224 // Temporarily convert string values to an array
225 if (!is_array($val)) {
226 // Try to auto-detect method of serialization
227 $val = strpos($val, ',') ?
explode(',', str_replace(', ', ',', $val)) : (array) CRM_Utils_Array
::explodePadded($val);
230 $entity = $field->getAttribute('data-api-entity');
231 // Get api params, ensure it is an array
232 $params = $field->getAttribute('data-api-params');
233 $params = $params ?
json_decode($params, TRUE) : [];
234 $result = civicrm_api3($entity, 'getlist', ['id' => $val] +
$params);
235 // Purify label output of entityreference fields
236 if (!empty($result['values'])) {
237 foreach ($result['values'] as &$res) {
238 if (!empty($res['label'])) {
239 $res['label'] = CRM_Utils_String
::purifyHTML($res['label']);
243 if ($field->isFrozen()) {
244 // Prevent js from treating frozen entityRef as a "live" field
245 $field->removeAttribute('class');
247 if (!empty($result['values'])) {
248 $field->setAttribute('data-entity-value', json_encode($result['values']));
250 // CRM-15803 - Remove invalid values
251 $val = array_intersect($val, CRM_Utils_Array
::collect('id', $result['values']));
253 // Convert array values back to a string
254 $field->setValue(implode(',', $val));
258 * Render datepicker as text.
261 * @param HTML_QuickForm_element $field
263 public function renderFrozenDatepicker(&$el, $field) {
264 $settings = json_decode($field->getAttribute('data-crm-datepicker'), TRUE);
265 $settings +
= ['date' => TRUE, 'time' => TRUE];
266 $val = $field->getValue();
269 if (!$settings['time']) {
270 $val = substr($val, 0, 10);
272 elseif (!$settings['date']) {
273 $dateFormat = Civi
::settings()->get('dateformatTime');
275 $val = CRM_Utils_Date
::customFormat($val, $dateFormat);
277 $el['html'] = $val . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
281 * Render select2 as text.
284 * @param HTML_QuickForm_element $field
286 public function renderFrozenSelect2(&$el, $field) {
287 $params = json_decode($field->getAttribute('data-select-params'), TRUE);
288 $val = $field->getValue();
289 if ($val && !empty($params['data'])) {
291 foreach (explode(',', $val) as $item) {
292 $match = CRM_Utils_Array
::findInTree($item, $params['data']);
293 if (isset($match['text']) && strlen($match['text'])) {
294 $display[] = CRM_Utils_String
::purifyHTML($match['text']);
297 $el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
302 * Render entity references as text.
303 * If user has permission, format as link (for now limited to contacts).
306 * @param HTML_QuickForm_element $field
308 public function renderFrozenEntityRef(&$el, $field) {
309 $entity = $field->getAttribute('data-api-entity');
310 $vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
313 // Custom fields of type contactRef store their data in a slightly different format
314 if ($field->getAttribute('data-crm-custom') && $entity == 'Contact') {
315 $vals = [['id' => $vals['id'], 'label' => $vals['text']]];
318 foreach ($vals as $val) {
319 // Format contact as link
320 if ($entity == 'Contact' && CRM_Contact_BAO_Contact_Permission
::allow($val['id'], CRM_Core_Permission
::VIEW
)) {
321 $url = CRM_Utils_System
::url("civicrm/contact/view", ['reset' => 1, 'cid' => $val['id']]);
322 $val['label'] = '<a class="view-contact no-popup" href="' . $url . '" title="' . ts('View Contact') . '">' . CRM_Utils_String
::purifyHTML($val['label']) . '</a>';
324 $display[] = $val['label'];
327 $el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
331 * Pre-fill contact name for a custom field of type ContactReference
333 * Todo: Migrate contact reference fields to use EntityRef
335 * @param HTML_QuickForm_element $field
337 public static function preprocessContactReference($field) {
338 $val = $field->getValue();
339 if ($val && is_numeric($val)) {
341 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
342 'contact_reference_options'
345 $return = array_unique(array_merge(['sort_name'], $list));
347 $contact = civicrm_api('contact', 'getsingle', ['id' => $val, 'return' => $return, 'version' => 3]);
349 if (!empty($contact['id'])) {
351 foreach ($return as $fld) {
352 if (!empty($contact[$fld])) {
353 $view[] = $contact[$fld];
356 $field->setAttribute('data-entity-value', json_encode([
357 'id' => $contact['id'],
358 'text' => implode(' :: ', $view),
366 * @param HTML_QuickForm_element $field
368 public function addOptionsEditLink(&$el, $field) {
369 if (CRM_Core_Permission
::check('administer CiviCRM')) {
370 // NOTE: $path is used on the client-side to know which option lists need rebuilding,
371 // that's why we need that bit of data both in the link and in the form element
372 $path = $field->getAttribute('data-option-edit-path');
373 // NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ?
374 $url = CRM_Utils_System
::url($path, 'reset=1');
375 $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>';
381 * @param HTML_QuickForm_element $field
383 public function appendUnselectButton(&$el, $field) {
384 // Initially hide if not needed
385 // Note: visibility:hidden prevents layout jumping around unlike display:none
386 $display = $field->getValue() !== NULL ?
'' : ' style="visibility:hidden;"';
387 $el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear') . '"><i class="crm-i fa-times"></i></a>';