From c9eb6a47a674d726d09e351d3f46536af0ad3ab0 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Wed, 17 Jul 2013 18:55:28 -0400 Subject: [PATCH] Improve mark items as read --- assets/js/app.js | 18 ++++++++++-------- index.php | 10 ++++------ model.php | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index cfd0509..a2f2b8b 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -27,15 +27,16 @@ request.send(); } - function mark_all_as_read() + + function mark_items_as_read() { - var $articles = document.getElementsByTagName("article"); + var articles = document.getElementsByTagName("article"); var idlist = []; - for (var i=0;i<$articles.length;i++) - { - idlist.push($articles[i].getAttribute("data-item-id")); + + for (var i = 0, ilen = articles.length; i < ilen; i++) { + idlist.push(articles[i].getAttribute("data-item-id")); } - + var request = new XMLHttpRequest(); request.onload = function() { @@ -43,10 +44,11 @@ window.location.href = "?action=unread"; }; - request.open("POST", "?action=mark-all-read", true); + request.open("POST", "?action=mark-items-as-read", true); request.send(JSON.stringify(idlist)); } + function mark_as_read(item_id) { var request = new XMLHttpRequest(); @@ -436,7 +438,7 @@ break; case 'mark-all-read': e.preventDefault(); - mark_all_as_read(); + mark_items_as_read(); break; case 'original-link': var item_id = e.target.getAttribute("data-item-id"); diff --git a/index.php b/index.php index e604b0a..7d36bb7 100644 --- a/index.php +++ b/index.php @@ -332,17 +332,15 @@ Router\get_action('mark-as-read', function() { Response\redirect('?action=unread'); }); -Router\post_action('mark-all-read', function(){ - $values = Request\values(); - foreach($values as $value) { - $id = Model\decode_item_id($value); - Model\set_item_read($id); - } +// Mark sent items id as read (Ajax request) +Router\post_action('mark-items-as-read', function(){ + Model\mark_items_as_read(Request\values()); Response\json(array('OK')); }); + // Confirmation box to flush history Router\get_action('confirm-flush-history', function() { diff --git a/model.php b/model.php index 2436dc7..b4adf02 100644 --- a/model.php +++ b/model.php @@ -515,6 +515,7 @@ function switch_item_status($id) } +// Mark all items as read function mark_as_read() { \PicoTools\singleton('db') @@ -524,6 +525,19 @@ function mark_as_read() } +// Mark only specified items as read +function mark_items_as_read(array $items_id) +{ + \PicoTools\singleton('db')->startTransaction(); + + foreach($items_id as $encoded_id) { + set_item_read(decode_item_id($encoded_id)); + } + + \PicoTools\singleton('db')->closeTransaction(); +} + + function mark_as_removed() { \PicoTools\singleton('db')