Core  3.2
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  * @package FDL
5 */
6 /**
7  * Import CSV
8  *
9  * @author Anakeen
10  * @version $Id: generic_importcsv.php,v 1.19 2008/09/01 06:21:09 marc Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("FDL/Class.Dir.php");
18 include_once ("FDL/import_file.php");
19 include_once ("FDL/modcard.php");
20 include_once ("GENERIC/generic_util.php");
21 /**
22  * View a document
23  * @param Action &$action current action
24  * @global policy string Http var : add|update|keep police case of similar document
25  * @global category string Http var :
26  * @global analyze string Http var : "Y" if just analyze
27  * @global key1 string Http var : primary key for double
28  * @global key2 string Http var : secondary key for double
29  * @global classid int Http var : document family to import
30  * @global colorder string Http var : array to describe CSV column attributes
31  * @global file string Http var : path to import file (only with wsh)
32  */
34 {
35  // -----------------------------------
36  global $_FILES;
37  $action->parent->AddJsRef($action->GetParam("CORE_JSURL") . "/subwindow.js");
38  // Get all the params
39  $policy = GetHttpVars("policy", "update");
40  $category = GetHttpVars("category");
41  $analyze = (GetHttpVars("analyze", "N") == "Y"); // just analyze
42  $key1 = GetHttpVars("key1", "title"); // primary key for double
43  $key2 = GetHttpVars("key2", ""); // secondary key for double
44  $classid = GetHttpVars("classid", getDefFam($action)); // document family to import
45  $comma = GetHttpVars("comma", ";"); // Column separator -default is comma-
46  $tcolorder[$classid] = GetHttpVars("colorder"); // column order
47  $dbaccess = $action->dbaccess;
48 
50  $ddoc = createDoc($dbaccess, $classid);
51  setPostVars($ddoc); // memorize default import values
52  if (isset($_FILES["vcardfile"])) {
53  // importation
54  $vcardfile = $_FILES["vcardfile"]["tmp_name"];
55  } else {
56  $vcardfile = GetHttpVars("file");
57  }
58 
59  if (seemsODS($vcardfile)) {
60  $cvsfile = ods2csv($vcardfile);
61  $fdoc = fopen($cvsfile, "r");
62  } else {
63  $fdoc = fopen($vcardfile, "r");
64  }
65 
66  if (!$fdoc) $action->exitError(_("no csv import file specified"));
67  $dir = new_Doc($dbaccess, getDefFld($action));
68 
69  if ($analyze) $action->lay->set("importresult", _("import analysis result"));
70  else $action->lay->set("importresult", _("import results"));
71 
72  $tvalue = array();
73 
74  $line = 0;
75  $cr = array();
76  while (!feof($fdoc)) {
77  $buffer = rtrim(fgets($fdoc, 4096));
78  $data = explode($comma, $buffer);
79  $line++;
80  $num = count($data);
81  if ($num < 1) continue;
82  if (is_numeric($data[1])) $fromid = $data[1];
83  else $fromid = getFamIdFromName($dbaccess, $data[1]);
84  switch ($data[0]) {
85  case "DOC":
86  if (isset($tkeys[$fromid])) $tk = $tkeys[$fromid];
87  else $tk = array(
88  $key1,
89  $key2
90  );
91 
92  $cr[$line] = csvAddDoc($dbaccess, $data, getDefFld($action) , $analyze, '', $policy, $tk, $ddoc->getValues() , $tcolorder[$fromid]);
93  if ($cr[$line]["err"] != "") {
94  } else {
95 
96  if ($cr[$line]["id"] > 0) {
97  // add in each selected folder
98  if (is_array($category)) {
99 
100  foreach ($category as $k => $v) {
101  /*
102  * @var Dir $catg
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 
seemsODS($filename)
global $action
isdoc2($var)
getDefFld(Action &$action)
setPostVars(Doc &$doc, &$info=array())
Definition: modcard.php:259
getOrder(array $orderdata)
Definition: import_file.php:97
getDefFam(Action &$action)
Set($name, &$parent)
exitError($texterr, $exit=true, $code="")
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
getFamIdFromName($dbaccess, $name)
generic_importcsv(Action &$action)
ods2csv($odsfile)
new_Doc($dbaccess, $id= '', $latest=false)
$dir
Definition: resizeimg.php:144
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
$dbaccess
Definition: checkVault.php:17
csvAddDoc($dbaccess, $data, $dirid=0, $analyze=false, $ldir= '', $policy="add", $tkey=array("title"), $prevalues=array(), $torder=array())
Definition: import_file.php:64
if($file) if($subject==""&&$file) if($subject=="") $err
$analyze
$data
← centre documentaire © anakeen