.
*/
namespace Fisharebest\Webtrees\Functions;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Bootstrap4;
use Fisharebest\Webtrees\Census\Census;
use Fisharebest\Webtrees\Census\CensusOfCzechRepublic;
use Fisharebest\Webtrees\Census\CensusOfDenmark;
use Fisharebest\Webtrees\Census\CensusOfDeutschland;
use Fisharebest\Webtrees\Census\CensusOfEngland;
use Fisharebest\Webtrees\Census\CensusOfFrance;
use Fisharebest\Webtrees\Census\CensusOfScotland;
use Fisharebest\Webtrees\Census\CensusOfUnitedStates;
use Fisharebest\Webtrees\Census\CensusOfWales;
use Fisharebest\Webtrees\Config;
use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\FontAwesome;
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;
use Fisharebest\Webtrees\Module\CensusAssistantModule;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Select2;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\User;
use Fisharebest\Webtrees\View;
use Ramsey\Uuid\Uuid;
/**
* Class FunctionsEdit - common functions for editing
*/
class FunctionsEdit {
/**
* Function edit_language_checkboxes
*
* @param string $parameter_name
* @param array $accepted_languages
*
* @return string
*/
public static function editLanguageCheckboxes($parameter_name, $accepted_languages) {
$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() {
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() {
$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() {
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() {
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() {
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() {
$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 = [];
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() {
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) {
$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 registration rules (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsRegistrationRules() {
return [
0 => I18N::translate('No predefined text'),
1 => I18N::translate('Predefined text that states all users can request a user account'),
2 => I18N::translate('Predefined text that states admin will decide on each request for a user account'),
3 => I18N::translate('Predefined text that states only family members can request a user account'),
4 => I18N::translate('Choose user defined welcome text typed below'),
];
}
/**
* A list of GEDCOM restrictions (e.g. for an edit control).
*
* @param bool $include_empty
*
* @return string[]
*/
public static function optionsRestrictions($include_empty) {
$options = [
'none' => I18N::translate('Show to visitors'), // Not valid GEDCOM, but very useful
'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 mail transport options (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsMailTransports() {
return [
'internal' => I18N::translate('Use PHP mail to send messages'),
'external' => I18N::translate('Use SMTP to send messages'),
];
}
/**
* A list SSL modes (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsSslModes() {
return [
'none' => I18N::translate('none'),
/* I18N: Secure Sockets Layer - a secure communications protocol*/ 'ssl' => I18N::translate('ssl'),
/* I18N: Transport Layer Security - a secure communications protocol */ 'tls' => I18N::translate('tls'),
];
}
/**
* A list of temple options (e.g. for an edit control).
*
* @return string[]
*/
public static function optionsTemples() {
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() {
$options = ['' => '-'];
foreach (User::all() as $user) {
$options[$user->getUserName()] = $user->getRealName() . ' - ' . $user->getUserName();
}
return $options;
}
/**
* Create a form control to select a family.
*
* @param Family|null $family
* @param string[] $attributes
*
* @return string
*/
public static function formControlFamily(Family $family = null, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($family !== null) {
$value = $family->getXref();
$options = [$value => View::make('selects/family', ['family' => $family])];
}
return Bootstrap4::select($options, $value, Select2::familyConfig() + $attributes);
}
/**
* Create a form control to select a flag.
*
* @param string $flag
* @param string[] $attributes
*
* @return string
*/
public static function formControlFlag($flag, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($flag !== '') {
$value = $flag;
$options = [$value => Select2::flagValue($flag)];
}
return Bootstrap4::select($options, $value, Select2::flagConfig() + $attributes);
}
/**
* Create a form control to select an individual.
*
* @param Individual|null $individual
* @param string[] $attributes
*
* @return string
*/
public static function formControlIndividual(Individual $individual = null, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($individual !== null) {
$value = $individual->getXref();
$options = [$value => View::make('selects/individual', ['individual' => $individual])];
}
return Bootstrap4::select($options, $value, Select2::individualConfig() + $attributes);
}
/**
* Create a form control to select a media object.
*
* @param Media|null $media
* @param string[] $attributes
*
* @return string
*/
public static function formControlMediaObject(Media $media = null, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($media !== null) {
$value = $media->getXref();
$options = [$value => View::make('selects/media', ['media' => $media])];
}
return Bootstrap4::select($options, $value, Select2::mediaObjectConfig() + $attributes);
}
/**
* Create a form control to select a note.
*
* @param Note|null $note
* @param string[]|null $attributes
*
* @return string
*/
public static function formControlNote(Note $note = null, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($note !== null) {
$value = $note->getXref();
$options = [$value => View::make('selects/note', ['note' => $note])];
}
return Bootstrap4::select($options, $value, Select2::noteConfig() + $attributes);
}
/**
* Create a form control to select a place.
*
* @param string $place
* @param string[] $attributes
*
* @return string
*/
public static function formControlPlace($place, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($place !== '') {
$options = [$place => $place];
}
return Bootstrap4::select($options, $value, Select2::placeConfig() + $attributes);
}
/**
* Create a form control to select a repository.
*
* @param Repository|null $repository
* @param string[] $attributes
*
* @return string
*/
public static function formControlRepository(Repository $repository = null, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($repository !== null) {
$value = $repository->getXref();
$options = [$value => View::make('selects/repository', ['repository' => $repository])];
}
return Bootstrap4::select($options, $value, Select2::repositoryConfig() + $attributes);
}
/**
* Create a form control to select a source.
*
* @param Source|null $source
* @param string[] $attributes
*
* @return string
*/
public static function formControlSource(Source $source = null, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($source !== null) {
$value = $source->getXref();
$options = [$value => View::make('selects/source', ['source' => $source])];
}
return Bootstrap4::select($options, $value, Select2::sourceConfig() + $attributes);
}
/**
* Create a form control to select a submitter.
*
* @param GedcomRecord|null $submitter
* @param string[] $attributes
*
* @return string
*/
public static function formControlSubmitter(GedcomRecord $submitter = null, array $attributes = []) {
$value = '';
$options = ['' => ''];
if ($submitter !== null) {
$value = $submitter->getXref();
$options = [$value => View::make('selects/submitter', ['submitter' => $submitter])];
}
return Bootstrap4::select($options, $value, Select2::submitterConfig() + $attributes);
}
/**
* Remove all links from $gedrec to $xref, and any sub-tags.
*
* @param string $gedrec
* @param string $xref
*
* @return string
*/
public static function removeLinks($gedrec, $xref) {
$gedrec = preg_replace('/\n1 ' . WT_REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n2 ' . WT_REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n3 ' . WT_REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n4 ' . WT_REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n5 ' . WT_REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec);
return $gedrec;
}
/**
* Input addon to generate a calendar widget.
*
* @param string $id
*
* @return string
*/
public static function inputAddonCalendar($id) {
return
'' .
FontAwesome::linkIcon('calendar', I18N::translate('Select a date'), ['href' => '#', 'onclick' => 'return calendarWidget("caldiv' . $id . '", "' . $id . '");']) .
'';
}
/**
* Input addon to select a special characterr using a virtual keyboard
*
* @param string $id
*
* @return string
*/
public static function inputAddonKeyboard($id) {
return
'' .
FontAwesome::linkIcon('keyboard', I18N::translate('Find a special character'), ['class' => 'wt-osk-trigger', 'href' => '#', 'data-id' => $id]) .
'';
}
/**
* Input addon to generate a help link.
*
* @param string $fact
*
* @return string
*/
public static function inputAddonHelp($fact) {
return '' . FunctionsPrint::helpLink($fact) . '';
}
/**
* 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 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
* @param string $extra optional text to display after the input field
* @param Individual $person For male/female translations
*
* @return string
*/
public static function addSimpleTag($tag, $upperlevel = '', $label = '', $extra = null, Individual $person = null) {
global $tags, $xref, $bdm, $action, $WT_TREE;
// Some form fields need access to previous form fields.
static $previous_ids = ['SOUR' => '', 'PLAC' => ''];
preg_match('/^(?:(\d+) (' . WT_REGEX_TAG . ') ?(.*))/', $tag, $match);
list(, $level, $fact, $value) = $match;
if ($level === '0') {
if ($upperlevel) {
$name = $upperlevel . '_' . $fact;
} else {
$name = $fact;
}
} else {
$name = 'text[]';
}
if ($level === '0') {
$id = $fact;
} else {
$id = $fact . Uuid::uuid4();
}
if ($upperlevel) {
$id = $upperlevel . '_' . $fact . Uuid::uuid4();
}
$previous_ids[$fact] = $id;
// field value
$islink = (substr($value, 0, 1) === '@' && substr($value, 0, 2) !== '@#');
if ($islink) {
$value = trim(substr($tag, strlen($fact) + 3), ' @\r');
} 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 .= '