/
listbox-multiple.class.php
54 lines (50 loc) · 1.88 KB
/
listbox-multiple.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*
* This file is part of MODX Revolution.
*
* Copyright (c) MODX, LLC. All Rights Reserved.
*
* For complete copyright and license information, see the COPYRIGHT and LICENSE
* files found in the top-level directory of this distribution.
*/
/**
* @package modx
* @subpackage processors.element.tv.renders.mgr.input
*/
class modTemplateVarInputRenderListboxMultiple extends modTemplateVarInputRender {
public function getTemplate() {
return 'element/tv/renders/input/listbox-multiple.tpl';
}
public function process($value,array $params = array()) {
$value = explode("||",$value);
$options = $this->getInputOptions();
$items = array();
foreach ($options as $option) {
$opt = explode("==",$option);
if (!isset($opt[1])) $opt[1] = $opt[0];
$items[] = array(
'text' => htmlspecialchars($opt[0],ENT_COMPAT,'UTF-8'),
'value' => htmlspecialchars($opt[1],ENT_COMPAT,'UTF-8'),
'selected' => in_array($opt[1],$value),
);
}
// preserve the order of selected values
$orderedItems = array();
// loop trough the selected values
foreach ($value as $val) {
// find the corresponding option in the items array
foreach ($items as $item => $values) {
// if found, add it in the right order to the $orderItems array
if ($values['value'] == $val) {
$orderedItems[] = $values;
// and remove it from the original $items array
unset($items[$item]);
}
}
}
// merge the correctly ordered items with the unselected remaining ones
$items = array_merge($orderedItems, $items);
$this->setPlaceholder('opts',$items);
}
}
return 'modTemplateVarInputRenderListboxMultiple';