3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2009-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Disponible depuis la Release 0.7alpha - 2009-02-09
41 use Laminas\Db\Adapter\Adapter
;
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2009-2014 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Disponible depuis la Release 0.7alpha - 2009-02-09
56 class CsvOut
extends Csv
58 const DEFAULT_DIRECTORY
= GALETTE_EXPORTS_PATH
;
61 private $parameted_path;
62 private $parameted_file = 'exports.xml';
67 public function __construct()
69 parent
::__construct(self
::DEFAULT_DIRECTORY
);
70 $this->parameted_path
= GALETTE_CONFIG_PATH
;
71 $this->parameted_file
= $this->parameted_path
. $this->parameted_file
;
75 * Export Array result set to CSV
77 * @param array $rs Results as an array
78 * @param string $separator The CSV separator (either '\t', ';' or ','
80 * @param char $quote how does fields should be quoted
81 * @param bool $titles does export shows column titles or not.
83 * @param object $file export to a file on disk. A file pointer
84 * should be passed here. Defaults to false.
86 * @return string CSV result
88 public function export($rs, $separator, $quote, $titles = false, $file = false)
93 //switch back to the default separator if not in accepted_separators array
94 if (!in_array($separator, $this->accepted_separators
)) {
95 $separator = self
::DEFAULT_SEPARATOR
;
97 //switch back to the default quote if not in accepted_quotes array
98 if (!in_array($quote, $this->accepted_quotes
)) {
99 $quote = self
::DEFAULT_QUOTE
;
104 foreach ($rs as $row) {
107 $this->max
= count($results);
108 $this->separator
= $separator;
109 $this->quote
= $quote;
110 //dubbing quote for escaping
111 $this->escaped
= $quote . $quote;
113 $this->current_line
= 0;
116 if ($titles && !is_array($titles)) {
118 foreach (array_keys((array)$row) as $field) {
119 $fields[] = $this->quote
. str_replace(
125 $this->result
.= implode($this->separator
, $fields) . self
::NEWLINE
;
126 } elseif ($titles && is_array($titles) && count($titles) > 1) {
127 foreach ($titles as $field) {
128 $field = str_replace(
129 array(':', ' '),
133 $fields[] = $this->quote
. str_replace(
139 $this->result
.= implode($this->separator
, $fields) . self
::NEWLINE
;
142 foreach ($results as $row) {
145 if (is_array($row) ||
is_object($row)) {
146 foreach ($row as $v) {
147 $elts[] = $this->quote
. str_replace(
154 $this->result
.= implode($this->separator
, $elts) . self
::NEWLINE
;
156 $this->current_line +
= 1;
162 return $this->result
;
167 * If a file is defined, export will be outpoutted into it.
168 * If not, it will be returned
170 * @param bool $last true if we write the latest line
174 private function write($last = false)
178 ||
!$last && $this->file
179 && ($this->current_line % self
::BUFLINES
) == 0
181 if ($this->file
=== true) {
184 fwrite($this->file
, $this->result
);
191 * Retrieve parameted export name
193 * @param string $id Parameted export identifier
197 public function getParamedtedExportName($id)
199 $xml = simplexml_load_file($this->parameted_file
);
200 $xpath = $xml->xpath(
201 '/exports/export[@id=\'' . $id . '\'][1]/@name'
203 return (string)$xpath[0];
207 * Get al list of all parameted exports
211 public function getParametedExports()
213 $parameted = array();
215 $xml = simplexml_load_file($this->parameted_file
);
217 foreach ($xml->export
as $export) {
218 if (!($export['inactive'] == 'inactive')) {
219 $parameted[] = array(
220 'id' => (string)$export['id'],
221 'name' => (string)$export['name'],
222 'description' => (string)$export['description']
230 * Run selected export
232 * @param string $id export's id to run
234 * @return string filename used
236 public function runParametedExport($id)
240 $xml = simplexml_load_file($this->parameted_file
);
242 $xpath = $xml->xpath(
243 '/exports/export[@id=\'' . $id . '\'][not(@inactive)][1]'
248 $results = $zdb->db
->query(
249 str_replace('galette_', PREFIX_DB
, $export->query
),
250 Adapter
::QUERY_MODE_EXECUTE
253 $filename = self
::DEFAULT_DIRECTORY
. $export['filename'];
255 $fp = fopen($filename, 'w');
257 $separator = ($export->separator
)
259 : self
::DEFAULT_SEPARATOR
;
260 $quote = ($export->quote
) ?
$export->quote
: self
::DEFAULT_QUOTE
;
261 if ($export->headers
->none
) {
265 $xpath = $export->xpath('headers/header');
266 if (count($xpath) == 0) {
271 foreach ($xpath as $header) {
272 $title[] = (string)$header;
277 $this->export($results, $separator, $quote, $title, $fp);
281 'File ' . $filename . ' is not writeable.',
284 return self
::FILE_NOT_WRITABLE
;
286 return $export['filename'];
287 } catch (\Exception
$e) {
289 'An error occurred while exporting | ' . $e->getMessage(),
292 return self
::DB_ERROR
;