Merge pull request #2495 from civicrm/4.4
[civicrm-core.git] / CRM / Core / Form / Renderer.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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 *
98 * @param $element HTML_QuickForm_element
99 * @param $required bool - Whether an element is required
100 * @param $error string - Error associated with the element
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
120 if ($element->getType() == 'select' && $element->getAttribute('data-option-group-url')) {
121 $this->addOptionsEditLink($el, $element);
122 }
123
124 if (!empty($el['frozen'])) {
125 if ($element->getAttribute('data-api-params') && $element->getAttribute('data-entity-value')) {
126 $this->renderFrozenEntityRef($el, $element);
127 }
128 $el['html'] = '<div class="crm-frozen-field">' . $el['html'] . '</div>';
129 }
130
131 return $el;
132 }
133
134 /**
135 * Update the attributes of this element and add a few CiviCRM
136 * based attributes so we can style this form element better
137 *
138 * @access private
139 *
140 * @param object An HTML_QuickForm_element object
141 * @param bool Whether an element is required
142 * @param string Error associated with the element
143 *
144 * @return array
145 * @static
146 */
147 static function updateAttributes(&$element, $required, $error) {
148 // lets create an id for all input elements, so we can generate nice label tags
149 // to make it nice and clean, we'll just use the elementName if it is non null
150 $attributes = array();
151 if (!$element->getAttribute('id')) {
152 $name = $element->getAttribute('name');
153 if ($name) {
154 $attributes['id'] = str_replace(array(']', '['),
155 array('', '_'),
156 $name
157 );
158 }
159 }
160
161 $class = $element->getAttribute('class');
162 $type = $element->getType();
163 if (!$class) {
164 if ($type == 'text') {
165 $size = $element->getAttribute('size');
166 if (!empty($size)) {
167 if (array_key_exists($size, self::$_sizeMapper)) {
168 $class .= ' ' . self::$_sizeMapper[$size];
169 }
170 }
171 }
172 }
173
174 $class .= ($class ? ' ' : '') . 'crm-form-' . $type;
175
176 if ($required) {
177 $class .= ' required';
178 }
179
180 if ($error) {
181 $class .= ' error';
182 }
183
184 $attributes['class'] = $class;
185 $element->updateAttributes($attributes);
186 }
187
188 /**
189 * Render entity references as text.
190 * If user has permission, format as link (or now limited to contacts).
191 * @param $el array
192 * @param $field HTML_QuickForm_element
193 */
194 function renderFrozenEntityRef(&$el, $field) {
195 $api = json_decode($field->getAttribute('data-api-params'), TRUE);
196 $vals = json_decode($field->getAttribute('data-entity-value'), TRUE);
197 if (isset($vals['id'])) {
198 $vals = array($vals);
199 }
200 $display = array();
201 foreach ($vals as $val) {
202 // Format contact as link
203 if ($api['entity'] == 'contact' && CRM_Contact_BAO_Contact_Permission::allow($val['id'], CRM_Core_Permission::VIEW)) {
204 $url = CRM_Utils_System::url("civicrm/contact/view", array('reset' => 1, 'cid' => $val['id']));
205 $val['text'] = '<a href="' . $url . '" title="' . ts('View Contact') . '">' . $val['text'] . '</a>';
206 }
207 $display[] = $val['text'];
208 }
209
210 $el['html'] = implode('; ', $display) . '<input type="hidden" value="'. $field->getValue() . '" name="' . $field->getAttribute('name') . '">';
211 }
212
213 /**
214 * @param array $el
215 * @param HTML_QuickForm_element $field
216 */
217 function addOptionsEditLink(&$el, $field) {
218 if (CRM_Core_Permission::check('administer CiviCRM')) {
219 $el['html'] .= ' &nbsp; <a href="#" class="crm-edit-optionvalue-link" title="' . ts('Edit Options') . '" data-option-group-url="' . $field->getAttribute('data-option-group-url') . '"><span class="batch-edit"></span></a>';
220 }
221 }
222 }
223 // end CRM_Core_Form_Renderer
224