Browse Source

Adapt displace for XML files.

master
Alex Puiu 2 years ago
parent
commit
20e63f3dcc
  1. 30
      app/Http/Controllers/FileController.php
  2. 24
      app/SearchDisplace/SearchAndDisplace.php
  3. 67
      app/SearchDisplace/SearchAndDisplaceXML.php

30
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'

24
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</$displaceValue>";
return "{{$displaceValue}}{$content}{/{$displaceValue}}";
}
protected function needReplaceInDocument($replacementIndexes)
{
$needReplace = false;
foreach($replacementIndexes as $replacement) {
if(count($replacement) > 0 ) {
$needReplace = true;
break;
}
}
return $needReplace;
}
}

67
app/SearchDisplace/SearchAndDisplaceXML.php

@ -0,0 +1,67 @@
<?php
namespace App\SearchDisplace;
use Symfony\Component\Process\Process;
class SearchAndDisplaceXML
{
protected $file;
protected $searchers;
public function __construct($file, $searchers)
{
$this->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);
}
}
Loading…
Cancel
Save