/**
* Performs a search and replace
*
* @param Tree $tree
*/
private function searchAndReplace(Tree $tree)
{
$this->generalSearch();
//-- don't try to make any changes if nothing was found
if (!$this->myindilist && !$this->myfamlist && !$this->mysourcelist && !$this->mynotelist) {
return;
}
Log::addEditLog("Search And Replace old:" . $this->query . " new:" . $this->replace);
$adv_name_tags = preg_split("/[\\s,;: ]+/", $tree->getPreference('ADVANCED_NAME_FACTS'));
$name_tags = array_unique(array_merge(Config::standardNameFacts(), $adv_name_tags));
$name_tags[] = '_MARNM';
$records_updated = 0;
foreach ($this->myindilist as $id => $record) {
$old_record = $record->getGedcom();
$new_record = $old_record;
if ($this->replaceAll) {
$new_record = preg_replace("~" . $this->query . "~i", $this->replace, $new_record);
} else {
if ($this->replaceNames) {
foreach ($name_tags as $tag) {
$new_record = preg_replace("~(\\d) " . $tag . " (.*)" . $this->query . "(.*)~i", "\$1 " . $tag . " \$2" . $this->replace . "\$3", $new_record);
}
}
if ($this->replacePlaces) {
if ($this->replacePlacesWord) {
$new_record = preg_replace('~(\\d) PLAC (.*)([,\\W\\s])' . $this->query . '([,\\W\\s])~i', "\$1 PLAC \$2\$3" . $this->replace . "\$4", $new_record);
} else {
$new_record = preg_replace("~(\\d) PLAC (.*)" . $this->query . "(.*)~i", "\$1 PLAC \$2" . $this->replace . "\$3", $new_record);
}
}
}
//-- if the record changed replace the record otherwise remove it from the search results
if ($new_record !== $old_record) {
$record->updateRecord($new_record, true);
$records_updated++;
} else {
unset($this->myindilist[$id]);
}
}
if ($records_updated) {
FlashMessages::addMessage(I18N::plural('%s individual has been updated.', '%s individuals have been updated.', $records_updated, I18N::number($records_updated)));
}
$records_updated = 0;
foreach ($this->myfamlist as $id => $record) {
$old_record = $record->getGedcom();
$new_record = $old_record;
if ($this->replaceAll) {
$new_record = preg_replace("~" . $this->query . "~i", $this->replace, $new_record);
} else {
if ($this->replacePlaces) {
if ($this->replacePlacesWord) {
$new_record = preg_replace('~(\\d) PLAC (.*)([,\\W\\s])' . $this->query . '([,\\W\\s])~i', "\$1 PLAC \$2\$3" . $this->replace . "\$4", $new_record);
} else {
$new_record = preg_replace("~(\\d) PLAC (.*)" . $this->query . "(.*)~i", "\$1 PLAC \$2" . $this->replace . "\$3", $new_record);
}
}
}
//-- if the record changed replace the record otherwise remove it from the search results
if ($new_record !== $old_record) {
$record->updateRecord($new_record, true);
$records_updated++;
} else {
unset($this->myfamlist[$id]);
}
}
if ($records_updated) {
FlashMessages::addMessage(I18N::plural('%s family has been updated.', '%s families have been updated.', $records_updated, I18N::number($records_updated)));
}
$records_updated = 0;
foreach ($this->mysourcelist as $id => $record) {
$old_record = $record->getGedcom();
$new_record = $old_record;
if ($this->replaceAll) {
$new_record = preg_replace("~" . $this->query . "~i", $this->replace, $new_record);
} else {
if ($this->replaceNames) {
$new_record = preg_replace("~(\\d) TITL (.*)" . $this->query . "(.*)~i", "\$1 TITL \$2" . $this->replace . "\$3", $new_record);
$new_record = preg_replace("~(\\d) ABBR (.*)" . $this->query . "(.*)~i", "\$1 ABBR \$2" . $this->replace . "\$3", $new_record);
}
if ($this->replacePlaces) {
if ($this->replacePlacesWord) {
$new_record = preg_replace('~(\\d) PLAC (.*)([,\\W\\s])' . $this->query . '([,\\W\\s])~i', "\$1 PLAC \$2\$3" . $this->replace . "\$4", $new_record);
} else {
$new_record = preg_replace("~(\\d) PLAC (.*)" . $this->query . "(.*)~i", "\$1 PLAC \$2" . $this->replace . "\$3", $new_record);
}
}
}
//-- if the record changed replace the record otherwise remove it from the search results
if ($new_record !== $old_record) {
$record->updateRecord($new_record, true);
$records_updated++;
} else {
unset($this->mysourcelist[$id]);
}
}
if ($records_updated) {
FlashMessages::addMessage(I18N::plural('%s source has been updated.', '%s sources have been updated.', $records_updated, I18N::number($records_updated)));
}
$records_updated = 0;
foreach ($this->mynotelist as $id => $record) {
$old_record = $record->getGedcom();
$new_record = $old_record;
if ($this->replaceAll) {
$new_record = preg_replace("~" . $this->query . "~i", $this->replace, $new_record);
}
//-- if the record changed replace the record otherwise remove it from the search results
if ($new_record != $old_record) {
$record->updateRecord($new_record, true);
$records_updated++;
} else {
unset($this->mynotelist[$id]);
}
}
if ($records_updated) {
FlashMessages::addMessage(I18N::plural('%s note has been updated.', '%s notes have been updated.', $records_updated, I18N::number($records_updated)));
}
}