diff options
Diffstat (limited to 'app/Module/BatchUpdate/BatchUpdateMarriedNamesPlugin.php')
| -rw-r--r-- | app/Module/BatchUpdate/BatchUpdateMarriedNamesPlugin.php | 283 |
1 files changed, 150 insertions, 133 deletions
diff --git a/app/Module/BatchUpdate/BatchUpdateMarriedNamesPlugin.php b/app/Module/BatchUpdate/BatchUpdateMarriedNamesPlugin.php index 6da5545e91..c91a507008 100644 --- a/app/Module/BatchUpdate/BatchUpdateMarriedNamesPlugin.php +++ b/app/Module/BatchUpdate/BatchUpdateMarriedNamesPlugin.php @@ -24,156 +24,173 @@ use Symfony\Component\HttpFoundation\Request; /** * Class BatchUpdateMarriedNamesPlugin Batch Update plugin: add missing 2 _MARNM records */ -class BatchUpdateMarriedNamesPlugin extends BatchUpdateBasePlugin { - /** @var string User option: add or replace husband’s surname */ - private $surname; +class BatchUpdateMarriedNamesPlugin extends BatchUpdateBasePlugin +{ + /** @var string User option: add or replace husband’s surname */ + private $surname; - /** - * User-friendly name for this plugin. - * - * @return string - */ - public function getName() { - return I18N::translate('Add missing married names'); - } + /** + * User-friendly name for this plugin. + * + * @return string + */ + public function getName() + { + return I18N::translate('Add missing married names'); + } - /** - * Description / help-text for this plugin. - * - * @return string - */ - public function getDescription() { - return I18N::translate('You can make it easier to search for married women by recording their married name. However not all women take their husband’s surname, so beware of introducing incorrect information into your database.'); - } + /** + * Description / help-text for this plugin. + * + * @return string + */ + public function getDescription() + { + return I18N::translate('You can make it easier to search for married women by recording their married name. However not all women take their husband’s surname, so beware of introducing incorrect information into your database.'); + } - /** - * Does this record need updating? - * - * @param GedcomRecord $record - * - * @return bool - */ - public function doesRecordNeedUpdate(GedcomRecord $record): bool { - $gedcom = $record->getGedcom(); + /** + * Does this record need updating? + * + * @param GedcomRecord $record + * + * @return bool + */ + public function doesRecordNeedUpdate(GedcomRecord $record): bool + { + $gedcom = $record->getGedcom(); - return preg_match('/^1 SEX F/m', $gedcom) && preg_match('/^1 NAME /m', $gedcom) && $this->surnamesToAdd($record); - } + return preg_match('/^1 SEX F/m', $gedcom) && preg_match('/^1 NAME /m', $gedcom) && $this->surnamesToAdd($record); + } - /** - * Apply any updates to this record - * - * @param GedcomRecord $record - * - * @return string - */ - public function updateRecord(GedcomRecord $record): string { - $old_gedcom = $record->getGedcom(); - $tree = $record->getTree(); + /** + * Apply any updates to this record + * + * @param GedcomRecord $record + * + * @return string + */ + public function updateRecord(GedcomRecord $record): string + { + $old_gedcom = $record->getGedcom(); + $tree = $record->getTree(); - $SURNAME_TRADITION = $tree->getPreference('SURNAME_TRADITION'); + $SURNAME_TRADITION = $tree->getPreference('SURNAME_TRADITION'); - preg_match('/^1 NAME (.*)/m', $old_gedcom, $match); - $wife_name = $match[1]; - $married_names = []; - foreach ($this->surnamesToAdd($record) as $surname) { - switch ($this->surname) { - case 'add': - $married_names[] = "\n2 _MARNM " . str_replace('/', '', $wife_name) . ' /' . $surname . '/'; - break; - case 'replace': - if ($SURNAME_TRADITION === 'polish') { - $surname = preg_replace(['/ski$/', '/cki$/', '/dzki$/'], ['ska', 'cka', 'dzka'], $surname); - } - $married_names[] = "\n2 _MARNM " . preg_replace('!/.*/!', '/' . $surname . '/', $wife_name); - break; - } - } + preg_match('/^1 NAME (.*)/m', $old_gedcom, $match); + $wife_name = $match[1]; + $married_names = []; + foreach ($this->surnamesToAdd($record) as $surname) { + switch ($this->surname) { + case 'add': + $married_names[] = "\n2 _MARNM " . str_replace('/', '', $wife_name) . ' /' . $surname . '/'; + break; + case 'replace': + if ($SURNAME_TRADITION === 'polish') { + $surname = preg_replace([ + '/ski$/', + '/cki$/', + '/dzki$/', + ], [ + 'ska', + 'cka', + 'dzka', + ], $surname); + } + $married_names[] = "\n2 _MARNM " . preg_replace('!/.*/!', '/' . $surname . '/', $wife_name); + break; + } + } - $new_gedcom = preg_replace('/(^1 NAME .*([\r\n]+[2-9].*)*)/m', '\\1' . implode('', $married_names), $old_gedcom, 1); + $new_gedcom = preg_replace('/(^1 NAME .*([\r\n]+[2-9].*)*)/m', '\\1' . implode('', $married_names), $old_gedcom, 1); - return $new_gedcom; - } + return $new_gedcom; + } - /** - * Generate a list of married surnames that are not already present. - * - * @param GedcomRecord $record - * - * @return string[] - */ - private function surnamesToAdd(GedcomRecord $record): array { - $gedcom = $record->getGedcom(); - $tree = $record->getTree(); + /** + * Generate a list of married surnames that are not already present. + * + * @param GedcomRecord $record + * + * @return string[] + */ + private function surnamesToAdd(GedcomRecord $record): array + { + $gedcom = $record->getGedcom(); + $tree = $record->getTree(); - $wife_surnames = $this->surnames($record); - $husb_surnames = []; - $missing_surnames = []; + $wife_surnames = $this->surnames($record); + $husb_surnames = []; + $missing_surnames = []; - preg_match_all('/^1 FAMS @(.+)@/m', $gedcom, $fmatch); - foreach ($fmatch[1] as $famid) { - $family = Family::getInstance($famid, $tree); - $famrec = $family->getGedcom(); + preg_match_all('/^1 FAMS @(.+)@/m', $gedcom, $fmatch); + foreach ($fmatch[1] as $famid) { + $family = Family::getInstance($famid, $tree); + $famrec = $family->getGedcom(); - if (preg_match('/^1 MARR/m', $famrec) && preg_match('/^1 HUSB @(.+)@/m', $famrec, $hmatch)) { - $spouse = Individual::getInstance($hmatch[1], $tree); - $husb_surnames = array_unique(array_merge($husb_surnames, $this->surnames($spouse))); - } - } - foreach ($husb_surnames as $husb_surname) { - if (!in_array($husb_surname, $wife_surnames)) { - $missing_surnames[] = $husb_surname; - } - } + if (preg_match('/^1 MARR/m', $famrec) && preg_match('/^1 HUSB @(.+)@/m', $famrec, $hmatch)) { + $spouse = Individual::getInstance($hmatch[1], $tree); + $husb_surnames = array_unique(array_merge($husb_surnames, $this->surnames($spouse))); + } + } + foreach ($husb_surnames as $husb_surname) { + if (!in_array($husb_surname, $wife_surnames)) { + $missing_surnames[] = $husb_surname; + } + } - return $missing_surnames; - } + return $missing_surnames; + } - /** - * Extract a list of surnames from a GEDCOM record. - * - * @param GedcomRecord $record - * - * @return string[] - */ - private function surnames(GedcomRecord $record): array { - $gedcom = $record->getGedcom(); + /** + * Extract a list of surnames from a GEDCOM record. + * + * @param GedcomRecord $record + * + * @return string[] + */ + private function surnames(GedcomRecord $record): array + { + $gedcom = $record->getGedcom(); - if (preg_match_all('/^(?:1 NAME|2 _MARNM) .*\/(.+)\//m', $gedcom, $match)) { - return $match[1]; - } else { - return []; - } - } + if (preg_match_all('/^(?:1 NAME|2 _MARNM) .*\/(.+)\//m', $gedcom, $match)) { + return $match[1]; + } else { + return []; + } + } - /** - * Process the user-supplied options. - * - * @param Request $request - */ - public function getOptions(Request $request) { - parent::getOptions($request); + /** + * Process the user-supplied options. + * + * @param Request $request + */ + public function getOptions(Request $request) + { + parent::getOptions($request); - $this->surname = $request->get('surname', 'replace'); - } + $this->surname = $request->get('surname', 'replace'); + } - /** - * Generate a form to ask the user for options. - * - * @return string - */ - public function getOptionsForm() { - return - '<div class="row form-group">' . - '<label class="col-sm-3 col-form-label">' . I18N::translate('Surname option') . '</label>' . - '<div class="col-sm-9">' . - '<select class="form-control" name="surname" onchange="this.form.submit();">' . - '<option value="replace" ' . - ($this->surname == 'replace' ? 'selected' : '') . - '">' . I18N::translate('Wife’s surname replaced by husband’s surname') . '</option><option value="add" ' . - ($this->surname == 'add' ? 'selected' : '') . - '">' . I18N::translate('Wife’s maiden surname becomes new given name') . '</option>' . - '</select>' . - '</div></div>' . - parent::getOptionsForm(); - } + /** + * Generate a form to ask the user for options. + * + * @return string + */ + public function getOptionsForm() + { + return + '<div class="row form-group">' . + '<label class="col-sm-3 col-form-label">' . I18N::translate('Surname option') . '</label>' . + '<div class="col-sm-9">' . + '<select class="form-control" name="surname" onchange="this.form.submit();">' . + '<option value="replace" ' . + ($this->surname == 'replace' ? 'selected' : '') . + '">' . I18N::translate('Wife’s surname replaced by husband’s surname') . '</option><option value="add" ' . + ($this->surname == 'add' ? 'selected' : '') . + '">' . I18N::translate('Wife’s maiden surname becomes new given name') . '</option>' . + '</select>' . + '</div></div>' . + parent::getOptionsForm(); + } } |
