Rajout du support d'Excel
[zf2.biz/galerie.git] / vendor / zf2biz / Custom / Export / AbstractWorkbook.php
1 <?php
2
3 /**
4  * ZF2BIZ
5  *
6  * PHP version 5.3
7  *
8  * @category  Components
9  * @package   Custom\Export
10  * @author    Sébastien Chazallet <sebastien.chazallet@laposte.net>
11  * @copyright 2011 InsPyration
12  * @license   GNU GPL http://www.gnu.org/licenses/gpl.html
13  * @link      http://zf2.biz
14  * @since     0.0.1
15  *
16  */
17
18 namespace Custom\Export;
19
20 /**
21  * @see Spreadsheet_Excel_Writer
22  */
23
24 require_once 'Spreadsheet/Excel/Writer.php';
25
26 /**
27  * Cette classe permet de renvoyer un fichier Excel.
28  *
29  * Elle rajoute des facilités telles que :
30  * > la gestion de la ligne courante
31  * > la gestion de la case courante
32  * > le passage automatisé à la case suivante  (celle de droite)
33  * > le passage automatisé à la ligne suivante (celle du dessous)
34  * > la gestion des formats
35  *
36  * 
37  * Elle permet d'aider à générer facilement des exports Excel,
38  * mais ne contient PAS de logique de présentation,
39  * conformément aux principes de séparation du code
40  *
41  * @category  Components
42  * @package   Custom\Export
43  * @author    Sébastien Chazallet <sebastien.chazallet@laposte.net>
44  * @copyright 2011 InsPyration
45  * @license   GNU GPL http://www.gnu.org/licenses/gpl.html
46  * @link      http://zf2.biz
47  * @since     0.0.1
48  *
49  */
50
51 abstract class AbstractWorkbook
52 {
53
54     /**
55      * Document D'export
56      * @var Spreadsheet_Excel_Writer
57      */
58     protected $workbook;
59
60     /**
61      * Feuille courante
62      * @var Spreadsheet_Excel_Worksheet
63      */
64     protected $current_worksheet;
65
66     /**
67      * Ligne courante
68      * @var int
69      */
70     protected $current_line = 0;
71     /**
72      * Colonne courante
73      * @var int
74      */
75     protected $current_column = 0;
76
77     /**
78      * Données à exporter
79      * @var array
80      */
81     protected $datas;
82
83
84     /**
85      * Formats disponibles.
86      * @var array
87      */
88     protected $formats = array();
89
90
91     /**
92      * Liste des styles utilisés pour les chiffres
93      * constante de type tableau
94      * @var array
95      */
96     protected $suffixes_entier = array(
97         'chiffre',
98     );
99
100
101     /**
102      * Créateur de l'export
103      *
104      * @param array $datas données à écrire
105      *
106      * @return null
107      */
108     public function build ($datas)
109     {
110         $this->datas = $datas;
111         //Initialisation de la feuille Excel.
112         $this->initWorkbook();
113         // Initialisation de la feuille courante
114         $this->initCurrentWorksheet();
115         // Initialisation des formats
116         $this->initFormats();
117
118         //On ecrit les données
119         $this->writeData();
120
121         // Finalisation du formatage
122         $this->postFormats();
123
124         // Fermeture de la feuille Excel
125         $this->closeWorkbook();
126
127     }
128
129     /**
130      * Initialisation de l'export Excel.
131      * Mise en place du nom du fichier et de l'UTF-8.
132      *
133      * @return null
134      */
135     protected function initWorkbook()
136     {
137         $this->workbook = new \Spreadsheet_Excel_Writer();
138         $this->workbook->setVersion(8);
139         $this->workbook->send($this->nomFichier());
140     }
141
142     /**
143      * Nom du fichier utilisé pour l'export.
144      *
145      * @return string
146      */
147     abstract protected function nomFichier();
148
149     /**
150      * Initialisation d'une feuille qui devient la feuille courante.
151      *
152      * @return null
153      */
154     protected function initCurrentWorksheet()
155     {
156         $this->current_worksheet = $this->workbook->addWorksheet();
157         $this->current_worksheet->setInputEncoding('utf-8');
158     }
159
160     /**
161      * Permet de composer les formats à utiliser.
162      *
163      * @return null
164      */
165     protected function initFormats()
166     {
167         $this->initFormatTitreString();
168         $this->initFormatCaseString();
169         $this->initFormatCaseChiffre();
170     }
171
172
173     /**
174      * Initialisation d'un format pour un classeur
175      *
176      * @return null
177      */
178     protected function initFormatTitreString()
179     {
180         $format = &$this->workbook->addFormat();
181         $format->setSize(10);
182         $format->setBold();
183         $format->setAlign('center');
184         $format->setAlign('vcenter');
185         $format->setBottom(1);
186         $format->setTop(1);
187         $format->setLeft(1);
188         $format->setRight(1);
189         $format->setTextWrap();
190         $format->setFgColor(30);
191         $this->formats['titre_string'] = $format;
192     }
193
194     /**
195      * Initialisation d'un format pour un classeur
196      *
197      * @return null
198      */
199     protected function initFormatCaseString()
200     {
201         $format = &$this->workbook->addFormat();
202         $format->setSize(10);
203         $format->setAlign('left');
204         $format->setAlign('vcenter');
205         $format->setTop(1);
206         $format->setBottom(1);
207         $format->setRight(1);
208         $format->setLeft(1);
209         $this->formats['case_string'] = $format;
210     }
211
212     /**
213      * Initialisation d'un format pour un classeur
214      *
215      * @return null
216      */
217     protected function initFormatCaseChiffre()
218     {
219         $format = &$this->workbook->addFormat();
220         $format->setSize(10);
221         $format->setAlign('center');
222         $format->setAlign('vcenter');
223         $format->setTop(1);
224         $format->setBottom(1);
225         $format->setRight(1);
226         $format->setLeft(1);
227         $format->setNumFormat('0.00');
228         $this->formats['case_chiffre'] = $format;
229     }
230
231     /**
232      * Méthode d'écriture des données dans la feuille courante.
233      *
234      * @return null
235      */
236     abstract protected function writeData();
237
238     /**
239      * Mise en forme après écriture.
240      *
241      * @return null
242      */
243     abstract protected function postFormats();
244
245     /**
246      * Fermeture propre de l'export Excel.
247      * Clot son écriture et renvoie l'objet au client.
248      *
249      * @return null
250      */
251     protected function closeWorkbook()
252     {
253         $this->workbook->close();
254     }
255
256
257
258
259     /**
260      * Méthode permettant d'écrire quelque chose dans une case.
261      *
262      * @param string $line   numero de la ligne, commence par 0.
263      * @param string $column numero de la colonne, commence par 0.
264      * @param string $value  valeur à inscrire dans la casae
265      * @param string $format format à utiliser pour la case
266      *
267      * @return null
268      */
269     protected function ecrireCase($line, $column, $value, $format)
270     {
271         $details = explode('_', $format);
272         if (in_array($details[1], $this->suffixes_entier)) {
273             $int = true;
274         } else {
275             $int = false;
276         }
277         if (!$value) {
278             $this->current_worksheet->writeBlank(
279                 $line,
280                 $column,
281                 $this->formats[$format]
282             );
283         } else {
284             if ($int === false) {
285                 $this->current_worksheet->writeString(
286                     $line,
287                     $column,
288                     $value,
289                     $this->formats[$format]
290                 );
291             } else {
292                 $this->current_worksheet->writeNumber(
293                     $line,
294                     $column,
295                     $value,
296                     $this->formats[$format]
297                 );
298             }
299         }
300     }
301
302     /**
303      * Méthode permettant d'écrire quelque chose dans la case courante
304      *
305      * @param string $value   valeur à mettre dans la case courante
306      * @param string $format  format à utiliser pour la case courante
307      *
308      * @return null
309      */
310     protected function ecrireCaseCourante($value, $format)
311     {
312         $line = $this->current_line;
313         $column = $this->current_column;
314         $this->ecrireCase(
315             $this->current_line,
316             $this->current_column,
317             $value,
318             $format
319         );
320         $this->next();
321     }
322
323     /**
324      * Méthode permettant de merger avec la case précédente.
325      *
326      * @param string $line   numero de la ligne, commence par 0.
327      * @param string $column numero de la colonne, commence par 0.
328      *
329      * @return null
330      */
331     protected function mergerAvecCasePrecedente($line=null, $column=null)
332     {
333         if ($line === null ) {
334             $line = $this->current_line;
335         }
336         if ($column === null) {
337             $column = $this->current_column;
338         }
339         $this->current_worksheet->mergeCells(
340             $line,
341             $column-1,
342             $line,
343             $column
344         );
345     }
346
347     /**
348      * Passage à la case suivante.
349      *
350      * @return null
351      */
352     protected function next()
353     {
354         $this->current_column++;
355     }
356
357     /**
358      * Passage à la ligne suivante.
359      *
360      * @return null
361      */
362     protected function nextLine()
363     {
364         $this->current_column=0;
365         $this->current_line++;
366     }
367
368 }