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