13 protected $exportProfil =
false;
15 protected $exportFiles =
false;
17 protected $exportDocumentNumericIdentiers =
false;
19 const utf8Encoding =
"utf-8";
20 const latinEncoding =
"iso8859-15";
21 protected $outputFileEncoding = self::utf8Encoding;
23 protected $useUserColumnParameter =
false;
25 const csvRawOutputFormat =
"I";
26 const csvRawOnlyDataOutputFormat =
"R";
27 const csvDisplayValueOutputFormat =
"F";
28 const xmlArchiveOutputFormat =
"X";
29 const xmlFileOutputFormat =
"Y";
31 protected $outputFormat = self::csvRawOutputFormat;
32 protected $verifyAttributeAccess =
false;
34 protected $cvsSeparator =
";";
35 protected $cvsEnclosure =
"";
36 protected $profileAccountType = ExportDocument::useAclAccountType;
38 protected $outputFilePath =
'';
42 protected $exportStatusId =
'';
46 protected $collectionDocument = null;
50 protected $documentList = null;
54 protected $outHandler = null;
55 protected $moreDocuments = array();
56 protected $familyData = array();
63 $this->verifyAttributeAccess = $verifyAttributeAccess;
70 $this->cvsEnclosure = $cvsEnclosure;
79 $this->collectionDocument = $collectionDocument;
89 $this->outputFilePath = $outputFilePath;
98 $this->exportStatusId = $exportStatusId;
106 $this->profileAccountType = $profileAccountType;
115 return $this->outputFilePath;
124 if (strlen($cvsSeparator) !== 1) {
125 throw new Exception(
"EXPC0016", $cvsSeparator);
127 $this->cvsSeparator = $cvsSeparator;
136 $this->exportDocumentNumericIdentiers = $exportDocumentNumericIdentiers;
144 $this->exportFiles = $exportFiles;
152 $this->exportProfil = $exportProfil;
161 $allowedFormat = array(
165 if (!in_array($outputFileEncding, $allowedFormat)) {
166 throw new Exception(
"EXPC0015", $outputFileEncding, implode(
",", $allowedFormat));
168 $this->outputFileEncoding = $outputFileEncding;
177 $allowedFormat = array(
178 self::csvRawOutputFormat,
179 self::csvRawOnlyDataOutputFormat,
180 self::csvDisplayValueOutputFormat,
181 self::xmlArchiveOutputFormat,
182 self::xmlFileOutputFormat
184 if (!in_array($outputFormat, $allowedFormat)) {
185 throw new Exception(
"EXPC0001", $outputFormat, implode(
",", $allowedFormat));
187 $this->outputFormat = $outputFormat;
195 $this->useUserColumnParameter = $useUserColumnParameter;
204 if (empty($this->outputFilePath)) {
207 if (file_exists($this->outputFilePath) && !is_writable($this->outputFilePath)) {
208 throw new Exception(
"EXPC0003", $this->outputFilePath);
210 if ($this->documentList === null) {
214 switch ($this->outputFormat) {
215 case self::csvRawOutputFormat:
216 case self::csvRawOnlyDataOutputFormat:
217 case self::csvDisplayValueOutputFormat:
221 case self::xmlArchiveOutputFormat:
222 case self::xmlFileOutputFormat:
223 $this->exportCollectionXml();
233 $this->documentList = $documentList;
239 protected function exportCsv()
241 $dl = $this->documentList;
242 $exportDoc = new \Dcp\ExportDocument();
243 $exportDoc->setCsvEnclosure($this->cvsEnclosure);
244 $exportDoc->setCsvSeparator($this->cvsSeparator);
245 $exportDoc->setExportAccountType($this->profileAccountType);
246 $exportDoc->setVerifyAttributeAccess($this->verifyAttributeAccess);
248 if ($this->exportFiles) {
249 $outDir = tempnam(
getTmpDir() ,
'exportFolder');
250 if (is_file($outDir)) {
254 $fdlcsv = $outDir .
"/fdl.csv";
255 $this->outHandler = fopen($fdlcsv,
"w");
256 if (!$this->outHandler) {
257 throw new Exception(
"EXPC0012", $fdlcsv);
260 $this->outHandler = fopen($this->outputFilePath,
"w");
261 if (!$this->outHandler) {
262 throw new Exception(
"EXPC0005", $this->outputFilePath);
268 if ($this->exportProfil) {
269 foreach ($dl as
$doc) {
270 $this->
recordStatus(sprintf(_(
"Record documents %d/%d") ,
$c, $rc));
271 if ($doc->doctype ===
"C") {
277 $this->
recordStatus(sprintf(_(
"Record documents %d/%d") ,
$c, $rc));
279 $this->csvFamilyExport($doc);
282 $this->writeFamilies();
284 $fileInfos = array();
285 foreach ($dl as $doc) {
287 if ($doc->doctype !==
"C") {
291 $this->
recordStatus(sprintf(_(
"Record documents %d/%d") ,
$c, $rc));
293 $exportDoc->csvExport($doc, $fileInfos, $this->outHandler, $this->exportProfil, $this->exportFiles, $this->exportDocumentNumericIdentiers, ($this->outputFileEncoding === self::utf8Encoding) , !$this->useUserColumnParameter, $this->outputFormat);
296 fclose($this->outHandler);
298 if ($this->exportFiles) {
299 $this->zipFiles($outDir, $fileInfos);
309 if ($this->exportStatusId) {
313 $warnings = $action->parent->getWarningMsg();
314 if (count($warnings) > 0) {
315 array_unshift($warnings, _(
"Export:Warnings"));
318 $action->parent->clearWarningMsg();
320 $action->register($this->exportStatusId, array(
322 "warnings" => $warnings,
333 $this->moreDocuments[$documentId] =
true;
350 if ($family->profid != $family->id) {
352 $tmoredoc[$fp[
"id"]] =
"famprof";
353 $this->addDocumentToExport($fp[
"id"]);
354 if ($fp[
"name"] !=
"") $fpname = $fp[
"name"];
355 else $fpname = $fp[
"id"];
358 if ($family->cprofid) {
360 if ($cp[
"name"] !=
"") $cpname = $cp[
"name"];
361 else $cpname = $cp[
"id"];
362 $tmoredoc[$cp[
"id"]] =
"cprofid";
363 $this->addDocumentToExport($cp[
"id"]);
365 if ($family->ccvid > 0) {
367 if ($cv[
"name"] !=
"") $cvname = $cv[
"name"];
368 else $cvname = $cv[
"id"];
371 foreach ($tmskid as $kmsk => $imsk) {
375 $tmoredoc[$msk[
"id"]] =
"mask";
376 $this->addDocumentToExport($msk[
"id"]);
381 $tmoredoc[$cv[
"id"]] =
"cv";
382 $this->addDocumentToExport($cv[
"id"]);
385 if ($family->wid > 0) {
386 $wdoc = \new_doc(
$dbaccess, $family->wid);
387 if ($wdoc->name !=
"") $wname = $wdoc->name;
388 else $wname = $wdoc->id;
389 $tattr = $wdoc->getAttributes();
390 foreach ($tattr as $ka => $oa) {
391 if ($oa->type ==
"docid") {
392 $tdid = $wdoc->getMultipleRawValues($ka);
393 foreach ($tdid as $did) {
397 if ($m[
"doctype"] !==
"C") {
398 $tmoredoc[$m[
"initid"]] =
"wrel";
399 $this->addDocumentToExport($m[
"initid"]);
401 if (!empty($m[
"cv_mskid"])) {
403 foreach ($tmskid as $kmsk => $imsk) {
407 $tmoredoc[$msk[
"id"]] =
"wmask";
408 $this->addDocumentToExport($msk[
"initid"]);
413 if (!empty($m[
"tm_tmail"])) {
414 $tmskid = $family->
rawValueToArray(str_replace(
'<BR>',
"\n", $m[
"tm_tmail"]));
415 foreach ($tmskid as $kmsk => $imsk) {
419 $tmoredoc[$msk[
"id"]] =
"tmask";
420 $this->addDocumentToExport($msk[
"initid"]);
431 $tmoredoc[$family->wid] =
"wid";
432 $this->addDocumentToExport($family->wid);
435 if ($cvname || $wname || $cpname || $fpname) {
436 $this->familyData[$family->id][] = array(
446 $this->familyData[$family->id][] = array(
452 $this->familyData[$family->id][] = array(
458 $this->familyData[$family->id][] = array(
463 if ($family->cprofid) {
464 $this->familyData[$family->id][] = array(
469 $this->familyData[$family->id][] = array(
480 $exportDoc = new \Dcp\ExportDocument();
481 $exportDoc->setCsvEnclosure($this->cvsEnclosure);
482 $exportDoc->setCsvSeparator($this->cvsSeparator);
484 $more = new \DocumentList();
485 $more->addDocumentIdentifiers(array_keys($this->moreDocuments));
486 $searchDl = $more->getSearchDocument();
487 $searchDl->setOrder(
"fromid, name, id");
488 foreach ($more as $adoc) {
490 $exportDoc->csvExport($adoc, $fileInfos, $this->outHandler,
false, $this->exportFiles, $this->exportDocumentNumericIdentiers, ($this->outputFileEncoding === self::utf8Encoding) , !$this->useUserColumnParameter, $this->outputFormat);
492 foreach ($more as $adoc) {
494 $exportDoc->exportProfil($this->outHandler, $adoc->id);
497 foreach ($this->familyData as
$famid => $aRow) {
498 $family = \new_doc(
"",
$famid);
499 if ($family->profid == $family->id) {
500 $exportDoc->exportProfil($this->outHandler, $family->id);
504 foreach ($this->familyData as
$famid => $aRow) {
509 protected function zipFiles($directory, array $infos)
512 foreach ($infos as
$info) {
513 $source = $info[
"path"];
514 $ddir = $directory .
'/' . $info[
"ldir"];
515 if (!is_dir($ddir)) mkdir($ddir);
516 $dest = $ddir .
'/' . $info[
"fname"];
517 if (!copy($source,
$dest)) {
522 $zipfile = $this->outputFilePath .
".zip";
523 $cmd = sprintf(
"cd %s && zip -r %s -- * > /dev/null && mv %s %s", escapeshellarg($directory) , escapeshellarg($zipfile) , escapeshellarg($zipfile) , escapeshellarg($this->outputFilePath));
525 if (!is_file($this->outputFilePath)) {
527 throw new Exception(
"EXPC0012", $this->outputFilePath);
530 protected function exportCollectionXml()
532 $dl = $this->documentList;
534 $foutdir = uniqid(
getTmpDir() .
"/exportxml");
535 if (!mkdir($foutdir)) {
536 throw new Exception(
"EXPC0006", $foutdir);
539 VerifyAttributeAccess::clearCache();
541 $exd = new \Dcp\ExportXmlDocument();
543 $exd->setExportFiles($this->exportFiles);
544 $exd->setExportDocumentNumericIdentiers($this->exportDocumentNumericIdentiers);
545 $exd->setStructureAttributes(
true);
546 $exd->setIncludeSchemaReference(
true);
547 $exd->setVerifyAttributeAccess($this->verifyAttributeAccess);
554 foreach ($dl as $doc) {
557 $this->
recordStatus(sprintf(_(
"Record documents %d/%d") ,
$c, $rc));
560 $ftitle = $this->cleanFileName($doc->getTitle());
561 $suffix = sprintf(
"{%d}.xml", $doc->id);
563 $fname = sprintf(
"%s/%s%s", $foutdir, mb_strcut($ftitle, 0, $maxBytesLen,
'UTF-8') , $suffix);
565 $exd->setDocument($doc);
566 $exd->writeTo($fname);
569 if ($this->outputFormat === self::xmlFileOutputFormat) {
570 $this->catXml($foutdir);
571 } elseif ($this->outputFormat === self::xmlArchiveOutputFormat) {
572 $this->zipXml($foutdir);
574 system(sprintf(
"rm -fr %s", escapeshellarg($foutdir)));
583 return str_replace(array(
608 $zipfile = $this->outputFilePath .
".zip";
609 $cmd = sprintf(
"cd %s && zip -r %s -- * > /dev/null && mv %s %s", escapeshellarg($directory) , escapeshellarg($zipfile) , escapeshellarg($zipfile) , escapeshellarg($this->outputFilePath));
612 if (!is_file($this->outputFilePath)) {
614 throw new Exception(
"EXPC0012", $this->outputFilePath);
625 $fout = fopen($this->outputFilePath,
"w");
627 throw new Exception(
"EXPC0005", $this->outputFilePath);
630 <?xml version=
"1.0" encoding=
"UTF-8"?>
631 <documents date=
"%s" author=
"%s" name=
"%s">
634 if ($this->collectionDocument) {
635 $exportname = $this->collectionDocument->getTitle();
637 $exportname = sprintf(
"Custom '\"export");
639 $xml_head = sprintf($xml_head, htmlspecialchars(strftime(
"%FT%T")) , htmlspecialchars(\
Account::getDisplayName(
getCurrentUser()->
id) , ENT_QUOTES) , htmlspecialchars($exportname, ENT_QUOTES));
641 $ret = fwrite($fout, $xml_head);
642 if (
$ret ===
false) {
643 throw new Exception(
"EXPC0005", $this->outputFilePath);
650 $ret = chdir($directory);
651 if (
$ret ===
false) {
653 throw new Exception(
"EXPC0008", $this->outputFilePath);
656 if (count($this->documentList) > 0) {
657 $cmd = sprintf(
"cat -- *xml | grep -v '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' >> %s", escapeshellarg($this->outputFilePath));
662 if (
$ret ===
false) {
667 $ret = fseek($fout, 0, SEEK_END);
670 throw new Exception(
"EXPC0010", $this->outputFilePath);
673 $xml_footer =
"</documents>";
674 $ret = fwrite($fout, $xml_footer);
676 throw new Exception(
"EXPC0011", $this->outputFilePath);
setVerifyAttributeAccess($verifyAttributeAccess)
setExportStatusId($exportStatusId)
getTDoc($dbaccess, $id, $sqlfilters=array(), $result=array())
setProfileAccountType($profileAccountType)
addDocumentToExport($documentId)
csvFamilyExport(\DocFam $family)
print $fam getTitle() $fam name
static rawValueToArray($v)
Exception class use exceptionCode to identifiy correctly exception.
setOutputFileEncoding($outputFileEncding)
static cleanFileName($fileName)
setOutputFormat($outputFormat)
recordStatus($msg, $endStatus=false)
recordStatus(Action &$action, $exportId, $msg, $endStatus=false)
foreach($argv as $arg) $cmd
setExportFiles($exportFiles)
setDocumentlist(\DocumentList $documentList)
setExportDocumentNumericIdentiers($exportDocumentNumericIdentiers)
setUseUserColumnParameter($useUserColumnParameter)
zipFiles($directory, array $infos)
setCvsSeparator($cvsSeparator)
static getDisplayName($uid)
setCollectionDocument(\DocCollection $collectionDocument)
setCvsEnclosure($cvsEnclosure)
if($_REQUEST['submitted']) else $cwd
setExportProfil($exportProfil)
static fput($handler, array $data)
setOutputFilePath($outputFilePath)
class use to search documents