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 if (!empty($el['frozen'])) {
121 if ($element->getAttribute('data-api-params') && $element->getAttribute('data-entity-value')) {
122 $this->renderFrozenEntityRef($el, $element);
124 $el['html'] = '<div class="crm-frozen-field">' . $el['html'] . '</div>';
131 * Update the attributes of this element and add a few CiviCRM
132 * based attributes so we can style this form element better
136 * @param object An HTML_QuickForm_element object
137 * @param bool Whether an element is required
138 * @param string Error associated with the element
143 static function updateAttributes(&$element, $required, $error) {
144 // lets create an id for all input elements, so we can generate nice label tags
145 // to make it nice and clean, we'll just use the elementName if it is non null
146 $attributes = array();
147 if (!$element->getAttribute('id')) {
148 $name = $element->getAttribute('name');
150 $attributes['id'] = str_replace(array(']', '['),
157 $class = $element->getAttribute('class');
158 $type = $element->getType();
160 $class = 'form-' . $type;
162 if ($type == 'text') {
163 $size = $element->getAttribute('size');
165 if (array_key_exists($size, self
::$_sizeMapper)) {
166 $class = $class . ' ' . self
::$_sizeMapper[$size];
173 $class .= ' required';
180 $attributes['class'] = $class;
181 $element->updateAttributes($attributes);
185 * Render entity references as text.
186 * If user has permission, format as link (or now limited to contacts).
188 * @param $field HTML_QuickForm_element
190 function renderFrozenEntityRef(&$el, $field) {
191 $api = json_decode($field->getAttribute('data-api-params'), TRUE);
192 $vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
193 if (isset($vals['id'])) {
194 $vals = array($vals);
197 foreach ($vals as $val) {
198 // Format contact as link
199 if ($api['entity'] == 'contact' && CRM_Contact_BAO_Contact_Permission
::allow($val['id'], CRM_Core_Permission
::VIEW
)) {
200 $url = CRM_Utils_System
::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id']));
201 $val['text'] = '<a href="' . $url . '" title="' . ts('View Contact') . '">' . $val['text'] . '</a>';
203 $display[] = $val['text'];
206 $el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
209 // end CRM_Core_Form_Renderer