230 lines
9.3 KiB
JavaScript
230 lines
9.3 KiB
JavaScript
var REST_PARAMS = REST_PARAMS || {};
|
|
REST_PARAMS.per_page = 'per_page=10';
|
|
FONTE.page_loaded = 0;
|
|
|
|
function aggiornaPages() {
|
|
var jsonurl = weburl + "wp-json/wp/v2/pages/";
|
|
$.ajax({
|
|
dataType: "json"
|
|
, url: jsonurl
|
|
, success: function (data) {
|
|
$.each(data, function (key, val) {
|
|
var id = val.id;
|
|
var time = val.modified;
|
|
var title = val.title.rendered;
|
|
var content = val.content.rendered;
|
|
FONTE.dbHandler.executeSql('INSERT INTO xm24_pages VALUES (?,?,?,?)', [id, title, time, content], function (resultSet) {
|
|
console.log('resultSet.insertId: ' + resultSet.insertId);
|
|
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
|
|
}, function (error) {
|
|
console.log('INSERT error: ' + error.message);
|
|
});
|
|
});
|
|
}
|
|
, error: JSONErrorHandler
|
|
});
|
|
};
|
|
|
|
function aggiornaSingleCategory(category) {
|
|
//resetta la variabile d'errore per le chiamate ajax asincrone
|
|
FONTE.ajaxError = false;
|
|
|
|
var jsonurl = weburl + "wp-json/wp/v2/posts?" + REST_PARAMS.per_page;
|
|
|
|
var storage = window.localStorage;
|
|
var lastUpdate = storage.getItem('db_update_date');
|
|
|
|
if (lastUpdate) jsonurl += "&filter[date_query][column]=post_modified_gmt&filter[date_query][after]=" + lastUpdate;
|
|
getSinglePageRecur(jsonurl, category, 1)
|
|
};
|
|
|
|
function aggiornaTagsToPosts(id, tagId) {
|
|
FONTE.dbHandler.executeSql('INSERT INTO xm24_tags_to_posts VALUES (?,?)', [id, tagId], function (resultSet) {
|
|
console.log('resultSet.insertId: ' + resultSet.insertId);
|
|
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
|
|
}, function (error) {
|
|
console.log('INSERT error: ' + error.message);
|
|
//feedback
|
|
$("#aggiorna").attr("src", "./image/aggiorna.png");
|
|
});
|
|
}
|
|
|
|
function aggiornaTags(pagenum) {
|
|
var jsonurl = weburl + "wp-json/wp/v2/tags?per_page=100&page=" + pagenum;
|
|
$.ajax({
|
|
dataType: "json"
|
|
, url: jsonurl
|
|
, success: function (data, status, xhr) {
|
|
var totalPages = parseInt(xhr.getResponseHeader("X-WP-TotalPages"));
|
|
$.each(data, function (key, val) {
|
|
var name = val.name;
|
|
var slug = val.slug;
|
|
var id = val.id;
|
|
var blob = JSON.stringify(val);
|
|
FONTE.dbHandler.executeSql('INSERT INTO xm24_tags VALUES (?,?,?)', [id, slug, blob], function (resultSet) {
|
|
console.log('resultSet.insertId: ' + resultSet.insertId);
|
|
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
|
|
}, function (error) {
|
|
console.log('INSERT error: ' + error.message);
|
|
});
|
|
});
|
|
pagenum += 1
|
|
if (pagenum <= totalPages) aggiornaTags(pagenum)
|
|
}
|
|
, error: JSONErrorHandler
|
|
});
|
|
};
|
|
|
|
function aggiornaora() {
|
|
$("#aggiornaora").popup("open");
|
|
};
|
|
|
|
function resetAndUpdate() {
|
|
var storage = window.localStorage;
|
|
//resetto la data e droppo le tabelle
|
|
storage.setItem('db_update_date', null);
|
|
FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_posts;');
|
|
FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_posts (id INTEGER PRIMARY KEY, title, date, category, post)');
|
|
partialUpdate();
|
|
}
|
|
|
|
function update() {
|
|
FONTE.dbHandler.executeSql('SELECT COUNT(*) AS numPosts FROM xm24_posts', [], function (resultSet) {
|
|
var numPosts = resultSet.rows.item(0).numPosts;
|
|
if (numPosts < 300) { //questo numero e' scelto in maniera arbitraria, ricordati Cri.
|
|
resetAndUpdate()
|
|
} else {
|
|
partialUpdate();
|
|
}
|
|
});
|
|
}
|
|
|
|
function partialUpdate() {
|
|
var storage = window.localStorage;
|
|
var jsonurl = weburl + "wp-json/wp/v2/posts?" + REST_PARAMS.per_page + "&categories=" + FONTE.categoriesInfo.join(',');
|
|
|
|
FONTE.updatedCategoriesCount = 0;
|
|
FONTE.updatedPostsCount = 0;
|
|
FONTE.neededPostsCount = 0;
|
|
|
|
FONTE.ajaxError = false;
|
|
|
|
//feedback
|
|
$("#aggiorna").attr("src", "./image/icona_aggiorna.gif");
|
|
|
|
var lastUpdate = storage.getItem('db_update_date');
|
|
if (lastUpdate)
|
|
jsonurl += "&after=" + lastUpdate;
|
|
//jsonurl += "&filter[date_query][column]=post_modified_gmt&filter[date_query][after]=" + lastUpdate;
|
|
|
|
var firstTime = storage.getItem('db_lastnews_firsttime');
|
|
if (firstTime == null) storage.setItem('db_lastnews_firsttime', 'yes');
|
|
if (firstTime == 'yes') storage.setItem('db_lastnews_firsttime', 'no');
|
|
|
|
mygetSinglePageRecur(jsonurl, 1, FONTE.categoriesInfo);
|
|
};
|
|
|
|
var updateLocalStorageDate = function(){
|
|
var date = new Date();
|
|
|
|
var currentUpdate = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2);
|
|
currentUpdate += 'T' + ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ':' + ('0' + date.getSeconds()).slice(-2);
|
|
|
|
var lastUpdate = window.localStorage.getItem('db_update_date')
|
|
|
|
window.localStorage.setItem('db_update_date', currentUpdate);
|
|
window.localStorage.setItem('db_lastnews_date', lastUpdate);
|
|
}
|
|
|
|
function mygetSinglePageRecur(url, pagenum, categories) {
|
|
console.log('url:'+url);
|
|
$.ajax({
|
|
dataType: "json"
|
|
, url: url + "&page=" + pagenum
|
|
, success: function (data, textStatus, jqXHR) {
|
|
var totalPages = jqXHR.getResponseHeader('X-WP-TotalPages');
|
|
if (pagenum == totalPages || totalPages == 0)
|
|
FONTE.updatedCategoriesCount += 1;
|
|
|
|
FONTE.neededPostsCount += data.length;
|
|
if (FONTE.neededPostsCount == 0) {
|
|
toastr.info('Nessun nuovo articolo.');
|
|
updateLocalStorageDate();
|
|
$("#aggiorna").attr("src", "./image/aggiorna.png");
|
|
if(FONTE.info_page_loaded == 0) {
|
|
checkloader(categories);
|
|
}
|
|
}
|
|
|
|
var n_error = 0
|
|
$.each(data, function (key, val) {
|
|
var id = val.id;
|
|
var time = val.modified;
|
|
var title = val.title.rendered;
|
|
var tags = val.tags;
|
|
var postCategories = val.categories;
|
|
var blob = JSON.stringify(val);
|
|
|
|
FONTE.dbHandler.executeSql('INSERT OR REPLACE INTO xm24_posts VALUES (?,?,?,?)', [id, title, time, blob], function (resultSet) {
|
|
FONTE.updatedPostsCount++;
|
|
console.log('Voci aggiornate: ' + FONTE.updatedPostsCount);
|
|
|
|
if (n_error + FONTE.updatedPostsCount >= FONTE.neededPostsCount) {
|
|
updateLocalStorageDate();
|
|
toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.');
|
|
checkloader(categories);
|
|
} //manca il caso in cui sono stati cancellati dei post online, vanno cancellati anche dall'app.
|
|
}, function (error) {
|
|
n_error++;
|
|
if (n_error + FONTE.updatedPostsCount >= FONTE.neededPostsCount && FONTE.updatedPostsCount > 0 ) {
|
|
updateLocalStorageDate();
|
|
toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.');
|
|
checkloader(categories);
|
|
}
|
|
});
|
|
|
|
// $.each(tags, function (key, val) {
|
|
// aggiornaTagsToPosts(id, val);
|
|
// });
|
|
|
|
$.each(postCategories, function (key, val) {
|
|
insertOrUpdateCatToPost(id, val);
|
|
});
|
|
});
|
|
// if (pagenum < totalPages) {
|
|
// pagenum += 1;
|
|
// getSinglePageRecur(url, category, pagenum)
|
|
// }
|
|
}
|
|
, error: JSONErrorHandler
|
|
});
|
|
};
|
|
|
|
// function partialUpdate() {
|
|
// //feedback
|
|
// $("#aggiorna").attr("src", "./image/icona_aggiorna.gif");
|
|
// var storage = window.localStorage;
|
|
// var jsonurl = weburl + "wp-json/wp/v2/posts?" + REST_PARAMS.per_page + "&categories=";
|
|
//
|
|
// FONTE.updatedCategoriesCount = 0;
|
|
// FONTE.updatedPostsCount = 0;
|
|
// FONTE.neededPostsCount = 0;
|
|
// for (var index in FONTE.categoriesList) {
|
|
// aggiornaSingleCategory(FONTE.categoriesList[index])
|
|
// }
|
|
// FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_tags;');
|
|
// // FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_tags_to_posts;');
|
|
// FONTE.dbHandler.executeSql('DROP TABLE IF EXISTS xm24_pages;');
|
|
// FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags (id INTEGER PRIMARY KEY, title, tag)');
|
|
// // FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags_to_posts (id, tagid)');
|
|
// FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_pages (id INTEGER PRIMARY KEY, title, date, page)');
|
|
// aggiornaTags(1);
|
|
// aggiornaPages();
|
|
//
|
|
// var firstTime = storage.getItem('db_lastnews_firsttime');
|
|
// if (firstTime == null) storage.setItem('db_lastnews_firsttime', 'yes');
|
|
// if (firstTime == 'yes') storage.setItem('db_lastnews_firsttime', 'no');
|
|
// $("#benvenuta").popup("close");
|
|
// checkloader();
|
|
//
|
|
// };
|