support upload multiple

This commit is contained in:
Jephté Clain 2024-05-20 22:49:10 +04:00
parent a372b76f9e
commit a3f9094d8f
3 changed files with 30 additions and 9 deletions

View File

@ -13,7 +13,8 @@ class ControlFile extends ControlVisualInput {
"accept" => ["?string", ".pdf,image/*", "types MIME acceptés"],
"btn_class" => ["?string", "btn-default", "Classe du bouton"],
"btn_style" => ["?string", null, "Style du bouton"],
"btn_label" => ["content", "Sélectionner un fichier", "Texte du bouton"],
"btn_label" => ["content", null, "Texte du bouton"],
"multiple" => ["?bool", null, "est-il possible de sélectionner plusieurs fichiers?"],
];
function __construct(Form $form, ?array $params=null) {
@ -31,9 +32,15 @@ class ControlFile extends ControlVisualInput {
/** @var string|array */
protected $ppBtnLabel;
/** @var ?bool */
protected $ppMultiple;
function buildControl(): array {
$name = $this->ppName;
$value = $this->ppValue;
$multiple = $this->ppMultiple;
$defaultLabel = $multiple ? "Sélectionner des fichiers" : "Sélectionner un fichier";
$btnLabel = $this->ppBtnLabel ?? $defaultLabel;
return v::tag("label", [
"class" => ["btn btn-file", $this->ppBtnClass],
v::tag1("input", [
@ -44,13 +51,14 @@ class ControlFile extends ControlVisualInput {
"readonly" => $this->ppReadonly? "readonly": false,
"disabled" => $this->ppDisabled? "disabled": false,
"required" => $this->ppRequired? "required": false,
"multiple" => $multiple ? "multiple": false,
"style" => "display: inline; visibility: hidden; width: 0px; height: 0px; vertical-align: middle;",
$this->ppAttrs,
]),
v::span([
"class" => ["file-label", $this->ppClass],
"style" => $this->ppStyle,
$this->ppBtnLabel,
$btnLabel,
]),
]);
}

View File

@ -31,7 +31,7 @@ class formfilePlugin extends BasePlugin {
$this->readyLabelSuffix = $readyLabelSuffix;
}
private $readyLabelClass = "btn-success";
private $readyLabelClass = "btn-success active";
function setReadyLabelClass(string $readyLabelClass): void {
$this->readyLabelClass = $readyLabelClass;
@ -51,11 +51,24 @@ class formfilePlugin extends BasePlugin {
jQuery.noConflict()(function($) {
$("input[type='file']").change(function() {
var $this = $(this);
var filename = $this.prop("value");
var files = this.files;
var filename;
if (files !== undefined) {
var count = files.length;
if (count === 1) {
filename = files[0].name;
} else if (count === 2) {
filename = "".concat(files[0].name, ", ", files[1].name, " (", count, ")");
} else {
filename = "".concat(files[0].name, ", ... (", count, ")");
}
} else {
filename = $this.prop("value");
var posas = filename.lastIndexOf("\\");
var posfs = filename.lastIndexOf("/");
if (posas !== -1) filename = filename.substr(posas + 1);
else if (posfs !== -1) filename = filename.substr(posfs + 1);
if (posas !== -1) filename = filename.substring(posas + 1);
else if (posfs !== -1) filename = filename.substring(posfs + 1);
}
var $label = $this.parent("label");
$label.addClass(<?=js::qv($this->readyLabelClass)?>);
var prefix = <?=js::qv($this->readyLabelPrefix)?>;

View File

@ -70,7 +70,7 @@ class Upload extends BaseArray {
"tmpName" => "tmp_name",
"fullPath" => "full_path",
];
function __get($name) {
function &__get($name) {
$name = static::_AUTO_PROPERTIES[$name] ?? $name;
return parent::__get($name);
}