Commit c60aeff0 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Gère correctement l'upload de multiples fichiers.

Showing with 62 additions and 28 deletions
+62 -28
......@@ -9,7 +9,9 @@ namespace Irstea\FileUploadBundle\Form\DataTranformer;
use Irstea\FileUploadBundle\Entity\UploadedFile;
use Irstea\FileUploadBundle\Service\FileManagerInterface;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\Form\DataTransformerInterface;
use Traversable;
/**
* Description of UploadedFileTransformer
......@@ -39,8 +41,16 @@ class UploadedFileTransformer implements DataTransformerInterface
*/
public function reverseTransform($value)
{
$result = $value instanceof UploadedFile ? $value : (is_string($value) ? $this->reverseTransformFile($value) : null);
return $result;
if(!$this->multiple) {
return $this->reverseTransformFile($value);
}
$result = [];
if(is_array($value) || $value instanceof Traversable) {
foreach($value as $index => $file) {
$result[$index] = $this->reverseTransformFile($file);
}
}
return array_filter($result);
}
/**
......@@ -48,27 +58,44 @@ class UploadedFileTransformer implements DataTransformerInterface
*/
public function transform($value)
{
$result = $value instanceof UploadedFile ? $this->transformFile($value) : null;
return $result;
if(!$this->multiple) {
return $this->transformFile($value);
}
$result = [];
if(is_array($value) || $value instanceof Traversable) {
foreach($value as $index => $file) {
$result[$index] = $this->transformFile($file);
}
}
return array_filter($result);
}
/**
*
* @param UploadedFile $file
* @return array
* @param mixed $value
* @return mixed
*/
protected function transformFile(UploadedFile $file)
protected function transformFile($value)
{
return $file;
return $value instanceof UploadedFile ? $value : null;
}
/**
*
* @param string $id
* @param mixed $value
* @return UploadedFile
*/
protected function reverseTransformFile($id)
protected function reverseTransformFile($value)
{
return $this->fileManager->get($id);
if(is_string($value) && Uuid::isValid($value)) {
$value = $this->fileManager->get($value);
}
if($value instanceof UploadedFile) {
if($value->isOrphelin()) {
$value->setEtat(UploadedFile::ETAT_NORMAL);
}
return $value;
}
return null;
}
}
......@@ -54,7 +54,7 @@ class FileUploadType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setCompound(false);
$builder->addViewTransformer(new UploadedFileTransformer($this->fileManager));
$builder->addViewTransformer(new UploadedFileTransformer($this->fileManager, $options['multiple']));
}
public function buildView(FormView $view, FormInterface $form, array $options)
......@@ -63,6 +63,7 @@ class FileUploadType extends AbstractType
$view->vars['widget_attr']['data-create-url'] = $this->router->generate('file_upload_create');
$view->vars['csrfToken'] = $this->csrfProvider->generateCsrfToken(UploadController::CSRF_INTENTION);
$view->vars['multiple'] = $options['multiple'];
}
protected function getWidgetDefaults()
......
......@@ -20,26 +20,32 @@
data-upload-prototype="{{ block('file_upload_progress_prototype')|e }}">
<div class="fileinput-entries">
{%- if value is not empty %}
{%- set file = value %}
{%- block file_upload_entry_prototype %}
{%- set file = file|default([]) %}
<p class="template-download">
<input type="hidden" name="{{ full_name }}" value="{{ file.id|default }}"/>
<button class="btn btn-xs btn-danger delete" title="{% trans %}button.delete{% endtrans %}">
{{- irstea_icon('trash') -}}
</button>&nbsp;<span class="name">
<a{% if file %} href="{{ path('file_upload_get', {id: file.id, token: csrfToken}) }}"{% endif %}>{{ file.originalFilename|default }}</a>
</span>&nbsp;(<span class="size">
{{- file.size|default -}}
</span>)
<span class="error danger"></span>
</p>
{% endblock -%}
{%- if multiple %}
{% for file in value %}
{{ block('file_upload_entry_prototype') }}
{% endfor %}
{%- else %}
{%- set file = value %}
{%- block file_upload_entry_prototype %}
{%- set file = file|default([]) %}
<p class="template-download">
<input type="hidden" name="{{ full_name }}{% if multiple %}[]{% endif %}" value="{{ file.id|default }}"/>
<button class="btn btn-xs btn-danger delete" title="{% trans %}button.delete{% endtrans %}">
{{- irstea_icon('trash') -}}
</button>&nbsp;<span class="name">
<a{% if file %} href="{{ path('file_upload_get', {id: file.id, token: csrfToken}) }}"{% endif %}>{{ file.originalFilename|default }}</a>
</span>&nbsp;(<span class="size">
{{- file.size|default -}}
</span>)
<span class="error danger"></span>
</p>
{% endblock -%}
{% endif -%}
{% endif -%}
</div>
<span class="btn btn-primary fileinput-button">
<span>{{ irstea_icon('upload') }}&nbsp;{% trans %}button.upload{% endtrans %}</span>
<input type="file" name="_hack_{{ full_name }}"{% if widget_attr['data-create-url'] %} multiple="multiple"{% endif %}/>
<input type="file" name="_hack_{{ full_name }}"{% if multiple %} multiple="multiple"{% endif %}/>
</span>
</div>
{% endblock file_upload_widget %}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment