xref: /webtrees/resources/views/modals/create-repository.phtml (revision df46d43da3a4c72efd0428881e933e94412cd945)
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 select
46          // edit control, then insert the result and select it.
47          select.tomselect.clearOptions();
48          select.tomselect.addOption({ value: data.value, text: data.text });
49          select.tomselect.refreshOptions();
50          select.tomselect.clear();
51          select.tomselect.addItem(data.value);
52          select.tomselect.refreshItems();
53
54          $('#wt-ajax-modal').modal('hide');
55        } else {
56          modal_content.innerHTML = data.html;
57        }
58      },
59      failure: function (data) {
60        modal_content.innerHTML = data.html;
61      },
62    });
63  });
64</script>
65