INFRA-132 - CRM/Core - phpcbf
[civicrm-core.git] / CRM / Core / QuickForm / GroupMultiSelect.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright U.S. PIRG Education Fund (c) 2007 |
7 | Licensed to CiviCRM under the Academic Free License version 3.0. |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
32 * @copyright U.S. PIRG Education Fund 2007
33 * $Id$
34 *
35 */
36class CRM_Core_QuickForm_GroupMultiSelect extends CRM_Core_QuickForm_NestedAdvMultiSelect {
b5c2afd0
EM
37 /**
38 * Returns the HTML generated for the advanced mutliple select component
39 *
b5c2afd0
EM
40 * @return string
41 * @since version 0.4.0 (2005-06-25)
42 */
00be9182 43 public function toHtml() {
6a488035
TO
44 if ($this->_flagFrozen) {
45 return $this->getFrozenHtml();
46 }
47
48 $tabs = $this->_getTabs();
49 $tab = $this->_getTab();
50 $strHtml = '';
51
52 if ($this->getComment() != '') {
53 $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->" . PHP_EOL;
54 }
55
56 $selectName = $this->getName() . '[]';
57
58 // placeholder {unselected} existence determines if we will render
59 if (strpos($this->_elementTemplate, '{unselected}') === FALSE) {
60 // ... a single multi-select with checkboxes
61
62 $id = $this->getAttribute('id');
63
64 $strHtmlSelected = $tab . '<div id="' . $id . 'amsSelected">' . PHP_EOL;
65
66 foreach ($this->_options as $option) {
67
68 $_labelAttributes = array('style', 'class', 'onmouseover', 'onmouseout');
69 $labelAttributes = array();
70 foreach ($_labelAttributes as $attr) {
71 if (isset($option['attr'][$attr])) {
72 $labelAttributes[$attr] = $option['attr'][$attr];
73 unset($option['attr'][$attr]);
74 }
75 }
76
2aa397bc 77 if (is_array($this->_values) && in_array((string) $option['attr']['value'], $this->_values)) {
6a488035
TO
78 // The items is *selected*
79 $checked = ' checked="checked"';
80 }
81 else {
82 // The item is *unselected* so we want to put it
83 $checked = '';
84 }
85 $strHtmlSelected .= $tab . '<label' . $this->_getAttrString($labelAttributes) . '>' . '<input type="checkbox"' . ' id="' . $this->getName() . '"' . ' name="' . $selectName . '"' . $checked . $this->_getAttrString($option['attr']) . ' />' . $option['text'] . '</label>' . PHP_EOL;
86 }
87 $strHtmlSelected .= $tab . '</div>' . PHP_EOL;
88
89 $strHtmlHidden = '';
90 $strHtmlUnselected = '';
91 $strHtmlAdd = '';
92 $strHtmlRemove = '';
93
94 // build the select all button with all its attributes
95 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('" . $this->getName() . "', 1);");
96 $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes);
97 $attrStrAll = $this->_getAttrString($this->_allButtonAttributes);
98 $strHtmlAll = "<input$attrStrAll />" . PHP_EOL;
99
100 // build the select none button with all its attributes
101 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('" . $this->getName() . "', 0);");
102 $this->_noneButtonAttributes = array_merge($this->_noneButtonAttributes, $attributes);
103 $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes);
104 $strHtmlNone = "<input$attrStrNone />" . PHP_EOL;
105
106 // build the toggle selection button with all its attributes
107 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('" . $this->getName() . "', 2);");
108 $this->_toggleButtonAttributes = array_merge($this->_toggleButtonAttributes, $attributes);
109 $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes);
110 $strHtmlToggle = "<input$attrStrToggle />" . PHP_EOL;
111
112 $strHtmlMoveUp = '';
113 $strHtmlMoveDown = '';
114 }
115 else {
116 // ... or a dual multi-select
117
118 // set name of Select From Box
119 $this->_attributesUnselected = array('name' => '__' . $selectName, 'ondblclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'add')");
120 $this->_attributesUnselected = array_merge($this->_attributes, $this->_attributesUnselected);
121 $attrUnselected = $this->_getAttrString($this->_attributesUnselected);
122
123 // set name of Select To Box
124 $this->_attributesSelected = array('name' => '_' . $selectName, 'ondblclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'remove')");
125 $this->_attributesSelected = array_merge($this->_attributes, $this->_attributesSelected);
126 $attrSelected = $this->_getAttrString($this->_attributesSelected);
127
128 // set name of Select hidden Box
129 $this->_attributesHidden = array('name' => $selectName, 'style' => 'overflow: hidden; visibility: hidden; width: 1px; height: 0;');
130 $this->_attributesHidden = array_merge($this->_attributes, $this->_attributesHidden);
131 $attrHidden = $this->_getAttrString($this->_attributesHidden);
132
133 // prepare option tables to be displayed as in POST order
134 $append = count($this->_values);
135 if ($append > 0) {
136 $arrHtmlSelected = array_fill(0, $append, ' ');
137 }
138 else {
139 $arrHtmlSelected = array();
140 }
141
142 $options = count($this->_options);
143 $arrHtmlUnselected = array();
144 if ($options > 0) {
145 $arrHtmlHidden = array_fill(0, $options, ' ');
146
147 foreach ($this->_options as $option) {
148 if (is_array($this->_values) &&
2aa397bc 149 in_array((string) $option['attr']['value'], $this->_values)
6a488035
TO
150 ) {
151 // Get the post order
152 $key = array_search($option['attr']['value'], $this->_values);
153
154 // The items is *selected* so we want to put it in the 'selected' multi-select
155 $arrHtmlSelected[$key] = $option;
156 // Add it to the 'hidden' multi-select and set it as 'selected'
157 $option['attr']['selected'] = 'selected';
158 $arrHtmlHidden[$key] = $option;
159 }
160 else {
161 // The item is *unselected* so we want to put it in the 'unselected' multi-select
162 $arrHtmlUnselected[] = $option;
163 // Add it to the hidden multi-select as 'unselected'
164 $arrHtmlHidden[$append] = $option;
165 $append++;
166 }
167 }
168 }
169 else {
170 $arrHtmlHidden = array();
171 }
172
173 // The 'unselected' multi-select which appears on the left
174 $strHtmlUnselected = "<select$attrUnselected>" . PHP_EOL;
175 if (count($arrHtmlUnselected) > 0) {
176 foreach ($arrHtmlUnselected as $data) {
177 $strHtmlUnselected .= $tabs . $tab . '<option' . $this->_getAttrString($data['attr']) . '>' . $data['text'] . '</option>' . PHP_EOL;
178 }
179 }
180 $strHtmlUnselected .= '</select>';
181
182 // The 'selected' multi-select which appears on the right
183 $strHtmlSelected = "<select$attrSelected>" . PHP_EOL;
184 if (count($arrHtmlSelected) > 0) {
185 foreach ($arrHtmlSelected as $data) {
186 $strHtmlSelected .= $tabs . $tab . '<option' . $this->_getAttrString($data['attr']) . '>' . $data['text'] . '</option>' . PHP_EOL;
187 }
188 }
189 $strHtmlSelected .= '</select>';
190
191 // The 'hidden' multi-select
192 $strHtmlHidden = "<select$attrHidden>" . PHP_EOL;
193 if (count($arrHtmlHidden) > 0) {
194 foreach ($arrHtmlHidden as $data) {
195 $strHtmlHidden .= $tabs . $tab . '<option' . $this->_getAttrString($data['attr']) . '>' . $data['text'] . '</option>' . PHP_EOL;
196 }
197 }
198 $strHtmlHidden .= '</select>';
199
200 // build the remove button with all its attributes
201 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'remove'); return false;");
202 $this->_removeButtonAttributes = array_merge($this->_removeButtonAttributes, $attributes);
203 $attrStrRemove = $this->_getAttrString($this->_removeButtonAttributes);
204 $strHtmlRemove = "<input$attrStrRemove />" . PHP_EOL;
205
206 // build the add button with all its attributes
207 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'add'); return false;");
208 $this->_addButtonAttributes = array_merge($this->_addButtonAttributes, $attributes);
209 $attrStrAdd = $this->_getAttrString($this->_addButtonAttributes);
210 $strHtmlAdd = "<input$attrStrAdd />" . PHP_EOL;
211
212 // build the select all button with all its attributes
213 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'all'); return false;");
214 $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes);
215 $attrStrAll = $this->_getAttrString($this->_allButtonAttributes);
216 $strHtmlAll = "<input$attrStrAll />" . PHP_EOL;
217
218 // build the select none button with all its attributes
219 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'none'); return false;");
220 $this->_noneButtonAttributes = array_merge($this->_noneButtonAttributes, $attributes);
221 $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes);
222 $strHtmlNone = "<input$attrStrNone />" . PHP_EOL;
223
224 // build the toggle button with all its attributes
225 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'toggle'); return false;");
226 $this->_toggleButtonAttributes = array_merge($this->_toggleButtonAttributes, $attributes);
227 $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes);
228 $strHtmlToggle = "<input$attrStrToggle />" . PHP_EOL;
229
230 // build the move up button with all its attributes
231 $attributes = array('onclick' => "{$this->_jsPrefix}moveUp(this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "']); return false;");
232 $this->_upButtonAttributes = array_merge($this->_upButtonAttributes, $attributes);
233 $attrStrUp = $this->_getAttrString($this->_upButtonAttributes);
234 $strHtmlMoveUp = "<input$attrStrUp />" . PHP_EOL;
235
236 // build the move down button with all its attributes
237 $attributes = array('onclick' => "{$this->_jsPrefix}moveDown(this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "']); return false;");
238 $this->_downButtonAttributes = array_merge($this->_downButtonAttributes, $attributes);
239 $attrStrDown = $this->_getAttrString($this->_downButtonAttributes);
240 $strHtmlMoveDown = "<input$attrStrDown />" . PHP_EOL;
241 }
242
243 // render all part of the multi select component with the template
244 $strHtml = $this->_elementTemplate;
245
246 // Prepare multiple labels
247 $labels = $this->getLabel();
248 if (is_array($labels)) {
249 array_shift($labels);
250 }
251 // render extra labels, if any
252 if (is_array($labels)) {
253 foreach ($labels as $key => $text) {
254 $key = is_int($key) ? $key + 2 : $key;
255 $strHtml = str_replace("{label_{$key}}", $text, $strHtml);
256 $strHtml = str_replace("<!-- BEGIN label_{$key} -->", '', $strHtml);
257 $strHtml = str_replace("<!-- END label_{$key} -->", '', $strHtml);
258 }
259 }
260 // clean up useless label tags
261 if (strpos($strHtml, '{label_')) {
262 $strHtml = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i', '', $strHtml);
263 }
264
265 $placeHolders = array(
266 '{stylesheet}', '{javascript}', '{class}',
267 '{unselected}', '{selected}',
268 '{add}', '{remove}',
269 '{all}', '{none}', '{toggle}',
270 '{moveup}', '{movedown}',
271 );
272 $htmlElements = array(
273 $this->getElementCss(FALSE), $this->getElementJs(FALSE), $this->_tableAttributes,
274 $strHtmlUnselected, $strHtmlSelected . $strHtmlHidden,
275 $strHtmlAdd, $strHtmlRemove,
276 $strHtmlAll, $strHtmlNone, $strHtmlToggle,
277 $strHtmlMoveUp, $strHtmlMoveDown,
278 );
279
280 $strHtml = str_replace($placeHolders, $htmlElements, $strHtml);
281
282 return $strHtml;
283 }
284}