Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
exportfld.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Export Document from Folder
8  *
9  * @author Anakeen
10  * @version $Id: exportfld.php,v 1.44 2009/01/12 13:23:11 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("FDL/Lib.Dir.php");
18 include_once ("FDL/Lib.Util.php");
19 include_once ("FDL/Class.DocAttr.php");
20 include_once ("VAULT/Class.VaultFile.php");
21 include_once ("FDL/import_file.php");
22 /**
23  * Exportation of documents from folder or searches
24  * @param Action &$action current action
25  * @param string $aflid Folder identifier to use if no "id" http vars
26  * @param string $famid Family restriction to filter folder content
27  * @param string $outputPath where put export, if wfile outputPath is a directory
28  * @param bool $exportInvisibleVisibilities set to true to export invisible attribute also
29  * @throws Dcp\Exception
30  * @throws Exception
31  * @global string $fldid Http var : folder identifier to export
32  * @global string $wprof Http var : (Y|N) if Y export associated profil also
33  * @global string $wfile Http var : (Y|N) if Y export attached file export format will be tgz
34  * @global string $wident Http var : (Y|N) if Y specid column is set with identifier of document
35  * @global string $wutf8 Http var : (Y|N) if Y encoding is utf-8 else iso8859-1
36  * @global string $wcolumn Http var : if - export preferences are ignored
37  * @global string $eformat Http var : (I|R|F) I: for reimport, R: Raw data, F: Formatted data
38  * @global string $selection Http var : JSON document selection object
39  * @return void
40  */
41 function exportfld(Action & $action, $aflid = "0", $famid = "", $outputPath = "", $exportInvisibleVisibilities = false)
42 {
43  $dbaccess = $action->dbaccess;
44 
45  $usage = new ActionUsage($action);
46 
47  $wprof = ($usage->addOptionalParameter("wprof", "With profil", array(
48  "Y",
49  "N"
50  ) , "N") == "Y");
51  $wfile = ($usage->addOptionalParameter("wfile", "With files", array(
52  "Y",
53  "N"
54  ) , "N") == "Y");
55  $wident = ($usage->addOptionalParameter("wident", "With document numeric identifiers", array(
56  "Y",
57  "N"
58  ) , "Y") == "Y");
59 
60  $fileEncoding = $usage->addOptionalParameter("code", "File encoding", array(
61  "utf8",
62  "iso8859-15"
63  ) , "utf8");
64  $profilType = $usage->addOptionalParameter("wproftype", "Profil option type", array(
65  \Dcp\ExportDocument::useAclAccountType,
66  \Dcp\ExportDocument::useAclDocumentType
67  ) , \Dcp\ExportDocument::useAclAccountType);
68  $wutf8 = ($fileEncoding !== "iso8859-15");
69 
70  $nopref = ($usage->addOptionalParameter("wcolumn", "if - export preferences are ignored") == "-"); // no preference read
71  $eformat = $usage->addOptionalParameter("eformat", "Export format", array(
72  "I",
73  "R",
74  "F",
75  "X",
76  "Y"
77  ) , "I");
78  $selection = $usage->addOptionalParameter("selection", "export selection object (JSON)");
79  $statusOnly = ($usage->addHiddenParameter("statusOnly", "Export progress status") != ""); // export selection object (JSON)
80  $exportId = $usage->addHiddenParameter("exportId", "Export status id"); // export status id
81  if (!$aflid && !$selection && !$statusOnly) {
82  $fldid = $usage->addRequiredParameter("id", "Folder identifier");
83  } else {
84  $fldid = $usage->addOptionalParameter("id", "Folder identifier", array() , $aflid);
85  }
86 
87  $csvSeparator = $usage->addOptionalParameter("csv-separator", "character to delimiter fields - generaly a comma", function ($values, $argName, ApiUsage $apiusage)
88  {
89  if ($values === ApiUsage::GET_USAGE) {
90  return sprintf(' use single character or "auto"');
91  }
92  if (!is_string($values)) {
93  return sprintf("must be a character [%s] ", print_r($values, true));
94  }
95  if ($values != "auto") {
96  if (mb_strlen($values) > 1) {
97  return sprintf("must be a only one character [%s] ", $values);
98  }
99  if (mb_strlen($values) === 0) {
100  return sprintf("empty separator is not allowed [%s] ", $values);
101  }
102  }
103  return '';
104  }
105  , ";");
106 
107  $csvEnclosure = $usage->addOptionalParameter("csv-enclosure", "character to enclose fields - generaly double-quote", function ($values, $argName, ApiUsage $apiusage)
108  {
109  if ($values === ApiUsage::GET_USAGE) {
110  return sprintf(' use single character or "auto"');
111  }
112  if (!is_string($values)) {
113  return sprintf("must be a character [%s] ", print_r($values, true));
114  }
115  if ($values != "auto") {
116  if (mb_strlen($values) > 1) {
117  return sprintf("must be a only one character [%s] ", $values);
118  }
119  }
120  return '';
121  }
122  , "");
123  $usage->verify();
124 
125  if ($statusOnly) {
126 
127  header('Content-Type: application/json');
128  $action->lay->noparse = true;
129  $action->lay->template = json_encode($action->read($exportId));
130  return;
131  }
132  setMaxExecutionTimeTo(3600);
133  $exportCollection = new Dcp\ExportCollection();
134  $exportCollection->setExportStatusId($exportId);
135  $exportCollection->setOutputFormat($eformat);
136  $exportCollection->setExportProfil($wprof);
137  $exportCollection->setExportDocumentNumericIdentiers($wident);
138  $exportCollection->setUseUserColumnParameter(!$nopref);
139  $exportCollection->setOutputFileEncoding($wutf8 ? Dcp\ExportCollection::utf8Encoding : Dcp\ExportCollection::latinEncoding);
140  $exportCollection->setVerifyAttributeAccess(!$exportInvisibleVisibilities);
141  $exportCollection->setProfileAccountType($profilType);
142 
143  if ((!$fldid) && $selection) {
144  $selection = json_decode($selection);
145  include_once ("DATA/Class.DocumentSelection.php");
146  include_once ("FDL/Class.SearchDoc.php");
147  $os = new Fdl_DocumentSelection($selection);
148  $ids = $os->getIdentificators();
149  $exportCollection->recordStatus(_("Retrieve documents from database"));
150  $s = new SearchDoc($dbaccess);
151  $s->setObjectReturn(true);
152  $s->addFilter(getSqlCond($ids, "id", true));
153  $s->setOrder("fromid, id");
154  $s->search();
155  $fname = "selection";
156  } else {
157  if (!$fldid) $action->exitError(_("no export folder specified"));
158 
159  $fld = new_Doc($dbaccess, $fldid);
160  if ($famid == "") $famid = GetHttpVars("famid");
161  $fname = str_replace(array(
162  " ",
163  "'"
164  ) , array(
165  "_",
166  ""
167  ) , $fld->getTitle());
168 
169  $exportCollection->recordStatus(_("Retrieve documents from database"));
170 
171  $s = new SearchDoc($dbaccess, $famid);
172  $s->setObjectReturn(true);
173  $s->setOrder("fromid, id");
174  $s->useCollection($fld->initid);
175  $s->search();
176  }
177 
178  $exportCollection->setDocumentlist($s->getDocumentList());
179  $exportCollection->setExportFiles($wfile);
180  //usort($tdoc, "orderbyfromid");
181  if ($outputPath) {
182  if ($wfile) {
183  if (!is_dir($outputPath)) {
184  mkdir($outputPath);
185  }
186  $foutname = $outputPath . "/fdl.zip";
187  } else {
188  $foutname = $outputPath;
189  }
190  } else {
191  $foutname = uniqid(getTmpDir() . "/exportfld");
192  }
193 
194  if (file_exists($foutname)) {
195  $action->exitError(sprintf("export is not allowed to override existing file %s") , $outputPath);
196  }
197 
198  $exportCollection->setOutputFilePath($foutname);
199  $exportCollection->setCvsSeparator($csvSeparator);
200  $exportCollection->setCvsEnclosure($csvEnclosure);
201  $action->setParamU("EXPORT_CSVSEPARATOR", $csvSeparator);
202  $action->setParamU("EXPORT_CSVENCLOSURE", $csvEnclosure);
203 
204  try {
205  $exportCollection->export();
206  if (is_file($foutname)) {
207  switch ($eformat) {
209  $fname.= ".xml";
210  $fileMime = "text/xml";
211  break;
212 
214  $fname.= ".zip";
215  $fileMime = "application/x-zip";
216  break;
217 
218  default:
219  if ($wfile) {
220 
221  $fname.= ".zip";
222  $fileMime = "application/x-zip";
223  } else {
224  $fname.= ".csv";
225  $fileMime = "text/csv";
226  }
227  }
228  $exportCollection->recordStatus(_("Export done") , true);
229  if (!$outputPath) {
230  Http_DownloadFile($foutname, $fname, $fileMime, false, false, true);
231  }
232  }
233  }
234  catch(Dcp\Exception $e) {
235  throw $e;
236  }
237 }
238 /**
239  * @param Action $action
240  * @param $exportId
241  * @param $msg
242  * @param bool $endStatus
243  * @see Dcp\ExportCollection::recordStatus()
244  * @deprecated use Dcp\ExportCollection::recordStatus() instead
245  */
246 function recordStatus(Action & $action, $exportId, $msg, $endStatus = false)
247 {
248  $action->register($exportId, array(
249  "status" => $msg,
250  "end" => $endStatus
251  ));
252 }
253 /**
254  * Removes content of the directory (not sub directory)
255  *
256  * @param string $dirname the directory name to remove
257  * @return boolean True/False whether the directory was deleted.
258  * @deprecated To delete (not used)
259  */
261 {
262  if (!is_dir($dirname)) return false;
263  $dcur = realpath($dirname);
264  $darr = array();
265  $darr[] = $dcur;
266  if ($d = opendir($dcur)) {
267  while ($f = readdir($d)) {
268  if ($f == '.' || $f == '..') continue;
269  $f = $dcur . '/' . $f;
270  if (is_file($f)) {
271  unlink($f);
272  $darr[] = $f;
273  }
274  }
275  closedir($d);
276  }
277 
278  return true;
279 }
global $action
deleteContentDirectory($dirname)
Definition: exportfld.php:260
$csvEnclosure
exportfld(Action &$action, $aflid="0", $famid="", $outputPath="", $exportInvisibleVisibilities=false)
Definition: exportfld.php:41
if($famId) $s
const GET_USAGE
setParamU($name, $val)
$d
Definition: dav.php:77
$dirname
Definition: vault_init.php:25
recordStatus(Action &$action, $exportId, $msg, $endStatus=false)
Definition: exportfld.php:246
exitError($texterr, $exit=true, $code="")
$csvSeparator
Verify arguments for action function.
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
new_Doc($dbaccess, $id= '', $latest=false)
Http_DownloadFile($filename, $name, $mime_type= '', $inline=false, $cache=true, $deleteafter=false)
Definition: Lib.Http.php:225
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
$dbaccess
Definition: checkVault.php:17
$usage
Verify arguments for wsh programs.
← centre documentaire © anakeen