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