.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Functions;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Census\Census;
use Fisharebest\Webtrees\Config;
use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\GedcomCode\GedcomCodeAdop;
use Fisharebest\Webtrees\GedcomCode\GedcomCodeName;
use Fisharebest\Webtrees\GedcomCode\GedcomCodePedi;
use Fisharebest\Webtrees\GedcomCode\GedcomCodeQuay;
use Fisharebest\Webtrees\GedcomCode\GedcomCodeRela;
use Fisharebest\Webtrees\GedcomCode\GedcomCodeStat;
use Fisharebest\Webtrees\GedcomCode\GedcomCodeTemp;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\GedcomTag;
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Module\CensusAssistantModule;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\Services\UserService;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Tree;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Request;
/**
* Class FunctionsEdit - common functions for editing
*/
class FunctionsEdit
{
/** @var string[] - a list of GEDCOM tags in the edit form. */
private static $tags = [];
/**
* Function edit_language_checkboxes
*
* @param string $parameter_name
* @param array $accepted_languages
*
* @return string
*/
public static function editLanguageCheckboxes($parameter_name, $accepted_languages): string
{
$html = '';
foreach (I18N::activeLocales() as $locale) {
$html .= '
';
$html .= '';
$html .= '
';
}
return $html;
}
/**
* A list of access levels (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsAccessLevels(): array
{
return [
Auth::PRIV_PRIVATE => I18N::translate('Show to visitors'),
Auth::PRIV_USER => I18N::translate('Show to members'),
Auth::PRIV_NONE => I18N::translate('Show to managers'),
Auth::PRIV_HIDE => I18N::translate('Hide from everyone'),
];
}
/**
* A list of active languages (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsActiveLanguages(): array
{
$languages = [];
foreach (I18N::activeLocales() as $locale) {
$languages[$locale->languageTag()] = $locale->endonym();
}
return $languages;
}
/**
* A list of calendar conversions (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsCalendarConversions(): array
{
return ['none' => I18N::translate('No calendar conversion')] + Date::calendarNames();
}
/**
* A list of contact methods (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsContactMethods(): array
{
return [
'messaging' => I18N::translate('Internal messaging'),
'messaging2' => I18N::translate('Internal messaging with emails'),
'messaging3' => I18N::translate('webtrees sends emails with no storage'),
'mailto' => I18N::translate('Mailto link'),
'none' => I18N::translate('No contact'),
];
}
/**
* A list of hide/show options (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsHideShow(): array
{
return [
'0' => I18N::translate('no'),
'1' => I18N::translate('yes'),
];
}
/**
* A list of installed languages (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsInstalledLanguages(): array
{
$languages = [];
foreach (I18N::installedLocales() as $locale) {
$languages[$locale->languageTag()] = $locale->endonym();
}
return $languages;
}
/**
* A list of integers (e.g. for an edit control).
*
* @param int[] $integers
*
* @return string[]
*/
public static function numericOptions($integers): array
{
$array = [];
foreach ($integers as $integer) {
if ($integer === -1) {
$array[$integer] = I18N::translate('All');
} else {
$array[$integer] = I18N::number($integer);
}
}
return $array;
}
/**
* A list of no/yes options (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsNoYes(): array
{
return [
'0' => I18N::translate('no'),
'1' => I18N::translate('yes'),
];
}
/**
* A list of GEDCOM relationships (e.g. for an edit control).
*
* @param string $relationship
*
* @return string[]
*/
public static function optionsRelationships($relationship): array
{
$relationships = GedcomCodeRela::getValues();
// The user is allowed to specify values that aren't in the list.
if (!array_key_exists($relationship, $relationships)) {
$relationships[$relationship] = I18N::translate($relationship);
}
return $relationships;
}
/**
* A list of GEDCOM restrictions for inline data.
*
* @param bool $include_empty
*
* @return string[]
*/
public static function optionsRestrictions($include_empty): array
{
$options = [
'none' => I18N::translate('Show to visitors'),
'privacy' => I18N::translate('Show to members'),
'confidential' => I18N::translate('Show to managers'),
'locked' => I18N::translate('Only managers can edit'),
];
if ($include_empty) {
$options = ['' => ''] + $options;
}
return $options;
}
/**
* A list of GEDCOM restrictions for privacy rules.
*
* @param bool $include_empty
*
* @return string[]
*/
public static function optionsRestrictionsRule(): array
{
$options = [
'none' => I18N::translate('Show to visitors'),
'privacy' => I18N::translate('Show to members'),
'confidential' => I18N::translate('Show to managers'),
'hidden' => I18N::translate('Hide from everyone'),
];
return $options;
}
/**
* A list of temple options (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsTemples(): array
{
return ['' => I18N::translate('No temple - living ordinance')] + GedcomCodeTemp::templeNames();
}
/**
* A list of user options (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsUsers(): array
{
$options = ['' => '-'];
foreach ((new UserService())->all() as $user) {
$options[$user->userName()] = $user->realName() . ' - ' . $user->userName();
}
return $options;
}
/**
* add a new tag input field
* called for each fact to be edited on a form.
* Fact level=0 means a new empty form : data are POSTed by name
* else data are POSTed using arrays :
* glevels[] : tag level
* islink[] : tag is a link
* tag[] : tag name
* text[] : tag value
*
* @param Tree $tree
* @param string $tag fact record to edit (eg 2 DATE xxxxx)
* @param string $upperlevel optional upper level tag (eg BIRT)
* @param string $label An optional label to echo instead of the default
*
* @return string
*/
public static function addSimpleTag(Tree $tree, $tag, $upperlevel = '', $label = ''): string
{
$request = app(Request::class);
$xref = $request->get('xref', '');
// Some form fields need access to previous form fields.
static $previous_ids = [
'SOUR' => '',
'PLAC' => '',
];
preg_match('/^(?:(\d+) (' . Gedcom::REGEX_TAG . ') ?(.*))/', $tag, $match);
[, $level, $fact, $value] = $match;
if ($level === '0') {
if ($upperlevel) {
$name = $upperlevel . '_' . $fact;
} else {
$name = $fact;
}
} else {
$name = 'text[]';
}
$id = $fact . Uuid::uuid4()->toString();
$previous_ids[$fact] = $id;
// field value
$islink = (substr($value, 0, 1) === '@' && substr($value, 0, 2) !== '@#');
if ($islink) {
$value = trim($value, '@');
} else {
$value = (string) substr($tag, strlen($fact) + 3);
}
if ($fact === 'REPO' || $fact === 'SOUR' || $fact === 'OBJE' || $fact === 'FAMC') {
$islink = true;
}
if ($fact === 'SHARED_NOTE_EDIT' || $fact === 'SHARED_NOTE') {
$islink = true;
$fact = 'NOTE';
}
$row_class = 'form-group row';
switch ($fact) {
case 'DATA':
case 'MAP':
// These GEDCOM tags should have no data, just child tags.
if ($value === '') {
$row_class .= ' d-none';
}
break;
case 'LATI':
case 'LONG':
// Indicate that this row is a child of a previous row, so we can expand/collapse them.
$row_class .= ' child_of_' . $previous_ids['PLAC'];
if ($value === '') {
$row_class .= ' collapse';
}
break;
}
$html = '';
$html .= '