From 20e63f3dccfe98d4ab4dd355bb5439523de9819e Mon Sep 17 00:00:00 2001 From: Alex Puiu Date: Thu, 24 Feb 2022 17:02:43 +0200 Subject: [PATCH] Adapt displace for XML files. --- app/Http/Controllers/FileController.php | 30 +-------- app/SearchDisplace/SearchAndDisplace.php | 24 +++++++- app/SearchDisplace/SearchAndDisplaceXML.php | 67 +++++++++++++++++++++ 3 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 app/SearchDisplace/SearchAndDisplaceXML.php diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index 4d08c04..e31445e 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -7,7 +7,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Storage; use Illuminate\Http\UploadedFile; use GuzzleHttp\Exception\BadResponseException; -use App\SearchDisplace\SearchAndDisplace; +use App\SearchDisplace\SearchAndDisplaceXML; use Symfony\Component\Process\Process; class FileController extends Controller @@ -75,34 +75,8 @@ class FileController extends Controller $searchers = request()->input('searchers'); $xml = storage_path() . "/app/contracts/{$fileId}/{$fileId}.xml"; - $dom = new \DOMDocument(); - $dom->load($xml); - foreach($dom->getElementsByTagName('p') as $key => $text) { - $is_image = false; - if($text->childNodes) { - foreach($text->childNodes as $child) { - if(isset($child->tagName) && $child->tagName == 'draw:frame') { - $is_image = true; - } - } - } - if(!$is_image) { - $searchAndDisplace = new SearchAndDisplace( - stripslashes($text->textContent), - [ - 'searchers' => $searchers, - ] - ); - - $response = $searchAndDisplace->execute(); - $text->textContent = $response['content']; - } - } - - $dom->save($xml); - $process = new Process(['soffice', '--convert-to', 'odt', $xml, '--outdir', storage_path('app/tmp/')]); - $process->run(); + (new SearchAndDisplaceXML($xml, $searchers))->execute(); return response()->json([ 'path' => 'tmp/' . $fileId . '.odt' diff --git a/app/SearchDisplace/SearchAndDisplace.php b/app/SearchDisplace/SearchAndDisplace.php index f999bc0..de34f2f 100644 --- a/app/SearchDisplace/SearchAndDisplace.php +++ b/app/SearchDisplace/SearchAndDisplace.php @@ -9,12 +9,14 @@ class SearchAndDisplace protected $documentContent; protected $info; protected $searchOnly = false; + protected $isDocument = false; - public function __construct($documentContent, $info, $searchOnly = false) + public function __construct($documentContent, $info, $searchOnly = false, $isDocument = false) { $this->documentContent = $documentContent; $this->info = $info; $this->searchOnly = $searchOnly; + $this->isDocument = $isDocument; } /** @@ -102,6 +104,12 @@ class SearchAndDisplace $updatedDocumentContent = $updatedDocumentContent . substr($this->documentContent, $currentIndex); } + if($this->isDocument) { + $needReplace = $this->needReplaceInDocument($replacementIndexes); + + if(!$needReplace) return false; + } + return [ 'content' => $updatedDocumentContent, 'indexes' => $replacementIndexes, @@ -113,4 +121,18 @@ class SearchAndDisplace // return "<$displaceValue>$content"; return "{{$displaceValue}}{$content}{/{$displaceValue}}"; } + + protected function needReplaceInDocument($replacementIndexes) + { + $needReplace = false; + + foreach($replacementIndexes as $replacement) { + if(count($replacement) > 0 ) { + $needReplace = true; + break; + } + } + + return $needReplace; + } } diff --git a/app/SearchDisplace/SearchAndDisplaceXML.php b/app/SearchDisplace/SearchAndDisplaceXML.php new file mode 100644 index 0000000..379807a --- /dev/null +++ b/app/SearchDisplace/SearchAndDisplaceXML.php @@ -0,0 +1,67 @@ +file = $file; + $this->searchers = $searchers; + } + + public function execute() + { + $this->applySD(); + + $this->convertToOdt(); + } + + protected function convertToOdt() + { + (new Process(['soffice', '--convert-to', 'odt', $this->file, '--outdir', storage_path('app/tmp/')]))->run(); + } + + protected function applySD() + { + $dom = new \DOMDocument(); + $dom->load($this->file); + $replacements = []; + + foreach($dom->getElementsByTagName('p') as $p) { + $is_image = false; + if($p->childNodes) { + foreach($p->childNodes as $child) { + if(isset($child->tagName) && $child->tagName == 'draw:frame') { + $is_image = true; + } + } + } + if(!$is_image) { + $search = new SearchAndDisplace( + stripslashes($p->textContent), + [ + 'searchers' => $this->searchers, + ], + false, + true + ); + + $changed = $search->execute(); + + if(!$changed) { + continue; + } + + $p->textContent = $changed['content']; + } + } + + $dom->save($this->file); + } +}