diff options
Diffstat (limited to 'app/Http/Controllers/AbstractEditController.php')
| -rw-r--r-- | app/Http/Controllers/AbstractEditController.php | 632 |
1 files changed, 323 insertions, 309 deletions
diff --git a/app/Http/Controllers/AbstractEditController.php b/app/Http/Controllers/AbstractEditController.php index 4e40ff2e85..607b295815 100644 --- a/app/Http/Controllers/AbstractEditController.php +++ b/app/Http/Controllers/AbstractEditController.php @@ -23,346 +23,360 @@ use Fisharebest\Webtrees\Tree; /** * Common logic for edit controllers. */ -abstract class AbstractEditController extends AbstractBaseController { - protected $glevels = []; - protected $tag = []; - protected $islink = []; - protected $text = []; +abstract class AbstractEditController extends AbstractBaseController +{ + protected $glevels = []; + protected $tag = []; + protected $islink = []; + protected $text = []; - protected $glevelsSOUR = []; - protected $tagSOUR = []; - protected $islinkSOUR = []; - protected $textSOUR = []; + protected $glevelsSOUR = []; + protected $tagSOUR = []; + protected $islinkSOUR = []; + protected $textSOUR = []; - protected $glevelsRest = []; - protected $tagRest = []; - protected $islinkRest = []; - protected $textRest = []; + protected $glevelsRest = []; + protected $tagRest = []; + protected $islinkRest = []; + protected $textRest = []; - /** - * This function splits the $glevels, $tag, $islink, and $text arrays so that the - * entries associated with a SOUR record are separate from everything else. - * - * Input arrays: - * - $glevels[] - an array of the gedcom level for each line that was edited - * - $tag[] - an array of the tags for each gedcom line that was edited - * - $islink[] - an array of 1 or 0 values to indicate when the text is a link element - * - $text[] - an array of the text data for each line - * - * Output arrays: - * ** For the SOUR record: - * - $glevelsSOUR[] - an array of the gedcom level for each line that was edited - * - $tagSOUR[] - an array of the tags for each gedcom line that was edited - * - $islinkSOUR[] - an array of 1 or 0 values to indicate when the text is a link element - * - $textSOUR[] - an array of the text data for each line - * ** For the remaining records: - * - $glevelsRest[] - an array of the gedcom level for each line that was edited - * - $tagRest[] - an array of the tags for each gedcom line that was edited - * - $islinkRest[] - an array of 1 or 0 values to indicate when the text is a link element - * - $textRest[] - an array of the text data for each line - */ - protected function splitSource() { - $this->glevelsSOUR = []; - $this->tagSOUR = []; - $this->islinkSOUR = []; - $this->textSOUR = []; + /** + * This function splits the $glevels, $tag, $islink, and $text arrays so that the + * entries associated with a SOUR record are separate from everything else. + * + * Input arrays: + * - $glevels[] - an array of the gedcom level for each line that was edited + * - $tag[] - an array of the tags for each gedcom line that was edited + * - $islink[] - an array of 1 or 0 values to indicate when the text is a link element + * - $text[] - an array of the text data for each line + * + * Output arrays: + * ** For the SOUR record: + * - $glevelsSOUR[] - an array of the gedcom level for each line that was edited + * - $tagSOUR[] - an array of the tags for each gedcom line that was edited + * - $islinkSOUR[] - an array of 1 or 0 values to indicate when the text is a link element + * - $textSOUR[] - an array of the text data for each line + * ** For the remaining records: + * - $glevelsRest[] - an array of the gedcom level for each line that was edited + * - $tagRest[] - an array of the tags for each gedcom line that was edited + * - $islinkRest[] - an array of 1 or 0 values to indicate when the text is a link element + * - $textRest[] - an array of the text data for each line + */ + protected function splitSource() + { + $this->glevelsSOUR = []; + $this->tagSOUR = []; + $this->islinkSOUR = []; + $this->textSOUR = []; - $this->glevelsRest = []; - $this->tagRest = []; - $this->islinkRest = []; - $this->textRest = []; + $this->glevelsRest = []; + $this->tagRest = []; + $this->islinkRest = []; + $this->textRest = []; - $inSOUR = false; - $levelSOUR = 0; + $inSOUR = false; + $levelSOUR = 0; - for ($i = 0; $i < count($this->glevels); $i++) { - if ($inSOUR) { - if ($levelSOUR < $this->glevels[$i]) { - $dest = 'S'; - } else { - $inSOUR = false; - $dest = 'R'; - } - } else { - if ($this->tag[$i] === 'SOUR') { - $inSOUR = true; - $levelSOUR = $this->glevels[$i]; - $dest = 'S'; - } else { - $dest = 'R'; - } - } - if ($dest === 'S') { - $this->glevelsSOUR[] = $this->glevels[$i]; - $this->tagSOUR[] = $this->tag[$i]; - $this->islinkSOUR[] = $this->islink[$i]; - $this->textSOUR[] = $this->text[$i]; - } else { - $this->glevelsRest[] = $this->glevels[$i]; - $this->tagRest[] = $this->tag[$i]; - $this->islinkRest[] = $this->islink[$i]; - $this->textRest[] = $this->text[$i]; - } - } - } + for ($i = 0; $i < count($this->glevels); $i++) { + if ($inSOUR) { + if ($levelSOUR < $this->glevels[$i]) { + $dest = 'S'; + } else { + $inSOUR = false; + $dest = 'R'; + } + } else { + if ($this->tag[$i] === 'SOUR') { + $inSOUR = true; + $levelSOUR = $this->glevels[$i]; + $dest = 'S'; + } else { + $dest = 'R'; + } + } + if ($dest === 'S') { + $this->glevelsSOUR[] = $this->glevels[$i]; + $this->tagSOUR[] = $this->tag[$i]; + $this->islinkSOUR[] = $this->islink[$i]; + $this->textSOUR[] = $this->text[$i]; + } else { + $this->glevelsRest[] = $this->glevels[$i]; + $this->tagRest[] = $this->tag[$i]; + $this->islinkRest[] = $this->islink[$i]; + $this->textRest[] = $this->text[$i]; + } + } + } - /** - * Add new GEDCOM lines from the $xxxRest interface update arrays, which - * were produced by the splitSOUR() function. - * See the FunctionsEdit::handle_updatesges() function for details. - * - * @param string $inputRec - * @param string $levelOverride - * - * @return string - */ - protected function updateRest($inputRec, $levelOverride = 'no') { - if (count($this->tagRest) === 0) { - return $inputRec; // No update required - } + /** + * Add new GEDCOM lines from the $xxxRest interface update arrays, which + * were produced by the splitSOUR() function. + * See the FunctionsEdit::handle_updatesges() function for details. + * + * @param string $inputRec + * @param string $levelOverride + * + * @return string + */ + protected function updateRest($inputRec, $levelOverride = 'no') + { + if (count($this->tagRest) === 0) { + return $inputRec; // No update required + } - // Save original interface update arrays before replacing them with the xxxRest ones - $glevelsSave = $this->glevels; - $tagSave = $this->tag; - $islinkSave = $this->islink; - $textSave = $this->text; + // Save original interface update arrays before replacing them with the xxxRest ones + $glevelsSave = $this->glevels; + $tagSave = $this->tag; + $islinkSave = $this->islink; + $textSave = $this->text; - $this->glevels = $this->glevelsRest; - $this->tag = $this->tagRest; - $this->islink = $this->islinkRest; - $this->text = $this->textRest; + $this->glevels = $this->glevelsRest; + $this->tag = $this->tagRest; + $this->islink = $this->islinkRest; + $this->text = $this->textRest; - $myRecord = $this->handleUpdates($inputRec, $levelOverride); // Now do the update + $myRecord = $this->handleUpdates($inputRec, $levelOverride); // Now do the update - // Restore the original interface update arrays (just in case ...) - $this->glevels = $glevelsSave; - $this->tag = $tagSave; - $this->islink = $islinkSave; - $this->text = $textSave; + // Restore the original interface update arrays (just in case ...) + $this->glevels = $glevelsSave; + $this->tag = $tagSave; + $this->islink = $islinkSave; + $this->text = $textSave; - return $myRecord; - } + return $myRecord; + } - /** - * Add new gedcom lines from interface update arrays - * The edit_interface and FunctionsEdit::add_simple_tag function produce the following - * arrays incoming from the $_POST form - * - $glevels[] - an array of the gedcom level for each line that was edited - * - $tag[] - an array of the tags for each gedcom line that was edited - * - $islink[] - an array of 1 or 0 values to tell whether the text is a link element and should be surrounded by @@ - * - $text[] - an array of the text data for each line - * With these arrays you can recreate the gedcom lines like this - * <code>$glevel[0].' '.$tag[0].' '.$text[0]</code> - * There will be an index in each of these arrays for each line of the gedcom - * fact that is being edited. - * If the $text[] array is empty for the given line, then it means that the - * user removed that line during editing or that the line is supposed to be - * empty (1 DEAT, 1 BIRT) for example. To know if the line should be removed - * there is a section of code that looks ahead to the next lines to see if there - * are sub lines. For example we don't want to remove the 1 DEAT line if it has - * a 2 PLAC or 2 DATE line following it. If there are no sub lines, then the line - * can be safely removed. - * - * @param string $newged the new gedcom record to add the lines to - * @param string $levelOverride Override GEDCOM level specified in $glevels[0] - * - * @return string The updated gedcom record - */ - protected function handleUpdates($newged, $levelOverride = 'no') { - if ($levelOverride === 'no' || count($this->glevels) === 0) { - $levelAdjust = 0; - } else { - $levelAdjust = $levelOverride - $this->glevels[0]; - } + /** + * Add new gedcom lines from interface update arrays + * The edit_interface and FunctionsEdit::add_simple_tag function produce the following + * arrays incoming from the $_POST form + * - $glevels[] - an array of the gedcom level for each line that was edited + * - $tag[] - an array of the tags for each gedcom line that was edited + * - $islink[] - an array of 1 or 0 values to tell whether the text is a link element and should be surrounded by @@ + * - $text[] - an array of the text data for each line + * With these arrays you can recreate the gedcom lines like this + * <code>$glevel[0].' '.$tag[0].' '.$text[0]</code> + * There will be an index in each of these arrays for each line of the gedcom + * fact that is being edited. + * If the $text[] array is empty for the given line, then it means that the + * user removed that line during editing or that the line is supposed to be + * empty (1 DEAT, 1 BIRT) for example. To know if the line should be removed + * there is a section of code that looks ahead to the next lines to see if there + * are sub lines. For example we don't want to remove the 1 DEAT line if it has + * a 2 PLAC or 2 DATE line following it. If there are no sub lines, then the line + * can be safely removed. + * + * @param string $newged the new gedcom record to add the lines to + * @param string $levelOverride Override GEDCOM level specified in $glevels[0] + * + * @return string The updated gedcom record + */ + protected function handleUpdates($newged, $levelOverride = 'no') + { + if ($levelOverride === 'no' || count($this->glevels) === 0) { + $levelAdjust = 0; + } else { + $levelAdjust = $levelOverride - $this->glevels[0]; + } - for ($j = 0; $j < count($this->glevels); $j++) { + for ($j = 0; $j < count($this->glevels); $j++) { - // Look for empty SOUR reference with non-empty sub-records. - // This can happen when the SOUR entry is deleted but its sub-records - // were incorrectly left intact. - // The sub-records should be deleted. - if ($this->tag[$j] === 'SOUR' && ($this->text[$j] === '@@' || $this->text[$j] === '')) { - $this->text[$j] = ''; - $k = $j + 1; - while (($k < count($this->glevels)) && ($this->glevels[$k] > $this->glevels[$j])) { - $this->text[$k] = ''; - $k++; - } - } + // Look for empty SOUR reference with non-empty sub-records. + // This can happen when the SOUR entry is deleted but its sub-records + // were incorrectly left intact. + // The sub-records should be deleted. + if ($this->tag[$j] === 'SOUR' && ($this->text[$j] === '@@' || $this->text[$j] === '')) { + $this->text[$j] = ''; + $k = $j + 1; + while (($k < count($this->glevels)) && ($this->glevels[$k] > $this->glevels[$j])) { + $this->text[$k] = ''; + $k++; + } + } - if (trim($this->text[$j]) !== '') { - $pass = true; - } else { - //-- for facts with empty values they must have sub records - //-- this section checks if they have subrecords - $k = $j + 1; - $pass = false; - while (($k < count($this->glevels)) && ($this->glevels[$k] > $this->glevels[$j])) { - if ($this->text[$k] !== '') { - if (($this->tag[$j] !== 'OBJE') || ($this->tag[$k] === 'FILE')) { - $pass = true; - break; - } - } - $k++; - } - } + if (trim($this->text[$j]) !== '') { + $pass = true; + } else { + //-- for facts with empty values they must have sub records + //-- this section checks if they have subrecords + $k = $j + 1; + $pass = false; + while (($k < count($this->glevels)) && ($this->glevels[$k] > $this->glevels[$j])) { + if ($this->text[$k] !== '') { + if (($this->tag[$j] !== 'OBJE') || ($this->tag[$k] === 'FILE')) { + $pass = true; + break; + } + } + $k++; + } + } - //-- if the value is not empty or it has sub lines - //--- then write the line to the gedcom record - //-- we have to let some emtpy text lines pass through... (DEAT, BIRT, etc) - if ($pass) { - $newline = $this->glevels[$j] + $levelAdjust . ' ' . $this->tag[$j]; - if ($this->text[$j] !== '') { - if ($this->islink[$j]) { - $newline .= ' @' . $this->text[$j] . '@'; - } else { - $newline .= ' ' . $this->text[$j]; - } - } - $newged .= "\n" . str_replace("\n", "\n" . (1 + substr($newline, 0, 1)) . ' CONT ', $newline); - } - } + //-- if the value is not empty or it has sub lines + //--- then write the line to the gedcom record + //-- we have to let some emtpy text lines pass through... (DEAT, BIRT, etc) + if ($pass) { + $newline = $this->glevels[$j] + $levelAdjust . ' ' . $this->tag[$j]; + if ($this->text[$j] !== '') { + if ($this->islink[$j]) { + $newline .= ' @' . $this->text[$j] . '@'; + } else { + $newline .= ' ' . $this->text[$j]; + } + } + $newged .= "\n" . str_replace("\n", "\n" . (1 + substr($newline, 0, 1)) . ' CONT ', $newline); + } + } - return $newged; - } + return $newged; + } - /** - * Create a form to add a new fact. - * - * @param Tree $tree - * @param string $fact - * - * @return string - */ - protected function addNewFact(Tree $tree, $fact) { - $FACT = Filter::post($fact); - $DATE = Filter::post($fact . '_DATE'); - $PLAC = Filter::post($fact . '_PLAC'); - if ($DATE || $PLAC || $FACT && $FACT !== 'Y') { - if ($FACT && $FACT !== 'Y') { - $gedrec = "\n1 " . $fact . ' ' . $FACT; - } else { - $gedrec = "\n1 " . $fact; - } - if ($DATE) { - $gedrec .= "\n2 DATE " . $DATE; - } - if ($PLAC) { - $gedrec .= "\n2 PLAC " . $PLAC; + /** + * Create a form to add a new fact. + * + * @param Tree $tree + * @param string $fact + * + * @return string + */ + protected function addNewFact(Tree $tree, $fact) + { + $FACT = Filter::post($fact); + $DATE = Filter::post($fact . '_DATE'); + $PLAC = Filter::post($fact . '_PLAC'); + if ($DATE || $PLAC || $FACT && $FACT !== 'Y') { + if ($FACT && $FACT !== 'Y') { + $gedrec = "\n1 " . $fact . ' ' . $FACT; + } else { + $gedrec = "\n1 " . $fact; + } + if ($DATE) { + $gedrec .= "\n2 DATE " . $DATE; + } + if ($PLAC) { + $gedrec .= "\n2 PLAC " . $PLAC; - if (preg_match_all('/(' . WT_REGEX_TAG . ')/', $tree->getPreference('ADVANCED_PLAC_FACTS'), $match)) { - foreach ($match[1] as $tag) { - $TAG = Filter::post($fact . '_' . $tag); - if ($TAG) { - $gedrec .= "\n3 " . $tag . ' ' . $TAG; - } - } - } - $LATI = Filter::post($fact . '_LATI'); - $LONG = Filter::post($fact . '_LONG'); - if ($LATI || $LONG) { - $gedrec .= "\n3 MAP\n4 LATI " . $LATI . "\n4 LONG " . $LONG; - } - } - if (Filter::postBool('SOUR_' . $fact)) { - return $this->updateSource($gedrec, 2); - } else { - return $gedrec; - } - } elseif ($FACT === 'Y') { - if (Filter::postBool('SOUR_' . $fact)) { - return $this->updateSource("\n1 " . $fact . ' Y', 2); - } else { - return "\n1 " . $fact . ' Y'; - } - } else { - return ''; - } - } + if (preg_match_all('/(' . WT_REGEX_TAG . ')/', $tree->getPreference('ADVANCED_PLAC_FACTS'), $match)) { + foreach ($match[1] as $tag) { + $TAG = Filter::post($fact . '_' . $tag); + if ($TAG) { + $gedrec .= "\n3 " . $tag . ' ' . $TAG; + } + } + } + $LATI = Filter::post($fact . '_LATI'); + $LONG = Filter::post($fact . '_LONG'); + if ($LATI || $LONG) { + $gedrec .= "\n3 MAP\n4 LATI " . $LATI . "\n4 LONG " . $LONG; + } + } + if (Filter::postBool('SOUR_' . $fact)) { + return $this->updateSource($gedrec, 2); + } else { + return $gedrec; + } + } elseif ($FACT === 'Y') { + if (Filter::postBool('SOUR_' . $fact)) { + return $this->updateSource("\n1 " . $fact . ' Y', 2); + } else { + return "\n1 " . $fact . ' Y'; + } + } else { + return ''; + } + } - /** - * Add new GEDCOM lines from the $xxxSOUR interface update arrays, which - * were produced by the splitSOUR() function. - * See the FunctionsEdit::handle_updatesges() function for details. - * - * @param string $inputRec - * @param string $levelOverride - * - * @return string - */ - protected function updateSource($inputRec, $levelOverride = 'no') { - if (count($this->tagSOUR) === 0) { - return $inputRec; // No update required - } + /** + * Add new GEDCOM lines from the $xxxSOUR interface update arrays, which + * were produced by the splitSOUR() function. + * See the FunctionsEdit::handle_updatesges() function for details. + * + * @param string $inputRec + * @param string $levelOverride + * + * @return string + */ + protected function updateSource($inputRec, $levelOverride = 'no') + { + if (count($this->tagSOUR) === 0) { + return $inputRec; // No update required + } - // Save original interface update arrays before replacing them with the xxxSOUR ones - $glevelsSave = $this->glevels; - $tagSave = $this->tag; - $islinkSave = $this->islink; - $textSave = $$this->text; + // Save original interface update arrays before replacing them with the xxxSOUR ones + $glevelsSave = $this->glevels; + $tagSave = $this->tag; + $islinkSave = $this->islink; + $textSave = $$this->text; - $this->glevels = $this->glevelsSOUR; - $this->tag = $this->tagSOUR; - $this->islink = $this->islinkSOUR; - $this->text = $this->textSOUR; + $this->glevels = $this->glevelsSOUR; + $this->tag = $this->tagSOUR; + $this->islink = $this->islinkSOUR; + $this->text = $this->textSOUR; - $myRecord = $this->handleUpdates($inputRec, $levelOverride); // Now do the update + $myRecord = $this->handleUpdates($inputRec, $levelOverride); // Now do the update - // Restore the original interface update arrays (just in case ...) - $this->glevels = $glevelsSave; - $this->tag = $tagSave; - $this->islink = $islinkSave; - $this->text = $textSave; + // Restore the original interface update arrays (just in case ...) + $this->glevels = $glevelsSave; + $this->tag = $tagSave; + $this->islink = $islinkSave; + $this->text = $textSave; - return $myRecord; - } + return $myRecord; + } - /** - * Create a form to add a sex record. - * - * @return string - */ - protected function addNewSex() { - switch (Filter::post('SEX', '[MF]', 'U')) { - case 'M': - return "\n1 SEX M"; - case 'F': - return "\n1 SEX F"; - default: - return "\n1 SEX U"; - } - } + /** + * Create a form to add a sex record. + * + * @return string + */ + protected function addNewSex() + { + switch (Filter::post('SEX', '[MF]', 'U')) { + case 'M': + return "\n1 SEX M"; + case 'F': + return "\n1 SEX F"; + default: + return "\n1 SEX U"; + } + } - /** - * Assemble the pieces of a newly created record into gedcom - * - * @param Tree $tree - * - * @return string - */ - public static function addNewName(Tree $tree) { - $gedrec = "\n1 NAME " . Filter::post('NAME'); + /** + * Assemble the pieces of a newly created record into gedcom + * + * @param Tree $tree + * + * @return string + */ + public static function addNewName(Tree $tree) + { + $gedrec = "\n1 NAME " . Filter::post('NAME'); - $tags = ['NPFX', 'GIVN', 'SPFX', 'SURN', 'NSFX']; + $tags = [ + 'NPFX', + 'GIVN', + 'SPFX', + 'SURN', + 'NSFX', + ]; - if (preg_match_all('/(' . WT_REGEX_TAG . ')/', $tree->getPreference('ADVANCED_NAME_FACTS'), $match)) { - $tags = array_merge($tags, $match[1]); - } + if (preg_match_all('/(' . WT_REGEX_TAG . ')/', $tree->getPreference('ADVANCED_NAME_FACTS'), $match)) { + $tags = array_merge($tags, $match[1]); + } - // Paternal and Polish and Lithuanian surname traditions can also create a _MARNM - $SURNAME_TRADITION = $tree->getPreference('SURNAME_TRADITION'); - if ($SURNAME_TRADITION === 'paternal' || $SURNAME_TRADITION === 'polish' || $SURNAME_TRADITION === 'lithuanian') { - $tags[] = '_MARNM'; - } + // Paternal and Polish and Lithuanian surname traditions can also create a _MARNM + $SURNAME_TRADITION = $tree->getPreference('SURNAME_TRADITION'); + if ($SURNAME_TRADITION === 'paternal' || $SURNAME_TRADITION === 'polish' || $SURNAME_TRADITION === 'lithuanian') { + $tags[] = '_MARNM'; + } - foreach (array_unique($tags) as $tag) { - $TAG = Filter::post($tag); - if ($TAG) { - $gedrec .= "\n2 {$tag} {$TAG}"; - } - } + foreach (array_unique($tags) as $tag) { + $TAG = Filter::post($tag); + if ($TAG) { + $gedrec .= "\n2 {$tag} {$TAG}"; + } + } - return $gedrec; - } + return $gedrec; + } } |
