From 4f755cffb97875e07779acd30737d34fe83f5363 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Wed, 4 Feb 2015 16:33:26 +0100
Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20CRUD=20pour=20les=20fichiers=20u?=
 =?UTF-8?q?pload=C3=A9s.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Controller/UploadedFileController.php        | 71 ++++++++++++++++
 Resources/translations/messages.fr.yml       | 26 +++++-
 Resources/views/UploadedFile/index.html.twig | 44 ++++++++++
 Resources/views/UploadedFile/show.html.twig  | 86 ++++++++++++++++++++
 4 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100644 Controller/UploadedFileController.php
 create mode 100644 Resources/views/UploadedFile/index.html.twig
 create mode 100644 Resources/views/UploadedFile/show.html.twig

diff --git a/Controller/UploadedFileController.php b/Controller/UploadedFileController.php
new file mode 100644
index 00000000..048dc3f4
--- /dev/null
+++ b/Controller/UploadedFileController.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Irstea\FileUploadBundle\Controller;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
+use Irstea\FileUploadBundle\Entity\UploadedFile;
+
+use Pagerfanta\Adapter\DoctrineORMAdapter;
+use Pagerfanta\Pagerfanta;
+
+/**
+ * UploadedFile controller.
+ *
+ * @Route("/files")
+ */
+class UploadedFileController extends Controller
+{
+    /**
+     * Lists all UploadedFile entities.
+     *
+     * @Route("/", name="files")
+     * @Method("GET")
+     * @Template()
+     */
+    public function indexAction(Request $request)
+    {
+        $em = $this->getDoctrine()->getManager();
+
+        $queryBuilder = $em->getRepository('IrsteaFileUploadBundle:UploadedFile')
+            ->createQueryBuilder("u")
+            ->orderBy("u.createdAt", "DESC");
+
+        $pager = new Pagerfanta(new DoctrineORMAdapter($queryBuilder));
+        $pager->setCurrentPage($request->query->get("page", 1));
+
+        $uploadedFiles = $pager->getCurrentPageResults();
+
+        return [
+            'uploadedFiles' => $uploadedFiles,
+            'pager' => $pager,
+        ];
+    }
+
+    /**
+     * Finds and displays a UploadedFile entity.
+     *
+     * @Route("/{id}", name="files_show")
+     * @Method("GET")
+     * @Template()
+     */
+    public function showAction(UploadedFile $uploadedFile)
+    {
+
+        return [
+            'uploadedFile' => $uploadedFile,
+        ];
+    }
+
+    /**
+     * @param string $message
+     * @param array $parameters
+     */
+    public function notice($message, array $parameters = [])
+    {
+        $this->get('monolog.logger.irstea_logger')->notice($message, $parameters);
+    }
+}
diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml
index bdbe1a71..a3cd9019 100644
--- a/Resources/translations/messages.fr.yml
+++ b/Resources/translations/messages.fr.yml
@@ -1,2 +1,26 @@
+button.upload: 'Ajouter un fichier'
 
