Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Action/Freedom/freedom_import.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Import document descriptions
8  *
9  * @author Anakeen
10  * @version $Id: freedom_import.php,v 1.13 2008/02/27 11:43:08 eric Exp $
11  * @package FDL
12  * @subpackage GED
13  */
14 /**
15  */
16 
17 include_once ("FDL/import_file.php");
18 /**
19  * Import documents
20  * @param Action &$action current action
21  * @global string $file Http var : documents to export
22  * @global string $log Http var : log file output
23  */
25 {
26  // -----------------------------------
27  global $_FILES;
28 
29  $usage = new ActionUsage($action);
30  $usage->setDefinitionText("Import documents");
31  $separator = $usage->addOptionalParameter("separator", "character to delimiter fields - generaly a comma", function ($values, $argName, ApiUsage $apiusage)
32  {
33  if ($values === ApiUsage::GET_USAGE) {
34  return sprintf(' use single character or "auto"');
35  }
36  if (!is_string($values)) {
37  return sprintf("must be a character [%s] ", print_r($values, true));
38  }
39  if ($values != "auto") {
40  if (mb_strlen($values) > 1) {
41  return sprintf("must be a only one character [%s] ", $values);
42  }
43  }
44  return '';
45  });
46  $enclosure = $usage->addOptionalParameter("enclosure", "character to enclose fields - generaly double-quote", function ($values, $argName, ApiUsage $apiusage)
47  {
48  if ($values === ApiUsage::GET_USAGE) {
49  return sprintf(' use single character or "auto"');
50  }
51  if (!is_string($values)) {
52  return sprintf("must be a character [%s] ", print_r($values, true));
53  }
54  if ($values != "auto") {
55  if (mb_strlen($values) > 1) {
56  return sprintf("must be a only one character [%s] ", $values);
57  }
58  }
59  return '';
60  }
61  , "");
62 
63  $archive = ($usage->addOptionalParameter("archive", "is archive", array(
64  "yes",
65  "no"
66  ) , 'no') == "yes");
67 
68  $linebreak = $usage->addOptionalParameter("linebreak", "csv linebreak", array() , '\n');
69 
70  $analyze = ($usage->addOptionalParameter("analyze", "analyze", array(
71  "Y",
72  "N"
73  ) , "N") == "Y"); // just analyze
74  $usage->setStrictMode(false);
75  $usage->verify();
76 
77  $action->parent->addJsRef("lib/jquery/jquery.js");
78  $action->parent->addJsRef("lib/jquery-ui/js/jquery-ui.js");
79  $action->parent->addCssRef("css/dcp/jquery-ui.css");
80  $action->parent->addJsRef("lib/jquery-dataTables/js/jquery.dataTables.min.js");
81  $action->parent->addCssRef("lib/jquery-dataTables/css/jquery.dataTables_themeroller.css");
82 
83  setMaxExecutionTimeTo(3600); // 60 minutes
84  $csvfile = '';
85  $ext = '';
86  $filename='';
87  if (isset($_FILES["file"])) {
88  if ($_FILES["file"]["error"]) {
89  $action->exitError(_("No description import file"));
90  }
91  $filename = $_FILES["file"]['name'];
92  $csvfile = $_FILES["file"]['tmp_name'];
93  $ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
94  if ($ext === "gz") {
95  $ext = strtolower(substr($filename, strrpos($filename, '.', -4) + 1));
96  }
97  rename($csvfile, $csvfile . ".$ext");
98  $csvfile.= ".$ext";
99  } else {
100  $action->exitError(_("No description import file"));
101  }
102  if ($ext === "tgz" || $ext === "tar.gz") {
103  $archive = true;
104  } elseif ($ext !== "zip") {
105  $archive = false;
106  }
107 
108  if (!in_array($ext, array(
109  "csv",
110  "ods",
111  "xml",
112  "zip",
113  "tgz",
114  "tar.gz"
115  ))) {
116  $action->exitError(_("Not supported file format"));
117  }
118  $pseparator = $action->getParam("FREEDOM_CSVSEPARATOR");
119  $penclosure = $action->getParam("FREEDOM_CSVENCLOSURE");
120  $plinebreak = $action->getParam("FREEDOM_CSVLINEBREAK");
121 
122  if (!$analyze) {
123  if ($separator) {
124  if ($plinebreak != $linebreak) {
125  $action->setParamU("FREEDOM_CSVLINEBREAK", $linebreak);
126  }
127  if ($pseparator != $separator) {
128  $action->setParamU("FREEDOM_CSVSEPARATOR", $separator);
129  }
130  if ($penclosure != $enclosure) {
131  $action->setParamU("FREEDOM_CSVENCLOSURE", $enclosure);
132  }
133  }
134  }
135 
136  $oImport = new ImportDocument();
137  if ($separator) {
138  $oImport->setCsvOptions($separator, $enclosure, $linebreak);
139  }
140  if (preg_match("/admin\\.php$/", $_SERVER["SCRIPT_NAME"])) {
141  $oImport->setVerifyAttributeAccess(false);
142  }
143  $oImport->importDocuments($action, $csvfile, $analyze, $archive);
144  $oImport->writeHtmlCr($action->lay);
145  $action->lay->eset("basename", $filename);
146  $action->parent->AddJsRef($action->GetParam("CORE_JSURL") . "/subwindow.js");
147 
148  if (isset($_FILES["file"])) @unlink($csvfile); // tmp file
149 
150 }
global $action
$archive
$filename
const GET_USAGE
setParamU($name, $val)
$csvfile
exitError($texterr, $exit=true, $code="")
Verify arguments for action function.
global $_SERVER
getParam($name, $def="")
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
if($dirid) $oImport
$analyze
$usage
freedom_import(Action &$action)
Verify arguments for wsh programs.
← centre documentaire © anakeen