Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
csv2sql.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * to big importation
8  *
9  * @author Anakeen
10  * @version $Id: csv2sql.php,v 1.2 2007/12/13 16:54:21 eric Exp $
11  * @package FDL
12  * @subpackage WSH
13  */
14 /**
15  */
16 
17 include_once ("FDL/import_file.php");
18 
19 $usage = new ApiUsage();
20 $usage->setDefinitionText("to big importation");
21 $fimport = $usage->addRequiredParameter("file", "file name");
22 $usage->verify();
23 
24 if (seemsODS($fimport)) {
25  $cvsfile = ods2csv($fimport);
26  $fdoc = fopen($cvsfile, "r");
27 } else {
28  $fdoc = fopen($fimport, "r");
29 }
30 if (!$fdoc) exit(1);
31 
32 global $action;
33 $dbaccess = $action->dbaccess;
34 $idoc = new doc($dbaccess);
35 while (!feof($fdoc)) {
36 
37  $buffer = rtrim(fgets($fdoc, 16384));
38  $data = explode(";", $buffer);
39  $nline++;
40 
41  if ($data[0] == 'ORDER') {
42  if (is_numeric($data[1])) $orfromid = $data[1];
43  else $orfromid = getFamIdFromName($dbaccess, $data[1]);
44  $tcolorder[$orfromid] = getOrder($data);
45  $cdoc = createDoc($dbaccess, $orfromid);
46  $ta = $cdoc->GetTitleAttributes();
47  $titles[$orfromid] = array();
48  foreach ($ta as $k => $v) {
49  $titles[$orfromid][] = $v->id;
50  }
51 
52  foreach ($idoc->fields as $k => $v) {
53  if ($cdoc->$v != "") $tval[$orfromid][$v] = "'" . $cdoc->$v . "'";
54  }
55  $tval[$orfromid]["id"] = "(select nextval ('seq_id_doc'))";
56  $tval[$orfromid]["initid"] = "(select currval ('seq_id_doc'))";
57  $tval[$orfromid]["owner"] = $action->user->id;
58  } else if ($data[0] == 'DOC') {
59  if (is_numeric($data[1])) $fromid = $data[1];
60  else $fromid = getFamIdFromName($dbaccess, $data[1]);
61 
62  if (!isset($tval[$fromid])) {
63  print "-- order not defined for $fromid;\n";
64  continue;
65  }
66 
67  $ini = $tval[$fromid];
68 
69  $idx = 4;
70  foreach ($tcolorder[$fromid] as $k => $v) {
71  $ini[$v] = "'" . pg_escape_string($data[$idx]) . "'";
72  $idx++;
73  }
74  $title = "";
75  foreach ($titles[$orfromid] as $k => $v) {
76  $title.= substr($ini[$v], 1, -1) . " ";
77  }
78  $ini["title"] = "'" . pg_escape_string(trim($title)) . "'";
79 
80  $sval = implode($ini, ",");
81  $skey = implode(array_keys($ini) , ",");
82  $sql = sprintf("INSERT INTO doc%d (%s) values (%s);\n", $fromid, $skey, $sval);
83  print $sql;
84  }
85 }
86 // update family index when finished
87 foreach ($titles as $fromid => $v) {
88  $sql = sprintf("select setval ('seq_doc%d',(select max(id) from doc%d));\n", $fromid, $fromid);
89  print $sql;
90 }
seemsODS($filename)
global $action
Definition: csv2sql.php:32
$dbaccess
Definition: csv2sql.php:33
getOrder(array $orderdata)
Definition: import_file.php:97
$idoc
Definition: csv2sql.php:34
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
getFamIdFromName($dbaccess, $name)
print
Definition: checklist.php:49
switch($command) exit
Definition: checkVault.php:46
ods2csv($odsfile)
$usage
Definition: csv2sql.php:19
$fimport
Definition: csv2sql.php:21
Verify arguments for wsh programs.
$data
← centre documentaire © anakeen