3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
36 require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
39 * Customize QF output to meet our specific requirements
41 class CRM_Core_Form_Renderer
extends HTML_QuickForm_Renderer_ArraySmarty
{
44 * We only need one instance of this object. So we use the singleton
45 * pattern and cache the instance in this variable
49 static private $_singleton = NULL;
52 * The converter from array size to css class.
56 static $_sizeMapper = array(
70 public function __construct() {
71 $template = CRM_Core_Smarty
::singleton();
72 parent
::__construct($template);
76 * Static instance provider.
78 * Method providing static instance of as in Singleton pattern.
80 public static function &singleton() {
81 if (!isset(self
::$_singleton)) {
82 self
::$_singleton = new CRM_Core_Form_Renderer();
84 return self
::$_singleton;
88 * Creates an array representing an element containing.
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
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.
102 public function _elementToArray(&$element, $required, $error) {
103 self
::updateAttributes($element, $required, $error);
105 $el = parent
::_elementToArray($element, $required, $error);
108 if (!empty($el['label'])) {
109 $id = $element->getAttribute('id');
111 $el['label'] = '<label for="' . $id . '">' . $el['label'] . '</label>';
114 $el['label'] = "<label>{$el['label']}</label>";
118 // Display-only (frozen) elements
119 if (!empty($el['frozen'])) {
120 if ($element->getAttribute('data-api-entity') && $element->getAttribute('data-entity-value')) {
121 $this->renderFrozenEntityRef($el, $element);
123 elseif ($element->getAttribute('type') == 'text' && $element->getAttribute('formatType')) {
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') . '">';
128 // Render html for wysiwyg textareas
129 if ($el['type'] == 'textarea' && isset($element->_attributes
['class']) && strstr($element->_attributes
['class'], 'wysiwyg')) {
130 $el['html'] = '<span class="crm-frozen-field">' . $el['value'] . '</span>';
133 $el['html'] = '<span class="crm-frozen-field">' . $el['html'] . '</span>';
136 // Active form elements
138 if ($element->getType() == 'select' && $element->getAttribute('data-option-edit-path')) {
139 $this->addOptionsEditLink($el, $element);
142 if ($element->getAttribute('allowClear')) {
143 $this->appendUnselectButton($el, $element);
151 * Update the attributes of this element and add a few CiviCRM
152 * based attributes so we can style this form element better
155 * @param HTML_QuickForm_element $element
156 * @param bool $required
157 * Whether an element is required.
158 * @param string $error
159 * Error associated with the element.
162 public static function updateAttributes(&$element, $required, $error) {
163 // lets create an id for all input elements, so we can generate nice label tags
164 // to make it nice and clean, we'll just use the elementName if it is non null
165 $attributes = array();
166 if (!$element->getAttribute('id')) {
167 $name = $element->getAttribute('name');
169 $attributes['id'] = str_replace(array(']', '['),
176 $class = $element->getAttribute('class');
177 $type = $element->getType();
179 if ($type == 'text') {
180 $size = $element->getAttribute('size');
182 $class = CRM_Utils_Array
::value($size, self
::$_sizeMapper);
187 if ($type == 'select' && $element->getAttribute('multiple')) {
188 $type = 'multiselect';
190 // Add widget-specific class
191 if (!$class ||
strpos($class, 'crm-form-') === FALSE) {
192 $class = ($class ?
"$class " : '') . 'crm-form-' . $type;
194 elseif (strpos($class, 'crm-form-entityref') !== FALSE) {
195 self
::preProcessEntityRef($element);
197 elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) {
198 self
::preprocessContactReference($element);
200 // Hack to support html5 fields (number, url, etc)
202 foreach (CRM_Core_Form
::$html5Types as $type) {
203 if (strpos($class, "crm-form-$type") !== FALSE) {
204 $element->setAttribute('type', $type);
205 // Also add the "base" class for consistent styling
206 $class .= ' crm-form-text';
213 $class .= ' required';
220 $attributes['class'] = $class;
221 $element->updateAttributes($attributes);
225 * Convert IDs to values and format for display.
227 * @param HTML_QuickForm_element $field
229 public static function preProcessEntityRef($field) {
230 $val = $field->getValue();
231 // Temporarily convert string values to an array
232 if (!is_array($val)) {
233 // Try to auto-detect method of serialization
234 $val = strpos($val, ',') ?
explode(',', str_replace(', ', ',', $val)) : (array) CRM_Utils_Array
::explodePadded($val);
237 $entity = $field->getAttribute('data-api-entity');
238 // Get api params, ensure it is an array
239 $params = $field->getAttribute('data-api-params');
240 $params = $params ?
json_decode($params, TRUE) : array();
241 $result = civicrm_api3($entity, 'getlist', array('id' => $val) +
$params);
242 if ($field->isFrozen()) {
243 // Prevent js from treating frozen entityRef as a "live" field
244 $field->removeAttribute('class');
246 if (!empty($result['values'])) {
247 $field->setAttribute('data-entity-value', json_encode($result['values']));
249 // CRM-15803 - Remove invalid values
250 $val = array_intersect($val, CRM_Utils_Array
::collect('id', $result['values']));
252 // Convert array values back to a string
253 $field->setValue(implode(',', $val));
257 * Render entity references as text.
258 * If user has permission, format as link (for now limited to contacts).
261 * @param HTML_QuickForm_element $field
263 public function renderFrozenEntityRef(&$el, $field) {
264 $entity = strtolower($field->getAttribute('data-api-entity'));
265 $vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
268 // Custom fields of type contactRef store their data in a slightly different format
269 if ($field->getAttribute('data-crm-custom') && $entity == 'contact') {
270 $vals = array(array('id' => $vals['id'], 'label' => $vals['text']));
273 foreach ($vals as $val) {
274 // Format contact as link
275 if ($entity == 'contact' && CRM_Contact_BAO_Contact_Permission
::allow($val['id'], CRM_Core_Permission
::VIEW
)) {
276 $url = CRM_Utils_System
::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id']));
277 $val['label'] = '<a class="view-' . $entity . ' no-popup" href="' . $url . '" title="' . ts('View Contact') . '">' . $val['label'] . '</a>';
279 $display[] = $val['label'];
282 $el['html'] = implode('; ', $display) . '<input type="hidden" value="' . $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
286 * Pre-fill contact name for a custom field of type ContactReference
288 * Todo: Migrate contact reference fields to use EntityRef
290 * @param HTML_QuickForm_element $field
292 public static function preprocessContactReference($field) {
293 $val = $field->getValue();
294 if ($val && is_numeric($val)) {
296 $list = array_keys(CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
297 'contact_reference_options'
300 $return = array_unique(array_merge(array('sort_name'), $list));
302 $contact = civicrm_api('contact', 'getsingle', array('id' => $val, 'return' => $return, 'version' => 3));
304 if (!empty($contact['id'])) {
306 foreach ($return as $fld) {
307 if (!empty($contact[$fld])) {
308 $view[] = $contact[$fld];
311 $field->setAttribute('data-entity-value', json_encode(array(
312 'id' => $contact['id'],
313 'text' => implode(' :: ', $view),
321 * @param HTML_QuickForm_element $field
323 public function addOptionsEditLink(&$el, $field) {
324 if (CRM_Core_Permission
::check('administer CiviCRM')) {
325 // NOTE: $path is used on the client-side to know which option lists need rebuilding,
326 // that's why we need that bit of data both in the link and in the form element
327 $path = $field->getAttribute('data-option-edit-path');
328 // NOTE: If we ever needed to support arguments in this link other than reset=1 we could split $path here if it contains a ?
329 $url = CRM_Utils_System
::url($path, 'reset=1');
330 $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>';
336 * @param HTML_QuickForm_element $field
338 public function appendUnselectButton(&$el, $field) {
339 // Initially hide if not needed
340 // Note: visibility:hidden prevents layout jumping around unlike display:none
341 $display = $field->getValue() !== NULL ?
'' : ' style="visibility:hidden;"';
342 $el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear') . '"><i class="crm-i fa-times"></i></a>';