function getInputHTML($value) { $valInSelect = false; if ($value !== false) { $value = strval($value); $valInSelect = in_array($value, HTMLFormField::flattenOptions($this->getOptions()), true); } $selected = $valInSelect ? $value : 'other'; $select = new XmlSelect($this->mName, $this->mID, $selected); $select->addOptions($this->getOptions()); $select->setAttribute('class', 'mw-htmlform-select-or-other'); $tbAttribs = array('id' => $this->mID . '-other', 'size' => $this->getSize()); if (!empty($this->mParams['disabled'])) { $select->setAttribute('disabled', 'disabled'); $tbAttribs['disabled'] = 'disabled'; } if (isset($this->mParams['tabindex'])) { $select->setAttribute('tabindex', $this->mParams['tabindex']); $tbAttribs['tabindex'] = $this->mParams['tabindex']; } $select = $select->getHTML(); if (isset($this->mParams['maxlength'])) { $tbAttribs['maxlength'] = $this->mParams['maxlength']; } if ($this->mClass !== '') { $tbAttribs['class'] = $this->mClass; } $textbox = Html::input($this->mName . '-other', $valInSelect ? '' : $value, 'text', $tbAttribs); return "{$select}<br />\n{$textbox}"; }
function filterDataForSubmit($data) { $options = HTMLFormField::flattenOptions($this->getOptions()); $res = array(); foreach ($options as $opt) { $res["{$opt}"] = in_array($opt, $data); } return $res; }
function validate($value, $alldata) { $p = parent::validate($value, $alldata); if ($p !== true) { return $p; } $validOptions = HTMLFormField::flattenOptions($this->getOptions()); if (in_array(strval($value), $validOptions, true)) { return true; } else { return $this->msg('htmlform-select-badoption')->parse(); } }
/** * @param $user User * @param $context IContextSource * @param $defaultPreferences * @return void */ static function datetimePreferences( $user, IContextSource $context, &$defaultPreferences ) { ## Date and time ##################################### $dateOptions = self::getDateOptions( $context ); if ( $dateOptions ) { $defaultPreferences['date'] = array( 'type' => 'radio', 'options' => $dateOptions, 'label' => ' ', 'section' => 'datetime/dateformat', ); } // Info $now = wfTimestampNow(); $lang = $context->getLanguage(); $nowlocal = Xml::element( 'span', array( 'id' => 'wpLocalTime' ), $lang->time( $now, true ) ); $nowserver = $lang->time( $now, false ) . Html::hidden( 'wpServerTime', (int)substr( $now, 8, 2 ) * 60 + (int)substr( $now, 10, 2 ) ); $defaultPreferences['nowserver'] = array( 'type' => 'info', 'raw' => 1, 'label-message' => 'servertime', 'default' => $nowserver, 'section' => 'datetime/timeoffset', ); $defaultPreferences['nowlocal'] = array( 'type' => 'info', 'raw' => 1, 'label-message' => 'localtime', 'default' => $nowlocal, 'section' => 'datetime/timeoffset', ); // Grab existing pref. $tzOffset = $user->getOption( 'timecorrection' ); $tz = explode( '|', $tzOffset, 3 ); $tzOptions = self::getTimezoneOptions( $context ); $tzSetting = $tzOffset; if ( count( $tz ) > 1 && $tz[0] == 'Offset' ) { $minDiff = $tz[1]; $tzSetting = sprintf( '%+03d:%02d', floor( $minDiff / 60 ), abs( $minDiff ) % 60 ); } elseif ( count( $tz ) > 1 && $tz[0] == 'ZoneInfo' && !in_array( $tzOffset, HTMLFormField::flattenOptions( $tzOptions ) ) ) { # Timezone offset can vary with DST $userTZ = timezone_open( $tz[2] ); if ( $userTZ !== false ) { $minDiff = floor( timezone_offset_get( $userTZ, date_create( 'now' ) ) / 60 ); $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}"; } } $defaultPreferences['timecorrection'] = array( 'class' => 'HTMLSelectOrOtherField', 'label-message' => 'timezonelegend', 'options' => $tzOptions, 'default' => $tzSetting, 'size' => 20, 'section' => 'datetime/timeoffset', ); }
function validate($value, $alldata) { $p = parent::validate($value, $alldata); if ($p !== true) { return $p; } if (!is_string($value) && !is_int($value)) { return false; } $validOptions = HTMLFormField::flattenOptions($this->mParams['options']); if (in_array($value, $validOptions)) { return true; } else { return $this->msg('htmlform-select-badoption')->parse(); } }
/** * Return an associative array mapping preferences keys to the kind of a preference they're * used for. Different kinds are handled differently when setting or reading preferences. * * See User::listOptionKinds for the list of valid option types that can be provided. * * @see User::listOptionKinds * @param IContextSource $context * @param array $options Assoc. array with options keys to check as keys. * Defaults to $this->mOptions. * @return array The key => kind mapping data */ public function getOptionKinds(IContextSource $context, $options = null) { $this->loadOptions(); if ($options === null) { $options = $this->mOptions; } $prefs = Preferences::getPreferences($this, $context); $mapping = array(); // Pull out the "special" options, so they don't get converted as // multiselect or checkmatrix. $specialOptions = array_fill_keys(Preferences::getSaveBlacklist(), true); foreach ($specialOptions as $name => $value) { unset($prefs[$name]); } // Multiselect and checkmatrix options are stored in the database with // one key per option, each having a boolean value. Extract those keys. $multiselectOptions = array(); foreach ($prefs as $name => $info) { if (isset($info['type']) && $info['type'] == 'multiselect' || isset($info['class']) && $info['class'] == 'HTMLMultiSelectField') { $opts = HTMLFormField::flattenOptions($info['options']); $prefix = isset($info['prefix']) ? $info['prefix'] : $name; foreach ($opts as $value) { $multiselectOptions["{$prefix}{$value}"] = true; } unset($prefs[$name]); } } $checkmatrixOptions = array(); foreach ($prefs as $name => $info) { if (isset($info['type']) && $info['type'] == 'checkmatrix' || isset($info['class']) && $info['class'] == 'HTMLCheckMatrix') { $columns = HTMLFormField::flattenOptions($info['columns']); $rows = HTMLFormField::flattenOptions($info['rows']); $prefix = isset($info['prefix']) ? $info['prefix'] : $name; foreach ($columns as $column) { foreach ($rows as $row) { $checkmatrixOptions["{$prefix}{$column}-{$row}"] = true; } } unset($prefs[$name]); } } // $value is ignored foreach ($options as $key => $value) { if (isset($prefs[$key])) { $mapping[$key] = 'registered'; } elseif (isset($multiselectOptions[$key])) { $mapping[$key] = 'registered-multiselect'; } elseif (isset($checkmatrixOptions[$key])) { $mapping[$key] = 'registered-checkmatrix'; } elseif (isset($specialOptions[$key])) { $mapping[$key] = 'special'; } elseif (substr($key, 0, 7) === 'userjs-') { $mapping[$key] = 'userjs'; } else { $mapping[$key] = 'unused'; } } return $mapping; }
function getInputHTML($value) { $oldClass = $this->mClass; $this->mClass = (array) $this->mClass; $valInSelect = false; $ret = ''; if ($this->getOptions()) { if ($value !== false) { $value = strval($value); $valInSelect = in_array($value, HTMLFormField::flattenOptions($this->getOptions()), true); } $selected = $valInSelect ? $value : 'other'; $select = new XmlSelect($this->mName . '-select', $this->mID . '-select', $selected); $select->addOptions($this->getOptions()); $select->setAttribute('class', 'mw-htmlform-select-or-other'); if (!empty($this->mParams['disabled'])) { $select->setAttribute('disabled', 'disabled'); } if (isset($this->mParams['tabindex'])) { $select->setAttribute('tabindex', $this->mParams['tabindex']); } $ret = $select->getHTML() . "<br />\n"; $this->mClass[] = 'mw-htmlform-hide-if'; } if ($valInSelect) { $value = ''; } else { $key = array_search(strval($value), $this->autocomplete, true); if ($key !== false) { $value = $key; } } $this->mClass[] = 'mw-htmlform-autocomplete'; $ret .= parent::getInputHTML($valInSelect ? '' : $value); $this->mClass = $oldClass; return $ret; }
/** * @param $data array * @return array */ function filterDataForSubmit($data) { // Support for separating MultiSelect preferences into multiple preferences // Due to lack of array support. foreach ($this->mFlatFields as $fieldname => $field) { $info = $field->mParams; if ($field instanceof HTMLMultiSelectField) { $options = HTMLFormField::flattenOptions($info['options']); $prefix = isset($info['prefix']) ? $info['prefix'] : $fieldname; foreach ($options as $opt) { $data["{$prefix}{$opt}"] = in_array($opt, $data[$fieldname]); } unset($data[$fieldname]); } } return $data; }
function filterDataForSubmit($data) { $columns = HTMLFormField::flattenOptions($this->mParams['columns']); $rows = HTMLFormField::flattenOptions($this->mParams['rows']); $res = []; foreach ($columns as $column) { foreach ($rows as $row) { // Make sure option hasn't been forced $thisTag = "{$column}-{$row}"; if ($this->isTagForcedOff($thisTag)) { $res[$thisTag] = false; } elseif ($this->isTagForcedOn($thisTag)) { $res[$thisTag] = true; } else { $res[$thisTag] = in_array($thisTag, $data); } } } return $res; }
function filterDataForSubmit($data) { $data = HTMLFormField::forceToStringRecursive($data); $options = HTMLFormField::flattenOptions($this->getOptions()); $res = []; foreach ($options as $opt) { $res["{$opt}"] = in_array($opt, $data, true); } return $res; }
/** * Return an associative array mapping preferences keys to the kind of a preference they're * used for. Different kinds are handled differently when setting or reading preferences. * * See User::listOptionKinds for the list of valid option types that can be provided. * * @see User::listOptionKinds * @param $context IContextSource * @param array $options assoc. array with options keys to check as keys. Defaults to $this->mOptions. * @return array the key => kind mapping data */ public function getOptionKinds( IContextSource $context, $options = null ) { $this->loadOptions(); if ( $options === null ) { $options = $this->mOptions; } $prefs = Preferences::getPreferences( $this, $context ); $mapping = array(); // Multiselect and checkmatrix options are stored in the database with // one key per option, each having a boolean value. Extract those keys. $multiselectOptions = array(); foreach ( $prefs as $name => $info ) { if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) || ( isset( $info['class'] ) && $info['class'] == 'HTMLMultiSelectField' ) ) { $opts = HTMLFormField::flattenOptions( $info['options'] ); $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name; foreach ( $opts as $value ) { $multiselectOptions["$prefix$value"] = true; } unset( $prefs[$name] ); } } $checkmatrixOptions = array(); foreach ( $prefs as $name => $info ) { if ( ( isset( $info['type'] ) && $info['type'] == 'checkmatrix' ) || ( isset( $info['class'] ) && $info['class'] == 'HTMLCheckMatrix' ) ) { $columns = HTMLFormField::flattenOptions( $info['columns'] ); $rows = HTMLFormField::flattenOptions( $info['rows'] ); $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name; foreach ( $columns as $column ) { foreach ( $rows as $row ) { $checkmatrixOptions["$prefix-$column-$row"] = true; } } unset( $prefs[$name] ); } } // $value is ignored foreach ( $options as $key => $value ) { if ( isset( $prefs[$key] ) ) { $mapping[$key] = 'registered'; } elseif ( isset( $multiselectOptions[$key] ) ) { $mapping[$key] = 'registered-multiselect'; } elseif ( isset( $checkmatrixOptions[$key] ) ) { $mapping[$key] = 'registered-checkmatrix'; } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) { $mapping[$key] = 'userjs'; } else { $mapping[$key] = 'unused'; } } return $mapping; }
/** * @param $data array * @return array */ function filterDataForSubmit($data) { // Support for separating multi-option preferences into multiple preferences // Due to lack of array support. foreach ($this->mFlatFields as $fieldname => $field) { $info = $field->mParams; if ($field instanceof HTMLMultiSelectField) { $options = HTMLFormField::flattenOptions($info['options']); $prefix = isset($info['prefix']) ? $info['prefix'] : $fieldname; foreach ($options as $opt) { $data["{$prefix}{$opt}"] = in_array($opt, $data[$fieldname]); } unset($data[$fieldname]); } elseif ($field instanceof HTMLCheckMatrix) { $columns = HTMLFormField::flattenOptions($info['columns']); $rows = HTMLFormField::flattenOptions($info['rows']); $prefix = isset($info['prefix']) ? $info['prefix'] : $fieldname; foreach ($columns as $column) { foreach ($rows as $row) { // Make sure option hasn't been removed if (!isset($info['remove-options']) || !in_array("{$column}-{$row}", $info['remove-options'])) { $data["{$prefix}-{$column}-{$row}"] = in_array("{$column}-{$row}", $data[$fieldname]); } } } unset($data[$fieldname]); } } return $data; }