Commit bdab6e07 authored by Florian Azizian's avatar Florian Azizian

FEAT #7546 RE add fileNameStructure option in fileImport module

parent 940d7955
This diff is collapsed.
......@@ -16,6 +16,10 @@
<input name="MoveDirectory">/opt/maarch/MaarchCapture/files/TEST_IMPORT/backup/</input>
<input name="Recursive">0</input>
<input name="CreateFolders">0</input>
<input name="Extensions"></input>
<input name="NbMaxFoldersToImport">0</input>
<input name="DeleteSubFolders">false</input>
<input name="fileNameStructure">[destination]_[barcode]</input><!-- extension is automatically set in extension metadata -->
</step>
<!-- REST SAMPLE -->
......
<?php
class FileImport
extends DOMXPath
class FileImport extends DOMXPath
{
private $Batch;
private $Target = "File";
......@@ -14,7 +13,7 @@ class FileImport
private $DeleteSubFolders = false;
private $SubFoldersToDel = array();
function __construct()
public function __construct()
{
$this->Batch = $_SESSION['capture']->Batch;
$Config = new DOMDocument();
......@@ -24,25 +23,27 @@ class FileImport
parent::__construct($Config);
}
function ImportFiles(
public function ImportFiles(
$Directory,
$Target='File',
$Action='none',
$MoveDirectory=false,
$Recursive=false,
$CreateFolders=false,
$Extensions='',
$Target = 'File',
$Action = 'none',
$MoveDirectory = false,
$Recursive = false,
$CreateFolders = false,
$Extensions = '',
$NbMaxFoldersToImport = 0,
$DeleteSubFolders = false
) {
$this->Target = $Target;
$this->Action = $Action;
$this->MoveDirectory = $MoveDirectory;
$this->Recursive = $Recursive;
$this->CreateFolders = $CreateFolders;
$this->Extensions = $Extensions;
$DeleteSubFolders = false,
$fileNameStructure = ""
) {
$this->Target = $Target;
$this->Action = $Action;
$this->MoveDirectory = $MoveDirectory;
$this->Recursive = $Recursive;
$this->CreateFolders = $CreateFolders;
$this->Extensions = $Extensions;
$this->NbMaxFoldersToImport = $NbMaxFoldersToImport;
$this->DeleteSubFolders = $DeleteSubFolders;
$this->DeleteSubFolders = $DeleteSubFolders;
$this->fileNameStructure = $fileNameStructure;
$_SESSION['capture']->logEvent(
"Scanning directory $Directory for file import..."
......@@ -52,7 +53,7 @@ class FileImport
$_SESSION['capture']->sendError("$Directory not exists");
}
$result =
$result =
$this->ScanDirectory(
$Directory,
$this->Batch
......@@ -71,7 +72,7 @@ class FileImport
return $result;
}
function ScanDirectory(
public function ScanDirectory(
$Directory,
$Parent
) {
......@@ -80,9 +81,10 @@ class FileImport
********************************************************************************/
$dirhdl = opendir($Directory);
if(!$dirhdl) {
if (!$dirhdl) {
$_SESSION['capture']->logEvent(
"Unable to open directory '$Directory' !", 2
"Unable to open directory '$Directory' !",
2
);
trigger_error(
"Unable to open directory '$Directory' !",
......@@ -90,11 +92,11 @@ class FileImport
);
}
if($this->CreateFolders) {
if ($this->CreateFolders) {
$_SESSION['capture']->logEvent(
"Adding Folder with path '$Directory'"
);
$Container =
$Container =
$Parent->addFolder(
$Directory
);
......@@ -103,18 +105,19 @@ class FileImport
}
$nbDir = 0;
while($entry_name = readdir($dirhdl)) {
while ($entry_name = readdir($dirhdl)) {
$entry_path = $Directory . DIRECTORY_SEPARATOR . $entry_name;
/* not a file or sub folder
********************************************************************************/
if($entry_name == '.' || $entry_name == '..')
if ($entry_name == '.' || $entry_name == '..') {
continue;
}
/* sub folder, process recursively if requested
********************************************************************************/
if(is_dir($entry_path)) {
if($this->Recursive) {
if (is_dir($entry_path)) {
if ($this->Recursive) {
$nbDir++;
if ($nbDir <= $this->NbMaxFoldersToImport) {
array_push($this->SubFoldersToDel, $entry_path);
......@@ -133,16 +136,16 @@ class FileImport
********************************************************************************/
$extArr = array();
if (
is_string($this->Extensions) &&
isset($this->Extensions) &&
$this->Extensions <> ''
is_string($this->Extensions) &&
isset($this->Extensions) &&
$this->Extensions <> ''
) {
$extArr = explode(' ', strtolower($this->Extensions));
$extArr = explode(' ', strtolower($this->Extensions));
}
$entry_ext = substr(strrchr($entry_path , '.'), 1);
if(count($extArr) > 0) {
if(!in_array(strtolower($entry_ext), $extArr)) {
echo 'discard ' . $entry_path . PHP_EOL;
$entry_ext = substr(strrchr($entry_path, '.'), 1);
if (count($extArr) > 0) {
if (!in_array(strtolower($entry_ext), $extArr)) {
echo 'discard ' . $entry_path . PHP_EOL;
$this->discard(
$entry_path,
$entry_name
......@@ -167,38 +170,46 @@ class FileImport
"Adding ".$this->Target." with source '$entry_path'"
);
switch($this->Target) {
switch ($this->Target) {
case 'Document':
$Content =
$Container->addDocument(
$entry_path
);
$Content =
$Container->addDocument(
$entry_path
);
break;
case 'File':
default:
$Content =
$Container->addFile(
$entry_path
);
$Content =
$Container->addFile(
$entry_path
);
break;
}
if (!$Content) {
//do nothing ! The resource will be processed next batch
} else {
$this->discard($entry_path, $entry_name);
}
if (!$Content) {
//do nothing ! The resource will be processed next batch
} else {
$this->discard($entry_path, $entry_name);
if (!empty($this->fileNameStructure)) {
preg_match_all('/(?<=\[)(.*?)(?=\])/m', $this->fileNameStructure, $matches, PREG_PATTERN_ORDER);
$pathInfo = pathinfo($entry_name);
$filenameValues = explode("_", $pathInfo['filename']);
foreach ($filenameValues as $key => $value) {
$Content->setMetadata($matches[0][$key], $value);
}
$Content->setMetadata('extension', $pathInfo['extension']);
}
}
}
}
function discard(
public function discard(
$entry_path,
$entry_name
) {
/********************************************************************************
** Original File action
********************************************************************************/
********************************************************************************/
switch ($this->Action) {
case 'move':
$_SESSION['capture']->logEvent(
......@@ -218,7 +229,6 @@ class FileImport
default:
// Nothing
}
}
/**
......@@ -227,7 +237,7 @@ class FileImport
* @param $delay
* @param $pointer position in the file
*/
function isCompleteFile($file, $delay=200, $pointer=0)
public function isCompleteFile($file, $delay = 200, $pointer = 0)
{
if ($file == null) {
return false;
......@@ -247,5 +257,4 @@ class FileImport
return $this->isCompleteFile($file, $delay, $currentPos);
}
}
}
......@@ -23,7 +23,7 @@ class MaarchWSClient extends DOMXPath
$Config->load(
__DIR__ . DIRECTORY_SEPARATOR . "MaarchWSClient.xml"
);
parent::__construct($Config);
parent::__construct($Config);
}
}
......@@ -432,7 +432,7 @@ class MaarchWSClient extends DOMXPath
}
if (
isset($argValue['value']) &&
isset($argValue['value']) &&
is_array($argValue['value']) &&
empty($argValue['value'])
) {
......@@ -534,7 +534,7 @@ class MaarchWSClient extends DOMXPath
$argContentValue = $this->parseArgument($argContent, $Element);
$argContentHasValue = $argContentIsArray = false;
if (count($argValue[$argContentName]) > 0) {
if (!empty($argValue[$argContentName]) && count($argValue[$argContentName]) > 0) {
$argContentHasValue = true;
}
if (isset($argValue[$argContentName][0])) {
......@@ -688,7 +688,6 @@ class MaarchWSClient extends DOMXPath
if (isset($entity['errors'][0])) {
$errorDetails = $entity['errors'][0];
fwrite($f, $errorDetails);
//var_dump($errorDetails);
} else {
fwrite($f, print_r($entity, true));
}
......
......@@ -75,7 +75,11 @@ class MailCapture
) {
$errors = array();
$errors = imap_errors();
$num_errors = count($errors);
if (!empty($errors)) {
$num_errors = count($errors);
} else {
$num_errors = 0;
}
$actual_errors = 0;
$clearError = false;
for($i=0; $i<$num_errors; $i++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment