CRM add missing comment blocks
[civicrm-core.git] / CRM / Core / QuickForm / GroupMultiSelect.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 {
37 function toHtml() {
38 if ($this->_flagFrozen) {
39 return $this->getFrozenHtml();
40 }
41
42 $tabs = $this->_getTabs();
43 $tab = $this->_getTab();
44 $strHtml = '';
45
46 if ($this->getComment() != '') {
47 $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->" . PHP_EOL;
48 }
49
50 $selectName = $this->getName() . '[]';
51
52 // placeholder {unselected} existence determines if we will render
53 if (strpos($this->_elementTemplate, '{unselected}') === FALSE) {
54 // ... a single multi-select with checkboxes
55
56 $id = $this->getAttribute('id');
57
58 $strHtmlSelected = $tab . '<div id="' . $id . 'amsSelected">' . PHP_EOL;
59
60 foreach ($this->_options as $option) {
61
62 $_labelAttributes = array('style', 'class', 'onmouseover', 'onmouseout');
63 $labelAttributes = array();
64 foreach ($_labelAttributes as $attr) {
65 if (isset($option['attr'][$attr])) {
66 $labelAttributes[$attr] = $option['attr'][$attr];
67 unset($option['attr'][$attr]);
68 }
69 }
70
71 if (is_array($this->_values) && in_array((string)$option['attr']['value'], $this->_values)) {
72 // The items is *selected*
73 $checked = ' checked="checked"';
74 }
75 else {
76 // The item is *unselected* so we want to put it
77 $checked = '';
78 }
79 $strHtmlSelected .= $tab . '<label' . $this->_getAttrString($labelAttributes) . '>' . '<input type="checkbox"' . ' id="' . $this->getName() . '"' . ' name="' . $selectName . '"' . $checked . $this->_getAttrString($option['attr']) . ' />' . $option['text'] . '</label>' . PHP_EOL;
80 }
81 $strHtmlSelected .= $tab . '</div>' . PHP_EOL;
82
83 $strHtmlHidden = '';
84 $strHtmlUnselected = '';
85 $strHtmlAdd = '';
86 $strHtmlRemove = '';
87
88 // build the select all button with all its attributes
89 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('" . $this->getName() . "', 1);");
90 $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes);
91 $attrStrAll = $this->_getAttrString($this->_allButtonAttributes);
92 $strHtmlAll = "<input$attrStrAll />" . PHP_EOL;
93
94 // build the select none button with all its attributes
95 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('" . $this->getName() . "', 0);");
96 $this->_noneButtonAttributes = array_merge($this->_noneButtonAttributes, $attributes);
97 $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes);
98 $strHtmlNone = "<input$attrStrNone />" . PHP_EOL;
99
100 // build the toggle selection button with all its attributes
101 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}('" . $this->getName() . "', 2);");
102 $this->_toggleButtonAttributes = array_merge($this->_toggleButtonAttributes, $attributes);
103 $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes);
104 $strHtmlToggle = "<input$attrStrToggle />" . PHP_EOL;
105
106 $strHtmlMoveUp = '';
107 $strHtmlMoveDown = '';
108 }
109 else {
110 // ... or a dual multi-select
111
112 // set name of Select From Box
113 $this->_attributesUnselected = array('name' => '__' . $selectName, 'ondblclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'add')");
114 $this->_attributesUnselected = array_merge($this->_attributes, $this->_attributesUnselected);
115 $attrUnselected = $this->_getAttrString($this->_attributesUnselected);
116
117 // set name of Select To Box
118 $this->_attributesSelected = array('name' => '_' . $selectName, 'ondblclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'remove')");
119 $this->_attributesSelected = array_merge($this->_attributes, $this->_attributesSelected);
120 $attrSelected = $this->_getAttrString($this->_attributesSelected);
121
122 // set name of Select hidden Box
123 $this->_attributesHidden = array('name' => $selectName, 'style' => 'overflow: hidden; visibility: hidden; width: 1px; height: 0;');
124 $this->_attributesHidden = array_merge($this->_attributes, $this->_attributesHidden);
125 $attrHidden = $this->_getAttrString($this->_attributesHidden);
126
127 // prepare option tables to be displayed as in POST order
128 $append = count($this->_values);
129 if ($append > 0) {
130 $arrHtmlSelected = array_fill(0, $append, ' ');
131 }
132 else {
133 $arrHtmlSelected = array();
134 }
135
136 $options = count($this->_options);
137 $arrHtmlUnselected = array();
138 if ($options > 0) {
139 $arrHtmlHidden = array_fill(0, $options, ' ');
140
141 foreach ($this->_options as $option) {
142 if (is_array($this->_values) &&
143 in_array((string)$option['attr']['value'], $this->_values)
144 ) {
145 // Get the post order
146 $key = array_search($option['attr']['value'], $this->_values);
147
148 // The items is *selected* so we want to put it in the 'selected' multi-select
149 $arrHtmlSelected[$key] = $option;
150 // Add it to the 'hidden' multi-select and set it as 'selected'
151 $option['attr']['selected'] = 'selected';
152 $arrHtmlHidden[$key] = $option;
153 }
154 else {
155 // The item is *unselected* so we want to put it in the 'unselected' multi-select
156 $arrHtmlUnselected[] = $option;
157 // Add it to the hidden multi-select as 'unselected'
158 $arrHtmlHidden[$append] = $option;
159 $append++;
160 }
161 }
162 }
163 else {
164 $arrHtmlHidden = array();
165 }
166
167 // The 'unselected' multi-select which appears on the left
168 $strHtmlUnselected = "<select$attrUnselected>" . PHP_EOL;
169 if (count($arrHtmlUnselected) > 0) {
170 foreach ($arrHtmlUnselected as $data) {
171 $strHtmlUnselected .= $tabs . $tab . '<option' . $this->_getAttrString($data['attr']) . '>' . $data['text'] . '</option>' . PHP_EOL;
172 }
173 }
174 $strHtmlUnselected .= '</select>';
175
176 // The 'selected' multi-select which appears on the right
177 $strHtmlSelected = "<select$attrSelected>" . PHP_EOL;
178 if (count($arrHtmlSelected) > 0) {
179 foreach ($arrHtmlSelected as $data) {
180 $strHtmlSelected .= $tabs . $tab . '<option' . $this->_getAttrString($data['attr']) . '>' . $data['text'] . '</option>' . PHP_EOL;
181 }
182 }
183 $strHtmlSelected .= '</select>';
184
185 // The 'hidden' multi-select
186 $strHtmlHidden = "<select$attrHidden>" . PHP_EOL;
187 if (count($arrHtmlHidden) > 0) {
188 foreach ($arrHtmlHidden as $data) {
189 $strHtmlHidden .= $tabs . $tab . '<option' . $this->_getAttrString($data['attr']) . '>' . $data['text'] . '</option>' . PHP_EOL;
190 }
191 }
192 $strHtmlHidden .= '</select>';
193
194 // build the remove button with all its attributes
195 $attributes = array('onclick' => "{$this->_jsPrefix}{$this->_jsPostfix}(this.form.elements['__" . $selectName . "'], this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "'], 'remove'); return false;");
196 $this->_removeButtonAttributes = array_merge($this->_removeButtonAttributes, $attributes);
197 $attrStrRemove = $this->_getAttrString($this->_removeButtonAttributes);
198 $strHtmlRemove = "<input$attrStrRemove />" . PHP_EOL;
199
200 // build the add 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 . "'], 'add'); return false;");
202 $this->_addButtonAttributes = array_merge($this->_addButtonAttributes, $attributes);
203 $attrStrAdd = $this->_getAttrString($this->_addButtonAttributes);
204 $strHtmlAdd = "<input$attrStrAdd />" . PHP_EOL;
205
206 // build the select all 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 . "'], 'all'); return false;");
208 $this->_allButtonAttributes = array_merge($this->_allButtonAttributes, $attributes);
209 $attrStrAll = $this->_getAttrString($this->_allButtonAttributes);
210 $strHtmlAll = "<input$attrStrAll />" . PHP_EOL;
211
212 // build the select none 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 . "'], 'none'); return false;");
214 $this->_noneButtonAttributes = array_merge($this->_noneButtonAttributes, $attributes);
215 $attrStrNone = $this->_getAttrString($this->_noneButtonAttributes);
216 $strHtmlNone = "<input$attrStrNone />" . PHP_EOL;
217
218 // build the toggle 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 . "'], 'toggle'); return false;");
220 $this->_toggleButtonAttributes = array_merge($this->_toggleButtonAttributes, $attributes);
221 $attrStrToggle = $this->_getAttrString($this->_toggleButtonAttributes);
222 $strHtmlToggle = "<input$attrStrToggle />" . PHP_EOL;
223
224 // build the move up button with all its attributes
225 $attributes = array('onclick' => "{$this->_jsPrefix}moveUp(this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "']); return false;");
226 $this->_upButtonAttributes = array_merge($this->_upButtonAttributes, $attributes);
227 $attrStrUp = $this->_getAttrString($this->_upButtonAttributes);
228 $strHtmlMoveUp = "<input$attrStrUp />" . PHP_EOL;
229
230 // build the move down button with all its attributes
231 $attributes = array('onclick' => "{$this->_jsPrefix}moveDown(this.form.elements['_" . $selectName . "'], this.form.elements['" . $selectName . "']); return false;");
232 $this->_downButtonAttributes = array_merge($this->_downButtonAttributes, $attributes);
233 $attrStrDown = $this->_getAttrString($this->_downButtonAttributes);
234 $strHtmlMoveDown = "<input$attrStrDown />" . PHP_EOL;
235 }
236
237 // render all part of the multi select component with the template
238 $strHtml = $this->_elementTemplate;
239
240 // Prepare multiple labels
241 $labels = $this->getLabel();
242 if (is_array($labels)) {
243 array_shift($labels);
244 }
245 // render extra labels, if any
246 if (is_array($labels)) {
247 foreach ($labels as $key => $text) {
248 $key = is_int($key) ? $key + 2 : $key;
249 $strHtml = str_replace("{label_{$key}}", $text, $strHtml);
250 $strHtml = str_replace("<!-- BEGIN label_{$key} -->", '', $strHtml);
251 $strHtml = str_replace("<!-- END label_{$key} -->", '', $strHtml);
252 }
253 }
254 // clean up useless label tags
255 if (strpos($strHtml, '{label_')) {
256 $strHtml = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i', '', $strHtml);
257 }
258
259 $placeHolders = array(
260 '{stylesheet}', '{javascript}', '{class}',
261 '{unselected}', '{selected}',
262 '{add}', '{remove}',
263 '{all}', '{none}', '{toggle}',
264 '{moveup}', '{movedown}',
265 );
266 $htmlElements = array(
267 $this->getElementCss(FALSE), $this->getElementJs(FALSE), $this->_tableAttributes,
268 $strHtmlUnselected, $strHtmlSelected . $strHtmlHidden,
269 $strHtmlAdd, $strHtmlRemove,
270 $strHtmlAll, $strHtmlNone, $strHtmlToggle,
271 $strHtmlMoveUp, $strHtmlMoveDown,
272 );
273
274 $strHtml = str_replace($placeHolders, $htmlElements, $strHtml);
275
276 return $strHtml;
277 }
278}
279