Alex Puiu
2 years ago
3 changed files with 92 additions and 29 deletions
-
30app/Http/Controllers/FileController.php
-
24app/SearchDisplace/SearchAndDisplace.php
-
67app/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); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue