Core  3.2
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  * @package FDL
5 */
6 
7 require_once 'FDL/Class.Doc.php';
8 require_once 'FDL/freedom_util.php';
9 require_once 'EXTERNALS/fdl.php';
10 /**
11  * Export a report in CSV
12  *
13  * @note
14  * @verbatim
15  Usage :
16  * --app=FDL : <application name>
17  * --action=REPORT_EXPORT_CSV : <action name>
18  * --id=<the id of the report>
19  * Options:
20  * --refresh=<would you refresh doc before build report> [TRUE|FALSE], default is 'FALSE'
21  * --kind=<the kind of report> [simple|pivot], default is 'simple'
22  * --pivot=<the pivot attr>, default is 'id'
23  * --delimiter=<the CSV delimiter>, default is ';'
24  * --enclosure=<the CSV enclosure>, default is '"'
25  * --encoding=<the CSV encoding>, default is 'ISO-8859-15//TRANSLIT'
26  * --decimalSeparator=<the decimalSeparator>, default is '.'
27  * --dateFormat=<the dateFormat> [US|FR|ISO], default is 'US
28  * @endverbatim
29  * @param Action $action
30  * @throws \Dcp\Core\Exception
31  */
32 function report_export_csv(Action & $action)
33 {
34  $dbaccess = $action->dbaccess;
36  $argumentsCSV = array();
37  $defaultArgument = json_decode(getParam("REPORT_DEFAULT_CSV", "[]") , true);
38  $action->parent->addJsRef("lib/jquery/jquery.js");
39 
40  $usage = new ActionUsage($action);
41  $defaultDocArg = array();
42 
43  $id = $usage->addRequiredParameter("id", "the id of the report");
44  $currentDoc = null;
45  if ($id != "") {
46  $currentDoc = new_Doc($dbaccess, $id);
47  $currentUserTag = $currentDoc->getUTag("document_export_csv");
48  if ($currentUserTag) $defaultDocArg = json_decode($currentUserTag->comment, true);
49  }
50 
51  $csvTmpFile = $usage->addHiddenParameter("csvDownloadFile", "tmp file to download");
52  $expVarName = $usage->addHiddenParameter("exportId", "expert ident");
53  $statusOnly = $usage->addHiddenParameter("statusOnly", "get status only");
54 
55  $refresh = ($usage->addOptionalParameter("refresh", "would you refresh doc before build report", array(
56  "TRUE",
57  "FALSE"
58  ) , "FALSE") == "TRUE");
59 
60  $default = isset($defaultDocArg["kind"]) ? $defaultDocArg["kind"] : 'simple';
61  $kind = $usage->addOptionalParameter("kind", "the kind of report", array(
62  "simple",
63  "pivot"
64  ) , $default);
65  $default = isset($defaultDocArg["pivot"]) ? $defaultDocArg["pivot"] : 'id';
66  $pivot = $usage->addOptionalParameter("pivot", "the pivot attr", array() , $default);
67 
68  $default = isset($defaultDocArg["stripHtmlTag"]) ? $defaultDocArg["stripHtmlTag"] : false;
69  $applyHtmlStrip = $usage->addOptionalParameter("stripHtmlTag", "strip html tags", array() , $default);
70  $applyHtmlStrip = ($applyHtmlStrip != "1");
71 
72  $default = isset($defaultArgument["delimiter"]) ? $defaultArgument["delimiter"] : ';';
73  $argumentsCSV["delimiter"] = $usage->addOptionalParameter("delimiter", "the CSV delimiter", array() , $default);
74  $default = isset($defaultArgument["enclosure"]) ? $defaultArgument["enclosure"] : '"';
75  $argumentsCSV["enclosure"] = $usage->addOptionalParameter("enclosure", "the CSV enclosure", array() , $default);
76  $default = isset($defaultArgument["encoding"]) ? $defaultArgument["encoding"] : 'ISO-8859-15//TRANSLIT';
77  $argumentsCSV["encoding"] = $usage->addOptionalParameter("encoding", "the CSV encoding", array() , $default);
78  $default = isset($defaultArgument["decimalSeparator"]) ? $defaultArgument["decimalSeparator"] : '.';
79  $argumentsCSV["decimalSeparator"] = $usage->addOptionalParameter("decimalSeparator", "the decimalSeparator", array() , $default);
80  $default = isset($defaultArgument["dateFormat"]) ? $defaultArgument["dateFormat"] : 'US';
81 
82  $argumentsCSV["dateFormat"] = $usage->addOptionalParameter("dateFormat", "the dateFormat", array(
83  'US',
84  'FR',
85  'ISO'
86  ) , $default);
87 
88  $default = isset($defaultArgument["numericRender"]) ? $defaultArgument["numericRender"] : 'raw';
89  $argumentsCSV["numericRender"] = $usage->addOptionalParameter("numericRender", "the number render", array(
90  'raw',
91  'format'
92  ) , $default);
93 
94  $displayForm = $usage->addHiddenParameter("displayForm", "");
95  $updateDefault = $usage->addHiddenParameter("updateDefault", "");
96 
97  $usage->setStrictMode(false);
98  $usage->verify();
99  $usageArguments = array(
100  "sole",
101  "app",
102  "action",
103  "id",
104  "csvDownloadFile",
105  "exportId",
106  "statusOnly",
107  "refresh",
108  "kind",
109  "pivot",
110  "stripHtmlTag",
111  "delimiter",
112  "enclosure",
113  "encoding",
114  "decimalSeparator",
115  "dateFormat",
116  "displayForm",
117  "updateDefault"
118  );
119  $addedArguments = array();
120 
121  foreach ($_GET as $key => $value) {
122  if (!in_array($key, $usageArguments)) {
123  $addedArguments[] = array(
124  "argumentName" => $key,
125  "argumentValue" => $value
126  );
127  }
128  }
129 
130  $action->lay->eSetBlockData("addedArguments", $addedArguments);
131 
132  if ($csvTmpFile) {
133 
134  $fileName = sprintf("%s_%s.%s", $currentDoc->getTitle() , date("Y_m_d-H_m_s") , "csv");
135  Http_DownloadFile($csvTmpFile, $fileName, "text/csv", false, false, true);
136  }
137 
138  if ($statusOnly && $expVarName) {
139  header('Content-Type: application/json');
140  $action->lay->noparse = true;
141  $action->lay->template = json_encode($action->read($expVarName));
142  return;
143  }
144  if ($updateDefault) {
145  $action->setParamU("REPORT_DEFAULT_CSV", json_encode($argumentsCSV));
146  $err = $currentDoc->addUTag($action->user->id, "document_export_csv", json_encode(array(
147  "kind" => $kind,
148  "pivot" => $pivot,
149  "stripHtmlTag" => $applyHtmlStrip
150  )));
151  if ($err) {
152  error_log(__LINE__ . var_export($err, true));
153  }
154  }
155 
156  if ($displayForm) {
157  $action->lay->set("id", $currentDoc->id);
158  $expVarName = uniqid("EXPCSV");
159  $action->lay->eset("exportId", $expVarName);
160  $action->Register($expVarName, array(
161  "status" => "init"
162  ));
163  $attr = getDocAttr($dbaccess, $currentDoc->getRawValue("se_famid", 1));
164  $attributeLay = array();
165  $attributeLay[] = array(
166  "selected" => "",
167  "key" => "id",
168  "libelle" => _("EXPORT_CSV : identifiant unique")
169  );
170 
171  $isSelected = function ($currentValue, $selectedValue)
172  {
173  return $currentValue == $selectedValue ? "selected='selected'" : "";
174  };
175 
176  foreach ($attr as $currentAttr) {
177  $attributeLay[] = array(
178  "key" => $currentAttr[1],
179  "libelle" => strip_tags($currentAttr[0]) ,
180  "selected" => $isSelected($currentAttr[1], $pivot) ,
181  );
182  }
183 
184  $action->lay->setBlockData("pivotAttribute", $attributeLay);
185 
186  $kinds = array(
187  array(
188  "key" => "simple",
189  "selected" => $isSelected("simple", $kind) ,
190  "label" => _("EXPORT_CSV Simple")
191  ) ,
192  array(
193  "key" => "pivot",
194  "selected" => $isSelected("pivot", $kind) ,
195  "label" => _("EXPORT_CSV pivot")
196  )
197  );
198  $action->lay->setBlockData("kinds", $kinds);
199 
200  $stripHtml = array(
201  array(
202  "key" => "0",
203  "selected" => $isSelected("0", $applyHtmlStrip) ,
204  "label" => _("Strip Html tags")
205  ) ,
206  array(
207  "key" => "1",
208  "selected" => $isSelected("1", $applyHtmlStrip) ,
209  "label" => _("No strip Html tags")
210  )
211  );
212  $action->lay->setBlockData("stripHtml", $stripHtml);
213 
214  $encodings = array(
215  array(
216  "key" => "UTF-8",
217  "selected" => $isSelected("UTF-8", $argumentsCSV["encoding"]) ,
218  "label" => _("EXPORT_CSV utf8")
219  ) ,
220  array(
221  "key" => "ISO-8859-15//TRANSLIT",
222  "selected" => $isSelected("ISO-8859-15//TRANSLIT", $argumentsCSV["encoding"]) ,
223  "label" => _("EXPORT_CSV ISO-8859-15 (european)")
224  )
225  );
226  $action->lay->setBlockData("encodings", $encodings);
227 
228  $dateFormats = array(
229  array(
230  "key" => "US",
231  "selected" => $isSelected("US", $argumentsCSV["dateFormat"]) ,
232  "label" => _("EXPORT_CSV Date format US")
233  ) ,
234  array(
235  "key" => "FR",
236  "selected" => $isSelected("FR", $argumentsCSV["dateFormat"]) ,
237  "label" => _("EXPORT_CSV Date format FR")
238  ) ,
239  array(
240  "key" => "ISO",
241  "selected" => $isSelected("ISO", $argumentsCSV["dateFormat"]) ,
242  "label" => _("EXPORT_CSV Date format ISO")
243  )
244  );
245  $action->lay->setBlockData("dateFormats", $dateFormats);
246 
247  $numericRender = array(
248  array(
249  "key" => "raw",
250  "selected" => $isSelected("raw", $argumentsCSV["numericRender"]) ,
251  "label" => _("EXPORT_CSV raw numbers")
252  ) ,
253  array(
254  "key" => "format",
255  "selected" => $isSelected("format", $argumentsCSV["numericRender"]) ,
256  "label" => _("EXPORT_CSV formatted numbers ")
257  )
258  );
259  $action->lay->setBlockData("numericRender", $numericRender);
260 
261  $action->lay->eset("delimiter", $argumentsCSV["delimiter"]);
262  $action->lay->eset("enclosure", $argumentsCSV["enclosure"]);
263  $action->lay->eset("decimalSeparator", $argumentsCSV["decimalSeparator"]);
264  } else {
265 
266  $action->parent->setVolatileParam("exportSession", $expVarName);
267  $reportFamId = getIdFromName($dbaccess, "REPORT");
268  /**
269  * @var \Dcp\Family\Report $currentDoc
270  */
271 
272  $familyIdArray = $currentDoc->getFromDoc();
273  if (in_array($reportFamId, $familyIdArray)) {
274  switch ($kind) {
275  case "pivot":
276  $csvStruct = $currentDoc->generateCSVReportStruct(true, $pivot, $argumentsCSV["decimalSeparator"], $argumentsCSV["dateFormat"], $refresh, $applyHtmlStrip, $argumentsCSV["numericRender"]);
277  break;
278 
279  default:
280  $csvStruct = $currentDoc->generateCSVReportStruct(false, "", $argumentsCSV["decimalSeparator"], $argumentsCSV["dateFormat"], $refresh, $applyHtmlStrip, $argumentsCSV["numericRender"]);
281  }
282  $csvFile = tempnam(getTmpDir() , "csv");
283  if ($csvFile === false) {
284  $err = sprintf(_("Error creating temporary file in '%s'.") , getTmpDir());
285  $action->exitError($err);
286  }
287  $fp = fopen($csvFile, 'w');
288 
289  foreach ($csvStruct as $currentLine) {
290  $encoding = $argumentsCSV["encoding"];
291  if ($encoding != "UTF-8") {
292  $currentLine = convertLine($currentLine, $encoding);
293  }
294  fputcsv($fp, $currentLine, $argumentsCSV["delimiter"], $argumentsCSV["enclosure"]);
295  }
296 
297  fclose($fp);
298 
299  if (empty($_SERVER['HTTP_HOST'])) {
300  $content = file_get_contents($csvFile);
301  $action->lay->noparse = true;
302  $action->lay->template = $content;
303  unlink($csvFile);
304  } else {
305 
306  $fileName = sprintf("%s_%s.%s", $currentDoc->getTitle() , date("Y_m_d-H_m_s") , "csv");
307 
308  $action->Register($expVarName, array(
309  "status" => _("Export Done") ,
310  "end" => true
311  ));
312  Http_DownloadFile($csvFile, $fileName, "text/csv", false, false, true);
313  }
314  } else {
315  $action->exitError('The document is not a report');
316  }
317  }
318 }
319 
320 function convertLine($currentLine, $encoding)
321 {
322  $returnArray = array();
323  foreach ($currentLine as $currentValue) {
324 
325  $returnArray[] = iconv("UTF-8", $encoding, $currentValue);
326  }
327  return $returnArray;
328 }
global $action
Register($k, $v)
convertLine($currentLine, $encoding)
setParamU($name, $val)
exitError($texterr, $exit=true, $code="")
global $_GET
Definition: wsh.php:37
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
Verify arguments for action function.
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
global $_SERVER
getDocAttr($dbaccess, $famid, $name="")
new_Doc($dbaccess, $id= '', $latest=false)
Http_DownloadFile($filename, $name, $mime_type= '', $inline=false, $cache=true, $deleteafter=false)
Definition: Lib.Http.php:225
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
$dbaccess
Definition: checkVault.php:17
getIdFromName($dbaccess, $name)
if($file) if($subject==""&&$file) if($subject=="") $err
$usage
$value
← centre documentaire © anakeen