diff --git a/client/scripts/database.js b/client/scripts/database.js index 5d4b45a..817c247 100644 --- a/client/scripts/database.js +++ b/client/scripts/database.js @@ -483,255 +483,48 @@ function sync() { if (!canUseLocalDB) return false; if (syncInProgress > 0) return false; - var now = Math.floor(Date.now() / 1000); + return new Promise(function(resolve) { + var now = Math.floor(Date.now() / 1000); - db.transaction('update_times').objectStore('update_times').getAll().onsuccess = function (event) { - var localTimes = {}; - event.target.result.forEach(function (entry) { - localTimes[entry['table']] = entry['time']; - }); + db.transaction('update_times').objectStore('update_times').getAll().onsuccess = function (event) { + var localTimes = {}; + event.target.result.forEach(function (entry) { + localTimes[entry['table']] = entry['time']; + }); - syncInProgress = 11; - var syncOkay = true; - console.log("Sync Start"); - $('#i-sync').addClass('fa-spin'); + syncInProgress = 11; + var syncOkay = true; + console.log("Sync Start"); + $('#i-sync').addClass('fa-spin'); - var interval = window.setInterval(function () { - if (syncInProgress <= 0) { - window.clearInterval(interval); - if (syncOkay) { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'last_sync', time: now }); + var interval = window.setInterval(function () { + if (syncInProgress <= 0) { + window.clearInterval(interval); + if (syncOkay) { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'last_sync', time: now }); + } + console.log("Sync Stop"); + setTimeout(function(){ + $('#i-sync').removeClass('fa-spin'); + }, 500); + + if (typeof onAfterSync === 'function') { + onAfterSync(); + } + runPageScript(); + resolve(); } - console.log("Sync Stop"); - setTimeout(function(){ - $('#i-sync').removeClass('fa-spin'); - }, 500); + }, 100); - if (typeof onAfterSync === 'function') { - onAfterSync(); - } - runPageScript(); - } - }, 100); + getJSON(QUERY_URL + 'get_update_time', function (code, serverTimes) { + if (code == 200) { - getJSON(QUERY_URL + 'get_update_time', function (code, serverTimes) { - if (code == 200) { - - // CLUBS - if (localTimes['clubs'] < serverTimes['clubs']) { - getJSON(QUERY_URL + 'get_clubs?changed-after=' + localTimes['clubs'], function (code, data) { - if (code == 200) { - var os = db.transaction('clubs', 'readwrite').objectStore('clubs'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'clubs', time: serverTimes['clubs'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - // BOATS - if (localTimes['boats'] < serverTimes['boats']) { - getJSON(QUERY_URL + 'get_boats?changed-after=' + localTimes['boats'], function (code, data) { - if (code == 200) { - var os = db.transaction('boats', 'readwrite').objectStore('boats'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'boats', time: serverTimes['boats'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - // SAILORS - if (localTimes['sailors'] < serverTimes['sailors']) { - getJSON(QUERY_URL + 'get_sailors?changed-after=' + localTimes['sailors'], function (code, data) { - if (code == 200) { - var os = db.transaction('sailors', 'readwrite').objectStore('sailors'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'sailors', time: serverTimes['sailors'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - // REGATTAS - if (localTimes['regattas'] < serverTimes['regattas']) { - getJSON(QUERY_URL + 'get_regattas?changed-after=' + localTimes['regattas'], function (code, data) { - if (code == 200) { - var os = db.transaction('regattas', 'readwrite').objectStore('regattas'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = async function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - // update years - var regattas = await dbGetData('regattas'); - var years = {}; - for (id in regattas) { - var entry = regattas[id]; - var date = parseDate(entry['date']); - var y = date.getFullYear(); - years[y] = y; - } - var osYears = db.transaction('years', 'readwrite').objectStore('years'); - osYears.clear().onsuccess = function (event) { - for (var y in years) { - osYears.put({ year: y }); - } - } - - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'regattas', time: serverTimes['regattas'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - // RESULTS - if (localTimes['results'] < serverTimes['results']) { - getJSON(QUERY_URL + 'get_results?changed-after=' + localTimes['results'], function (code, data) { - if (code == 200) { - var os = db.transaction('results', 'readwrite').objectStore('results'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'results', time: serverTimes['results'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - // PLANNINGS - if (localTimes['plannings'] < serverTimes['plannings']) { - getJSON(QUERY_URL + 'get_plannings?changed-after=' + localTimes['plannings'], function (code, data) { - if (code == 200) { - var os = db.transaction('plannings', 'readwrite').objectStore('plannings'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'plannings', time: serverTimes['plannings'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - if (isLoggedIn()) { - // TRIM_BOATS - if (localTimes['trim_boats'] < serverTimes['trim_boats']) { - getJSON(QUERY_URL + 'get_trim_boats?changed-after=' + localTimes['trim_boats'], function (code, data) { + // CLUBS + if (localTimes['clubs'] < serverTimes['clubs']) { + getJSON(QUERY_URL + 'get_clubs?changed-after=' + localTimes['clubs'], function (code, data) { if (code == 200) { - var os = db.transaction('trim_boats', 'readwrite').objectStore('trim_boats'); + var os = db.transaction('clubs', 'readwrite').objectStore('clubs'); console.log(data); data.data.forEach(function (entry) { os.put(entry); @@ -745,7 +538,7 @@ function sync() { cursor.continue(); } else { var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'trim_boats', time: serverTimes['trim_boats'] }); + osUpdateTimes.put({ table: 'clubs', time: serverTimes['clubs'] }); syncInProgress --; } }; @@ -759,11 +552,11 @@ function sync() { syncInProgress --; } - // TRIM_USERS - if (localTimes['trim_users'] < serverTimes['trim_users']) { - getJSON(QUERY_URL + 'get_trim_users?changed-after=' + localTimes['trim_users'], function (code, data) { + // BOATS + if (localTimes['boats'] < serverTimes['boats']) { + getJSON(QUERY_URL + 'get_boats?changed-after=' + localTimes['boats'], function (code, data) { if (code == 200) { - var os = db.transaction('trim_users', 'readwrite').objectStore('trim_users'); + var os = db.transaction('boats', 'readwrite').objectStore('boats'); console.log(data); data.data.forEach(function (entry) { os.put(entry); @@ -777,7 +570,7 @@ function sync() { cursor.continue(); } else { var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'trim_users', time: serverTimes['trim_users'] }); + osUpdateTimes.put({ table: 'boats', time: serverTimes['boats'] }); syncInProgress --; } }; @@ -791,11 +584,11 @@ function sync() { syncInProgress --; } - // TRIM_TRIMS - if (localTimes['trim_trims'] < serverTimes['trim_trims']) { - getJSON(QUERY_URL + 'get_trim_trims?changed-after=' + localTimes['trim_trims'], function (code, data) { + // SAILORS + if (localTimes['sailors'] < serverTimes['sailors']) { + getJSON(QUERY_URL + 'get_sailors?changed-after=' + localTimes['sailors'], function (code, data) { if (code == 200) { - var os = db.transaction('trim_trims', 'readwrite').objectStore('trim_trims'); + var os = db.transaction('sailors', 'readwrite').objectStore('sailors'); console.log(data); data.data.forEach(function (entry) { os.put(entry); @@ -809,7 +602,284 @@ function sync() { cursor.continue(); } else { var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'trim_trims', time: serverTimes['trim_trims'] }); + osUpdateTimes.put({ table: 'sailors', time: serverTimes['sailors'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + // REGATTAS + if (localTimes['regattas'] < serverTimes['regattas']) { + getJSON(QUERY_URL + 'get_regattas?changed-after=' + localTimes['regattas'], function (code, data) { + if (code == 200) { + var os = db.transaction('regattas', 'readwrite').objectStore('regattas'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = async function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + // update years + var regattas = await dbGetData('regattas'); + var years = {}; + for (id in regattas) { + var entry = regattas[id]; + var date = parseDate(entry['date']); + var y = date.getFullYear(); + years[y] = y; + } + var osYears = db.transaction('years', 'readwrite').objectStore('years'); + osYears.clear().onsuccess = function (event) { + for (var y in years) { + osYears.put({ year: y }); + } + } + + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'regattas', time: serverTimes['regattas'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + // RESULTS + if (localTimes['results'] < serverTimes['results']) { + getJSON(QUERY_URL + 'get_results?changed-after=' + localTimes['results'], function (code, data) { + if (code == 200) { + var os = db.transaction('results', 'readwrite').objectStore('results'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'results', time: serverTimes['results'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + // PLANNINGS + if (localTimes['plannings'] < serverTimes['plannings']) { + getJSON(QUERY_URL + 'get_plannings?changed-after=' + localTimes['plannings'], function (code, data) { + if (code == 200) { + var os = db.transaction('plannings', 'readwrite').objectStore('plannings'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'plannings', time: serverTimes['plannings'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + if (isLoggedIn()) { + // TRIM_BOATS + if (localTimes['trim_boats'] < serverTimes['trim_boats']) { + getJSON(QUERY_URL + 'get_trim_boats?changed-after=' + localTimes['trim_boats'], function (code, data) { + if (code == 200) { + var os = db.transaction('trim_boats', 'readwrite').objectStore('trim_boats'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'trim_boats', time: serverTimes['trim_boats'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + // TRIM_USERS + if (localTimes['trim_users'] < serverTimes['trim_users']) { + getJSON(QUERY_URL + 'get_trim_users?changed-after=' + localTimes['trim_users'], function (code, data) { + if (code == 200) { + var os = db.transaction('trim_users', 'readwrite').objectStore('trim_users'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'trim_users', time: serverTimes['trim_users'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + // TRIM_TRIMS + if (localTimes['trim_trims'] < serverTimes['trim_trims']) { + getJSON(QUERY_URL + 'get_trim_trims?changed-after=' + localTimes['trim_trims'], function (code, data) { + if (code == 200) { + var os = db.transaction('trim_trims', 'readwrite').objectStore('trim_trims'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'trim_trims', time: serverTimes['trim_trims'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + } else { + syncInProgress -= 3; + } + + // NEWS + if (localTimes['news'] < serverTimes['news']) { + getJSON(QUERY_URL + 'get_news?changed-after=' + localTimes['news'], function (code, data) { + if (code == 200) { + var os = db.transaction('news', 'readwrite').objectStore('news'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'news', time: serverTimes['news'] }); + syncInProgress --; + } + }; + } else { + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress --; + } + }); + } else { + syncInProgress --; + } + + // USERS + if (localTimes['users'] < serverTimes['users']) { + getJSON(QUERY_URL + 'get_users?changed-after=' + localTimes['users'], function (code, data) { + if (code == 200) { + var os = db.transaction('users', 'readwrite').objectStore('users'); + console.log(data); + data.data.forEach(function (entry) { + os.put(entry); + }); + os.openCursor().onsuccess = function (event) { + var cursor = event.target.result; + if (cursor) { + if (!data.keys.includes(parseInt(cursor.key))) { + os.delete(cursor.key); + } + cursor.continue(); + } else { + var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); + osUpdateTimes.put({ table: 'users', time: serverTimes['users'] }); syncInProgress --; } }; @@ -824,80 +894,13 @@ function sync() { } } else { - syncInProgress -= 3; + console.log("Something went wrong (HTTP " + code + ")"); + syncOkay = false; + syncInProgress = 0; } - - // NEWS - if (localTimes['news'] < serverTimes['news']) { - getJSON(QUERY_URL + 'get_news?changed-after=' + localTimes['news'], function (code, data) { - if (code == 200) { - var os = db.transaction('news', 'readwrite').objectStore('news'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'news', time: serverTimes['news'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - // USERS - if (localTimes['users'] < serverTimes['users']) { - getJSON(QUERY_URL + 'get_users?changed-after=' + localTimes['users'], function (code, data) { - if (code == 200) { - var os = db.transaction('users', 'readwrite').objectStore('users'); - console.log(data); - data.data.forEach(function (entry) { - os.put(entry); - }); - os.openCursor().onsuccess = function (event) { - var cursor = event.target.result; - if (cursor) { - if (!data.keys.includes(parseInt(cursor.key))) { - os.delete(cursor.key); - } - cursor.continue(); - } else { - var osUpdateTimes = db.transaction('update_times', 'readwrite').objectStore('update_times'); - osUpdateTimes.put({ table: 'users', time: serverTimes['users'] }); - syncInProgress --; - } - }; - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress --; - } - }); - } else { - syncInProgress --; - } - - } else { - console.log("Something went wrong (HTTP " + code + ")"); - syncOkay = false; - syncInProgress = 0; - } - }); - }; + }); + }; + }); } function checkSync() { diff --git a/server/content/planning_edit.php b/server/content/planning_edit.php index ef7d037..4ef708c 100644 --- a/server/content/planning_edit.php +++ b/server/content/planning_edit.php @@ -1,21 +1,31 @@ Saison-Planung bearbeiten"; + $content .= $tpl->load('select', ['html-id' => 'select-year', 'placeholder' => 'Jahr', 'css-class' => 'mt-3 mb-0']); - $content = $tpl->load('error', ['404', 'Seite existiert noch nicht']); - $content .= '
';
- $content .= 'Du kannst die Saison-Planung momentan leider noch nicht in der App bearbeiten.
';
- $content .= 'Bis diese Funktion implementiert wurde, erstelle Deine Saison-Planung bitte auf unserer Website.
';
- $content .= 'Deine Saison-Planung wird dann automatisch synchronisiert und ist dann auch in dieser App verfügbar.
';
- $content .= 'Wir arbeiten daran, dass Du Deine Saison-Planung bald auch in der App bearbeiten kannst.
';
- $content .= '
Gib mindestens 3 Zeichen ein
'; + $('#menu-sailor').find('.content').find('.list-group').append(item); + } +} + +async function initSailors() { + sailors = []; + var dbSailors = await dbGetData('sailors'); + dbSailors.sort(function(a,b){ + return a.name.localeCompare(b.name); + }); + for (s in dbSailors) { + var item = ''; + item += '' + dbSailors[s].name + ''; + item += ''; + item += ''; + sailors.push({ + keywords: [dbSailors[s].name], + content: item + }); + } +} + +async function planningChangeCrew(sid = null) { + if (sid !== null) { + showLoader(); + var rid = $('#switch-planning-include').data('regatta'); + // remove sailor + var auth = { + id: localStorage.getItem('auth_id'), + hash: localStorage.getItem('auth_hash') + } + $.ajax({ + url: QUERY_URL + 'planning_remove_crew', + method: 'POST', + data: { + auth: auth, + regatta: rid, + sailor: sid + }, + error: function (xhr, status, error) { + if (xhr.status == 401) { + console.log('authentification failed'); + toastError('Authentifizierung fehlgeschlagen. Versuche es erneut.'); + } else if (xhr.status == 0) { + toastError('Du bist momentan offline.