FusionDirectory
class_csvExporter.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2003-2010 Cajus Pollmeier
5  Copyright (C) 2011-2018 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
31 {
32  var $result;
33 
45  function __construct ($headline, $header, $entries, $columns = [])
46  {
47  // If no preset, render all columns
48  if (!count($columns)) {
49  foreach ($header as $index => $dummy) {
50  $columns[] = $index;
51  }
52  }
53 
54  // Generate header
55  $this->result = '#';
56  foreach ($columns as $index) {
57  if (isset($header[$index])) {
58  $this->result .= trim(html_entity_decode($header[$index], ENT_QUOTES, 'UTF-8').";");
59  } else {
60  $this->result .= ";";
61  }
62  }
63  $this->result = preg_replace('/;$/', '', $this->result)."\n";
64 
65  // Append entries
66  foreach ($entries as $row) {
67  foreach ($columns as $index) {
68  if (isset($row["_sort$index"])) {
69  $this->result .= trim(html_entity_decode($row["_sort$index"], ENT_QUOTES, 'UTF-8')).";";
70  } else {
71  $this->result .= ";";
72  }
73  }
74  $this->result = preg_replace('/;$/', '', $this->result)."\n";
75  }
76  }
77 
81  function query ()
82  {
83  return $this->result;
84  }
85 
86  static function export (managementListing $listing)
87  {
88  $columns = $listing->getColumns();
89  $iterator = $listing->getIterator();
90 
91  // Generate header
92  $result = '#';
93  foreach ($columns as $column) {
94  if ($column->isExportable()) {
95  $result .= $column->getLabel().';';
96  }
97  }
98  $result = preg_replace('/;$/', '', $result)."\n";
99 
100  // Append entries
101  foreach ($iterator as $entry) {
102  foreach ($columns as $column) {
103  if ($column->isExportable()) {
104  $result .= implode(',', $column->getRawExportValues($entry)).';';
105  }
106  }
107  $result = preg_replace('/;$/', '', $result)."\n";
108  }
109 
110  return $result;
111  }
112 
116  static function getInfo ()
117  {
118  return ["exportCSV" => [ "label" => _("CSV"), "image" => "geticon.php?context=mimetypes&icon=text-csv&size=16", "class" => "csvExporter", "mime" => "text/x-csv", "filename" => "export.csv" ]];
119  }
120 }
__construct($headline, $header, $entries, $columns=[])
Create a csvExporter.
query()
Get the result.
This class handles the entries list for a management instance.
static getInfo()
Get Informations.
This class contains all the functions needed for csv export.