xref: /webtrees/resources/views/edit/reorder-families.phtml (revision e124422c4f7627f85002fcf1ae8731fe4226b110)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\Fact;
6use Fisharebest\Webtrees\Gedcom;
7use Fisharebest\Webtrees\Http\RequestHandlers\ReorderFamiliesAction;
8use Fisharebest\Webtrees\I18N;
9use Fisharebest\Webtrees\Individual;
10use Fisharebest\Webtrees\Registry;
11use Fisharebest\Webtrees\Tree;
12use Fisharebest\Webtrees\View;
13use Illuminate\Support\Collection;
14
15/**
16 * @var Collection<int,Fact> $famc_facts
17 * @var Collection<int,Fact> $fams_facts
18 * @var Individual           $individual
19 * @var string               $title
20 * @var Tree                 $tree
21 */
22
23?>
24
25<h2 class="wt-page-title"><?= $title ?></h2>
26
27<form method="post" action="<?= e(route(ReorderFamiliesAction::class, ['tree' => $tree->name(), 'xref' => $individual->xref()])) ?>" class="wt-page-content">
28    <?php if ($fams_facts->count() < 2) : ?>
29        <?php foreach ($fams_facts as $fact) : ?>
30            <input type="hidden" name="order[]" value="<?= e($fact->id()) ?>">
31        <?php endforeach ?>
32    <?php else : ?>
33        <h3><?= I18N::translate('Spouses') ?></h3>
34
35        <p>
36            <?= I18N::translate('When an individual has more than one spouse, you should sort the families in date order.') ?>
37        </p>
38
39        <p>
40            <button class="btn btn-secondary" id="btn-default-order" type="button">
41                <?= view('icons/sort') ?>
42                <?= /* I18N: A button label. */ I18N::translate('sort by date of marriage') ?>
43            </button>
44        </p>
45
46        <div class="wt-sortable-list wt-sortable-list-fams">
47            <?php foreach ($fams_facts as $fact) : ?>
48                <div class="card mb-2 wt-sortable-item" data-wt-sort-by-date="<?= $fact->target()->getMarriageDate()->julianDay() ?>">
49                    <input type="hidden" name="order[]" value="<?= $fact->id() ?>">
50                    <div class="card-header">
51                        <?= view('edit/reorder-card-header', ['title' => $fact->target()->fullName()]) ?>
52                    </div>
53
54                    <div class="card-body">
55                        <?= $fact->target()->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 2) ?>
56                        <?= $fact->target()->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 2) ?>
57                    </div>
58                </div>
59            <?php endforeach ?>
60        </div>
61    <?php endif ?>
62
63    <?php if ($famc_facts->count() < 2) : ?>
64            <?php foreach ($famc_facts as $fact) : ?>
65                <input type="hidden" name="order[]" value="<?= e($fact->id()) ?>">
66            <?php endforeach ?>
67    <?php else : ?>
68        <?php if ($fams_facts->count() >= 2) : ?>
69        <hr>
70        <?php endif ?>
71
72        <h3><?= I18N::translate('Parents') ?></h3>
73
74        <p>
75            <?= I18N::translate('An individual can have more than one set of parents. For example, birth and adopted.') ?>
76            <br>
77            <?= I18N::translate('The first family in the list will be used in charts, lists, reports, etc.') ?>
78        </p>
79
80        <div class="wt-sortable-list wt-sortable-list-famc">
81            <?php foreach ($famc_facts as $fact) : ?>
82                <div class="card my-2 wt-sortable-item" data-sortbydate="<?= $fact->target()->getMarriageDate()->julianDay() ?>">
83                    <input type="hidden" name="order[]" value="<?= $fact->id() ?>">
84                    <div class="card-header">
85                        <?= view('edit/reorder-card-header', ['title' => $fact->target()->fullName() . ($fact->attribute('PEDI') === '' ? '' : ' — ' . Registry::elementFactory()->make('INDI:FAMC:PEDI')->value($fact->attribute('PEDI'), $tree))]) ?>
86                    </div>
87                    <div class="card-body">
88                        <?= $fact->target()->formatFirstMajorFact(Gedcom::MARRIAGE_EVENTS, 2) ?>
89                        <?= $fact->target()->formatFirstMajorFact(Gedcom::DIVORCE_EVENTS, 2) ?>
90                    </div>
91                </div>
92            <?php endforeach ?>
93        </div>
94    <?php endif ?>
95
96    <p>
97        <button class="btn btn-primary" type="submit">
98            <?= view('icons/save') ?>
99            <?= /* I18N: A button label. */ I18N::translate('save') ?>
100        </button>
101
102        <a class="btn btn-secondary" href="<?= e($individual->url()) ?>">
103            <?= view('icons/cancel') ?>
104            <?= /* I18N: A button label. */ I18N::translate('cancel') ?>
105        </a>
106    </p>
107
108    <?= csrf_field() ?>
109</form>
110
111<?php View::push('javascript') ?>
112<script>
113    const famc_list = document.querySelector(".wt-sortable-list-famc");
114    const fams_list = document.querySelector(".wt-sortable-list-fams");
115
116    if (famc_list !== null) {
117        new Sortable(famc_list, {
118            handle: ".card-header",
119        });
120    }
121
122    if (fams_list !== null) {
123        new Sortable(fams_list, {
124            handle: ".card-header",
125        });
126
127        $("#btn-default-order").on("click", function () {
128            $(".wt-sortable-list-fams .wt-sortable-item").sort(function (x, y) {
129                return Math.sign(x.dataset.wtSortByDate - y.dataset.wtSortByDate);
130            }).appendTo(".wt-sortable-list-fams");
131        });
132    }
133</script>
134<?php View::endpush() ?>
135