Rajout d'un export Excel
authorSébastien CHAZALLET <s.chazallet@gmail.com>
Tue, 27 Nov 2012 20:59:53 +0000 (21:59 +0100)
committerSébastien CHAZALLET <s.chazallet@gmail.com>
Tue, 27 Nov 2012 20:59:53 +0000 (21:59 +0100)
module/Galerie/Module.php
module/Galerie/autoload_classmap.php
module/Galerie/config/module.config.php
module/Galerie/src/Galerie/Controller/IndexController.php
module/Galerie/src/Galerie/Export/GalerieWorkbook.php [new file with mode: 0644]

index ab7faad..a21e4ec 100644 (file)
@@ -15,6 +15,7 @@ use Zend\Mvc\ModuleRouteListener;
 use Galerie\Model\GalerieTable;
 use Galerie\Model\GalerieInfoTable;
 use Galerie\Form\GalerieForm;
+use Galerie\Export\GalerieWorkbook;
 
 use Custom\View\Helper\Format;
 
@@ -72,6 +73,9 @@ class Module implements
                     $result->initialize();
                     return $result;
                 },
+                'Galerie\Export\GalerieWorkbook' => function ($sm) {
+                    return new GalerieWorkbook;
+                },
             ),
         );
     }
index 7f2deab..f59b8ba 100644 (file)
@@ -3,4 +3,6 @@
 return array(
     'Galerie\Controller\IndexController' =>
         __DIR__ . '/src/Galerie/Controller/IndexController.php',
+    'Galerie\Export\GalerieWorkbook' =>
+        __DIR__ . '/src/Galerie/Export/GalerieWorkbook.php'
 );
index 5f96569..21c80cc 100644 (file)
@@ -128,6 +128,16 @@ return array(
                         ),
                         'verb' => 'get',
                     ),
+                    'excel' => array(
+                        'type'    => 'Literal',
+                        'options' => array(
+                            'route'    => '/excel',
+                            'defaults' => array(
+                                'action' => 'excel',
+                            ),
+                        ),
+                        'verb' => 'get',
+                    ),
 /*
                     'default' => array(
                         'type'    => 'Segment',
index 1f358f3..551d046 100644 (file)
@@ -14,6 +14,7 @@ class IndexController extends AbstractActionController
     private $_galerieTable;
     private $_galerieInfoTable;
     private $_galerieForm;
+    private $_galerieInfoExporter;
 
     private $_translator;
 
@@ -54,6 +55,16 @@ class IndexController extends AbstractActionController
         return $this->_galerieForm;
     }
 
+    private function _getGalerieInfoExporter()
+    {
+        if (!$this->_galerieInfoExporter) {
+            $sm = $this->getServiceLocator();
+            $this->_galerieInfoExporter = $sm->get('Galerie\Export\GalerieWorkbook');
+        }
+        return $this->_galerieInfoExporter;
+    }
+
+
 
 
     public function indexAction() 
@@ -76,15 +87,30 @@ class IndexController extends AbstractActionController
         $response = $this->getResponse();
         $response->setStatusCode(200);
 
+        // Modification des entêtes
         $headers = $this->getResponse()->getHeaders();
         $headers->addHeaderLine('Content-Type', 'text/csv; charset=utf-8');
         $headers->addHeaderLine('Content-Disposition', 'attachment; filename="export_galerie.csv"');
 
-
         $response->setContent(implode("\r\n", $content));
+
         return $response;
     }
 
+
+    public function excelAction() {
+        // Récupération des informations brutes
+        $modelManager = $this->_getGalerieInfoTable();
+        $datas = $modelManager->all();
+
+        $exporter = $this->_getGalerieInfoExporter();
+        $exporter->build($datas);
+
+        // Renvoi d'une réponse vide pour désactiver le rendu de la vue
+        return $this->getResponse();
+    }
+
+
     public function listAction() 
     { 
         // Récupération de l'objet requête
diff --git a/module/Galerie/src/Galerie/Export/GalerieWorkbook.php b/module/Galerie/src/Galerie/Export/GalerieWorkbook.php
new file mode 100644 (file)
index 0000000..2e55466
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+
+namespace Galerie\Export;
+
+use Custom\Export\AbstractWorkbook;
+
+
+class GalerieWorkbook extends AbstractWorkbook
+{
+
+    /**
+     * Nom du fichier utilisé pour l'export.
+     *
+     * @return string
+     */
+    protected function nomFichier()
+    {
+        return 'export_galerie.xls';
+    }
+
+
+
+
+    /**
+     * Méthode d'écriture des données dans la feuille courante.
+     *
+     * @return null
+     */
+    protected function writeData()
+    {
+        $this->ecrireCaseCourante("Nom", 'titre_string');
+        $this->ecrireCaseCourante("Description", 'titre_string');
+        $this->ecrireCaseCourante("Propriétaire", 'titre_string');
+        $this->ecrireCaseCourante("Photos", 'titre_string');
+        $this->nextLine();
+        foreach($this->datas as $d) {
+            $this->ecrireCaseCourante($d->name, 'case_string');
+            $this->ecrireCaseCourante($d->description, 'case_string');
+            $this->ecrireCaseCourante($d->username, 'case_string');
+            $this->ecrireCaseCourante($d->nb, 'case_chiffre');
+            $this->nextLine();
+        }
+    }
+
+
+
+
+    /**
+     * Mise en forme après écriture.
+     *
+     * @return null
+     */
+    protected function postFormats()
+    {
+        $this->current_worksheet->setColumn(0, 0, '10');
+        $this->current_worksheet->setColumn(1, 1, '40');
+        $this->current_worksheet->setColumn(2, 2, '10');
+        $this->current_worksheet->setColumn(3, 3, '5');
+
+        $this->current_worksheet->setRow(0, 50);
+        $c = count($this->datas);
+        for($i=1;$i<=$c;$i++) {
+            $this->current_worksheet->setRow($i, 30);
+        }
+
+        $this->current_worksheet->setLandscape();
+        $this->current_worksheet->hideGridLines();
+    }  
+
+}
+