-button.upload: Ajouter un fichier
+UploadedFile:
+    index:
+        title: Liste des fichiers
+        breadcrumb: Fichiers
+    id.label: Identifiant
+    displayName.label: Nom
+    etat:
+        label: Etat
+        en-cours: En cours
+        orphelin: Orphelin
+        normal: Normal
+        corrompu: Corrompu
+        manquant: Manquant
+        rejete: Refusé
+    mimeType.label: Type MIME
+    size.label: Taille
+    checksum.label: Somme de contrôle
+    createdAt.label: Date de création
+    show.title(%uploadedFile%): Fichier %uploadedFile%
+    path.label: Chemin
+    createdBy.label: Créateur
+    createdFrom.label: Adresse IP
+    metadata.label: Métadonnées
+    description.label: Description
diff --git a/Resources/views/UploadedFile/index.html.twig b/Resources/views/UploadedFile/index.html.twig
new file mode 100644
index 00000000..73092aa4
--- /dev/null
+++ b/Resources/views/UploadedFile/index.html.twig
@@ -0,0 +1,44 @@
+{% extends '::base.html.twig' %}
+
+{% block titrePage %}{% trans %}UploadedFile.index.title{% endtrans %}{% endblock titrePage %}
+
+{% block breadcrumb_content %}
+    <li>{% trans %}UploadedFile.index.breadcrumb{% endtrans %}</li>
+{% endblock breadcrumb_content %}
+
+
+{% block content -%}
+    <table class="table table-striped records_list">
+        <thead>
+            <tr>
+                <th>{% trans %}UploadedFile.id.label{% endtrans %}</th>
+                <th>{% trans %}UploadedFile.displayName.label{% endtrans %}</th>
+                <th>{% trans %}UploadedFile.etat.label{% endtrans %}</th>
+                <th>{% trans %}UploadedFile.mimeType.label{% endtrans %}</th>
+                <th>{% trans %}UploadedFile.size.label{% endtrans %}</th>
+                <th>{% trans %}UploadedFile.createdAt.label{% endtrans %}</th>
+                <th>&nbsp;</th>
+            </tr>
+        </thead>
+        <tbody>
+        {% for uploadedFile in uploadedFiles %}
+            <tr>
+                <td><a href="{{ path('files_show', { 'id': uploadedFile.id }) }}">{{ uploadedFile.id }}</a></td>
+                <td>{{ uploadedFile.displayName }}</td>
+                <td><span class="label label-{{ uploadedFile.etat == 'orphelin' ? 'warning' : (uploadedFile.etat == 'normal' ? 'success' : (uploadedFile.etat == 'en-cours' ? 'default' : 'danger')) }}">{{ uploadedFile.etat }}</span></td>
+                <td>{% if uploadedFile.mimeType is not empty %}{{ irstea_file_icon(uploadedFile.mimeType) }}&nbsp;{{ uploadedFile.mimeType }}{% else %}-{% endif %}</td>
+                <td>{{ uploadedFile.size|irstea_file_size|default('-') }}</td>
+                <td>{% if uploadedFile.createdAt %}{{ uploadedFile.createdAt|localizeddate('medium', 'medium') }}{% endif %}</td>
+                <td>
+                    <a class="btn btn-xs btn-default" title="{% trans %}button.show{% endtrans %}" href="{{ path('files_show', { 'id': uploadedFile.id }) }}">{{ irstea_icon("search") }}</a>
+                </td>
+            </tr>
+        {% endfor %}
+        </tbody>
+        <tfoot>
+            <tr>
+                <td colspan="13"><div class="pagerfanta pull-right">{{ pagerfanta(pager) }}</div></td>
+            </tr>
+        </tfoot>
+    </table>
+{% endblock %}
diff --git a/Resources/views/UploadedFile/show.html.twig b/Resources/views/UploadedFile/show.html.twig
new file mode 100644
index 00000000..608aa782
--- /dev/null
+++ b/Resources/views/UploadedFile/show.html.twig
@@ -0,0 +1,86 @@
+{% extends '::base.html.twig' %}
+
+{% block titrePage %}{% trans %}UploadedFile.show.title(%uploadedFile%){% endtrans %}{% endblock titrePage %}
+
+{% block breadcrumb_content %}
+    <li><a href="{{ path('files') }}">{% trans %}UploadedFile.index.breadcrumb{% endtrans %}</a></li>
+    <li>{{ uploadedFile }}</li>
+{% endblock breadcrumb_content %}
+
+{% block content_right_header %}
+{% endblock content_right_header %}
+
+{% macro prettyPrint(data) %}
+    {%- if data is iterable and data is not empty %}
+        <div style="margin: 0.2em 0 1em 2em">
+            {%- for key, value in data %}
+                <div><strong>{{ key }}&nbsp;:</strong> {{ _self.prettyPrint(value) }}</div>
+            {%- endfor %}
+        </div>
+    {%- else %}
+        {{- data|json_encode }}
+    {%- endif %}
+{% endmacro %}
+
+{% block content -%}
+    <div class="row">
+        <div class="col-md-6">
+            <div class="panel panel-default">
+                <div class="panel-heading"><strong>Identification</strong></div>
+                <div class="panel-body row">
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.id.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.id }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.displayName.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.displayName }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.path.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.path }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.description.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.description|default('-') }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.etat.label{% endtrans %}&nbsp;:</strong> <span class="label label-{{ uploadedFile.etat == 'orphelin' ? 'warning' : (uploadedFile.etat == 'normal' ? 'success' : (uploadedFile.etat == 'en-cours' ? 'default' : 'danger')) }}">{{ uploadedFile.etat }}</span>
+                    </p>
+                </div>
+            </div>
+            <div class="panel panel-default">
+                <div class="panel-heading"><strong>Contenu</strong></div>
+                <div class="panel-body row">
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.mimeType.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.mimeType|default('-') }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.size.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.size|irstea_file_size|default('-') }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.checksum.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.checksum|default('-') }}
+                    </p>
+                </div>
+            </div>
+            <div class="panel panel-default">
+                <div class="panel-heading"><strong>Création</strong></div>
+                <div class="panel-body row">
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.createdAt.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.createdAt|localizeddate('medium', 'medium') }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.createdBy.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.createdBy|default('-') }}
+                    </p>
+                    <p>
+                        <strong class="col-md-3">{% trans %}UploadedFile.createdFrom.label{% endtrans %}&nbsp;:</strong> {{ uploadedFile.createdFrom|default('-') }}
+                    </p>
+                </div>
+            </div>
+        </div>
+        <div class="col-md-6">
+            <div class="panel panel-default">
+                <div class="panel-heading"><strong>Méta-données</strong></div>
+                <div class="panel-body row">
+                    {{ _self.prettyPrint(uploadedFile.metadata) }}
+                </div>
+            </div>
+        </div>
+    </div>
+{% endblock %}
-- 
GitLab