Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
report_export_csv.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 
8 require_once 'FDL/Class.Doc.php';
9 require_once 'FDL/freedom_util.php';
10 require_once 'EXTERNALS/fdl.php';
11 /**
12  * Export a report in CSV
13  *
14  * @note
15  * @verbatim
16  Usage :
17  --app=FDL : <application name>
18  --action=REPORT_EXPORT_CSV : <action name>
19  --id=<the id of the report>
20  Options:
21  --refresh=<would you refresh doc before build report> [TRUE|FALSE], default is 'FALSE'
22  --kind=<the kind of report> [simple|pivot], default is 'simple'
23  --pivot=<the pivot attr>, default is 'id'
24  --delimiter=<the CSV delimiter>, default is ';'
25  --enclosure=<the CSV enclosure>, default is '"'
26  --encoding=<the CSV encoding>, default is 'ISO-8859-15//TRANSLIT'
27  --decimalSeparator=<the decimalSeparator>, default is '.'
28  --dateFormat=<the dateFormat> [US|FR|ISO], default is 'US
29  @endverbatim
30  */
32 {
33  $dbaccess = getParam('FREEDOM_DB');
34 
35  $argumentsCSV = array();
36 
37  $defaultArgument = json_decode(getParam("REPORT_DEFAULT_CSV", "[]") , true);
38 
39  $usage = new ActionUsage($action);
40  $defaultDocArg = array();
41 
42  $id = $usage->addNeeded("id", "the id of the report");
43 
44  if ($id != "") {
45  $currentDoc = new_Doc($dbaccess, $id);
46  $currentUserTag = $currentDoc->getUTag("document_export_csv");
47  $defaultDocArg = json_decode($currentUserTag->comment, true);
48  }
49 
50  $refresh = ($usage->addOption("refresh", "would you refresh doc before build report", array(
51  "TRUE",
52  "FALSE"
53  ) , "FALSE") == "TRUE");
54 
55  $default = isset($defaultDocArg["kind"]) ? $defaultDocArg["kind"] : 'simple';
56  $kind = $usage->addOption("kind", "the kind of report", array(
57  "simple",
58  "pivot"
59  ) , $default);
60  $default = isset($defaultDocArg["pivot"]) ? $defaultDocArg["pivot"] : 'id';
61  $pivot = $usage->addOption("pivot", "the pivot attr", array() , $default);
62 
63  $default = isset($defaultArgument["delimiter"]) ? $defaultArgument["delimiter"] : ';';
64  $argumentsCSV["delimiter"] = $usage->addOption("delimiter", "the CSV delimiter", array() , $default);
65  $default = isset($defaultArgument["enclosure"]) ? $defaultArgument["enclosure"] : '"';
66  $argumentsCSV["enclosure"] = $usage->addOption("enclosure", "the CSV enclosure", array() , $default);
67  $default = isset($defaultArgument["encoding"]) ? $defaultArgument["encoding"] : 'ISO-8859-15//TRANSLIT';
68  $argumentsCSV["encoding"] = $usage->addOption("encoding", "the CSV encoding", array() , $default);
69  $default = isset($defaultArgument["decimalSeparator"]) ? $defaultArgument["decimalSeparator"] : '.';
70  $argumentsCSV["decimalSeparator"] = $usage->addOption("decimalSeparator", "the decimalSeparator", array() , $default);
71  $default = isset($defaultArgument["dateFormat"]) ? $defaultArgument["dateFormat"] : 'US';
72  $argumentsCSV["dateFormat"] = $usage->addOption("dateFormat", "the dateFormat", array(
73  'US',
74  'FR',
75  'ISO'
76  ) , $default);
77 
78  $displayForm = $usage->addHidden("displayForm", "");
79  $updateDefault = $usage->addHidden("updateDefault", "");
80 
81  $usage->strict(false);
82  $usage->verify();
83 
84  $usageArguments = array(
85  "sole",
86  "app",
87  "action",
88  "id",
89  "refresh",
90  "kind",
91  "pivot",
92  "delimiter",
93  "enclosure",
94  "encoding",
95  "decimalSeparator",
96  "dateFormat",
97  "displayForm",
98  "updateDefault"
99  );
100  $addedArguments = array();
101 
102  foreach ($_GET as $key => $value) {
103  if (!in_array($key, $usageArguments)) {
104  $addedArguments[] = array(
105  "argumentName" => $key,
106  "argumentValue" => $value
107  );
108  }
109  }
110 
111  $action->lay->setBlockData("addedArguments", $addedArguments);
112 
113  if ($updateDefault) {
114  $action->setParamU("REPORT_DEFAULT_CSV", json_encode($argumentsCSV));
115  $err = $currentDoc->addUTag($action->user->id, "document_export_csv", json_encode(array(
116  "kind" => $kind,
117  "pivot" => $pivot
118  )));
119  if ($err) {
120  error_log(__LINE__ . " " . var_export($err, true));
121  }
122  }
123 
124  if ($displayForm) {
125  $action->lay->set("id", $id);
126  $attr = getDocAttr($dbaccess, $currentDoc->getValue("se_famid", 1));
127  $attributeLay = array();
128  $attributeLay[] = array(
129  "key" => "id",
130  "libelle" => _("EXPORT_CSV : identifiant unique")
131  );
132 
133  $isSelected = function ($currentValue, $selectedValue)
134  {
135  return $currentValue == $selectedValue ? "selected='selected'" : "";
136  };
137 
138  foreach ($attr as $currentAttr) {
139  $attributeLay[] = array(
140  "key" => $currentAttr[1],
141  "libelle" => $currentAttr[0],
142  "selected" => $isSelected($currentAttr[1], $pivot) ,
143  );
144  }
145 
146  $action->lay->setBlockData("pivotAttribute", $attributeLay);
147 
148  $kinds = array(
149  array(
150  "key" => "simple",
151  "selected" => $isSelected("simple", $kind) ,
152  "label" => _("EXPORT_CSV Simple")
153  ) ,
154  array(
155  "key" => "pivot",
156  "selected" => $isSelected("pivot", $kind) ,
157  "label" => _("EXPORT_CSV pivot")
158  )
159  );
160  $action->lay->setBlockData("kinds", $kinds);
161 
162  $encodings = array(
163  array(
164  "key" => "UTF-8",
165  "selected" => $isSelected("UTF-8", $argumentsCSV["encoding"]) ,
166  "label" => _("EXPORT_CSV utf8")
167  ) ,
168  array(
169  "key" => "ISO-8859-15//TRANSLIT",
170  "selected" => $isSelected("ISO-8859-15//TRANSLIT", $argumentsCSV["encoding"]) ,
171  "label" => _("EXPORT_CSV ISO-8859-15 (european)")
172  )
173  );
174  $action->lay->setBlockData("encodings", $encodings);
175 
176  $dateFormats = array(
177  array(
178  "key" => "US",
179  "selected" => $isSelected("US", $argumentsCSV["dateFormat"]) ,
180  "label" => _("EXPORT_CSV Date format US")
181  ) ,
182  array(
183  "key" => "FR",
184  "selected" => $isSelected("FR", $argumentsCSV["dateFormat"]) ,
185  "label" => _("EXPORT_CSV Date format FR")
186  ) ,
187  array(
188  "key" => "ISO",
189  "selected" => $isSelected("ISO", $argumentsCSV["dateFormat"]) ,
190  "label" => _("EXPORT_CSV Date format ISO")
191  )
192  );
193  $action->lay->setBlockData("dateFormats", $dateFormats);
194 
195  $action->lay->set("delimiter", $argumentsCSV["delimiter"]);
196  $action->lay->set("enclosure", $argumentsCSV["enclosure"]);
197  $action->lay->set("decimalSeparator", $argumentsCSV["decimalSeparator"]);
198  } else {
199 
200  $reportFamId = getIdFromName($dbaccess, "REPORT");
201  $familyIdArray = $currentDoc->getFromDoc();
202 
203  if (in_array($reportFamId, $familyIdArray)) {
204  switch ($kind) {
205  case "pivot":
206  $csvStruct = $currentDoc->generateCSVReportStruct(true, $pivot, $argumentsCSV["decimalSeparator"], $argumentsCSV["dateFormat"], $refresh);
207  break;
208 
209  default:
210  $csvStruct = $currentDoc->generateCSVReportStruct(false, "", $argumentsCSV["decimalSeparator"], $argumentsCSV["dateFormat"], $refresh);
211  }
212 
213  $csvFile = tempnam($action->GetParam("CORE_TMPDIR", "/tmp") , "csv$id") . ".csv";
214  $fp = fopen($csvFile, 'w');
215 
216  foreach ($csvStruct as $currentLine) {
217  if ($argumentsCSV["encoding"] != "UTF-8") {
218  $currentLine = convertLine($currentLine, $argumentsCSV["encoding"]);
219  }
220  fputcsv($fp, $currentLine, $argumentsCSV["delimiter"], $argumentsCSV["enclosure"]);
221  }
222 
223  fclose($fp);
224 
225  if (!$_SERVER['HTTP_HOST']) {
226  $handle = fopen($csvFile, 'r');
227  $content = fread($handle, filesize($csvFile));
228  fclose($handle);
229  $action->lay->noParse = true;
230  $action->lay->template = $content;
231  } else {
232  $fileName = sprintf("%s_%s.%s", $currentDoc->getTitle() , date("Y_m_d-H_m_s") , "csv");
233  Http_DownloadFile($csvFile, $fileName, "text/csv", false, false, true);
234  }
235  } else {
236  $action->exitError('The document is not a report');
237  }
238  }
239 }
240 
241 function convertLine($currentLine, $encoding)
242 {
243  $returnArray = array();
244  foreach ($currentLine as $currentValue) {
245  $returnArray[] = iconv("UTF-8", $encoding, $currentValue);
246  }
247  return $returnArray;
248 }
← centre documentaire © anakeen - published under CC License - Dynacase