| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 7 |
use OOUI\Widget; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class HTMLFileField extends HTMLFormField { |
| 22 |
|
| 23 |
protected $mPlaceholder = ''; |
| 24 |
|
| 25 |
protected $mAccept = null; |
| 26 |
|
| 27 |
|
| 28 |
protected $mMultiple; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function __construct( $params ) { |
| 39 |
if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) { |
| 40 |
$params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off'; |
| 41 |
} |
| 42 |
|
| 43 |
parent::__construct( $params ); |
| 44 |
|
| 45 |
if ( isset( $params['placeholder-message'] ) ) { |
| 46 |
$this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text(); |
| 47 |
} elseif ( isset( $params['placeholder'] ) ) { |
| 48 |
$this->mPlaceholder = $params['placeholder']; |
| 49 |
} |
| 50 |
|
| 51 |
$this->mAccept = $params['accept'] ?? null; |
| 52 |
$this->mMultiple = !empty( $params['multiple'] ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function loadDataFromRequest( $request ) { |
| 59 |
return $request->getUpload( $this->mName )->getName(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function getInputHTML( $value ) { |
| 67 |
$attribs = [ |
| 68 |
'id' => $this->mID, |
| 69 |
'name' => $this->mName, |
| 70 |
'dir' => $this->mDir, |
| 71 |
] + $this->getTooltipAndAccessKey(); |
| 72 |
|
| 73 |
if ( $this->mClass !== '' ) { |
| 74 |
$attribs['class'] = $this->mClass; |
| 75 |
} |
| 76 |
if ( $this->mAccept ) { |
| 77 |
$attribs['accept'] = implode( ',', $this->mAccept ); |
| 78 |
} |
| 79 |
if ( $this->mMultiple ) { |
| 80 |
$attribs['multiple'] = ''; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
$allowedParams = [ |
| 85 |
'title', |
| 86 |
'tabindex', |
| 87 |
'disabled', |
| 88 |
'required', |
| 89 |
'autofocus', |
| 90 |
'readonly', |
| 91 |
]; |
| 92 |
|
| 93 |
$attribs += $this->getAttributes( $allowedParams ); |
| 94 |
|
| 95 |
return Html::input( $this->mName, $value ?? '', 'file', $attribs ); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function getInputOOUI( $value ) { |
| 103 |
$attribs = $this->getTooltipAndAccessKeyOOUI(); |
| 104 |
|
| 105 |
if ( $this->mClass !== '' ) { |
| 106 |
$attribs['classes'] = [ $this->mClass ]; |
| 107 |
} |
| 108 |
if ( $this->mPlaceholder !== '' ) { |
| 109 |
$attribs['placeholder'] = $this->mPlaceholder; |
| 110 |
} |
| 111 |
if ( $this->mAccept ) { |
| 112 |
$attribs['accept'] = $this->mAccept; |
| 113 |
} |
| 114 |
if ( $this->mMultiple ) { |
| 115 |
$attribs['multiple'] = true; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
$allowedParams = [ |
| 121 |
'title', |
| 122 |
'tabindex', |
| 123 |
'disabled', |
| 124 |
'required', |
| 125 |
'autofocus', |
| 126 |
'readonly', |
| 127 |
]; |
| 128 |
|
| 129 |
$attribs += \OOUI\Element::configFromHtmlAttributes( |
| 130 |
$this->getAttributes( $allowedParams ) |
| 131 |
); |
| 132 |
|
| 133 |
return $this->getInputWidget( [ |
| 134 |
'id' => $this->mID, |
| 135 |
'name' => $this->mName, |
| 136 |
'dir' => $this->mDir, |
| 137 |
] + $attribs ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected function getInputWidget( $params ) { |
| 148 |
return new \OOUI\SelectFileInputWidget( $params ); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
protected function shouldInfuseOOUI() { |
| 156 |
return true; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
class_alias( HTMLFileField::class, 'HTMLFileField' ); |