INFRA-132 - CRM/Contribute - Convert single-line @param to multi-line
[civicrm-core.git] / CRM / Core / Form / Renderer.php
CommitLineData
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
37
38/**
39 * customize the output to meet our specific requirements
40 */
41class 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 /**
100fef9d 53 * The converter from array size to css class
6a488035
TO
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 *
95ea96be
EM
72 */
73 function __construct() {
6a488035
TO
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 */
00be9182 83 public static function &singleton() {
6a488035
TO
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 *
6a488035 96 *
704d3260
CW
97 * @param $element HTML_QuickForm_element
98 * @param $required bool - Whether an element is required
99 * @param $error string - Error associated with the element
6a488035
TO
100 *
101 * @return array
102 */
00be9182 103 public function _elementToArray(&$element, $required, $error) {
6a488035
TO
104 self::updateAttributes($element, $required, $error);
105
106 $el = parent::_elementToArray($element, $required, $error);
107
108 // add label html
109 if (!empty($el['label'])) {
110 $id = $element->getAttribute('id');
111 if (!empty($id)) {
112 $el['label'] = '<label for="' . $id . '">' . $el['label'] . '</label>';
113 }
114 else {
115 $el['label'] = "<label>{$el['label']}</label>";
116 }
117 }
118
4a143c04 119 // Display-only (frozen) elements
704d3260 120 if (!empty($el['frozen'])) {
549be786 121 if ($element->getAttribute('data-api-entity') && $element->getAttribute('data-entity-value')) {
704d3260
CW
122 $this->renderFrozenEntityRef($el, $element);
123 }
f0366d0b 124 $el['html'] = '<span class="crm-frozen-field">' . $el['html'] . '</span>';
704d3260 125 }
4a143c04
CW
126 // Active form elements
127 else {
87831073 128 if ($element->getType() == 'select' && $element->getAttribute('data-option-edit-path')) {
4a143c04
CW
129 $this->addOptionsEditLink($el, $element);
130 }
131
b847e6e7 132 if ($element->getType() == 'group' && $element->getAttribute('allowClear')) {
4a143c04
CW
133 $this->appendUnselectButton($el, $element);
134 }
135 }
704d3260 136
6a488035
TO
137 return $el;
138 }
139
140 /**
141 * Update the attributes of this element and add a few CiviCRM
142 * based attributes so we can style this form element better
143 *
6a488035 144 *
dafd2d75
CW
145 * @param $element HTML_QuickForm_element object
146 * @param $required bool Whether an element is required
147 * @param $error string Error associated with the element
6a488035
TO
148 *
149 * @return array
150 * @static
151 */
00be9182 152 public static function updateAttributes(&$element, $required, $error) {
6a488035
TO
153 // lets create an id for all input elements, so we can generate nice label tags
154 // to make it nice and clean, we'll just use the elementName if it is non null
155 $attributes = array();
156 if (!$element->getAttribute('id')) {
157 $name = $element->getAttribute('name');
158 if ($name) {
159 $attributes['id'] = str_replace(array(']', '['),
160 array('', '_'),
161 $name
162 );
163 }
164 }
165
166 $class = $element->getAttribute('class');
167 $type = $element->getType();
c5b53a6f 168 if (!$class) {
6a488035
TO
169 if ($type == 'text') {
170 $size = $element->getAttribute('size');
171 if (!empty($size)) {
dafd2d75 172 $class = CRM_Utils_Array::value($size, self::$_sizeMapper);
6a488035
TO
173 }
174 }
175 }
176
4f95329c
CW
177 if ($type == 'select' && $element->getAttribute('multiple')) {
178 $type = 'multiselect';
179 }
76ec9ca7
CW
180 // Add widget-specific class
181 if (!$class || strpos($class, 'crm-form-') === FALSE) {
182 $class = ($class ? "$class " : '') . 'crm-form-' . $type;
183 }
75f59466
CW
184 elseif (strpos($class, 'crm-form-entityref') !== FALSE) {
185 self::preProcessEntityRef($element);
186 }
06508628
CW
187 elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) {
188 self::preprocessContactReference($element);
189 }
c5b53a6f 190
6a488035
TO
191 if ($required) {
192 $class .= ' required';
193 }
194
195 if ($error) {
196 $class .= ' error';
197 }
198
199 $attributes['class'] = $class;
200 $element->updateAttributes($attributes);
201 }
704d3260 202
75f59466
CW
203 /**
204 * Convert IDs to values and format for display
06508628
CW
205 *
206 * @param $field HTML_QuickForm_element
75f59466 207 */
00be9182 208 public static function preProcessEntityRef($field) {
75f59466
CW
209 $val = $field->getValue();
210 // Support array values
211 if (is_array($val)) {
212 $val = implode(',', $val);
213 $field->setValue($val);
214 }
215 if ($val) {
216 $entity = $field->getAttribute('data-api-entity');
e6d37e50
CW
217 // Get api params, ensure it is an array
218 $params = $field->getAttribute('data-api-params');
219 $params = $params ? json_decode($params, TRUE) : array();
75f59466
CW
220 // Support serialized values
221 if (strpos($val, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) {
222 $val = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($val, CRM_Core_DAO::VALUE_SEPARATOR));
223 $field->setValue($val);
224 }
61d869af 225 $result = civicrm_api3($entity, 'getlist', array('id' => $val) + $params);
75f59466
CW
226 if ($field->isFrozen()) {
227 $field->removeAttribute('class');
228 }
229 if (!empty($result['values'])) {
75f59466
CW
230 $field->setAttribute('data-entity-value', json_encode($result['values']));
231 }
232 }
233 }
234
704d3260
CW
235 /**
236 * Render entity references as text.
b56fec55 237 * If user has permission, format as link (for now limited to contacts).
704d3260
CW
238 * @param $el array
239 * @param $field HTML_QuickForm_element
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
260 $el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
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 *
268 * @param $field HTML_QuickForm_element
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 }
289 $field->setAttribute('data-entity-value', json_encode(array('id' => $contact['id'], 'text' => implode(' :: ', $view))));
290 }
291 }
292 }
293
5fafc9b0
CW
294 /**
295 * @param array $el
296 * @param HTML_QuickForm_element $field
297 */
00be9182 298 public function addOptionsEditLink(&$el, $field) {
5fafc9b0 299 if (CRM_Core_Permission::check('administer CiviCRM')) {
87831073
CW
300 // NOTE: $path is used on the client-side to know which option lists need rebuilding,
301 // that's why we need that bit of data both in the link and in the form element
302 $path = $field->getAttribute('data-option-edit-path');
303 // NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ?
304 $url = CRM_Utils_System::url($path, 'reset=1');
5ce7d22c 305 $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
306 }
307 }
4a143c04
CW
308
309 /**
310 * @param array $el
311 * @param HTML_QuickForm_element $field
312 */
00be9182 313 public function appendUnselectButton(&$el, $field) {
4a143c04
CW
314 // Initially hide if not needed
315 // Note: visibility:hidden prevents layout jumping around unlike display:none
8a4f27dc 316 $display = $field->getValue() !== NULL ? '' : ' style="visibility:hidden;"';
4a143c04
CW
317 $el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear') . '"><span class="icon close-icon"></span></a>';
318 }
6a488035 319}