Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 TO |
55 | */ |
56 | static $_sizeMapper = array( | |
57 | 2 => 'two', | |
58 | 4 => 'four', | |
59 | 6 => 'six', | |
60 | 8 => 'eight', | |
61 | 12 => 'twelve', | |
62 | 20 => 'medium', | |
63 | 30 => 'big', | |
64 | 45 => 'huge', | |
65 | ); | |
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 | } | |
69b1c921 | 123 | elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('formatType')) { |
b691052b | 124 | list($date, $time) = CRM_Utils_Date::setDateDefaults($element->getValue(), $element->getAttribute('formatType'), $element->getAttribute('format'), $element->getAttribute('timeformat')); |
125 | $date .= ($element->getAttribute('timeformat')) ? " $time" : ''; | |
126 | $el['html'] = $date . '<input type="hidden" value="' . $element->getValue() . '" name="' . $element->getAttribute('name') . '">'; | |
657cae7c ML |
127 | } |
128 | ||
f0366d0b | 129 | $el['html'] = '<span class="crm-frozen-field">' . $el['html'] . '</span>'; |
704d3260 | 130 | } |
4a143c04 CW |
131 | // Active form elements |
132 | else { | |
87831073 | 133 | if ($element->getType() == 'select' && $element->getAttribute('data-option-edit-path')) { |
4a143c04 CW |
134 | $this->addOptionsEditLink($el, $element); |
135 | } | |
136 | ||
b847e6e7 | 137 | if ($element->getType() == 'group' && $element->getAttribute('allowClear')) { |
4a143c04 CW |
138 | $this->appendUnselectButton($el, $element); |
139 | } | |
140 | } | |
704d3260 | 141 | |
6a488035 TO |
142 | return $el; |
143 | } | |
144 | ||
145 | /** | |
146 | * Update the attributes of this element and add a few CiviCRM | |
147 | * based attributes so we can style this form element better | |
148 | * | |
6a488035 | 149 | * |
3c7fd868 CW |
150 | * @param HTML_QuickForm_element $element |
151 | * @param bool $required | |
152 | * Whether an element is required. | |
153 | * @param string $error | |
154 | * Error associated with the element. | |
6a488035 | 155 | * |
6a488035 | 156 | */ |
00be9182 | 157 | public static function updateAttributes(&$element, $required, $error) { |
6a488035 TO |
158 | // lets create an id for all input elements, so we can generate nice label tags |
159 | // to make it nice and clean, we'll just use the elementName if it is non null | |
160 | $attributes = array(); | |
161 | if (!$element->getAttribute('id')) { | |
162 | $name = $element->getAttribute('name'); | |
163 | if ($name) { | |
164 | $attributes['id'] = str_replace(array(']', '['), | |
165 | array('', '_'), | |
166 | $name | |
167 | ); | |
168 | } | |
169 | } | |
170 | ||
171 | $class = $element->getAttribute('class'); | |
172 | $type = $element->getType(); | |
c5b53a6f | 173 | if (!$class) { |
6a488035 TO |
174 | if ($type == 'text') { |
175 | $size = $element->getAttribute('size'); | |
176 | if (!empty($size)) { | |
dafd2d75 | 177 | $class = CRM_Utils_Array::value($size, self::$_sizeMapper); |
6a488035 TO |
178 | } |
179 | } | |
180 | } | |
181 | ||
4f95329c CW |
182 | if ($type == 'select' && $element->getAttribute('multiple')) { |
183 | $type = 'multiselect'; | |
184 | } | |
76ec9ca7 CW |
185 | // Add widget-specific class |
186 | if (!$class || strpos($class, 'crm-form-') === FALSE) { | |
187 | $class = ($class ? "$class " : '') . 'crm-form-' . $type; | |
188 | } | |
75f59466 CW |
189 | elseif (strpos($class, 'crm-form-entityref') !== FALSE) { |
190 | self::preProcessEntityRef($element); | |
191 | } | |
06508628 CW |
192 | elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) { |
193 | self::preprocessContactReference($element); | |
194 | } | |
d8f1758d CW |
195 | // Hack to support html5 fields (number, url, etc) |
196 | else { | |
197 | foreach (CRM_Core_Form::$html5Types as $type) { | |
198 | if (strpos($class, "crm-form-$type") !== FALSE) { | |
199 | $element->setAttribute('type', $type); | |
200 | // Also add the "base" class for consistent styling | |
201 | $class .= ' crm-form-text'; | |
202 | break; | |
203 | } | |
204 | } | |
092cb9c5 | 205 | } |
c5b53a6f | 206 | |
6a488035 TO |
207 | if ($required) { |
208 | $class .= ' required'; | |
209 | } | |
210 | ||
211 | if ($error) { | |
212 | $class .= ' error'; | |
213 | } | |
214 | ||
215 | $attributes['class'] = $class; | |
216 | $element->updateAttributes($attributes); | |
217 | } | |
704d3260 | 218 | |
75f59466 | 219 | /** |
fe482240 | 220 | * Convert IDs to values and format for display. |
06508628 | 221 | * |
3c7fd868 | 222 | * @param HTML_QuickForm_element $field |
75f59466 | 223 | */ |
00be9182 | 224 | public static function preProcessEntityRef($field) { |
75f59466 | 225 | $val = $field->getValue(); |
a8809160 CW |
226 | // Temporarily convert string values to an array |
227 | if (!is_array($val)) { | |
228 | // Try to auto-detect method of serialization | |
229 | $val = strpos($val, ',') ? explode(',', str_replace(', ', ',', $val)) : (array) CRM_Utils_Array::explodePadded($val); | |
75f59466 CW |
230 | } |
231 | if ($val) { | |
232 | $entity = $field->getAttribute('data-api-entity'); | |
e6d37e50 CW |
233 | // Get api params, ensure it is an array |
234 | $params = $field->getAttribute('data-api-params'); | |
235 | $params = $params ? json_decode($params, TRUE) : array(); | |
61d869af | 236 | $result = civicrm_api3($entity, 'getlist', array('id' => $val) + $params); |
75f59466 | 237 | if ($field->isFrozen()) { |
a8809160 | 238 | // Prevent js from treating frozen entityRef as a "live" field |
75f59466 CW |
239 | $field->removeAttribute('class'); |
240 | } | |
241 | if (!empty($result['values'])) { | |
75f59466 CW |
242 | $field->setAttribute('data-entity-value', json_encode($result['values'])); |
243 | } | |
a8809160 CW |
244 | // CRM-15803 - Remove invalid values |
245 | $val = array_intersect($val, CRM_Utils_Array::collect('id', $result['values'])); | |
75f59466 | 246 | } |
a8809160 CW |
247 | // Convert array values back to a string |
248 | $field->setValue(implode(',', $val)); | |
75f59466 CW |
249 | } |
250 | ||
704d3260 CW |
251 | /** |
252 | * Render entity references as text. | |
b56fec55 | 253 | * If user has permission, format as link (for now limited to contacts). |
3c7fd868 CW |
254 | * |
255 | * @param array $el | |
256 | * @param HTML_QuickForm_element $field | |
704d3260 | 257 | */ |
00be9182 | 258 | public function renderFrozenEntityRef(&$el, $field) { |
01057d41 | 259 | $entity = strtolower($field->getAttribute('data-api-entity')); |
704d3260 | 260 | $vals = json_decode($field->getAttribute('data-entity-value'), TRUE); |
704d3260 | 261 | $display = array(); |
a9b69e9f CW |
262 | |
263 | // Custom fields of type contactRef store their data in a slightly different format | |
264 | if ($field->getAttribute('data-crm-custom') && $entity == 'contact') { | |
265 | $vals = array(array('id' => $vals['id'], 'label' => $vals['text'])); | |
266 | } | |
267 | ||
704d3260 CW |
268 | foreach ($vals as $val) { |
269 | // Format contact as link | |
549be786 | 270 | if ($entity == 'contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) { |
704d3260 | 271 | $url = CRM_Utils_System::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id'])); |
b56fec55 | 272 | $val['label'] = '<a class="view-' . $entity . ' no-popup" href="' . $url . '" title="' . ts('View Contact') . '">' . $val['label'] . '</a>'; |
704d3260 | 273 | } |
549be786 | 274 | $display[] = $val['label']; |
704d3260 CW |
275 | } |
276 | ||
86bfa4f6 | 277 | $el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">'; |
704d3260 | 278 | } |
5fafc9b0 | 279 | |
06508628 CW |
280 | /** |
281 | * Pre-fill contact name for a custom field of type ContactReference | |
282 | * | |
283 | * Todo: Migrate contact reference fields to use EntityRef | |
284 | * | |
3c7fd868 | 285 | * @param HTML_QuickForm_element $field |
06508628 | 286 | */ |
00be9182 | 287 | public static function preprocessContactReference($field) { |
06508628 CW |
288 | $val = $field->getValue(); |
289 | if ($val && is_numeric($val)) { | |
290 | ||
291 | $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
292 | 'contact_reference_options' | |
293 | ), '1'); | |
294 | ||
295 | $return = array_unique(array_merge(array('sort_name'), $list)); | |
296 | ||
297 | $contact = civicrm_api('contact', 'getsingle', array('id' => $val, 'return' => $return, 'version' => 3)); | |
298 | ||
299 | if (!empty($contact['id'])) { | |
300 | $view = array(); | |
301 | foreach ($return as $fld) { | |
302 | if (!empty($contact[$fld])) { | |
303 | $view[] = $contact[$fld]; | |
304 | } | |
305 | } | |
353ffa53 TO |
306 | $field->setAttribute('data-entity-value', json_encode(array( |
307 | 'id' => $contact['id'], | |
317fceb4 | 308 | 'text' => implode(' :: ', $view), |
353ffa53 | 309 | ))); |
06508628 CW |
310 | } |
311 | } | |
312 | } | |
313 | ||
5fafc9b0 CW |
314 | /** |
315 | * @param array $el | |
316 | * @param HTML_QuickForm_element $field | |
317 | */ | |
00be9182 | 318 | public function addOptionsEditLink(&$el, $field) { |
5fafc9b0 | 319 | if (CRM_Core_Permission::check('administer CiviCRM')) { |
87831073 CW |
320 | // NOTE: $path is used on the client-side to know which option lists need rebuilding, |
321 | // that's why we need that bit of data both in the link and in the form element | |
322 | $path = $field->getAttribute('data-option-edit-path'); | |
323 | // NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ? | |
324 | $url = CRM_Utils_System::url($path, 'reset=1'); | |
972bd897 | 325 | $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 |
326 | } |
327 | } | |
4a143c04 CW |
328 | |
329 | /** | |
330 | * @param array $el | |
331 | * @param HTML_QuickForm_element $field | |
332 | */ | |
00be9182 | 333 | public function appendUnselectButton(&$el, $field) { |
4a143c04 CW |
334 | // Initially hide if not needed |
335 | // Note: visibility:hidden prevents layout jumping around unlike display:none | |
8a4f27dc | 336 | $display = $field->getValue() !== NULL ? '' : ' style="visibility:hidden;"'; |
80bc2820 | 337 | $el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear') . '"><i class="crm-i fa-times"></i></a>'; |
4a143c04 | 338 | } |
96025800 | 339 | |
6a488035 | 340 | } |