|
|
@ -6,6 +6,8 @@ use App\SearchDisplace\Documents\DocumentFile; |
|
|
|
use Illuminate\Support\Facades\Storage; |
|
|
|
use App\SearchDisplace\Convertor\Convertor; |
|
|
|
use DOMDocument; |
|
|
|
use DOMNode; |
|
|
|
use DOMText; |
|
|
|
|
|
|
|
class SearchAndDisplaceXML |
|
|
|
{ |
|
|
@ -58,24 +60,24 @@ class SearchAndDisplaceXML |
|
|
|
$dom->load($filePath . "/document.xml"); |
|
|
|
|
|
|
|
foreach($dom->getElementsByTagName('p') as $p) { |
|
|
|
if(count($p->childNodes) > 0 && isset($p->parentNode->tagName) && $p->parentNode->tagName !== "table:table-cell") { |
|
|
|
if( |
|
|
|
!$p instanceof DOMText && |
|
|
|
count($p->childNodes) > 0 && |
|
|
|
isset($p->parentNode->tagName) && |
|
|
|
$p->parentNode->tagName !== "table:table-cell" |
|
|
|
) { |
|
|
|
$replacements = []; |
|
|
|
foreach($p->childNodes as $child) { |
|
|
|
if(isset($child->tagName) && $child->tagName === "text:span") { |
|
|
|
$content = trim($child->textContent); |
|
|
|
if($content == '') { |
|
|
|
if (in_array($child, $replacements)) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
$this->replace($content, $child, $dom); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
$content = trim($p->textContent); |
|
|
|
if($content == '') { |
|
|
|
if (!$child instanceof DOMText) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
$this->replace($content, $p, $dom); |
|
|
|
$replacements = array_merge($replacements, $this->replace($child, $dom)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -87,13 +89,16 @@ class SearchAndDisplaceXML |
|
|
|
/** |
|
|
|
* Apply SD on document's paragraph |
|
|
|
* |
|
|
|
* @param string $content paragraph content |
|
|
|
* @param $element DOM element |
|
|
|
* @param DOMNode $element DOM element |
|
|
|
* @param DOMDocument $dom The document |
|
|
|
* |
|
|
|
* @return void |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
protected function replace($content, $element, $dom) |
|
|
|
protected function replace(DOMText &$element, DOMDocument &$dom) |
|
|
|
{ |
|
|
|
/** @var string $content */ |
|
|
|
$content = $element->textContent ?? $element->nodeValue; |
|
|
|
|
|
|
|
$search = new SearchAndDisplace( |
|
|
|
stripslashes($content), |
|
|
|
[ |
|
|
@ -105,6 +110,8 @@ class SearchAndDisplaceXML |
|
|
|
|
|
|
|
$changed = $search->execute(); |
|
|
|
|
|
|
|
$replacementNodes = []; |
|
|
|
|
|
|
|
if($changed) { |
|
|
|
if($this->searchOnly) { |
|
|
|
$content = $element->textContent; |
|
|
@ -114,23 +121,35 @@ class SearchAndDisplaceXML |
|
|
|
$indexes = $changed['indexes']; |
|
|
|
} |
|
|
|
|
|
|
|
foreach(array_keys($indexes) as $searcher) { |
|
|
|
if(empty($indexes[$searcher])) { |
|
|
|
foreach($indexes as $searcher => $changes) { |
|
|
|
if(empty($changes)) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
foreach($indexes[$searcher] as $change) { |
|
|
|
$first = substr($content, 0, $change['start']); |
|
|
|
$changed = substr($content, $change['start'], $change['end'] - $change['start'] + 1); |
|
|
|
$last = substr($content, $change['end'] + 1); |
|
|
|
foreach($changes as $change) { |
|
|
|
$firstContent = substr($content, 0, $change['start']); |
|
|
|
$changedContent = substr($content, $change['start'], $change['end'] - $change['start'] + 1); |
|
|
|
$lastContent = substr($content, $change['end'] + 1); |
|
|
|
|
|
|
|
$element->textContent = $first; |
|
|
|
$changed = $dom->createElement("text:span", $changed); |
|
|
|
$last = $dom->createElement("text:span", $last); |
|
|
|
//text:style-name="mark"
|
|
|
|
$changed->setAttribute('text:style-name', 'mark'); |
|
|
|
$element->appendChild($changed); |
|
|
|
$element->appendChild($last); |
|
|
|
|
|
|
|
// $firstNode = $dom->createElement("text:span", $firstContent);
|
|
|
|
$element->textContent = $firstContent; |
|
|
|
|
|
|
|
$changedNode = $dom->createElement("text:span", $changedContent); |
|
|
|
$changedNode->setAttribute('text:style-name', 'mark'); |
|
|
|
|
|
|
|
$lastNode = $dom->createElement("text:span", $lastContent); |
|
|
|
|
|
|
|
// Add the changed and last nodes after the current (element) node
|
|
|
|
// $element->parentNode->insertBefore($firstNode, $element->nextSibling);
|
|
|
|
|
|
|
|
# element->parentNode->insertBefore(... $element->nextSibling) inserts a new node before the node AFTER this one
|
|
|
|
# So we need to add the `last` node first, and then the `changed` node BEFORE the last.
|
|
|
|
$element->parentNode->insertBefore($lastNode, $element->nextSibling); |
|
|
|
$element->parentNode->insertBefore($changedNode, $element->nextSibling); |
|
|
|
|
|
|
|
$replacementNodes[] = $changedNode; |
|
|
|
$replacementNodes[] = $lastNode; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -139,6 +158,8 @@ class SearchAndDisplaceXML |
|
|
|
} |
|
|
|
$this->markedStyleCreated = true; |
|
|
|
} |
|
|
|
|
|
|
|
return $replacementNodes; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|