xref: /webtrees/resources/views/admin/data-fix-page.phtml (revision c5cb081f84eaf817fc43486524a23d6714850ec0)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
5ce42304aSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
6ce42304aSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\DataFixData;
7ce42304aSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\DataFixPage;
8ce42304aSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\DataFixUpdateAll;
93de8af15SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\HelpText;
106fd01894SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ManageTrees;
11ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
12ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleDataFixInterface;
13ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree;
14ce42304aSGreg Roachuse Fisharebest\Webtrees\View;
15ce42304aSGreg Roach
16ce42304aSGreg Roach/**
17ce42304aSGreg Roach * @var ModuleDataFixInterface $data_fix
18ce42304aSGreg Roach * @var string                 $latest_version
19ce42304aSGreg Roach * @var string                 $title
20ce42304aSGreg Roach * @var Tree                   $tree
21ce42304aSGreg Roach * @var string                 $pending_url
22ce42304aSGreg Roach */
23ce42304aSGreg Roach
24ce42304aSGreg Roach?>
25ce42304aSGreg Roach
266fd01894SGreg Roach<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), route(ManageTrees::class, ['tree' => $tree->name()]) => I18N::translate('Manage family trees'), route(DataFixPage::class, ['tree' => $tree->name()]) => view('icons/data-fix') . I18N::translate('Data fixes'), $title]]) ?>
27ce42304aSGreg Roach
28ce42304aSGreg Roach<h1><?= $title ?></h1>
29ce42304aSGreg Roach
30ce42304aSGreg Roach<form action="#" id="data-fix-options">
31ce42304aSGreg Roach    <p>
32ce42304aSGreg Roach        <?= $data_fix->description() ?>
33ce42304aSGreg Roach    </p>
34ce42304aSGreg Roach
35ce42304aSGreg Roach    <?= $data_fix->fixOptions($tree) ?>
36ce42304aSGreg Roach
379e3c2cf9SGreg Roach    <div class="row mb-3">
38ce42304aSGreg Roach        <div class="col-sm-3">
39ce42304aSGreg Roach        </div>
40ce42304aSGreg Roach
41ce42304aSGreg Roach        <div class="col-sm-9">
42ce42304aSGreg Roach            <button class="btn btn-primary" type="button" id="btn-search">
43ce42304aSGreg Roach                <?= view('icons/search') ?>
44ce42304aSGreg Roach                <?= I18N::translate('Search') ?>
45ce42304aSGreg Roach            </button>
46ce42304aSGreg Roach
47ce42304aSGreg Roach            <button class="btn btn-primary" type="button" id="btn-update-all">
48ce42304aSGreg Roach                <?= view('icons/data-fix') ?>
49ce42304aSGreg Roach                <?= I18N::translate('Update all') ?>
50ce42304aSGreg Roach            </button>
51ce42304aSGreg Roach        </div>
52ce42304aSGreg Roach    </div>
53ce42304aSGreg Roach</form>
54ce42304aSGreg Roach
55ce42304aSGreg Roach<div id="data-fix-table-container" class="d-none">
56ce42304aSGreg Roach    <table
57ce42304aSGreg Roach        id="data-fix-table"
58ce42304aSGreg Roach        class="table table-bordered table-sm table-hover wt-data-fix-table"
59ce42304aSGreg Roach        <?= view('lists/datatables-attributes') ?>
60ce42304aSGreg Roach        data-server-side="true"
61ce42304aSGreg Roach        data-filter="false"
62ad321e85SGreg Roach        data-processing="true"
63ce42304aSGreg Roach        data-sort="false"
64ce42304aSGreg Roach    >
65ce42304aSGreg Roach        <thead>
66ce42304aSGreg Roach            <tr>
67ce42304aSGreg Roach                <th class="w-75">
68ce42304aSGreg Roach                    <?= I18N::translate('Record') ?>
69ce42304aSGreg Roach                </th>
70ce42304aSGreg Roach                <th class="w-25">
71ce42304aSGreg Roach                    <?= view('icons/data-fix') ?>
72ce42304aSGreg Roach                    <?= I18N::translate('Data fix') ?>
73ce42304aSGreg Roach                </th>
74ce42304aSGreg Roach            </tr>
75ce42304aSGreg Roach        </thead>
76ce42304aSGreg Roach        <tbody>
77ce42304aSGreg Roach        </tbody>
78ce42304aSGreg Roach    </table>
793de8af15SGreg Roach
803de8af15SGreg Roach    <hr>
813de8af15SGreg Roach
82d4786c66SGreg Roach    <a href="#" data-bs-toggle="modal" data-bs-backdrop="static" data-bs-target="#wt-ajax-modal" data-wt-href="<?= e(route(HelpText::class, ['topic' => 'data-fixes'])) ?>">
833de8af15SGreg Roach        <?= view('icons/help') ?>
843de8af15SGreg Roach        <?= I18N::translate('Why does this list include records that do not need to be updated?') ?>
853de8af15SGreg Roach    </a>
86ce42304aSGreg Roach</div>
87ce42304aSGreg Roach
88ce42304aSGreg Roach<div id="data-fix-progress" class="d-none">
89*c5cb081fSGreg Roach    <div class="progress" role="progressbar">
90*c5cb081fSGreg Roach        <div class="progress-bar"></div>
91ce42304aSGreg Roach    </div>
92ce42304aSGreg Roach</div>
93ce42304aSGreg Roach
94ce42304aSGreg Roach<?= view('modals/ajax') ?>
95ce42304aSGreg Roach
96ce42304aSGreg Roach<?php View::push('javascript') ?>
97ce42304aSGreg Roach<script>
98ce42304aSGreg Roach  (function () {
99313cf418SGreg Roach    let form = document.getElementById('data-fix-options');
100313cf418SGreg Roach    let container = document.getElementById('data-fix-table-container');
101313cf418SGreg Roach    let table = document.getElementById('data-fix-table');
102313cf418SGreg Roach    let progress = document.getElementById('data-fix-progress');
103ce42304aSGreg Roach    let progressbar = progress.querySelector('.progress-bar');
104ce42304aSGreg Roach    let queue = [];
105ce42304aSGreg Roach
106ce42304aSGreg Roach    function getParams () {
107ce42304aSGreg Roach      let formData = new FormData(form);
108ce42304aSGreg Roach      let params = {};
109ce42304aSGreg Roach      formData.forEach(function (value, key) {
110ce42304aSGreg Roach        params[key] = value;
111ce42304aSGreg Roach      });
112ce42304aSGreg Roach
113ce42304aSGreg Roach      return params;
114ce42304aSGreg Roach    }
115ce42304aSGreg Roach
116ce42304aSGreg Roach    function addParamsToUrl (u) {
117ce42304aSGreg Roach      let url = new URL(u);
118ce42304aSGreg Roach      let formData = new FormData(form);
119ce42304aSGreg Roach      formData.forEach(function (value, key) {
120ce42304aSGreg Roach        url.searchParams.append(key, value);
121ce42304aSGreg Roach      });
122ce42304aSGreg Roach
123ce42304aSGreg Roach      return url.toString();
124ce42304aSGreg Roach
125ce42304aSGreg Roach    }
126ce42304aSGreg Roach
127ce42304aSGreg Roach    form.addEventListener('submit', function (event) {
128ce42304aSGreg Roach      event.preventDefault();
129ce42304aSGreg Roach    });
130ce42304aSGreg Roach
131ce42304aSGreg Roach    container.addEventListener('click', function (event) {
132ce42304aSGreg Roach      if ('updateUrl' in event.target.dataset) {
133ce42304aSGreg Roach        event.preventDefault();
134ce42304aSGreg Roach
135313cf418SGreg Roach        webtrees.httpPost(event.target.dataset.updateUrl)
136313cf418SGreg Roach          .then(function (response) {
137ce42304aSGreg Roach            $(table).DataTable().ajax.reload(null, false);
138313cf418SGreg Roach          })
139313cf418SGreg Roach          .catch(function (error) {
140ce42304aSGreg Roach            alert(error);
141ce42304aSGreg Roach        });
142ce42304aSGreg Roach      }
143ce42304aSGreg Roach    });
144ce42304aSGreg Roach
145ce42304aSGreg Roach    document.getElementById('btn-search').addEventListener('click', function (event) {
146ce42304aSGreg Roach      event.preventDefault();
147ce42304aSGreg Roach
148ce42304aSGreg Roach      // If we were in the middle of doing "update all", stop processing.
149ce42304aSGreg Roach      queue = [];
150ce42304aSGreg Roach
151ce42304aSGreg Roach      progress.classList.add('d-none');
152ce42304aSGreg Roach
153ce42304aSGreg Roach      if ($.fn.dataTable.isDataTable(table)) {
154ce42304aSGreg Roach        $(table).DataTable().ajax.reload();
155ce42304aSGreg Roach      } else {
156ce42304aSGreg Roach        $(table).DataTable({
157ce42304aSGreg Roach          'ajax': {
158728c8c27SGreg Roach            'url': <?= json_encode(route(DataFixData::class, ['tree' => $tree->name(), 'data_fix' => $data_fix->name()]), JSON_THROW_ON_ERROR) ?>,
159ce42304aSGreg Roach            'data': function (data) {
160ce42304aSGreg Roach              $.extend(data, getParams());
161ce42304aSGreg Roach            }
162ce42304aSGreg Roach          }
163ce42304aSGreg Roach        });
164ce42304aSGreg Roach      }
165ce42304aSGreg Roach
166ce42304aSGreg Roach      container.classList.remove('d-none');
167ce42304aSGreg Roach    });
168ce42304aSGreg Roach
169ce42304aSGreg Roach    document.getElementById('btn-update-all').addEventListener('click', function (event) {
170ce42304aSGreg Roach      event.preventDefault();
171ce42304aSGreg Roach
172ce42304aSGreg Roach      progressbar.innerHTML = '';
173ce42304aSGreg Roach      progressbar.style.width = '0%';
174ce42304aSGreg Roach
175ce42304aSGreg Roach      container.classList.add('d-none');
176ce42304aSGreg Roach      progress.classList.remove('d-none');
177ce42304aSGreg Roach
178728c8c27SGreg Roach      let url = addParamsToUrl(<?= json_encode(route(DataFixUpdateAll::class, ['tree' => $tree->name(), 'data_fix' => $data_fix->name()]), JSON_THROW_ON_ERROR) ?>);
179ce42304aSGreg Roach
180313cf418SGreg Roach      webtrees.httpPost(url)
181313cf418SGreg Roach        .then(function (response) {
182ce42304aSGreg Roach          return response.json();
183313cf418SGreg Roach        })
184313cf418SGreg Roach        .then(async function (data) {
185ce42304aSGreg Roach          queue = data;
186ce42304aSGreg Roach          while (queue.length > 0) {
187ce42304aSGreg Roach            let datum = queue.shift();
188313cf418SGreg Roach            await webtrees.httpPost(datum.url)
189313cf418SGreg Roach              .then(function () {
190ce42304aSGreg Roach                let progressbar = progress.querySelector('.progress-bar');
191ce42304aSGreg Roach                progressbar.innerHTML = datum.progress;
192ce42304aSGreg Roach                progressbar.style.width = datum.percent;
193ce42304aSGreg Roach              });
194ce42304aSGreg Roach          }
195313cf418SGreg Roach        })
196313cf418SGreg Roach        .catch(function (error) {
197ce42304aSGreg Roach          progress.innerHTML = error;
198ce42304aSGreg Roach        });
199ce42304aSGreg Roach    });
200ce42304aSGreg Roach  })();
201ce42304aSGreg Roach</script>
202ce42304aSGreg Roach<?php View::endpush() ?>
203