Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
generic_importcsv.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 CSV
9  *
10  * @author Anakeen 2004
11  * @version $Id: generic_importcsv.php,v 1.19 2008/09/01 06:21:09 marc 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.Dir.php");
20 include_once ("FDL/import_file.php");
21 include_once ("FDL/modcard.php");
22 include_once ("GENERIC/generic_util.php");
23 /**
24  * View a document
25  * @param Action &$action current action
26  * @global policy Http var : add|update|keep police case of similar document
27  * @global category Http var :
28  * @global analyze Http var : "Y" if just analyze
29  * @global key1 Http var : primary key for double
30  * @global key2 Http var : secondary key for double
31  * @global classid Http var : document family to import
32  * @global colorder Http var : array to describe CSV column attributes
33  * @global file Http var : path to import file (only with wsh)
34  * @global Http var :
35  */
37 {
38  // -----------------------------------
39  global $_FILES;
40  $action->parent->AddJsRef($action->GetParam("CORE_JSURL") . "/subwindow.js");
41  // Get all the params
42  $policy = GetHttpVars("policy", "update");
43  $category = GetHttpVars("category");
44  $analyze = (GetHttpVars("analyze", "N") == "Y"); // just analyze
45  $key1 = GetHttpVars("key1", "title"); // primary key for double
46  $key2 = GetHttpVars("key2", ""); // secondary key for double
47  $classid = GetHttpVars("classid", getDefFam($action)); // document family to import
48  $comma = GetHttpVars("comma", ";"); // Column separator -default is comma-
49  $tcolorder[$classid] = GetHttpVars("colorder"); // column order
50  $dbaccess = $action->GetParam("FREEDOM_DB");
51  if (ini_get("max_execution_time") < 180) ini_set("max_execution_time", 180); // 3 minutes
52  $ddoc = createDoc($dbaccess, $classid);
53  setPostVars($ddoc); // memorize default import values
54 
55  if (isset($_FILES["vcardfile"])) {
56  // importation
57  $vcardfile = $_FILES["vcardfile"]["tmp_name"];
58  } else {
59  $vcardfile = GetHttpVars("file");
60  }
61 
62  if (seemsODS($vcardfile)) {
63  $cvsfile = ods2csv($vcardfile);
64  $fdoc = fopen($cvsfile, "r");
65  } else {
66  $fdoc = fopen($vcardfile, "r");
67  }
68 
69  if (!$fdoc) $action->exitError(_("no csv import file specified"));
71 
72  if ($analyze) $action->lay->set("importresult", _("import analysis result"));
73  else $action->lay->set("importresult", _("import results"));
74 
75  $tvalue = array();
76 
77  $line = 0;
78  while (!feof($fdoc)) {
79  $buffer = rtrim(fgets($fdoc, 4096));
80  $data = explode($comma, $buffer);
81  $line++;
82  $num = count($data);
83  if ($num < 1) continue;
84  if (is_numeric($data[1])) $fromid = $data[1];
85  else $fromid = getFamIdFromName($dbaccess, $data[1]);
86  switch ($data[0]) {
87  case "DOC":
88  if (isset($tkeys[$fromid])) $tk = $tkeys[$fromid];
89  else $tk = array(
90  $key1,
91  $key2
92  );
93 
94  $cr[$line] = csvAddDoc($dbaccess, $data, getDefFld($action) , $analyze, '', $policy, $tk, $ddoc->getValues() , $tcolorder[$fromid]);
95  if ($cr[$line]["err"] != "") {
96  } else {
97 
98  if ($cr[$line]["id"] > 0) {
99  // add in each selected folder
100  if (is_array($category)) {
101 
102  foreach ($category as $k => $v) {
103 
104  $catg = new_Doc($dbaccess, $v);
105  $err = $catg->AddFile($cr[$line]["id"]);
106  $cr[$line]["err"].= $err;
107  if ($err == "") $cr[$line]["msg"].= sprintf(_("Add it in %s folder") , $catg->title);
108  }
109  }
110  }
111  }
112  break;
113 
114  case "ORDER":
115  $cr[$line] = array(
116  "err" => "",
117  "msg" => "",
118  "specmsg" => "",
119  "folderid" => 0,
120  "foldername" => "",
121  "filename" => "",
122  "title" => "",
123  "id" => "",
124  "values" => array() ,
125  "familyid" => 0,
126  "familyname" => "",
127  "action" => " "
128  );
129  $tcolorder[$fromid] = getOrder($data);
130  $cr[$line]["msg"] = sprintf(_("new column order %s") , implode(" - ", $tcolorder[$fromid]));
131 
132  break;
133 
134  case "KEYS":
135  $cr[$line] = array(
136  "err" => "",
137  "msg" => "",
138  "specmsg" => "",
139  "folderid" => 0,
140  "foldername" => "",
141  "filename" => "",
142  "title" => "",
143  "id" => "",
144  "values" => array() ,
145  "familyid" => 0,
146  "familyname" => "",
147  "action" => " "
148  );
149  $tkeys[$fromid] = getOrder($data);
150  if (($tkeys[$fromid][0] == "") || (count($tkeys[$fromid]) == 0)) {
151  $cr[$line]["err"] = sprintf(_("error in import keys : %s") , implode(" - ", $tkeys[$fromid]));
152  unset($tkeys[$fromid]);
153  $cr[$line]["action"] = "ignored";
154  } else {
155  $cr[$line]["msg"] = sprintf(_("new import keys : %s") , implode(" - ", $tkeys[$fromid]));
156  }
157 
158  break;
159  }
160  }
161 
162  fclose($fdoc);
163  foreach ($cr as $k => $v) {
164  $cr[$k]["taction"] = _($v["action"]); // translate action
165  $cr[$k]["order"] = $k; // translate action
166  $cr[$k]["svalues"] = "";
167 
168  foreach ($v["values"] as $ka => $va) {
169  $cr[$k]["svalues"].= "<LI>[$ka:$va]</LI>"; //
170 
171  }
172  }
173  $action->lay->SetBlockData("ADDEDDOC", $cr);
174  $nbdoc = count(array_filter($cr, "isdoc2"));
175  $action->lay->Set("nbdoc", "$nbdoc");
176  }
177  function isdoc2($var)
178  {
179  return (($var["action"] == "added") || ($var["action"] == "updated"));
180  }
181 ?>
← centre documentaire © anakeen - published under CC License - Dynacase