Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
exportfld.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  * Export Document from Folder
9  *
10  * @author Anakeen 2003
11  * @version $Id: exportfld.php,v 1.44 2009/01/12 13:23:11 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 
19 include_once ("FDL/Lib.Dir.php");
20 include_once ("FDL/Lib.Util.php");
21 include_once ("FDL/Class.DocAttr.php");
22 include_once ("VAULT/Class.VaultFile.php");
23 include_once ("FDL/import_file.php");
24 /**
25  * Exportation of documents from folder or searches
26  * @param Action &$action current action
27  * @global fldid Http var : folder identificator to export
28  * @global wprof Http var : (Y|N) if Y export associated profil also
29  * @global wfile Http var : (Y|N) if Y export attached file export format will be tgz
30  * @global wident Http var : (Y|N) if Y specid column is set with identificator of document
31  * @global wutf8 Http var : (Y|N) if Y encoding is utf-8 else iso8859-1
32  * @global wcolumn Http var : if - export preferences are ignored
33  * @global eformat Http var : (I|R|F) I: for reimport, R: Raw data, F: Formatted data
34  * @global selection Http var : JSON document selection object
35  */
36 function exportfld(Action & $action, $aflid = "0", $famid = "")
37 {
38  $dbaccess = $action->GetParam("FREEDOM_DB");
39  $fldid = GetHttpVars("id", $aflid);
40  $wprof = (GetHttpVars("wprof", "N") == "Y"); // with profil
41  $wfile = (GetHttpVars("wfile", "N") == "Y"); // with files
42  $wident = (GetHttpVars("wident", "Y") == "Y"); // with numeric identificator
43  $wutf8 = (GetHttpVars("code", "utf8") == "utf8"); // with numeric identificator
44  $nopref = (GetHttpVars("wcolumn") == "-"); // no preference read
45  $eformat = GetHttpVars("eformat", "I"); // export format
46  $selection = GetHttpVars("selection"); // export selection object (JSON)
47  if ($eformat == "X") {
48  // XML redirect
49  include_once ("FDL/exportxmlfld.php");
50  return exportxmlfld($action, $aflid, $famid);
51  }
52 
53  if ((!$fldid) && $selection) {
54  $selection = json_decode($selection);
55  include_once ("DATA/Class.DocumentSelection.php");
56  include_once ("FDL/Class.SearchDoc.php");
57  $os = new Fdl_DocumentSelection($selection);
58  $ids = $os->getIdentificators();
59  $s = new SearchDoc($dbaccess);
60  $s->addFilter(getSqlCond($ids, "id", true));
61  $tdoc = $s->search();
62  $fname = "selection";
63  } else {
64  if (!$fldid) $action->exitError(_("no export folder specified"));
65 
66  $fld = new_Doc($dbaccess, $fldid);
67  if ($famid == "") $famid = GetHttpVars("famid");
68  $fname = str_replace(array(
69  " ",
70  "'"
71  ) , array(
72  "_",
73  ""
74  ) , $fld->title);
75  $tdoc = getChildDoc($dbaccess, $fldid, "0", "ALL", array() , $action->user->id, "TABLE", $famid);
76  }
77  usort($tdoc, "orderbyfromid");
78 
79  if ($wfile) {
80  $foutdir = uniqid(getTmpDir() . "/exportfld");
81  if (!mkdir($foutdir)) exit();
82 
83  $foutname = $foutdir . "/fdl.csv";
84  } else {
85  $foutname = uniqid(getTmpDir() . "/exportfld") . ".csv";
86  }
87  $fout = fopen($foutname, "w");
88  // set encoding
89  if (!$wutf8) fputs_utf8($fout, "", true);
90 
91  if (count($tdoc) > 0) {
92 
93  $send = "\n"; // string to be writed in last
94  $doc = createDoc($dbaccess, 0);
95  // compose the csv file
96  reset($tdoc);
97 
98  $ef = array(); // files to export
99  $tmoredoc = array();
100  foreach ($tdoc as $k => $zdoc) {
101  if (!is_array($zdoc)) continue;
102  if ($zdoc["doctype"] == "C") {
103  $wname = "";
104  $cvname = "";
105  $cpname = "";
106  $fpname = "";
107  $doc->Affect($zdoc, true);
108  // it is a family
109  if ($wprof) {
110  if ($doc->profid != $doc->id) {
111  $fp = getTDoc($dbaccess, $doc->profid);
112  $tmoredoc[$fp["id"]] = $fp;
113  if ($fp["name"] != "") $fpname = $fp["name"];
114  else $fpname = $fp["id"];
115  } else {
116  exportProfil($fout, $dbaccess, $doc->profid);
117  }
118  if ($doc->cprofid) {
119  $cp = getTDoc($dbaccess, $doc->cprofid);
120  if ($cp["name"] != "") $cpname = $cp["name"];
121  else $cpname = $cp["id"];
122  $tmoredoc[$cp["id"]] = $cp;
123  }
124  if ($doc->ccvid > 0) {
125  $cv = getTDoc($dbaccess, $doc->ccvid);
126  if ($cv["name"] != "") $cvname = $cv["name"];
127  else $cvname = $cv["id"];
128  $tmskid = $doc->_val2array($cv["cv_mskid"]);
129 
130  foreach ($tmskid as $kmsk => $imsk) {
131  if ($imsk != "") {
132  $msk = getTDoc($dbaccess, $imsk);
133  if ($msk) $tmoredoc[$msk["id"]] = $msk;
134  }
135  }
136 
137  $tmoredoc[$cv["id"]] = $cv;
138  }
139 
140  if ($doc->wid > 0) {
141  $wdoc = new_doc($dbaccess, $doc->wid);
142  if ($wdoc->name != "") $wname = $wdoc->name;
143  else $wname = $wdoc->id;
144  $tattr = $wdoc->getAttributes();
145  foreach ($tattr as $ka => $oa) {
146  if ($oa->type == "docid") {
147  $tdid = $wdoc->getTValue($ka);
148  foreach ($tdid as $did) {
149  if ($did != "") {
150  $m = getTDoc($dbaccess, $did);
151  if ($m) {
152  $tmoredoc[$m["id"]] = $m;
153  if ($m["cv_mskid"] != '') {
154  $tmskid = $doc->_val2array($m["cv_mskid"]);
155  foreach ($tmskid as $kmsk => $imsk) {
156  if ($imsk != "") {
157  $msk = getTDoc($dbaccess, $imsk);
158  if ($msk) $tmoredoc[$msk["id"]] = $msk;
159  }
160  }
161  }
162  if ($m["tm_tmail"] != '') {
163  $tmskid = $doc->_val2array(str_replace('<BR>', "\n", $m["tm_tmail"]));
164  foreach ($tmskid as $kmsk => $imsk) {
165  if ($imsk != "") {
166  $msk = getTDoc($dbaccess, $imsk);
167  if ($msk) $tmoredoc[$msk["id"]] = $msk;
168  }
169  }
170  }
171  }
172  }
173  }
174  }
175  }
176  $tmoredoc[$doc->wid] = getTDoc($dbaccess, $doc->wid);
177  }
178  if ($cvname || $wname || $cpname || $fpname) {
179  $send.= "BEGIN;;;;;" . $doc->name . "\n";
180  if ($fpname) $send.= "PROFID;" . $fpname . "\n";
181  if ($cvname) $send.= "CVID;" . $cvname . "\n";
182  if ($wname) $send.= "WID;" . $wname . "\n";
183  if ($doc->cprofid) $send.= "CPROFID;" . $cpname . "\n";
184  $send.= "END;\n";
185  }
186  }
187  }
188  }
189 
190  $tdoc = array_merge($tdoc, $tmoredoc);
191  $cachedoc = array();
192  foreach ($tdoc as $k => $zdoc) {
193  if ($cachedoc[$zdoc["fromid"]]) $doc = $cachedoc[$zdoc["fromid"]];
194  else {
195  $cachedoc[$zdoc["fromid"]] = createDoc($dbaccess, $zdoc["fromid"], false);
196  $doc = $cachedoc[$zdoc["fromid"]];
197  }
198 
199  $doc->Affect($zdoc, true);
200 
201  if ($doc->doctype != "C") {
202  exportonedoc($doc, $ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat);
203  }
204  }
205  fputs_utf8($fout, $send);
206  }
207  fclose($fout);
208  if ($wfile) {
209  foreach ($ef as $info) {
210  $source = $info["path"];
211  $ddir = $foutdir . '/' . $info["ldir"];
212  if (!is_dir($ddir)) mkdir($ddir);
213  $dest = $ddir . '/' . $info["fname"];
214  if (!@copy($source, $dest)) $err.= sprintf(_("cannot copy %s") , $dest);
215  }
216  if ($err) $action->addWarningMsg($err);
217  system(sprintf("cd %s && zip -r fdl * > /dev/null", escapeshellarg($foutdir)) , $ret);
218  if (is_file("$foutdir/fdl.zip")) {
219  $foutname = $foutdir . "/fdl.zip";
220  Http_DownloadFile($foutname, "$fname.zip", "application/x-zip", false, false);
221  //if (deleteContentDirectory($foutdir)) rmdir($foutdir);
222 
223  } else {
224  $action->exitError(_("Zip Archive cannot be created"));
225  }
226  } else {
227  Http_DownloadFile($foutname, "$fname.csv", "text/csv", false, false);
228  unlink($foutname);
229  }
230  exit;
231 }
232 function fputs_utf8($r, $s, $iso = false)
233 {
234  static $utf8 = true;
235  if ($iso === true) $utf8 = false;
236 
237  if ($s) {
238  if (!$utf8) fputs($r, utf8_decode($s));
239  else fputs($r, $s);
240  }
241 }
242 function orderbyfromid($a, $b)
243 {
244 
245  if ($a["fromid"] == $b["fromid"]) return 0;
246  if ($a["fromid"] > $b["fromid"]) return 1;
247 
248  return -1;
249 }
250 /**
251  * Removes content of the directory (not sub directory)
252  *
253  * @param string the directory name to remove
254  * @return boolean True/False whether the directory was deleted.
255  */
257 {
258  if (!is_dir($dirname)) return false;
259  $dcur = realpath($dirname);
260  $darr = array();
261  $darr[] = $dcur;
262  if ($d = opendir($dcur)) {
263  while ($f = readdir($d)) {
264  if ($f == '.' || $f == '..') continue;
265  $f = $dcur . '/' . $f;
266  if (is_file($f)) {
267  unlink($f);
268  $darr[] = $f;
269  }
270  }
271  closedir($d);
272  }
273 
274  return true;;
275 }
276 function exportProfil($fout, $dbaccess, $docid)
277 {
278  if (!$docid) return;
279  // import its profile
280  $doc = new_Doc($dbaccess, $docid); // needed to have special acls
281  $doc->acls[] = "viewacl";
282  $doc->acls[] = "modifyacl";
283  if ($doc->name != "") $name = $doc->name;
284  else $name = $doc->id;
285  $q = new QueryDb($dbaccess, "DocPerm");
286  $q->AddQuery("docid=" . $doc->profid);
287  $acls = $q->Query(0, 0, "TABLE");
288 
289  $tpu = array();
290  $tpa = array();
291  if ($acls) {
292  foreach ($acls as $va) {
293  $up = $va["upacl"];
294  $un = $va["unacl"];
295  $uid = $va["userid"];
296 
297  foreach ($doc->acls as $acl) {
298  $bup = ($doc->ControlUp($up, $acl) == "");
299  $bun = ($doc->ControlUp($un, $acl) == "");
300  if ($bup || $bun) {
301  if ($uid >= STARTIDVGROUP) {
302  $vg = new Vgroup($dbaccess, $uid);
303  $qvg = new QueryDb($dbaccess, "VGroup");
304  $qvg->AddQuery("num=$uid");
305  $tvu = $qvg->Query(0, 1, "TABLE");
306  $uid = $tvu[0]["id"];
307  }
308 
309  $tpu[] = $uid;
310  if ($bup) $tpa[] = $acl;
311  else $tpa[] = "-" . $acl;
312  }
313  }
314  }
315  }
316  if (count($tpu) > 0) {
317  fputs_utf8($fout, "PROFIL;" . $name . ";;");
318 
319  foreach ($tpu as $ku => $uid) {
320  if ($uid > 0) $uid = getUserLogicName($dbaccess, $uid);
321  fputs_utf8($fout, ";" . $tpa[$ku] . "=" . $uid);
322  }
323  fputs_utf8($fout, "\n");
324  }
325 }
326 
328 {
329  $u = new User("", $uid);
330  if ($u->isAffected()) {
331  $du = getTDoc($dbaccess, $u->fid);
332  if (($du["name"] != "") && ($du["us_whatid"] == $uid)) return $du["name"];
333  }
334  return $uid;
335 }
336 function exportonedoc(&$doc, &$ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat)
337 {
338  static $prevfromid = - 1;
339  static $lattr;
340  static $trans = false;
341  static $fromname;
342  static $alreadyExported = array();
343 
344  if (!$doc->isAffected()) return;
345  if (in_array($doc->id, $alreadyExported)) return;
346  $alreadyExported[] = $doc->id;
347 
348  if (!$trans) {
349  // to invert HTML entities
350  $trans = get_html_translation_table(HTML_ENTITIES);
351  $trans = array_flip($trans);
352  $trans = array_map("utf8_encode", $trans);
353  }
354  $efldid = '-';
355  $dbaccess = $doc->dbaccess;
356  if ($prevfromid != $doc->fromid) {
357  if (($eformat != "I") && ($prevfromid > 0)) fputs_utf8($fout, "\n");
358  $adoc = $doc->getFamDoc();
359  if ($adoc->name != "") $fromname = $adoc->name;
360  else $fromname = $adoc->id;
361  if (!$fromname) return;
362  $lattr = $adoc->GetExportAttributes($wfile, $nopref);
363  if ($eformat == "I") fputs_utf8($fout, "//FAM;" . $adoc->title . "(" . $fromname . ");<specid>;<fldid>;");
364  foreach ($lattr as $ka => $attr) {
365  fputs_utf8($fout, str_replace(SEPCHAR, ALTSEPCHAR, $attr->getLabel()) . SEPCHAR);
366  }
367  fputs_utf8($fout, "\n");
368  if ($eformat == "I") {
369  fputs_utf8($fout, "ORDER;" . $fromname . ";;;");
370  foreach ($lattr as $ka => $attr) {
371  fputs_utf8($fout, $attr->id . ";");
372  }
373  fputs_utf8($fout, "\n");
374  }
375  $prevfromid = $doc->fromid;
376  }
377  reset($lattr);
378  if ($doc->name != "") $name = $doc->name;
379  else if ($wprof) {
380  $err = $doc->setNameAuto();
381  $name = $doc->name;
382  } else if ($wident) $name = $doc->id;
383  else $name = '';
384  if ($eformat == "I") fputs_utf8($fout, "DOC;" . $fromname . ";" . $name . ";" . $efldid . ";");
385  // write values
386  foreach ($lattr as $ka => $attr) {
387  if ($eformat == 'F') $value = str_replace(array(
388  '<BR>',
389  '<br/>'
390  ) , '\\n', $doc->getHtmlAttrValue($attr->id, '', false, -1, false));
391  else $value = $doc->getValue($attr->id);
392  // invert HTML entities
393  if (($attr->type == "image") || ($attr->type == "file")) {
394  $tfiles = $doc->vault_properties($attr);
395  $tf = array();
396  foreach ($tfiles as $f) {
397  $ldir = $doc->id . '-' . preg_replace('/[^a-zA-Z0-9_.-]/', '_', unaccent($doc->title)) . "_D";
398  $fname = $ldir . '/' . unaccent($f["name"]);
399  $tf[] = $fname;
400  $ef[$fname] = array(
401  "path" => $f["path"],
402  "ldir" => $ldir,
403  "fname" => unaccent($f["name"])
404  );
405  }
406  $value = implode("\n", $tf);
407  } else if ($attr->type == "docid") {
408  if ($value != "") {
409  if (strstr($value, "\n") || ($attr->getOption("multiple") == "yes")) {
410  $tid = $doc->_val2array($value);
411  $tn = array();
412  foreach ($tid as $did) {
413  $brtid = explode("<BR>", $did);
414  $tnbr = array();
415  foreach ($brtid as $brid) {
416  $n = getNameFromId($dbaccess, $brid);
417  if ($n) $tnbr[] = $n;
418  else $tnbr[] = $brid;
419  }
420  $tn[] = implode('<BR>', $tnbr);
421  }
422  $value = implode("\n", $tn);
423  } else {
424  $n = getNameFromId($dbaccess, $value);
425  if ($n) $value = $n;
426  }
427  }
428  } else {
429  $value = preg_replace("/(\&[a-zA-Z0-9\#]+;)/es", "strtr('\\1',\$trans)", $value);
430  // invert HTML entities which ascii code like &#232;
431  $value = preg_replace("/\&#([0-9]+);/es", "chr('\\1')", $value);
432  }
433  fputs_utf8($fout, str_replace(array(
434  "\n",
435  ";",
436  "\r"
437  ) , array(
438  "\\n",
439  ALTSEPCHAR,
440  ""
441  ) , $value) . ";");
442  }
443  fputs_utf8($fout, "\n");
444 
445  if ($wprof) {
446  if ($doc->profid == $doc->id) exportProfil($fout, $dbaccess, $doc->id);
447  else if ($doc->profid > 0) {
448  $name = getNameFromId($dbaccess, $doc->profid);
449  $dname = $doc->name;
450  if (!$dname) $dname = $doc->id;
451  if (!$name) $name = $doc->profid;
452  if (!isset($tdoc[$doc->profid])) {
453  $tdoc[$doc->profid] = true;
454  $pdoc = new_doc($dbaccess, $doc->profid);
455  exportonedoc($pdoc, $ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat);
456  // exportProfil($fout,$dbaccess,$doc->profid);
457 
458  }
459  fputs_utf8($fout, "PROFIL;$dname;$name;;\n");
460  }
461  }
462 }
463 ?>
← centre documentaire © anakeen - published under CC License - Dynacase