Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
import_file.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  * Import documents
9  *
10  * @author Anakeen 2000
11  * @version $Id: import_file.php,v 1.149 2008/11/14 12:40:07 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/Class.DocFam.php");
20 include_once ("FDL/Class.DocSearch.php");
21 include_once ("FDL/Class.Dir.php");
22 include_once ("FDL/Class.QueryDir.php");
23 include_once ("FDL/Lib.Attr.php");
24 include_once ("FDL/Class.DocAttrLDAP.php");
25 
26 define("ALTSEPCHAR", ' --- ');
27 define("SEPCHAR", ';');
28 
30 {
31  // -----------------------------------
32  $gerr = ""; // general errors
33  if (intval(ini_get("max_execution_time")) < 300) ini_set("max_execution_time", 300);
34  $dirid = GetHttpVars("dirid", 10); // directory to place imported doc
35  $analyze = (GetHttpVars("analyze", "N") == "Y"); // just analyze
36  $policy = GetHttpVars("policy", "update");
37  $reinit = GetHttpVars("reinitattr");
38  $comma = GetHttpVars("comma", SEPCHAR);
39 
40  $nbdoc = 0; // number of imported document
41  $dbaccess = $action->GetParam("FREEDOM_DB");
42  $cvsfile = "";
43  if (seemsODS($fimport)) {
44  $cvsfile = ods2csv($fimport);
45  $fdoc = fopen($cvsfile, "r");
46  } else {
47  $fdoc = fopen($fimport, "r");
48  }
49  if (!$fdoc) $action->exitError(_("no import file specified"));
50  $nline = 0;
51  while (!feof($fdoc)) {
52  $buffer = rtrim(fgets($fdoc, 16384));
53  $data = explode($comma, $buffer);
54  $nline++;
55 
56  if (!isUTF8($data)) $data = array_map("utf8_encode", $data);
57  // return structure
58  $num = count($data);
59  if ($num < 1) continue;
60  $tcr[$nline] = array(
61  "err" => "",
62  "msg" => "",
63  "specmsg" => "",
64  "folderid" => 0,
65  "foldername" => "",
66  "filename" => "",
67  "title" => "",
68  "id" => "",
69  "values" => array() ,
70  "familyid" => 0,
71  "familyname" => "",
72  "action" => " "
73  );
74  $tcr[$nline]["title"] = substr($data[0], 0, 10);
75  $data[0] = trim($data[0]);
76  switch ($data[0]) {
77  // -----------------------------------
78 
79  case "BEGIN":
80  $err = "";
81  // search from name or from id
82  if (($data[3] == "") || ($data[3] == "-")) $doc = new DocFam($dbaccess, getFamIdFromName($dbaccess, $data[5]) , '', 0, false);
83  else $doc = new DocFam($dbaccess, $data[3], '', 0, false);
84  $famicon = "";
85 
86  if (!$doc->isAffected()) {
87 
88  if (!$analyze) {
89  $doc = new DocFam($dbaccess);
90 
91  if (isset($data[3]) && ($data[3] > 0)) $doc->id = $data[3]; // static id
92  if (is_numeric($data[1])) $doc->fromid = $data[1];
93  else $doc->fromid = getFamIdFromName($dbaccess, $data[1]);
94  if (isset($data[5])) $doc->name = $data[5]; // internal name
95  $err = $doc->Add();
96  }
97  $tcr[$nline]["msg"] = sprintf(_("create %s family %s") , $data[2], $data[5]);
98  $tcr[$nline]["action"] = "added";
99  } else {
100  $tcr[$nline]["action"] = "updated";
101  $tcr[$nline]["msg"] = sprintf(_("update %s family %s") , $data[2], $data[5]);
102  }
103  if ($data[1] && ($data[1] != '-')) {
104  if (is_numeric($data[1])) $doc->fromid = $data[1];
105  else $doc->fromid = getFamIdFromName($dbaccess, $data[1]);
106  }
107  if ($data[2] && ($data[2] != '-')) $doc->title = $data[2];
108  if ($data[4] && ($data[4] != '-')) $doc->classname = $data[4]; // new classname for familly
109  if ($data[5] && ($data[5] != '-')) $doc->name = $data[5]; // internal name
110  $tcr[$nline]["err"].= $err;
111  if ($reinit == "yes") {
112  $tcr[$nline]["msg"].= sprintf(_("reinit all attributes"));
113  if ($analyze) continue;
114  $oattr = new DocAttr($dbaccess);
115  $oattr->docid = intval($doc->id);
116  if ($oattr->docid > 0) {
117  $err = $oattr->exec_query("delete from docattr where docid=" . $oattr->docid);
118  }
119  $tcr[$nline]["err"].= $err;
120  }
121 
122  break;
123 
124  case "RESET":
125  if ($data[1] == "attributes") {
126  $tcr[$nline]["msg"].= sprintf(_("reinit all attributes"));
127  if ($analyze) continue;
128  $oattr = new DocAttr($dbaccess);
129  $oattr->docid = intval($doc->id);
130  if ($oattr->docid > 0) {
131  $err = $oattr->exec_query("delete from docattr where docid=" . $oattr->docid);
132  }
133  } else {
134  $err = "reset argument must be 'attributes'";
135  }
136 
137  $tcr[$nline]["err"].= $err;
138  break;
139  // -----------------------------------
140 
141  case "END":
142  // add messages
143  $msg = sprintf(_("modify %s family") , $doc->title);
144  $tcr[$nline]["msg"] = $msg;
145 
146  if ($analyze) {
147  $nbdoc++;
148  continue;
149  }
150  if (($num > 3) && ($data[3] != "")) $doc->doctype = "S";
151 
152  $doc->modify();
153 
154  if ($doc->doctype == "C") {
155  global $tFamIdName;
156  $msg = refreshPhpPgDoc($dbaccess, $doc->id);
157  if (isset($tFamIdName)) $tFamIdName[$doc->name] = $doc->id; // refresh getFamIdFromName for multiple family import
158 
159  }
160 
161  if ((!$analyze) && ($famicon != "")) $doc->changeIcon($famicon);
162  $tcr[$nline]["msg"].= $doc->postImport();
163  $doc->AddComment(_("Update by importation"));
164 
165  $nbdoc++;
166 
167  break;
168  // -----------------------------------
169 
170  case "DOC":
171  // case of specific order
172  if (is_numeric($data[1])) $fromid = $data[1];
173  else $fromid = getFamIdFromName($dbaccess, $data[1]);
174 
175  if (isset($tkeys[$fromid])) $tk = $tkeys[$fromid];
176  else $tk = array(
177  "title"
178  );
179 
180  $tcr[$nline] = csvAddDoc($dbaccess, $data, $dirid, $analyze, '', $policy, $tk, array() , $tcolorder[$fromid]);
181  if ($tcr[$nline]["err"] == "") $nbdoc++;
182  break;
183  // -----------------------------------
184 
185  case "SEARCH":
186 
187  if ($data[1] > 0) {
188  $tcr[$nline]["id"] = $data[1];
189  $doc = new_Doc($dbaccess, $data[1]);
190  if (!$doc->isAffected()) {
191  $doc = createDoc($dbaccess, 5);
192  if (!$analyze) {
193  $doc->id = $data[1]; // static id
194  $err = $doc->Add();
195  }
196  $tcr[$nline]["msg"] = sprintf(_("update %s search") , $data[3]);
197  $tcr[$nline]["action"] = "updated";
198  }
199  } else {
200  $doc = createDoc($dbaccess, 5);
201  if (!$analyze) {
202  $err = $doc->Add();
203  }
204  $tcr[$nline]["msg"] = sprintf(_("add %s search") , $data[3]);
205  $tcr[$nline]["action"] = "added";
206  $tcr[$nline]["err"].= $err;
207  }
208  if (($err != "") && ($doc->id > 0)) { // case only modify
209  if ($doc->Select($doc->id)) $err = "";
210  }
211  if (!$analyze) {
212  // update title in finish
213  $doc->title = $data[3];
214  $err = $doc->modify();
215  $tcr[$nline]["err"].= $err;
216 
217  if (($data[4] != "")) { // specific search
218  $err = $doc->AddStaticQuery($data[4]);
219  $tcr[$nline]["err"].= $err;
220  }
221 
222  if ($data[2] > 0) { // dirid
223  $dir = new_Doc($dbaccess, $data[2]);
224  if ($dir->isAlive() && method_exists($dir, "AddFile")) $dir->AddFile($doc->id);
225  }
226  }
227  $nbdoc++;
228  break;
229  // -----------------------------------
230 
231  case "TYPE":
232  $doc->doctype = $data[1];
233  $tcr[$nline]["msg"] = sprintf(_("set doctype to '%s'") , $data[1]);
234  break;
235  // -----------------------------------
236 
237  case "GENVERSION":
238  $doc->genversion = $data[1];
239  $tcr[$nline]["msg"] = sprintf(_("generate version '%s'") , $data[1]);
240  break;
241  // -----------------------------------
242 
243  case "MAXREV":
244  $doc->maxrev = intval($data[1]);
245  $tcr[$nline]["msg"] = sprintf(_("max revision '%d'") , $data[1]);
246  break;
247  // -----------------------------------
248 
249  case "ICON": // for family
250  if ($doc->icon == "") {
251  $famicon = $data[1]; // reported to end section
252  $tcr[$nline]["msg"] = sprintf(_("set icon to '%s'") , $data[1]);
253  } else {
254  $tcr[$nline]["err"] = sprintf(_("icon already set. No update allowed"));
255  }
256  break;
257  // -----------------------------------
258 
259  case "DOCICON":
260  $idoc = new_doc($dbaccess, $data[1]);
261  if (!$analyze) $idoc->changeIcon($data[2]);
262  if ($idoc->isAlive()) $tcr[$nline]["msg"] = sprintf(_("document %s : set icon to '%s'") , $idoc->title, $data[2]);
263  else $tcr[$nline]["err"] = sprintf(_("no change icon : document %s not found") , $data[1]);
264 
265  break;
266  // -----------------------------------
267 
268  case "DFLDID":
269  if ($data[1] == "auto") {
270  if ($doc->dfldid == "") {
271  if (!$analyze) {
272  // create auto
273  include_once ("FDL/freedom_util.php");
275  $tcr[$nline]["msg"].= sprintf(_("create default folder (id [%d])\n") , $fldid);
276  }
277  } else {
278  $fldid = $doc->dfldid;
279  $tcr[$nline]["err"] = sprintf(_("default folder already set. Auto ignored"));
280  }
281  } elseif (is_numeric($data[1])) $fldid = $data[1];
282  else $fldid = getIdFromName($dbaccess, $data[1], 2);
283  $doc->dfldid = $fldid;
284  $tcr[$nline]["msg"].= sprintf(_("set default folder to '%s'") , $data[1]);
285  break;
286  // -----------------------------------
287 
288  case "CFLDID":
289  if (is_numeric($data[1])) $cfldid = $data[1];
290  else $cfldid = getIdFromName($dbaccess, $data[1]);
291  $doc->cfldid = $cfldid;
292  $tcr[$nline]["msg"] = sprintf(_("set primary folder to '%s'") , $data[1]);
293  break;
294  // -----------------------------------
295 
296  case "WID":
297  if (is_numeric($data[1])) $wid = $data[1];
298  else $wid = getIdFromName($dbaccess, $data[1], 20);
299  $doc->wid = $wid;
300  $tcr[$nline]["msg"] = sprintf(_("set default workflow to '%s'") , $data[1]);
301  break;
302  // -----------------------------------
303 
304  case "CVID":
305  if (is_numeric($data[1])) $cvid = $data[1];
306  else $cvid = getIdFromName($dbaccess, $data[1], 28);
307  $doc->ccvid = $cvid;
308  $tcr[$nline]["msg"] = sprintf(_("set view control to '%s'") , $data[1]);
309  break;
310  // -----------------------------------
311 
312  case "SCHAR":
313  $doc->schar = $data[1];
314  $tcr[$nline]["msg"] = sprintf(_("set special characteristics to '%s'") , $data[1]);
315  break;
316  // -----------------------------------
317 
318  case "METHOD":
319 
320  $s1 = $data[1][0];
321  if (($s1 == "+") || ($s1 == "*")) {
322  if ($s1 == "*") $method = $data[1];
323  else $method = substr($data[1], 1);
324 
325  if ($doc->methods == "") {
326  $doc->methods = $method;
327  } else {
328  $doc->methods.= "\n$method";
329  // not twice
330  $tmeth = explode("\n", $doc->methods);
331  $tmeth = array_unique($tmeth);
332  $doc->methods = implode("\n", $tmeth);
333  }
334  } else $doc->methods = $data[1];
335 
336  $tcr[$nline]["msg"] = sprintf(_("change methods to '%s'") , $doc->methods);
337 
338  break;
339  // -----------------------------------
340 
341  case "USEFORPROF":
342  $doc->usefor = "P";
343  $tcr[$nline]["msg"] = sprintf(_("change special use to '%s'") , $doc->usefor);
344  break;
345  // -----------------------------------
346 
347  case "USEFOR":
348  $doc->usefor = $data[1];
349  $tcr[$nline]["msg"] = sprintf(_("change special use to '%s'") , $doc->usefor);
350  break;
351  // -----------------------------------
352 
353  case "TAG":
354  $doc->AddATag($data[1]);
355  $tcr[$nline]["msg"] = sprintf(_("change application tag to '%s'") , $doc->atags);
356  break;
357  // -----------------------------------
358 
359  case "CPROFID":
360  if (is_numeric($data[1])) $pid = $data[1];
361  else $pid = getIdFromName($dbaccess, $data[1], 3);
362  $doc->cprofid = $pid;
363  $tcr[$nline]["msg"] = sprintf(_("change default creation profile id to '%s'") , $data[1]);
364  break;
365  // -----------------------------------
366 
367  case "PROFID":
368  if (is_numeric($data[1])) $pid = $data[1];
369  else $pid = getIdFromName($dbaccess, $data[1], 3);
370  $doc->setProfil($pid); // change profile
371  $tcr[$nline]["msg"] = sprintf(_("change profile id to '%s'") , $data[1]);
372  break;
373 
374  case "DEFAULT":
375  $defv = str_replace(array(
376  '\n',
377  ALTSEPCHAR
378  ) , array(
379  "\n",
380  SEPCHAR
381  ) , $data[2]);
382  $doc->setDefValue($data[1], $defv);
383  $force = (str_replace(" ", "", trim(strtolower($data[3]))) == "force=yes");
384  if ($force || (!$doc->getParamValue($data[1]))) {
385  $doc->setParam($data[1], $defv);
386  $tcr[$nline]["msg"] = "reset default parameter";
387  }
388 
389  $tcr[$nline]["msg"].= sprintf(_("add default value %s %s") , $data[1], $data[2]);
390  break;
391 
392  case "IATTR":
393  // import attribute definition from another family
394  $fiid = $data[3];
395  if (!is_numeric($fiid)) $fiid = getFamIdFromName($dbaccess, $fiid);
396  $fi = new_Doc($dbaccess, $fiid);
397  if ($fi->isAffected()) {
398  $fa = $fi->getAttribute($data[1]);
399  if ($fa) {
400  $oattri = new DocAttr($dbaccess, array(
401  $fiid,
402  strtolower($data[1])
403  ));
404  $oattr = new DocAttr($dbaccess, array(
405  $doc->id,
406  strtolower($data[1])
407  ));
408  $oattri->docid = $doc->id;
409  $tcr[$nline]["msg"] = sprintf(_("copy attribute %s from %s") , $data[1], $data[3]);
410  if (!$analyze) {
411  if ($oattr->isAffected()) {
412  $err = $oattri->modify();
413  } else {
414  $oattri->id = strtolower($data[1]);
415  $err = $oattri->add();
416  }
417  $tcr[$nline]["err"] = $err;
418  }
419 
420  if (($err == "") && (strtolower(get_class($fa)) == "fieldsetattribute")) {
421  $frameid = $fa->id;
422  // import attributes included in fieldset
423  foreach ($fi->attributes->attr as $k => $v) {
424  if (strtolower(get_class($v)) == "normalattribute") {
425 
426  if (($v->fieldSet->id == $frameid) || ($v->fieldSet->fieldSet->id == $frameid)) {
427  $tcr[$nline]["msg"].= "\n" . sprintf(_("copy attribute %s from %s") , $v->id, $data[3]);
428  $oattri = new DocAttr($dbaccess, array(
429  $fiid,
430  $v->id
431  ));
432  $oattr = new DocAttr($dbaccess, array(
433  $doc->id,
434  $v->id
435  ));
436  $oattri->docid = $doc->id;
437  if (!$analyze) {
438  if ($oattr->isAffected()) {
439  $err = $oattri->modify();
440  } else {
441  $oattri->id = $v->id;
442  $err = $oattri->add();
443  }
444  $tcr[$nline]["err"].= $err;
445  }
446  }
447  }
448  }
449  }
450  }
451  }
452  break;
453  // -----------------------------------
454 
455  case "PARAM":
456  case "OPTION":
457  case "ATTR":
458  if ($num < 9) {
459  $tcr[$nline]["err"] = "Error in line $nline: $num cols < 9";
460  break;
461  }
462  case "MODATTR":
463  if ($num < 3) {
464  $tcr[$nline]["err"] = "Error in line $nline: $num cols < 3";
465  break;
466  }
467  foreach ($data as $kd => $vd) {
468  $data[$kd] = str_replace(ALTSEPCHAR, $comma, $vd); // restore ; semi-colon
469 
470  }
471  if (trim($data[1]) == '') {
472  $tcr[$nline]["err"].= sprintf(_("attr key is empty"));
473  } else {
474  $modattr = ($data[0] == "MODATTR");
475  if ($data[0] == "MODATTR") $data[1] = ':' . $data[1]; // to mark the modified attribute
476  $tcr[$nline]["msg"].= sprintf(_("update %s attribute") , $data[1]);
477  if ($analyze) continue;
478  $oattr = new DocAttr($dbaccess, array(
479  $doc->id,
480  strtolower($data[1])
481  ));
482 
483  if ($data[0] == "PARAM") $oattr->usefor = 'Q'; // parameters
484  elseif ($data[0] == "OPTION") $oattr->usefor = 'O'; // options
485  $oattr->docid = $doc->id;
486  $oattr->id = trim(strtolower($data[1]));
487 
488  $oattr->frameid = trim(strtolower($data[2]));
489  $oattr->labeltext = $data[3];
490 
491  $oattr->title = ($data[4] == "Y") ? "Y" : "N";
492 
493  $oattr->abstract = ($data[5] == "Y") ? "Y" : "N";
494  if ($modattr) $oattr->abstract = $data[5];
495 
496  $oattr->type = trim($data[6]);
497 
498  $oattr->ordered = $data[7];
499  $oattr->visibility = $data[8];
500  $oattr->needed = ($data[9] == "Y") ? "Y" : "N";
501  if ($modattr) {
502  $oattr->title = $data[4];
503  $oattr->needed = $data[9];
504  }
505  $oattr->link = $data[10];
506  $oattr->phpfile = $data[11];
507  if (isset($data[13])) $oattr->elink = $data[13];
508  else $oattr->elink = '';
509  if (isset($data[14])) $oattr->phpconstraint = $data[14];
510  else $oattr->phpconstraint = '';
511  if (isset($data[15])) $oattr->options = $data[15];
512  else $oattr->options = '';
513  if (((($data[11] != "") && ($data[11] != "-")) || (($data[6] != "enum") && ($data[6] != "enumlist"))) || ($oattr->phpfunc == "") || (strpos($oattr->options, "system=yes") !== false)) $oattr->phpfunc = $data[12]; // don(t modify enum possibilities
514  if ($oattr->isAffected()) $err = $oattr->Modify();
515  else $err = $oattr->Add();
516 
517  $tcr[$nline]["err"].= $err;
518  }
519  break;
520 
521  case "ORDER":
522  if (is_numeric($data[1])) $orfromid = $data[1];
523  else $orfromid = getFamIdFromName($dbaccess, $data[1]);
524 
525  $tcolorder[$orfromid] = getOrder($data);
526  $tcr[$nline]["msg"] = sprintf(_("new column order %s") , implode(" - ", $tcolorder[$orfromid]));
527 
528  break;
529 
530  case "KEYS":
531  if (is_numeric($data[1])) $orfromid = $data[1];
532  else $orfromid = getFamIdFromName($dbaccess, $data[1]);
533 
534  $tkeys[$orfromid] = getOrder($data);
535  if (($tkeys[$orfromid][0] == "") || (count($tkeys[$orfromid]) == 0)) {
536  $tcr[$nline]["err"] = sprintf(_("error in import keys : %s") , implode(" - ", $tkeys[$orfromid]));
537  unset($tkeys[$orfromid]);
538  $tcr[$nline]["action"] = "ignored";
539  } else {
540  $tcr[$nline]["msg"] = sprintf(_("new import keys : %s") , implode(" - ", $tkeys[$orfromid]));
541  }
542  break;
543 
544  case "PROFIL":
545  if (ctype_digit(trim($data[1]))) $pid = trim($data[1]);
546  else $pid = getIdFromName($dbaccess, trim($data[1]));
547 
548  if (!($pid > 0)) $tcr[$nline]["err"] = sprintf(_("profil id unkonow %s") , $data[1]);
549  else {
550 
551  $pdoc = new_Doc($dbaccess, $pid);
552  if ($pdoc->isAlive()) {
553  $tcr[$nline]["msg"] = sprintf(_("change profil %s") , $data[1]);
554  $tcr[$nline]["action"] = "modprofil";
555  if ($analyze) continue;
556  $fpid = $data[2];
557  if (($fpid != "") && (!is_numeric($fpid))) $fpid = getIdFromName($dbaccess, $fpid);
558  if ($fpid != "") {
559  // profil related of other profil
560  $pdoc->setProfil($fpid);
561  $err = $pdoc->modify(false, array(
562  "profid"
563  ) , true);
564  } else {
565  // specific profil
566  if ($pdoc->profid != $pid) {
567  $pdoc->setProfil($pid);
568  $pdoc->SetControl(false);
569  $pdoc->disableEditControl(); // need because new profil is not enable yet
570  $tcr[$nline]["err"] = $pdoc->modify();
571  }
572  $optprof = strtoupper(trim($data[3]));
573  if ($optprof == "RESET") {
574  $pdoc->removeControl();
575  $tcr[$nline]["msg"].= "\n" . sprintf(_("reset profil %s") , $pid);
576  }
577  $tacls = array_slice($data, 2);
578  foreach ($tacls as $acl) {
579  if (preg_match("/([^=]+)=(.*)/", $acl, $reg)) {
580  $tuid = explode(",", $reg[2]);
581  $aclname = trim($reg[1]);
582  if (substr($aclname, 0, 1) == "-") {
583  $negative = true;
584  $aclname = substr($aclname, 1);
585  } else $negative = false;
586  $perr = "";
587  if ($optprof == "DELETE") {
588  foreach ($tuid as $uid) {
589  $perr.= $pdoc->delControl(trim($uid) , $aclname, $negative);
590  $tcr[$nline]["msg"].= "\n" . sprintf(_("delete %s for %s") , $aclname, $uid);
591  }
592  } else { // the "ADD" by default
593  foreach ($tuid as $uid) {
594  $perr.= $pdoc->addControl(trim($uid) , $aclname, $negative);
595  $tcr[$nline]["msg"].= "\n" . sprintf(_("add %s for %s") , $aclname, $uid);
596  }
597  }
598  $tcr[$nline]["err"] = $perr;
599  }
600  }
601  // need reset all documents
602  $pdoc->recomputeProfiledDocument();
603  }
604  } else {
605  $tcr[$nline]["err"] = sprintf(_("profil id unknow %s") , $data[1]);
606  }
607  }
608 
609  break;
610 
611  case "ACCESS":
612  if (ctype_digit(trim($data[1]))) $wid = trim($data[1]);
613  else {
614  $pid = getIdFromName($dbaccess, trim($data[1]));
615  $tdoc = getTDoc($dbaccess, $pid);
616  $wid = getv($tdoc, "us_whatid");
617  }
618  $idapp = $action->parent->GetIdFromName($data[2]);
619  if ($idapp == 0) {
620  $tcr[$nline]["err"] = sprintf(_("%s application not exists") , $data[2]);
621  } else {
622  $tcr[$nline]["msg"] = "user #$wid";
623  array_shift($data);
624  array_shift($data);
625  array_shift($data);
626  $q = new QueryDb("", "Acl");
627  $q->AddQuery("id_application=$idapp");
628  $la = $q->Query(0, 0, "TABLE");
629  if (!$la) {
630  $tcr[$nline]["err"] = sprintf(_("%s application has no aclss") , $data[2]);
631  } else {
632  $tacl = array();
633  foreach ($la as $k => $v) {
634  $tacl[$v["name"]] = $v["id"];
635  }
636 
637  $p = new Permission();
638  $p->id_user = $wid;
639  $p->id_application = $idapp;
640  foreach ($data as $v) {
641  $v = trim($v);
642  if ($v != "") {
643  if ($analyze) {
644  $tcr[$nline]["msg"].= "\n" . sprintf(_("try add acl %s") , $v);
645  $tcr[$nline]["action"] = "added";
646  continue;
647  }
648  if (substr($v, 0, 1) == '-') {
649  $aclneg = true;
650  $v = substr($v, 1);
651  } else $aclneg = false;
652  if (isset($tacl[$v])) {
653  $p->id_acl = $tacl[$v];
654  if ($aclneg) $p->id_acl = - $p->id_acl;
655  $p->deletePermission($p->id_user, $p->id_application, $p->id_acl);
656  $err = $p->Add();
657  if ($err) $tcr[$nline]["err"].= "\n$err";
658  else {
659  if ($aclneg) $tcr[$nline]["msg"].= "\n" . sprintf(_("add negative acl %s") , $v);
660  else $tcr[$nline]["msg"].= "\n" . sprintf(_("add acl %s") , $v);
661  }
662  } else {
663  $tcr[$nline]["err"].= "\n" . sprintf(_("unknow acl %s") , $v);
664  }
665  }
666  }
667  }
668  }
669  break;
670 
671  case "LDAPMAP":
672  if (is_numeric($data[1])) $fid = $data[1];
673  else $fid = getFamIdFromName($dbaccess, $data[1]);
674  $aid = (trim($data[2]));
675  $index = $data[5];
676  $oa = new DocAttrLDAP($dbaccess, array(
677  $fid,
678  $aid,
679  $index
680  ));
681  // print_r2($oa);
682  if (substr($data[2], 0, 2) == "::") $oa->ldapname = $data[2];
683  else $oa->ldapname = strtolower(trim($data[2]));
684 
685  $oa->ldapclass = trim($data[4]);
686  $oa->famid = $fid;
687  $oa->ldapmap = $data[3];
688  $oa->index = $index;
689  $oa->ldapname = $aid;
690 
691  if ($oa->isAffected()) {
692  if (!$analyze) $err = $oa->modify();
693  $tcr[$nline]["msg"] = sprintf(_("LDAP Attribute modified to %s %s") , $oa->ldapname, $oa->ldapmap);
694  $tcr[$nline]["action"] = "updated";
695  } else {
696  if (!$analyze) $err = $oa->add();
697 
698  $tcr[$nline]["msg"] = sprintf(_("LDAP Attribute added to %s %s") , $oa->ldapname, $oa->ldapmap);
699  $tcr[$nline]["action"] = "added";
700  }
701  $tcr[$nline]["err"].= $err;
702 
703  break;
704 
705  default:
706  // uninterpreted line
707  unset($tcr[$nline]);
708  }
709  }
710 
711  fclose($fdoc);
712 
713  if ($csvfile) unlink($csvfile); // temporary csvfile
714  return $tcr;
715  }
716  /**
717  * Add a document from csv import file
718  * @param string $dbaccess database specification
719  * @param array $data data information conform to {@link Doc::GetImportAttributes()}
720  * @param int $dirid default folder id to add new document
721  * @param bool $analyze true is want just analyze import file (not really import)
722  * @param string $ldir path where to search imported files
723  * @param string $policy add|update|keep policy use if similar document
724  * @param array $tkey attribute key to search similar documents
725  * @param array $prevalues default values for new documents
726  * @param array $torder array to describe CSV column attributes
727  * @global double Http var : Y if want double title document
728  * @return array properties of document added (or analyzed to be added)
729  */
730  function csvAddDoc($dbaccess, $data, $dirid = 10, $analyze = false, $ldir = '', $policy = "add", $tkey = array(
731  "title"
732  ) , $prevalues = array() , $torder = array())
733  {
734  // return structure
735  $tcr = array(
736  "err" => "",
737  "msg" => "",
738  "specmsg" => "",
739  "folderid" => 0,
740  "foldername" => "",
741  "filename" => "",
742  "title" => "",
743  "id" => "",
744  "values" => array() ,
745  "familyid" => 0,
746  "familyname" => "",
747  "action" => "-"
748  );
749  // like : DOC;120;...
750  $err = "";
751  if (is_numeric($data[1])) $fromid = $data[1];
752  else $fromid = getFamIdFromName($dbaccess, $data[1]);
753  if ($fromid == 0) {
754  $tcr["action"] = "ignored";
755  $tcr["err"] = sprintf(_("Not a family [%s]") , $data[1]);
756  return $tcr;
757  }
758  $docc = createDoc($dbaccess, $fromid);
759  if (!$docc) return;
760 
761  $msg = ""; // information message
762  $docc->fromid = $fromid;
763  $tcr["familyid"] = $docc->fromid;
764  $tcr["familyname"] = $docc->getTitle($docc->fromid);
765  if ($data[2] > 0) {
766  $docc->id = $data[2]; // static id
767  $docc->initid = $data[2];
768  } elseif (trim($data[2]) != "") {
769  if (!is_numeric(trim($data[2]))) {
770  $docc->name = trim($data[2]); // logical name
771  $docid = getIdFromName($dbaccess, $docc->name, $fromid);
772  if ($docid > 0) {
773  $docc->id = $docid;
774  $docc->initid = $docid;
775  }
776  }
777  }
778  if ($docc->id > 0) {
779  $doc = new_doc($docc->dbaccess, $docc->id, true);
780  if (!$doc->isAffected()) $doc = $docc;
781  } else {
782  $doc = $docc;
783  }
784 
785  if ((intval($doc->id) == 0) || (!$doc->isAffected())) {
786 
787  $tcr["action"] = "added";
788  } else {
789  if ($doc->fromid != $fromid) {
790  // $doc = new_Doc($doc->dbaccess,$doc->latestId());
791  $tcr["action"] = "ignored";
792  $tcr["id"] = $doc->id;
793  $tcr["err"] = _('not same family');
794  return $tcr;
795  }
796  if ($doc->doctype == 'Z') {
797  if (!$analyze) $doc->revive();
798  $tcr["msg"].= _("restore document") . "\n";
799  }
800 
801  if ($doc->locked == - 1) {
802  // $doc = new_Doc($doc->dbaccess,$doc->latestId());
803  $tcr["action"] = "ignored";
804  $tcr["id"] = $doc->id;
805  $tcr["err"] = _('fixed document');
806  return $tcr;
807  }
808 
809  $tcr["action"] = "updated";
810  $tcr["id"] = $doc->id;
811  $msg.= $err . sprintf(_("update id [%d] ") , $doc->id);
812  }
813 
814  if ($err != "") {
815  global $nline, $gerr;
816  $tcr["action"] = "ignored";
817  $gerr = "\nline $nline:" . $err;
818  $tcr["err"] = $err;
819  return $tcr;
820  }
821 
822  if (count($torder) == 0) {
823  $lattr = $doc->GetImportAttributes();
824  $torder = array_keys($lattr);
825  } else {
826  $lattr = $doc->GetNormalAttributes();
827  }
828  $iattr = 4; // begin in 5th column
829  foreach ($torder as $attrid) {
830  if (isset($lattr[$attrid])) {
831  $attr = $lattr[$attrid];
832  if (isset($data[$iattr]) && ($data[$iattr] != "")) {
833  $dv = str_replace(array(
834  '\n',
835  ALTSEPCHAR
836  ) , array(
837  "\n",
838  ';'
839  ) , $data[$iattr]);
840  if (!isUTF8($dv)) $dv = utf8_encode($dv);
841  if (($attr->type == "file") || ($attr->type == "image")) {
842  // insert file
843  $tcr["foldername"] = $ldir;
844  $tcr["filename"] = $dv;
845 
846  if (!$analyze) {
847  if ($attr->inArray()) {
848  $tabsfiles = $doc->_val2array($dv);
849  $tvfids = array();
850  foreach ($tabsfiles as $fi) {
851  if (preg_match(PREGEXPFILE, $fi, $reg)) {
852  $tvfids[] = $fi;
853  } elseif (preg_match('/^http:/', $fi, $reg)) {
854  $tvfids[] = '';
855  } elseif ($fi) {
856  $absfile = "$ldir/$fi";
857  $err = AddVaultFile($dbaccess, $absfile, $analyze, $vfid);
858  if ($err != "") {
859  $tcr["err"].= "$err: $fi\n";
860  } else {
861  $tvfids[] = $vfid;
862  }
863  } else {
864  $tvfids[] = '';
865  }
866  }
867  $doc->setValue($attr->id, $tvfids);
868  } else {
869  // one file only
870  if (preg_match(PREGEXPFILE, $dv, $reg)) {
871  $doc->setValue($attr->id, $dv);
872  $tcr["values"][$attr->getLabel() ] = $dv;
873  } elseif (preg_match('/^http:/', $dv, $reg)) {
874  // nothing
875 
876  } elseif ($dv) {
877  $absfile = "$ldir/$dv";
878  $err = AddVaultFile($dbaccess, $absfile, $analyze, $vfid);
879  if ($err != "") {
880  $tcr["err"] = $err;
881  } else {
882  $doc->setValue($attr->id, $vfid);
883  }
884  }
885  }
886  } else {
887  // just for analyze
888  if ($dv == $doc->getValue($attr->id)) $tcr["values"][$attr->getLabel() ] = ("/no change/");
889  else $tcr["values"][$attr->getLabel() ] = dv;
890  }
891  } else {
892  $doc->setValue($attr->id, $dv);
893  if ($doc->getOldValue($attr->id) !== false) $tcr["values"][$attr->getLabel() ] = $dv;
894  else $tcr["values"][$attr->getLabel() ] = ("/no change/");
895  }
896  }
897  }
898  $iattr++;
899  }
900 
901  if (($err == "") && (!$analyze)) {
902  if (($doc->id > 0) || ($policy != "update")) {
903  $err = $doc->preImport();
904  }
905  }
906  // update title in finish
907  if (!$analyze) $doc->refresh(); // compute read attribute
908  if ($err != "") {
909  $tcr["action"] = "ignored";
910  $tcr["err"] = $err;
911  return $tcr;
912  }
913 
914  if (($doc->id == "") && ($doc->name == "")) {
915  switch ($policy) {
916  case "add":
917  $tcr["action"] = "added"; # N_("added")
918  if (!$analyze) {
919 
920  if ($doc->id == "") {
921  // insert default values
922  foreach ($prevalues as $k => $v) {
923  $doc->setValue($k, $v);
924  }
925  $err = $doc->preImport();
926  if ($err != "") {
927  $tcr["action"] = "ignored";
928  $tcr["err"] = sprintf(_("pre-import:%s") , $err);
929  return $tcr;
930  }
931  $err = $doc->Add();
932  $tcr["err"] = $err;
933  }
934  if ($err == "") {
935  $tcr["id"] = $doc->id;
936  $msg.= $err . sprintf(_("add %s id [%d] ") , $doc->title, $doc->id);
937  $tcr["msg"] = sprintf(_("add %s id [%d] ") , $doc->title, $doc->id);
938  } else {
939  $tcr["action"] = "ignored";
940  }
941  } else {
942  $doc->RefreshTitle();
943  $tcr["msg"] = sprintf(_("%s to be add") , $doc->title);
944  }
945  break;
946 
947  case "update":
948  $doc->RefreshTitle();
949  $lsdoc = $doc->GetDocWithSameTitle($tkey[0], $tkey[1]);
950  // test if same doc in database
951  if (count($lsdoc) == 0) {
952  $tcr["action"] = "added";
953  if (!$analyze) {
954  if ($doc->id == "") {
955  // insert default values
956  foreach ($prevalues as $k => $v) {
957  if ($doc->getValue($k) == "") $doc->setValue($k, $v);
958  }
959  $err = $doc->preImport();
960  if ($err != "") {
961  $tcr["action"] = "ignored";
962  $tcr["err"] = sprintf(_("pre-import:%s") , $err);
963  return $tcr;
964  }
965  $err = $doc->Add();
966  $tcr["err"] = $err;
967  }
968  if ($err == "") {
969  $tcr["id"] = $doc->id;
970  $tcr["action"] = "added";
971  $tcr["msg"] = sprintf(_("add id [%d] ") , $doc->id);
972  } else {
973  $tcr["action"] = "ignored";
974  }
975  } else {
976  $tcr["msg"] = sprintf(_("%s to be add") , $doc->title);
977  }
978  } elseif (count($lsdoc) == 1) {
979  // no double title found
980  $tcr["action"] = "updated"; # N_("updated")
981  if (!$analyze) {
982  $err = $lsdoc[0]->preImport();
983  if ($err != "") {
984  $tcr["action"] = "ignored";
985  $tcr["err"] = sprintf(_("pre-import:%s") , $err);
986  return $tcr;
987  }
988  }
989  $lsdoc[0]->transfertValuesFrom($doc);
990  $doc = $lsdoc[0];
991  $tcr["id"] = $doc->id;
992  if (!$analyze) {
993  if (($data[2] != "") && (!is_numeric(trim($data[2]))) && ($doc->name == "")) {
994  $doc->name = $data[2];
995  }
996  $tcr["msg"] = sprintf(_("update %s [%d] ") , $doc->title, $doc->id);
997  } else {
998  $tcr["msg"] = sprintf(_("to be update %s [%d] ") , $doc->title, $doc->id);
999  }
1000  } else {
1001  //more than one double
1002  $tcr["action"] = "ignored"; # N_("ignored")
1003  $tcr["err"] = sprintf(_("too many similar document %s <B>ignored</B> ") , $doc->title);
1004  $msg.= $err . $tcr["err"];
1005  return $tcr;
1006  }
1007 
1008  break;
1009 
1010  case "keep":
1011  $doc->RefreshTitle();
1012  $lsdoc = $doc->GetDocWithSameTitle($tkey[0], $tkey[1]);
1013  if (count($lsdoc) == 0) {
1014  $tcr["action"] = "added";
1015  if (!$analyze) {
1016  if ($doc->id == "") {
1017  // insert default values
1018  foreach ($prevalues as $k => $v) {
1019  if ($doc->getValue($k) == "") $doc->setValue($k, $v);
1020  }
1021  $err = $doc->Add();
1022  }
1023  $tcr["id"] = $doc->id;
1024  $msg.= $err . sprintf(_("add id [%d] ") , $doc->id);
1025  } else {
1026  $tcr["msg"] = sprintf(_("%s to be add") , $doc->title);
1027  }
1028  } else {
1029  //more than one double
1030  $tcr["action"] = "ignored";
1031  $tcr["err"] = sprintf(_("similar document %s found. keep similar") , $doc->title);
1032  $msg.= $err . $tcr["err"];
1033  return $tcr;
1034  }
1035 
1036  break;
1037  }
1038  } else {
1039  // add special id
1040  if (!$doc->isAffected()) {
1041  $tcr["action"] = "added";
1042  if (!$analyze) {
1043  // insert default values
1044  foreach ($prevalues as $k => $v) {
1045  if ($doc->getValue($k) == "") $doc->setValue($k, $v);
1046  }
1047  $err = $doc->preImport();
1048  if ($err != "") {
1049  $tcr["action"] = "ignored";
1050  $tcr["err"] = sprintf(_("pre-import:%s") , $err);
1051  return $tcr;
1052  }
1053  $err = $doc->Add();
1054 
1055  $tcr["id"] = $doc->id;
1056  $msg.= $err . sprintf(_("add %s id [%d] ") , $doc->title, $doc->id);
1057  $tcr["msg"] = sprintf(_("add %s id [%d] ") , $doc->title, $doc->id);
1058  } else {
1059  $doc->RefreshTitle();
1060  $tcr["id"] = $doc->id;
1061  $tcr["msg"] = sprintf(_("%s to be add") , $doc->title);
1062  }
1063  }
1064  }
1065 
1066  $tcr["title"] = $doc->title;
1067  if (!$analyze) {
1068  if ($doc->isAffected()) {
1069  $tcr["specmsg"] = $doc->Refresh(); // compute read attribute
1070  $err = $doc->PostModify(); // compute read attribute
1071  if ($err == "") $err = $doc->modify();
1072  if ($err == "-") $err = ""; // not really an error add addfile must be tested after
1073  if ($err == "") {
1074  $doc->AddComment(sprintf(_("updated by import")));
1075  $msg.= $doc->postImport();
1076  }
1077  $tcr["err"].= $err;
1078  $tcr["msg"].= $msg;
1079  }
1080  }
1081  //------------------
1082  // add in folder
1083  if (($err == "") && ($data[3] != "-")) {
1084  $msg.= $doc->title;
1085  if (is_numeric($data[3])) $ndirid = $data[3];
1086  else $ndirid = getIdFromName($dbaccess, $data[3], 2);
1087 
1088  if ($ndirid > 0) { // dirid
1089  $dir = new_Doc($dbaccess, $ndirid);
1090  if ($dir->isAffected()) {
1091  $tcr["folderid"] = $dir->id;
1092  $tcr["foldername"] = dirname($ldir) . "/" . $dir->title;
1093  if (!$analyze) {
1094  if ($dir->isAlive() && method_exists($dir, "AddFile")) {
1095  $tcr["err"].= $dir->AddFile($doc->id);
1096  }
1097  }
1098  $tcr["msg"].= $err . " " . sprintf(_("and add in %s folder ") , $dir->title);
1099  }
1100  } else if ($ndirid == 0) {
1101  if ($dirid > 0) {
1102 
1103  $dir = new_Doc($dbaccess, $dirid);
1104  if ($dir->isAlive() && method_exists($dir, "AddFile")) {
1105  $tcr["folderid"] = $dir->id;
1106  $tcr["foldername"] = dirname($ldir) . "/" . $dir->title;
1107  if (!$analyze) {
1108  if ($dir->isAlive() && method_exists($dir, "AddFile")) {
1109  $tcr["err"].= $dir->AddFile($doc->id);
1110  }
1111  }
1112  $tcr["msg"].= $err . " " . sprintf(_("and add in %s folder ") , $dir->title);
1113  }
1114  }
1115  }
1116  }
1117 
1118  return $tcr;
1119  }
1120 
1121  function AddImportLog($msg)
1122  {
1123  global $action;
1124  if ($action->lay) {
1125  $tmsg = $action->lay->GetBlockData("MSG");
1126  $tmsg[] = array(
1127  "msg" => $msg
1128  );
1129  $action->lay->SetBlockData("MSG", $tmsg);
1130  } else {
1131  print "\n$msg";
1132  }
1133  }
1134 
1135  function getOrder($orderdata)
1136  {
1137  return array_map("trim", array_slice($orderdata, 4));
1138  }
1139 
1141  {
1142  global $importedFiles;
1143 
1144  $path = str_replace("//", "/", $path);
1145  // return same if already imported (case of multi links)
1146  if (isset($importedFiles[$path])) {
1147  $vid = $importedFiles[$path];
1148  return "";
1149  }
1150 
1151  $absfile2 = str_replace('"', '\\"', $path);
1152  // $mime=mime_content_type($absfile);
1153  $mime = trim(shell_exec(sprintf("file -ib %s", escapeshellarg($absfile2))));
1154  if (!$analyze) {
1156  $err = $vf->Store($path, false, $vid);
1157  }
1158  if ($err != "") {
1159 
1161  return $err;
1162  } else {
1163  $base = basename($path);
1164  $importedFiles[$path] = "$mime|$vid|$base";
1165  $vid = "$mime|$vid|$base";
1166 
1167  return "";
1168  }
1169  return false;
1170  }
1172  {
1173  if (preg_match('/\.ods$/', $filename)) return true;
1174  $sys = trim(shell_exec(sprintf("file -bi %s", escapeshellarg($filename))));
1175  if ($sys == "application/x-zip") return true;
1176  if ($sys == "application/vnd.oasis.opendocument.spreadsheet") return true;
1177  return false;
1178  }
1179  /**
1180  * convert ods file in csv file
1181  * the csv file must be delete by caller after using it
1182  * @return strint the path to the csv file
1183  */
1184  function ods2csv($odsfile)
1185  {
1186  ;
1187  $csvfile = uniqid(getTmpDir() . "/csv") . "csv";
1188  $wsh = getWshCmd();
1189  $cmd = sprintf("%s --api=ods2csv --odsfile=%s --csvfile=%s >/dev/null", getWshCmd() , escapeshellarg($odsfile) , escapeshellarg($csvfile));
1190  $err = system($cmd, $out);
1191  if ($err === false) return false;
1192  return $csvfile;
1193  }
1194 ?>
← centre documentaire © anakeen - published under CC License - Dynacase