| Index: trunk/extensions/Validator/includes/ParameterInput.php |
| — | — | @@ -0,0 +1,236 @@ |
| | 2 | +<?php |
| | 3 | + |
| | 4 | +/** |
| | 5 | + * Simple class to get a HTML input for the parameter. |
| | 6 | + * Usable for when creating a GUI from a parameter list. |
| | 7 | + * |
| | 8 | + * Based on 'addOptionInput' from Special:Ask in SMW 1.5.6. |
| | 9 | + * |
| | 10 | + * TODO: support lists (now only done when values are restricted to an array) |
| | 11 | + * TODO: nicify HTML |
| | 12 | + * |
| | 13 | + * @since 0.4.6 |
| | 14 | + * |
| | 15 | + * @file ParameterInput.php |
| | 16 | + * @ingroup Validator |
| | 17 | + * |
| | 18 | + * @licence GNU GPL v3 or later |
| | 19 | + * @author Jeroen De Dauw |
| | 20 | + */ |
| | 21 | +class ParameterInput { |
| | 22 | + |
| | 23 | + /** |
| | 24 | + * The parameter to print an input for. |
| | 25 | + * |
| | 26 | + * @since O.4.6 |
| | 27 | + * |
| | 28 | + * @var Parameter |
| | 29 | + */ |
| | 30 | + protected $param; |
| | 31 | + |
| | 32 | + /** |
| | 33 | + * The current value for the parameter. When provided, |
| | 34 | + * it'll be used as value for the input, otherwise the |
| | 35 | + * parameters default value will be used. |
| | 36 | + * |
| | 37 | + * @since 0.4.6 |
| | 38 | + * |
| | 39 | + * @var mixed: string or false |
| | 40 | + */ |
| | 41 | + protected $currentValue; |
| | 42 | + |
| | 43 | + /** |
| | 44 | + * Name for the input. |
| | 45 | + * |
| | 46 | + * @since 0.6.4 |
| | 47 | + * |
| | 48 | + * @var string |
| | 49 | + */ |
| | 50 | + protected $inputName; |
| | 51 | + |
| | 52 | + /** |
| | 53 | + * COnstructor. |
| | 54 | + * |
| | 55 | + * @since 0.4.6 |
| | 56 | + * |
| | 57 | + * @param Parameter $param |
| | 58 | + * @param mixed $currentValue |
| | 59 | + */ |
| | 60 | + public function __construct( Parameter $param, $currentValue = false ) { |
| | 61 | + $this->param = $param; |
| | 62 | + $this->currentValue = $currentValue; |
| | 63 | + $this->inputName = $param->getName(); |
| | 64 | + } |
| | 65 | + |
| | 66 | + /** |
| | 67 | + * Sets the name for the input; defaults to the name of the parameter. |
| | 68 | + * |
| | 69 | + * @since 0.6.4 |
| | 70 | + * |
| | 71 | + * @param string $name |
| | 72 | + */ |
| | 73 | + public function setInputName( $name ) { |
| | 74 | + $this->inputName = name; |
| | 75 | + } |
| | 76 | + |
| | 77 | + /** |
| | 78 | + * Returns the HTML for the parameter input. |
| | 79 | + * |
| | 80 | + * @since 0.4.6 |
| | 81 | + * |
| | 82 | + * @return string |
| | 83 | + */ |
| | 84 | + public function getHtml() { |
| | 85 | + $html = ''; |
| | 86 | + $valueList = array(); |
| | 87 | + |
| | 88 | + foreach ( $this->param->getCriteria() as $criterion ) { |
| | 89 | + if ( $criterion instanceof CriterionInArray ) { |
| | 90 | + $valueList[] = $criterion->getAllowedValues(); |
| | 91 | + } |
| | 92 | + } |
| | 93 | + |
| | 94 | + if ( count( $valueList ) > 0 ) { |
| | 95 | + $valueList = call_user_func_array( 'array_intersect', $valueList ); |
| | 96 | + $html = $this->param->isList() ? $this->getChckboxListInput( $valueList ) : $this->getSelectInput( $valueList ); |
| | 97 | + } |
| | 98 | + else { |
| | 99 | + switch ( $this->param->getType() ) { |
| | 100 | + case Paramater::TYPE_CHAR: |
| | 101 | + case Parameter::TYPE_FLOAT: |
| | 102 | + case Parameter::TYPE_INTEGER: |
| | 103 | + case Parameter::TYPE_NUMBER: |
| | 104 | + $html = $this->getNumberInput(); |
| | 105 | + break; |
| | 106 | + case Parameter::TYPE_BOOLEAN: |
| | 107 | + $html = $this->getBooleanInput(); |
| | 108 | + case Paramater::TYPE_STRING: |
| | 109 | + default: |
| | 110 | + $html = $this->getIntInput(); |
| | 111 | + break; |
| | 112 | + } |
| | 113 | + } |
| | 114 | + |
| | 115 | + return $html; |
| | 116 | + } |
| | 117 | + |
| | 118 | + /** |
| | 119 | + * Returns the value to initially display with the input. |
| | 120 | + * |
| | 121 | + * @since 0.4.6 |
| | 122 | + * |
| | 123 | + * @return mixed |
| | 124 | + */ |
| | 125 | + protected function getValueToUse() { |
| | 126 | + return $this->currentValue === false ? $this->param->getDefault() : $this->currentValue; |
| | 127 | + } |
| | 128 | + |
| | 129 | + /** |
| | 130 | + * Gets a short text input suitable for numbers. |
| | 131 | + * |
| | 132 | + * @since 0.4.6 |
| | 133 | + * |
| | 134 | + * @return string |
| | 135 | + */ |
| | 136 | + protected function getNumberInput() { |
| | 137 | + return Html::input( |
| | 138 | + $this->inputName, |
| | 139 | + $this->currentValue, |
| | 140 | + 'text', |
| | 141 | + array( |
| | 142 | + 'size' => 6 |
| | 143 | + ) |
| | 144 | + ); |
| | 145 | + } |
| | 146 | + |
| | 147 | + /** |
| | 148 | + * Gets a text input for a string. |
| | 149 | + * |
| | 150 | + * @since 0.4.6 |
| | 151 | + * |
| | 152 | + * @return string |
| | 153 | + */ |
| | 154 | + protected function getStrInput() { |
| | 155 | + return Html::input( |
| | 156 | + $this->inputName, |
| | 157 | + $this->currentValue, |
| | 158 | + 'text', |
| | 159 | + array( |
| | 160 | + 'size' => 32 |
| | 161 | + ) |
| | 162 | + ); |
| | 163 | + } |
| | 164 | + |
| | 165 | + /** |
| | 166 | + * Gets a checkbox. |
| | 167 | + * |
| | 168 | + * @since 0.4.6 |
| | 169 | + * |
| | 170 | + * @return string |
| | 171 | + */ |
| | 172 | + protected function getBooleanInput() { |
| | 173 | + return Xml::check( |
| | 174 | + $this->inputName, |
| | 175 | + $this->currentValue |
| | 176 | + ); |
| | 177 | + } |
| | 178 | + |
| | 179 | + /** |
| | 180 | + * Gets a select menue for the provided values. |
| | 181 | + * |
| | 182 | + * @since 0.4.6 |
| | 183 | + * |
| | 184 | + * @param array $valueList |
| | 185 | + * |
| | 186 | + * @return string |
| | 187 | + */ |
| | 188 | + protected function getSelectInput( array $valueList ) { |
| | 189 | + $options = array(); |
| | 190 | + $options[] = '<option value=""></option>'; |
| | 191 | + |
| | 192 | + foreach ( $valueList as $value ) { |
| | 193 | + $options[] = |
| | 194 | + '<option value="' . htmlspecialchars( $value ) . '"' . |
| | 195 | + ( in_array( $value, $this->currentValue ) ? ' selected' : '' ) . '>' . htmlspecialchars( $value ) . |
| | 196 | + '</option>'; // TODO |
| | 197 | + } |
| | 198 | + |
| | 199 | + return Html::element( |
| | 200 | + 'select', |
| | 201 | + array( |
| | 202 | + 'name' => $this->inputName |
| | 203 | + ), |
| | 204 | + implode( "\n", $options ) |
| | 205 | + ); |
| | 206 | + } |
| | 207 | + |
| | 208 | + /** |
| | 209 | + * Gets a list of input boxes for the provided values. |
| | 210 | + * |
| | 211 | + * @since 0.4.6 |
| | 212 | + * |
| | 213 | + * @param array $valueList |
| | 214 | + * |
| | 215 | + * @return string |
| | 216 | + */ |
| | 217 | + protected function getCheckboxListInput( array $valueList ) { |
| | 218 | + $boxes = array(); |
| | 219 | + |
| | 220 | + foreach ( $valueList as $value ) { |
| | 221 | + $boxes[] = Html::rawElement( |
| | 222 | + 'span', |
| | 223 | + array( |
| | 224 | + 'style' => 'white-space: nowrap; padding-right: 5px;' |
| | 225 | + ), |
| | 226 | + Xml::check( |
| | 227 | + $this->inputName . '[' . htmlspecialchars( $value ). ']', |
| | 228 | + in_array( $value, $this->currentValue ) |
| | 229 | + ) . |
| | 230 | + Html::element( 'tt', $value ) |
| | 231 | + ); |
| | 232 | + } |
| | 233 | + |
| | 234 | + return implode( "\n", $boxes ); |
| | 235 | + } |
| | 236 | + |
| | 237 | +} |
| Property changes on: trunk/extensions/Validator/includes/ParameterInput.php |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| 1 | 238 | + native |
| Index: trunk/extensions/Validator/Validator.php |
| — | — | @@ -50,6 +50,7 @@ |
| 51 | 51 | $wgAutoloadClasses['ListParameterManipulation'] = $incDir . 'ListParameterManipulation.php'; |
| 52 | 52 | $wgAutoloadClasses['Parameter'] = $incDir . 'Parameter.php'; |
| 53 | 53 | $wgAutoloadClasses['ParameterCriterion'] = $incDir . 'ParameterCriterion.php'; |
| | 54 | +$wgAutoloadClasses['ParameterInput'] = $incDir . 'ParameterInput.php'; |
| 54 | 55 | $wgAutoloadClasses['ParameterManipulation'] = $incDir . 'ParameterManipulation.php'; |
| 55 | 56 | $wgAutoloadClasses['ParserHook'] = $incDir . 'ParserHook.php'; |
| 56 | 57 | $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php'; |