3 * Copyright 2005 (c) GForge Group, LLC
4 * Copyright 2016-2017, Franck Villaume - TrivialDev
5 * http://fusionforge.org
7 * This file is part of FusionForge. FusionForge is free software;
8 * you can redistribute it and/or modify it under the terms of the
9 * GNU General Public License as published by the Free Software
10 * Foundation; either version 2 of the Licence, or (at your option)
13 * FusionForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 require_once $gfcommon.'tracker/ArtifactFactory.class.php';
27 session_require_perm('tracker', $ath->getID(), 'read');
29 $af = new ArtifactFactory($ath);
30 if (!$af || !is_object($af)) {
31 exit_error(_('Could Not Get Factory'),'tracker');
32 } elseif ($af->isError()) {
33 exit_error($af->getErrorMessage(),'tracker');
37 $headers = getIntFromRequest('headers');
38 $sep = getFilteredStringFromRequest('sep', '/^[,;]$/', ',');
39 $date_format = _('Y-m-d');
42 header('Content-type: text/csv');
43 header('Content-disposition: filename="trackers-'.date('Y-m-d-His').'.csv"');
45 $bom = getIntFromRequest('bom', 0);
46 $encoding = getStringFromRequest('encoding', 'UTF-8');
47 $offset = getStringFromRequest('offset');
48 $_sort_col = getStringFromRequest('_sort_col');
49 $_sort_ord = getStringFromRequest('_sort_ord');
50 $max_rows = getIntFromRequest('max_rows');
51 $set = getStringFromRequest('set');
52 $_assigned_to = getIntFromRequest('_assigned_to');
53 $_status = getIntFromRequest('_status');
54 $received_changed_from = getStringFromRequest('_changed_from', 0);
55 $overwrite_filter = getStringFromRequest('overwrite_filter', false);
56 if ($overwrite_filter) {
57 $set = $overwrite_filter;
59 if ($received_changed_from) {
60 $arrDateBegin = DateTime::createFromFormat($date_format, $received_changed_from);
61 $_changed_from = $arrDateBegin->getTimestamp();
66 $af->setup($offset, $_sort_col, $_sort_ord, $max_rows, $set, $_assigned_to, $_status, array(), $_changed_from);
68 $at_arr = $af->getArtifacts();
71 $s = 'artifact_id'.$sep.
76 'submitter_name'.$sep.
77 'assigned_to_id'.$sep.
78 'assigned_to_name'.$sep.
81 'last_modified_date'.$sep.
82 'last_modified_by'.$sep.
90 // Show the extra fields
92 $ef = $ath->getExtraFields();
93 $keys = array_keys($ef);
94 for ($i = 0; $i < count($keys); $i++) {
95 if ($ef[$keys[$i]]['field_type'] == ARTIFACT_EXTRAFIELDTYPE_EFFORT) {
96 $s .= $sep.'"'.'effort_unit for '.$ef[$keys[$i]]['field_name'].'"';
98 $s .= $sep.'"'.$ef[$keys[$i]]['field_name'].'"';
100 $s .= $sep.'comments';
104 for ($i = 0; $i < count($at_arr); $i++) {
106 $open_date = $at_arr[$i]->getOpenDate() ? date(_('Y-m-d H:i'),$at_arr[$i]->getOpenDate()) : '';
107 $update_date = $at_arr[$i]->getLastModifiedDate() ? date(_('Y-m-d H:i'),$at_arr[$i]->getLastModifiedDate()) : '';
108 $close_date = $at_arr[$i]->getCloseDate()? date(_('Y-m-d H:i'),$at_arr[$i]->getCloseDate()): '';
110 $votes = $at_arr[$i]->getVotes();
111 $s .= $at_arr[$i]->getID().$sep.
112 $at_arr[$i]->getStatusID().$sep.
113 '"'.$at_arr[$i]->getStatusName().'"'.$sep.
114 $at_arr[$i]->getPriority().$sep.
115 $at_arr[$i]->getSubmittedBy().$sep.
116 '"'.$at_arr[$i]->getSubmittedRealName().'"'.$sep.
117 $at_arr[$i]->getAssignedTo().$sep.
118 '"'.$at_arr[$i]->getAssignedRealName().'"'.$sep.
119 '"'.$open_date.'"'.$sep.
120 '"'.$close_date.'"'.$sep.
121 '"'.$update_date.'"'.$sep.
122 '"'.$at_arr[$i]->getLastModifiedRealName().'"'.$sep.
123 '"'.fix4csv($at_arr[$i]->getSummary()).'"'.$sep.
124 '"'.fix4csv($at_arr[$i]->getDetails()).'"'.$sep.
129 // Show the extra fields
130 $efd = $at_arr[$i]->getExtraFieldDataText();
131 foreach ( $efd as $key => $efd_pair ) {
132 if ($efd_pair['type'] == ARTIFACT_EXTRAFIELDTYPE_EFFORT) {
133 if (!isset($effortUnitSet)) {
134 $effortUnitSet = new EffortUnitSet($ath, $ath->getEffortUnitSet());
135 $effortUnitFactory = new EffortUnitFactory($effortUnitSet);
137 $units = $effortUnitFactory->getUnits();
138 $unitId = $effortUnitFactory->encodedToUnitId($efd_pair['value']);
139 $unittexts = _('Unknown');
140 foreach ($units as $unit) {
141 if ($unit->getID() == $unitId) {
142 $unittexts = $unit->getName();
145 $s .= $sep.'"'.fix4csv($unittexts).'"';
146 $value = $effortUnitFactory->encodedToValue($efd_pair['value']);
148 $value = $efd_pair["value"];
150 $s .= $sep.'"'.fix4csv($value).'"';
154 $result = $at_arr[$i]->getMessages();
156 while ($arr = db_fetch_array($result)) {
157 $date = date(_('Y-m-d H:i'), $arr['adddate']);
158 $realname = $arr['realname'];
159 $body = $arr['body'];
160 // replace all newline by ' ~ '
161 $body = str_replace(array("\r\n", "\r", "\n", PHP_EOL, chr(10), chr(13), chr(10).chr(13)), " ~ ", $body);
162 $comments .= ' *** '.$date.' --- '.$realname.' --- '.$body;
164 $s .= $sep.'"'.fix4csv($comments).'"';
169 if ($encoding == 'UTF-16LE') {
171 } elseif ($encoding == 'UTF-16BE') {
173 } elseif ($encoding == 'UTF-8') {
178 echo mb_convert_encoding($s, $encoding, "UTF-8");
180 function fix4csv($value) {
181 $value = util_unconvert_htmlspecialchars($value);
182 $value = str_replace("\r\n", "\n", $value);
183 $value = str_replace('"', '""', $value);