root / tags / typolight-2.7.5 / system / modules / backend / SelectMenu.php
History | View | Annotate | Download (3.5 KB)
| 1 | <?php if (!defined('TL_ROOT')) die('You can not access this file directly!'); |
|---|---|
| 2 | |
| 3 | /**
|
| 4 | * TYPOlight webCMS |
| 5 | * Copyright (C) 2005-2009 Leo Feyer |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this program. If not, please visit the Free |
| 19 | * Software Foundation website at http://www.gnu.org/licenses/. |
| 20 | * |
| 21 | * PHP version 5 |
| 22 | * @copyright Leo Feyer 2005-2009 |
| 23 | * @author Leo Feyer <leo@typolight.org> |
| 24 | * @package Backend |
| 25 | * @license LGPL |
| 26 | * @filesource |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | /**
|
| 31 | * Class SelectMenu |
| 32 | * |
| 33 | * Provide methods to handle select menus. |
| 34 | * @copyright Leo Feyer 2005-2009 |
| 35 | * @author Leo Feyer <leo@typolight.org> |
| 36 | * @package Controller |
| 37 | */ |
| 38 | class SelectMenu extends Widget |
| 39 | {
|
| 40 | |
| 41 | /**
|
| 42 | * Submit user input |
| 43 | * @var boolean |
| 44 | */ |
| 45 | protected $blnSubmitInput = true; |
| 46 | |
| 47 | /**
|
| 48 | * Template |
| 49 | * @var string |
| 50 | */ |
| 51 | protected $strTemplate = 'be_widget'; |
| 52 | |
| 53 | /**
|
| 54 | * Options |
| 55 | * @var array |
| 56 | */ |
| 57 | protected $arrOptions = array(); |
| 58 | |
| 59 | |
| 60 | /**
|
| 61 | * Add specific attributes |
| 62 | * @param string |
| 63 | * @param mixed |
| 64 | */ |
| 65 | public function __set($strKey, $varValue) |
| 66 | {
|
| 67 | switch ($strKey)
|
| 68 | {
|
| 69 | case 'size': |
| 70 | if ($this->multiple) |
| 71 | {
|
| 72 | $this->arrAttributes['size'] = $varValue; |
| 73 | } |
| 74 | break;
|
| 75 | |
| 76 | case 'options': |
| 77 | $this->arrOptions = deserialize($varValue);
|
| 78 | break;
|
| 79 | |
| 80 | case 'multiple': |
| 81 | if (strlen($varValue))
|
| 82 | {
|
| 83 | $this->arrAttributes[$strKey] = 'multiple'; |
| 84 | } |
| 85 | break;
|
| 86 | |
| 87 | case 'mandatory': |
| 88 | $this->arrConfiguration['mandatory'] = $varValue ? true : false; |
| 89 | break;
|
| 90 | |
| 91 | default:
|
| 92 | parent::__set($strKey, $varValue); |
| 93 | break;
|
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /**
|
| 99 | * Generate the widget and return it as string |
| 100 | * @return string |
| 101 | */ |
| 102 | public function generate() |
| 103 | {
|
| 104 | $arrOptions = array();
|
| 105 | $strClass = 'tl_select';
|
| 106 | |
| 107 | if ($this->multiple) |
| 108 | {
|
| 109 | $this->strName .= '[]'; |
| 110 | $strClass = 'tl_mselect';
|
| 111 | } |
| 112 | |
| 113 | // Add empty option (XHTML) if there are none
|
| 114 | if (!count($this->arrOptions)) |
| 115 | {
|
| 116 | $this->arrOptions = array(array('value'=>'', 'label'=>'-')); |
| 117 | } |
| 118 | |
| 119 | foreach ($this->arrOptions as $strKey=>$arrOption) |
| 120 | {
|
| 121 | if (isset($arrOption['value'])) |
| 122 | {
|
| 123 | $arrOptions[] = sprintf('<option value="%s"%s>%s</option>',
|
| 124 | specialchars($arrOption['value']),
|
| 125 | $this->isSelected($arrOption),
|
| 126 | $arrOption['label']);
|
| 127 | |
| 128 | continue;
|
| 129 | } |
| 130 | |
| 131 | $arrOptgroups = array();
|
| 132 | |
| 133 | foreach ($arrOption as $arrOptgroup) |
| 134 | {
|
| 135 | $arrOptgroups[] = sprintf('<option value="%s"%s>%s</option>',
|
| 136 | specialchars($arrOptgroup['value']),
|
| 137 | $this->isSelected($arrOptgroup),
|
| 138 | $arrOptgroup['label']);
|
| 139 | } |
| 140 | |
| 141 | $arrOptions[] = sprintf('<optgroup label=" %s">%s</optgroup>', specialchars($strKey), implode('', $arrOptgroups)); |
| 142 | } |
| 143 | |
| 144 | return sprintf('<select name="%s" id="ctrl_%s" class="%s%s"%s onfocus="Backend.getScrollOffset();">%s</select>%s', |
| 145 | $this->strName,
|
| 146 | $this->strId,
|
| 147 | $strClass, |
| 148 | (strlen($this->strClass) ? ' ' . $this->strClass : ''), |
| 149 | $this->getAttributes(),
|
| 150 | implode('', $arrOptions),
|
| 151 | $this->wizard);
|
| 152 | } |
| 153 | } |
| 154 | |
| 155 | ?> |