xref: /webtrees/resources/views/modals/create-repository.phtml (revision d4786c66945cca20d5ce34ac3cf08cf5a5110ae2)
1<?php
2
3use Fisharebest\Webtrees\Contracts\ElementInterface;
4use Fisharebest\Webtrees\Http\RequestHandlers\CreateRepositoryAction;
5use Fisharebest\Webtrees\I18N;
6use Fisharebest\Webtrees\Tree;
7
8/**
9 * @var array<string,ElementInterface> $elements
10 * @var Tree                           $tree
11 */
12
13?>
14
15<form method="post" action="<?= e(route(CreateRepositoryAction::class, ['tree' => $tree->name()])) ?>" id="wt-modal-form">
16    <?= csrf_field() ?>
17
18    <?= view('modals/header', ['title' => I18N::translate('Create a repository')]) ?>
19
20    <div class="modal-body">
21        <?= view('modals/repository-fields', ['elements' => $elements, 'tree' => $tree]) ?>
22    </div>
23
24    <?= view('modals/footer-save-cancel') ?>
25</form>
26
27<script>
28    // Submit the modal form using AJAX
29    document.getElementById("wt-modal-form").addEventListener("submit", function (event) {
30        event.preventDefault();
31        let form          = event.target;
32        let modal_content = document.querySelector("#wt-ajax-modal .modal-content");
33        let select        = document.getElementById(modal_content.dataset.wtSelectId);
34
35        $.ajax({
36            url:         form.action,
37            type:        form.method,
38            data:        new FormData(form),
39            async:       false,
40            cache:       false,
41            contentType: false,
42            processData: false,
43            success:     function (data) {
44                if (select) {
45                    // If this modal was activated by the "new" button in a select2
46                    // edit control, then insert the result and select it.
47                    let option       = new Option(data.text, data.id, true, true);
48                    option.innerHTML = option.innerText;
49
50                    $(select)
51                        .select2({ escapeMarkup: (x) => x })
52                        .empty()
53                        .append(option)
54                        .trigger("change");
55
56                    $("#wt-ajax-modal").modal("hide");
57                } else {
58                    modal_content.innerHTML = data.html;
59                }
60            },
61            failure:     function (data) {
62                modal_content.innerHTML = data.html;
63            },
64        });
65    });
66</script>
67