function myJsonCategory(Qcategory, div, addTagCategory) { $.mobile.loading('show'); FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts WHERE category = ? ORDER BY date COLLATE NOCASE DESC;", [Qcategory], function (resultSet) { var items = []; for (var i = 0; i < resultSet.rows.length; i++) { var val = JSON.parse(resultSet.rows.item(i).post); var id = val.id; var time = val.modified; var title = val.title.rendered; var url = val.guid.rendered; var splitTime = time.split("T"); var cleanTime = splitTime[0]; var tag = ''; var cattag = ''; /* if (addTagCategory) { $.each(val.tags, function (index, value) { if (value == 43) { } if (value == 36) { } }); //key mi dice l'indice' items.push("
" + content + "
"); } } function drawEvent(div_title, div_content, resultSet){ for (var i = 0; i < resultSet.rows.length; i++) { var val = resultSet.rows.item(i); var evInitDate = EVENTS.formatDate(val.initDate, EVENTS.displayDateFormat); var evEndDate = EVENTS.formatDate(val.endDate, EVENTS.displayDateFormat); var id = val.id; var title = val.title; var url = val.url; var content = val.description; var geo = val.geo; $(div_title).html(title); //key mi dice l'indice' $(div_content).html("" + content + "
"); } } function myJsonPost(Qid, div_title, div_content, _class) { var table = "xm24_posts"; if(_class){ table = EVENTS.table.single; } FONTE.dbHandler.executeSql("SELECT * FROM " + table + " WHERE id = ?;", [Qid], function(resultSet) { if(!_class){ drawPost(div_title, div_content, resultSet); } else { drawEvent(div_title, div_content, resultSet); } }, function (error) { console.log(error); }); } function myJsonPage(Qid, div_title, div_content) { $.mobile.loading('show'); var pageId = parseInt(Qid); FONTE.dbHandler.executeSql("SELECT * FROM xm24_pages WHERE id = ?;", [pageId], function (resultSet) { for (var i = 0; i < resultSet.rows.length; i++) { var data = resultSet.rows.item(i); var id = data.id; var time = data.time; var title = data.title; var content = data.page; content = findImg(content); $(div_title).html(title); //key mi dice l'indice' $(div_content).html("" + content + "
"); } }); $.mobile.loading('hide'); } function myJsonRandomPost(div_title, div_content) { $.mobile.loading('show'); FONTE.dbHandler.executeSql("SELECT * FROM xm24_posts ORDER BY RANDOM() LIMIT 1;", [], function (resultSet) { for (var i = 0; i < resultSet.rows.length; i++) { var val = JSON.parse(resultSet.rows.item(i).post); var id = val.id; var time = val.modified; var title = val.title.rendered; var url = val.guid.rendered; var content = val.content.rendered; content = findImg(content); $(div_title).html(title); //key mi dice l'indice' $(div_content).html("" + content + "
"); } }); $.mobile.loading('hide'); } function createDB() { //crea il databse per i posts se non esiste FONTE.dbHandler = window.sqlitePlugin.openDatabase({ name: FONTE.dbName, location: 'default' }, function () { FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_posts (id INTEGER PRIMARY KEY, title, date, post)'); FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_cats (catid INTEGER PRIMARY KEY, title, cat)'); FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_cats_to_posts (id, catid, PRIMARY KEY (id, catid))'); 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)'); FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_events_single (id INTEGER PRIMARY KEY, title, description, initDate, endDate, url, geo)'); FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_tags_to_events (id, tagid)'); FONTE.dbHandler.executeSql('CREATE TABLE IF NOT EXISTS xm24_events_repeat (id INTEGER PRIMARY KEY, title, description, initDate, endDate, url, geo)'); }, function () { console.log('error'); console.log(arguments); }); } function insertOrUpdateCatToPost(id, catId) { FONTE.dbHandler.executeSql('INSERT OR REPLACE INTO xm24_cats_to_posts VALUES (?,?)', [id, catId], 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 deleteDB() { window.sqlitePlugin.deleteDatabase({ name: FONTE.dbName, location: 'default' }); var storage = window.localStorage; storage.setItem('db_update_date', null); storage.setItem('db_lastnews_date', null); createDB(); } function getSinglePageRecur(url, category, pagenum) { $.ajax({ dataType: "json", url: url + "&categories=" + category + "&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.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.neededPostsCount === 0) { toastr.info('Nessun nuovo articolo.'); 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); //feedback $("#aggiorna").attr("src", "./image/aggiorna.png"); } $.each(data, function (key, val) { var id = val.id; var time = val.date; var title = val.title.rendered; var tags = val.tags; var blob = JSON.stringify(val); FONTE.dbHandler.executeSql('INSERT OR REPLACE INTO xm24_posts VALUES (?,?,?,?,?)', [id, title, time, category, blob], function (resultSet) { FONTE.updatedPostsCount += 1; console.log('Voci aggiornate: ' + FONTE.updatedPostsCount); if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.updatedPostsCount >= FONTE.neededPostsCount) { //window.localStorage.setItem('has_run', 'true'); 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); toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.'); //feedback $("#aggiorna").attr("src", "./image/aggiorna.png"); displayLastUpdateDate(); } //manca il caso in cui sono stati cancellati dei post online, vanno cancellati anche dall'app. }, function (error) { FONTE.updatedPostsCount += 1; if (FONTE.updatedCategoriesCount >= FONTE.categoriesList.length && FONTE.updatedPostsCount >= FONTE.neededPostsCount) { toastr.info('Aggiornati ' + FONTE.updatedPostsCount + ' articoli.'); //feedback $("#aggiorna").attr("src", "./image/aggiorna.png"); } }); $.each(tags, function (key, val) { aggiornaTagsToPosts(id, val); }); }); if (pagenum < totalPages) { pagenum += 1; getSinglePageRecur(url, category, pagenum); } }, error: JSONErrorHandler }); }