xref: /webtrees/resources/views/modals/create-media-object.phtml (revision 81b729d3a9a6e0a0e8b96285d1ad7955d2d0c659)
1<?php
2
3use Fisharebest\Webtrees\Http\RequestHandlers\CreateMediaObjectAction;
4use Fisharebest\Webtrees\I18N;
5use Fisharebest\Webtrees\Tree;
6
7/**
8 * @var string                   $max_upload_size
9 * @var array<int|string,string> $media_types
10 * @var Tree                     $tree
11 * @var array<string>            $unused_files
12 */
13
14?>
15
16<form method="post" action="<?= e(route(CreateMediaObjectAction::class, ['tree' => $tree->name()])) ?>" id="wt-modal-form">
17    <?= csrf_field() ?>
18
19    <?= view('modals/header', ['title' => I18N::translate('Create a media object')]) ?>
20
21    <div class="modal-body">
22        <?= view('modals/media-object-fields', ['max_upload_size' => $max_upload_size, 'media_types' => $media_types, 'unused_files' => $unused_files, 'tree' => $tree]) ?>
23    </div>
24
25    <?= view('modals/footer-save-cancel') ?>
26</form>
27
28<script>
29  // Submit the modal form using AJAX
30  document.getElementById('wt-modal-form').addEventListener('submit', function (event) {
31    event.preventDefault();
32    let form = event.target;
33    let modal_content = document.querySelector('#wt-ajax-modal .modal-content');
34    let select = document.getElementById(modal_content.dataset.wtSelectId);
35
36    $.ajax({
37      url: form.action,
38      type: form.method,
39      data: new FormData(form),
40      async: false,
41      cache: false,
42      contentType: false,
43      processData: false,
44      success: function (data) {
45        if (select) {
46          // If this modal was activated by the "new" button in a select2
47          // edit control, then insert the result and select it.
48          let option = new Option(data.text, data.id, true, true);
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