Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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 | /** | |
39 | * customize the output to meet our specific requirements | |
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 | |
48 | * @static | |
49 | */ | |
50 | static private $_singleton = NULL; | |
51 | ||
52 | /** | |
53 | * the converter from array size to css class | |
54 | * | |
55 | * @var array | |
56 | * @static | |
57 | */ | |
58 | static $_sizeMapper = array( | |
59 | 2 => 'two', | |
60 | 4 => 'four', | |
61 | 6 => 'six', | |
62 | 8 => 'eight', | |
63 | 12 => 'twelve', | |
64 | 20 => 'medium', | |
65 | 30 => 'big', | |
66 | 45 => 'huge', | |
67 | ); | |
68 | ||
69 | /** | |
70 | * Constructor | |
71 | * | |
72 | * @access public | |
73 | */ function __construct() { | |
74 | $template = CRM_Core_Smarty::singleton(); | |
75 | parent::__construct($template); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Static instance provider. | |
80 | * | |
81 | * Method providing static instance of as in Singleton pattern. | |
82 | */ | |
83 | static function &singleton() { | |
84 | if (!isset(self::$_singleton)) { | |
85 | self::$_singleton = new CRM_Core_Form_Renderer(); | |
86 | } | |
87 | return self::$_singleton; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Creates an array representing an element containing | |
92 | * the key for storing this. We allow the parent to do most of the | |
93 | * work, but then we add some CiviCRM specific enhancements to | |
94 | * make the html compliant with our css etc | |
95 | * | |
96 | * @access private | |
97 | * | |
704d3260 CW |
98 | * @param $element HTML_QuickForm_element |
99 | * @param $required bool - Whether an element is required | |
100 | * @param $error string - Error associated with the element | |
6a488035 TO |
101 | * |
102 | * @return array | |
103 | */ | |
104 | function _elementToArray(&$element, $required, $error) { | |
105 | self::updateAttributes($element, $required, $error); | |
106 | ||
107 | $el = parent::_elementToArray($element, $required, $error); | |
108 | ||
109 | // add label html | |
110 | if (!empty($el['label'])) { | |
111 | $id = $element->getAttribute('id'); | |
112 | if (!empty($id)) { | |
113 | $el['label'] = '<label for="' . $id . '">' . $el['label'] . '</label>'; | |
114 | } | |
115 | else { | |
116 | $el['label'] = "<label>{$el['label']}</label>"; | |
117 | } | |
118 | } | |
119 | ||
4a143c04 | 120 | // Display-only (frozen) elements |
704d3260 | 121 | if (!empty($el['frozen'])) { |
549be786 | 122 | if ($element->getAttribute('data-api-entity') && $element->getAttribute('data-entity-value')) { |
704d3260 CW |
123 | $this->renderFrozenEntityRef($el, $element); |
124 | } | |
f0366d0b | 125 | $el['html'] = '<span class="crm-frozen-field">' . $el['html'] . '</span>'; |
704d3260 | 126 | } |
4a143c04 CW |
127 | // Active form elements |
128 | else { | |
87831073 | 129 | if ($element->getType() == 'select' && $element->getAttribute('data-option-edit-path')) { |
4a143c04 CW |
130 | $this->addOptionsEditLink($el, $element); |
131 | } | |
132 | ||
b847e6e7 | 133 | if ($element->getType() == 'group' && $element->getAttribute('allowClear')) { |
4a143c04 CW |
134 | $this->appendUnselectButton($el, $element); |
135 | } | |
136 | } | |
704d3260 | 137 | |
6a488035 TO |
138 | return $el; |
139 | } | |
140 | ||
141 | /** | |
142 | * Update the attributes of this element and add a few CiviCRM | |
143 | * based attributes so we can style this form element better | |
144 | * | |
145 | * @access private | |
146 | * | |
dafd2d75 CW |
147 | * @param $element HTML_QuickForm_element object |
148 | * @param $required bool Whether an element is required | |
149 | * @param $error string Error associated with the element | |
6a488035 TO |
150 | * |
151 | * @return array | |
152 | * @static | |
153 | */ | |
154 | static function updateAttributes(&$element, $required, $error) { | |
155 | // lets create an id for all input elements, so we can generate nice label tags | |
156 | // to make it nice and clean, we'll just use the elementName if it is non null | |
157 | $attributes = array(); | |
158 | if (!$element->getAttribute('id')) { | |
159 | $name = $element->getAttribute('name'); | |
160 | if ($name) { | |
161 | $attributes['id'] = str_replace(array(']', '['), | |
162 | array('', '_'), | |
163 | $name | |
164 | ); | |
165 | } | |
166 | } | |
167 | ||
168 | $class = $element->getAttribute('class'); | |
169 | $type = $element->getType(); | |
c5b53a6f | 170 | if (!$class) { |
6a488035 TO |
171 | if ($type == 'text') { |
172 | $size = $element->getAttribute('size'); | |
173 | if (!empty($size)) { | |
dafd2d75 | 174 | $class = CRM_Utils_Array::value($size, self::$_sizeMapper); |
6a488035 TO |
175 | } |
176 | } | |
177 | } | |
178 | ||
4f95329c CW |
179 | if ($type == 'select' && $element->getAttribute('multiple')) { |
180 | $type = 'multiselect'; | |
181 | } | |
76ec9ca7 CW |
182 | // Add widget-specific class |
183 | if (!$class || strpos($class, 'crm-form-') === FALSE) { | |
184 | $class = ($class ? "$class " : '') . 'crm-form-' . $type; | |
185 | } | |
75f59466 CW |
186 | elseif (strpos($class, 'crm-form-entityref') !== FALSE) { |
187 | self::preProcessEntityRef($element); | |
188 | } | |
06508628 CW |
189 | elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) { |
190 | self::preprocessContactReference($element); | |
191 | } | |
c5b53a6f | 192 | |
6a488035 TO |
193 | if ($required) { |
194 | $class .= ' required'; | |
195 | } | |
196 | ||
197 | if ($error) { | |
198 | $class .= ' error'; | |
199 | } | |
200 | ||
201 | $attributes['class'] = $class; | |
202 | $element->updateAttributes($attributes); | |
203 | } | |
704d3260 | 204 | |
75f59466 CW |
205 | /** |
206 | * Convert IDs to values and format for display | |
06508628 CW |
207 | * |
208 | * @param $field HTML_QuickForm_element | |
75f59466 CW |
209 | */ |
210 | static function preProcessEntityRef($field) { | |
211 | $val = $field->getValue(); | |
212 | // Support array values | |
213 | if (is_array($val)) { | |
214 | $val = implode(',', $val); | |
215 | $field->setValue($val); | |
216 | } | |
217 | if ($val) { | |
218 | $entity = $field->getAttribute('data-api-entity'); | |
e6d37e50 CW |
219 | // Get api params, ensure it is an array |
220 | $params = $field->getAttribute('data-api-params'); | |
221 | $params = $params ? json_decode($params, TRUE) : array(); | |
75f59466 CW |
222 | // Support serialized values |
223 | if (strpos($val, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) { | |
224 | $val = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($val, CRM_Core_DAO::VALUE_SEPARATOR)); | |
225 | $field->setValue($val); | |
226 | } | |
61d869af | 227 | $result = civicrm_api3($entity, 'getlist', array('id' => $val) + $params); |
75f59466 CW |
228 | if ($field->isFrozen()) { |
229 | $field->removeAttribute('class'); | |
230 | } | |
231 | if (!empty($result['values'])) { | |
75f59466 CW |
232 | $field->setAttribute('data-entity-value', json_encode($result['values'])); |
233 | } | |
234 | } | |
235 | } | |
236 | ||
704d3260 CW |
237 | /** |
238 | * Render entity references as text. | |
b56fec55 | 239 | * If user has permission, format as link (for now limited to contacts). |
704d3260 CW |
240 | * @param $el array |
241 | * @param $field HTML_QuickForm_element | |
242 | */ | |
243 | function renderFrozenEntityRef(&$el, $field) { | |
549be786 | 244 | $entity = $field->getAttribute('data-api-entity'); |
704d3260 | 245 | $vals = json_decode($field->getAttribute('data-entity-value'), TRUE); |
704d3260 CW |
246 | $display = array(); |
247 | foreach ($vals as $val) { | |
248 | // Format contact as link | |
549be786 | 249 | if ($entity == 'contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) { |
704d3260 | 250 | $url = CRM_Utils_System::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id'])); |
b56fec55 | 251 | $val['label'] = '<a class="view-' . $entity . ' no-popup" href="' . $url . '" title="' . ts('View Contact') . '">' . $val['label'] . '</a>'; |
704d3260 | 252 | } |
549be786 | 253 | $display[] = $val['label']; |
704d3260 CW |
254 | } |
255 | ||
256 | $el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">'; | |
257 | } | |
5fafc9b0 | 258 | |
06508628 CW |
259 | /** |
260 | * Pre-fill contact name for a custom field of type ContactReference | |
261 | * | |
262 | * Todo: Migrate contact reference fields to use EntityRef | |
263 | * | |
264 | * @param $field HTML_QuickForm_element | |
265 | */ | |
266 | static function preprocessContactReference($field) { | |
267 | $val = $field->getValue(); | |
268 | if ($val && is_numeric($val)) { | |
269 | ||
270 | $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
271 | 'contact_reference_options' | |
272 | ), '1'); | |
273 | ||
274 | $return = array_unique(array_merge(array('sort_name'), $list)); | |
275 | ||
276 | $contact = civicrm_api('contact', 'getsingle', array('id' => $val, 'return' => $return, 'version' => 3)); | |
277 | ||
278 | if (!empty($contact['id'])) { | |
279 | $view = array(); | |
280 | foreach ($return as $fld) { | |
281 | if (!empty($contact[$fld])) { | |
282 | $view[] = $contact[$fld]; | |
283 | } | |
284 | } | |
285 | $field->setAttribute('data-entity-value', json_encode(array('id' => $contact['id'], 'text' => implode(' :: ', $view)))); | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
5fafc9b0 CW |
290 | /** |
291 | * @param array $el | |
292 | * @param HTML_QuickForm_element $field | |
293 | */ | |
294 | function addOptionsEditLink(&$el, $field) { | |
295 | if (CRM_Core_Permission::check('administer CiviCRM')) { | |
87831073 CW |
296 | // NOTE: $path is used on the client-side to know which option lists need rebuilding, |
297 | // that's why we need that bit of data both in the link and in the form element | |
298 | $path = $field->getAttribute('data-option-edit-path'); | |
299 | // NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ? | |
300 | $url = CRM_Utils_System::url($path, 'reset=1'); | |
c06dfbe4 | 301 | $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 edit-icon"></span></a>'; |
5fafc9b0 CW |
302 | } |
303 | } | |
4a143c04 CW |
304 | |
305 | /** | |
306 | * @param array $el | |
307 | * @param HTML_QuickForm_element $field | |
308 | */ | |
309 | function appendUnselectButton(&$el, $field) { | |
310 | // Initially hide if not needed | |
311 | // Note: visibility:hidden prevents layout jumping around unlike display:none | |
8a4f27dc | 312 | $display = $field->getValue() !== NULL ? '' : ' style="visibility:hidden;"'; |
4a143c04 CW |
313 | $el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear') . '"><span class="icon close-icon"></span></a>'; |
314 | } | |
6a488035 | 315 | } |
6a488035 | 316 |