3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 require_once 'HTML/QuickForm/Renderer/ArraySmarty.php';
39 * customize the 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
50 static private $_singleton = NULL;
53 * the converter from array size to css class
58 static $_sizeMapper = array(
73 */ function __construct() {
74 $template = CRM_Core_Smarty
::singleton();
75 parent
::__construct($template);
79 * Static instance provider.
81 * Method providing static instance of as in Singleton pattern.
83 static function &singleton() {
84 if (!isset(self
::$_singleton)) {
85 self
::$_singleton = new CRM_Core_Form_Renderer();
87 return self
::$_singleton;
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
98 * @param $element HTML_QuickForm_element
99 * @param $required bool - Whether an element is required
100 * @param $error string - Error associated with the element
104 function _elementToArray(&$element, $required, $error) {
105 self
::updateAttributes($element, $required, $error);
107 $el = parent
::_elementToArray($element, $required, $error);
110 if (!empty($el['label'])) {
111 $id = $element->getAttribute('id');
113 $el['label'] = '<label for="' . $id . '">' . $el['label'] . '</label>';
116 $el['label'] = "<label>{$el['label']}</label>";
120 // Display-only (frozen) elements
121 if (!empty($el['frozen'])) {
122 if ($element->getAttribute('data-api-entity') && $element->getAttribute('data-entity-value')) {
123 $this->renderFrozenEntityRef($el, $element);
125 $el['html'] = '<div class="crm-frozen-field">' . $el['html'] . '</div>';
127 // Active form elements
129 if ($element->getType() == 'select' && $element->getAttribute('data-option-group-url')) {
130 $this->addOptionsEditLink($el, $element);
133 if ($element->getType() == 'group' && $element->getAttribute('unselectable')) {
134 $this->appendUnselectButton($el, $element);
142 * Update the attributes of this element and add a few CiviCRM
143 * based attributes so we can style this form element better
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
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');
161 $attributes['id'] = str_replace(array(']', '['),
168 $class = $element->getAttribute('class');
169 $type = $element->getType();
171 if ($type == 'text') {
172 $size = $element->getAttribute('size');
174 $class = CRM_Utils_Array
::value($size, self
::$_sizeMapper);
179 if ($type == 'select' && $element->getAttribute('multiple')) {
180 $type = 'multiselect';
182 // Add widget-specific class
183 if (!$class ||
strpos($class, 'crm-form-') === FALSE) {
184 $class = ($class ?
"$class " : '') . 'crm-form-' . $type;
186 elseif (strpos($class, 'crm-form-entityref') !== FALSE) {
187 self
::preProcessEntityRef($element);
191 $class .= ' required';
198 $attributes['class'] = $class;
199 $element->updateAttributes($attributes);
203 * Convert IDs to values and format for display
205 static function preProcessEntityRef($field) {
206 $val = $field->getValue();
207 // Support array values
208 if (is_array($val)) {
209 $val = implode(',', $val);
210 $field->setValue($val);
213 $entity = $field->getAttribute('data-api-entity');
214 $select = json_decode($field->getAttribute('data-select-params'), TRUE);
215 // Support serialized values
216 if (strpos($val, CRM_Core_DAO
::VALUE_SEPARATOR
) !== FALSE) {
217 $val = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, ',', trim($val, CRM_Core_DAO
::VALUE_SEPARATOR
));
218 $field->setValue($val);
220 $result = civicrm_api3($entity, 'getlist', array('params' => array('id' => $val)));
221 if ($field->isFrozen()) {
222 $field->removeAttribute('class');
224 if (!empty($result['values'])) {
225 // Simplify array for single selects - makes client-side code simpler (but feels somehow wrong)
226 if (empty($select['multiple'])) {
227 $result['values'] = $result['values'][0];
229 $field->setAttribute('data-entity-value', json_encode($result['values']));
235 * Render entity references as text.
236 * If user has permission, format as link (or now limited to contacts).
238 * @param $field HTML_QuickForm_element
240 function renderFrozenEntityRef(&$el, $field) {
241 $entity = $field->getAttribute('data-api-entity');
242 $vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
243 if (isset($vals['id'])) {
244 $vals = array($vals);
247 foreach ($vals as $val) {
248 // Format contact as link
249 if ($entity == 'contact' && CRM_Contact_BAO_Contact_Permission
::allow($val['id'], CRM_Core_Permission
::VIEW
)) {
250 $url = CRM_Utils_System
::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id']));
251 $val['label'] = '<a href="' . $url . '" title="' . ts('View Contact') . '">' . $val['label'] . '</a>';
253 $display[] = $val['label'];
256 $el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
261 * @param HTML_QuickForm_element $field
263 function addOptionsEditLink(&$el, $field) {
264 if (CRM_Core_Permission
::check('administer CiviCRM')) {
265 $el['html'] .= ' <a href="#" class="crm-edit-optionvalue-link crm-hover-button" title="' . ts('Edit Options') . '" data-option-group-url="' . $field->getAttribute('data-option-group-url') . '"><span class="icon edit-icon"></span></a>';
271 * @param HTML_QuickForm_element $field
273 function appendUnselectButton(&$el, $field) {
274 // Initially hide if not needed
275 // Note: visibility:hidden prevents layout jumping around unlike display:none
276 $display = $field->getValue() !== NULL ?
'' : ' style="visibility:hidden;"';
277 $el['html'] .= ' <a href="#" class="crm-hover-button crm-clear-link"' . $display . ' title="' . ts('Clear') . '"><span class="icon close-icon"></span></a>';