Add items sorting by date

This commit is contained in:
Frédéric Guillot 2013-09-14 19:05:52 -04:00
parent 240a12631a
commit c2ce24a427
10 changed files with 86 additions and 31 deletions

View File

@ -88,7 +88,7 @@
}
// Set all items of the current page to the status read and redirect to the main page
function mark_items_as_read()
function mark_items_as_read(redirect)
{
var articles = document.getElementsByTagName("article");
var idlist = [];
@ -100,7 +100,7 @@
var request = new XMLHttpRequest();
request.onload = function() {
window.location.href = "?action=unread";
window.location.href = redirect;
};
request.open("POST", "?action=mark-items-as-read", true);
@ -499,7 +499,11 @@
break;
case 'mark-all-read':
e.preventDefault();
mark_items_as_read();
mark_items_as_read("?action=unread");
break;
case 'mark-feed-read':
e.preventDefault();
mark_items_as_read("?action=feed-items&feed_id=" + e.target.getAttribute("data-feed-id"));
break;
case 'original-link':
var item_id = e.target.getAttribute("data-item-id");

View File

@ -259,14 +259,19 @@ Router\get_action('feed-items', function() {
$offset = Request\int_param('offset', 0);
$nb_items = Model\count_feed_items($feed_id);
$feed = Model\get_feed($feed_id);
$order = Request\param('order', 'updated');
$direction = Request\param('direction', 'desc');
$items = Model\get_feed_items($feed_id, $offset, Model\get_config_value('items_per_page'), $order, $direction);
Response\html(Template\layout('feed_items', array(
'order' => $order,
'direction' => $direction,
'feed' => $feed,
'items' => Model\get_feed_items($feed_id, $offset, Model\get_config_value('items_per_page')),
'items' => $items,
'nb_items' => $nb_items,
'offset' => $offset,
'items_per_page' => Model\get_config_value('items_per_page'),
'menu' => 'feeds',
'menu' => 'feed-items',
'title' => '('.$nb_items.') '.$feed['title']
)));
});
@ -297,6 +302,15 @@ Router\get_action('mark-as-read', function() {
});
// Mark all unread items as read for a specific feed
Router\get_action('mark-feed-as-read', function() {
$feed_id = Request\int_param('feed_id');
Model\mark_feed_as_read($feed_id);
Response\redirect('?action=unread');
});
// Mark sent items id as read (Ajax request)
Router\post_action('mark-items-as-read', function(){
@ -782,13 +796,17 @@ Router\notfound(function() {
Model\autoflush();
$order = Request\param('order', 'updated');
$direction = Request\param('direction', 'desc');
$offset = Request\int_param('offset', 0);
$items = Model\get_items('unread', $offset, Model\get_config_value('items_per_page'));
$items = Model\get_items('unread', $offset, Model\get_config_value('items_per_page'), $order, $direction);
$nb_items = Model\count_items('unread');
if ($nb_items === 0) Response\redirect('?action=feeds&nothing_to_read=1');
Response\html(Template\layout('unread_items', array(
'order' => $order,
'direction' => $direction,
'items' => $items,
'nb_items' => $nb_items,
'nb_unread_items' => $nb_items,

View File

@ -1,6 +1,11 @@
<?php
return array(
'This subscription is empty, <a href="?action=unread">go back to unread items</a>' => 'Cet abonnement est vide, <a href="?action=unread">retourner à la liste des éléments non lus</a>',
'sort by date (%s)' => 'trier par date (%s)',
'most recent' => 'plus récents',
'older' => 'anciens d\'abord',
'Show only this subscription' => 'Montrer seulement cet abonnement',
'Go to unread' => 'Voir les éléments non lus',
'Go to bookmarks' => 'Voir les favoris',
'Go to history' => 'Voir l\'historique',

View File

@ -509,7 +509,7 @@ function disable_grabber_feed($feed_id)
}
function get_items($status, $offset = null, $limit = null)
function get_items($status, $offset = null, $limit = null, $order_column = 'updated', $order_direction = 'desc')
{
return \PicoTools\singleton('db')
->table('items')
@ -527,7 +527,7 @@ function get_items($status, $offset = null, $limit = null)
)
->join('feeds', 'id', 'feed_id')
->eq('status', $status)
->desc('updated')
->orderBy($order_column, $order_direction)
->offset($offset)
->limit($limit)
->findAll();
@ -583,12 +583,12 @@ function count_feed_items($feed_id)
return \PicoTools\singleton('db')
->table('items')
->eq('feed_id', $feed_id)
->in('status', array('read', 'unread'))
->eq('status', 'unread')
->count();
}
function get_feed_items($feed_id, $offset = null, $limit = null)
function get_feed_items($feed_id, $offset = null, $limit = null, $order_column = 'updated', $order_direction = 'desc')
{
return \PicoTools\singleton('db')
->table('items')
@ -604,9 +604,9 @@ function get_feed_items($feed_id, $offset = null, $limit = null)
'feeds.site_url'
)
->join('feeds', 'id', 'feed_id')
->in('status', array('read', 'unread'))
->eq('status', 'unread')
->eq('feed_id', $feed_id)
->desc('updated')
->orderBy($order_column, $order_direction)
->offset($offset)
->limit($limit)
->findAll();
@ -783,6 +783,26 @@ function mark_items_as_read(array $items_id)
}
// Mark all items of a feed as read
function mark_feed_as_read($feed_id)
{
\PicoTools\singleton('db')->startTransaction();
$items_id = \PicoTools\singleton('db')
->table('items')
->columns('items.id')
->eq('status', 'unread')
->eq('feed_id', $feed_id)
->listing('id', 'id');
foreach ($items_id as $id) {
set_item_read($id);
}
\PicoTools\singleton('db')->closeTransaction();
}
function mark_as_removed()
{
return \PicoTools\singleton('db')

View File

@ -24,7 +24,7 @@
<?= Helper\escape(Helper\summary(strip_tags($item['content']), 50, 300)) ?>
</p>
<p>
<a href="?action=feed-items&amp;feed_id=<?= $item['feed_id'] ?>"><?= Helper\escape($item['feed_title']) ?></a> |
<a href="?action=feed-items&amp;feed_id=<?= $item['feed_id'] ?>" title="<?= t('Show only this subscription') ?>"><?= Helper\escape($item['feed_title']) ?></a> |
<?= dt('%e %B %Y %k:%M', $item['updated']) ?> |
<span class="hide-mobile">

View File

@ -1,16 +1,26 @@
<?php if (empty($items)): ?>
<p class="alert alert-info"><?= t('No item') ?></p>
<p class="alert">
<?= t('This subscription is empty, <a href="?action=unread">go back to unread items</a>') ?>
</p>
<?php else: ?>
<div class="page-header">
<h2><?= Helper\escape($feed['title']) ?> (<?= $nb_items ?>)</h2>
<ul>
<li>
<a href="?action=feed-items&amp;feed_id=<?= $feed['id'] ?>&amp;order=updated&amp;direction=<?= $direction == 'asc' ? 'desc' : 'asc' ?>"><?= t('sort by date (%s)', $direction == 'desc' ? t('older') : t('most recent')) ?></a>
</li>
<li>
<a href="?action=mark-feed-as-read&amp;feed_id=<?= $feed['id'] ?>" data-action="mark-feed-read" data-feed-id="<?= $feed['id'] ?>"><?= t('mark all as read') ?></a>
</li>
</ul>
</div>
<section class="items" id="listing">
<?php foreach ($items as $item): ?>
<article id="item-<?= $item['id'] ?>" data-item-id="<?= $item['id'] ?>">
<article id="item-<?= $item['id'] ?>" data-item-id="<?= $item['id'] ?>" data-item-page="<?= $menu ?>" data-hide="true">
<h2>
<?= $item['bookmark'] ? '★ ' : '' ?>
<a
@ -43,11 +53,11 @@
<nav id="items-paging">
<?php if ($offset > 0): ?>
<a id="previous-page" href="?action=feed-items&amp;feed_id=<?= $feed['id'] ?>&amp;offset=<?= ($offset - $items_per_page) ?>"> <?= t('Previous page') ?></a>
<a id="previous-page" href="?action=feed-items&amp;feed_id=<?= $feed['id'] ?>&amp;offset=<?= ($offset - $items_per_page) ?>&amp;order=<?= $order ?>&amp;direction=<?= $direction ?>"> <?= t('Previous page') ?></a>
<?php endif ?>
&nbsp;
<?php if (($nb_items - $offset) > $items_per_page): ?>
<a id="next-page" href="?action=feed-items&amp;feed_id=<?= $feed['id'] ?>&amp;offset=<?= ($offset + $items_per_page) ?>"><?= t('Next page') ?> ⇾</a>
<a id="next-page" href="?action=feed-items&amp;feed_id=<?= $feed['id'] ?>&amp;offset=<?= ($offset + $items_per_page) ?>&amp;order=<?= $order ?>&amp;direction=<?= $direction ?>"><?= t('Next page') ?> ⇾</a>
<?php endif ?>
</nav>

View File

@ -28,7 +28,7 @@
<span id="loading-feed-<?= $feed['id'] ?>"></span>
<?php endif ?>
<a href="?action=feed-items&amp;feed_id=<?= $feed['id'] ?>"><?= Helper\escape($feed['title']) ?></a>
<a href="?action=feed-items&amp;feed_id=<?= $feed['id'] ?>" title="<?= t('Show only this subscription') ?>"><?= Helper\escape($feed['title']) ?></a>
<?php if ($feed['enabled']): ?>
<?php if ($feed['last_checked']): ?>

View File

@ -26,7 +26,7 @@
<?= Helper\escape(Helper\summary(strip_tags($item['content']), 50, 300)) ?>
</p>
<p>
<a href="?action=feed-items&amp;feed_id=<?= $item['feed_id'] ?>"><?= Helper\escape($item['feed_title']) ?></a> |
<a href="?action=feed-items&amp;feed_id=<?= $item['feed_id'] ?>" title="<?= t('Show only this subscription') ?>"><?= Helper\escape($item['feed_title']) ?></a> |
<span class="hide-mobile"><?= dt('%e %B %Y %k:%M', $item['updated']) ?> |</span>

View File

@ -8,9 +8,10 @@
<h2><?= t('<span id="page-counter">%s</span>unread items', isset($nb_items) ? $nb_items.' ' : '') ?></h2>
<ul>
<li>
<a href="?action=mark-as-read" data-action="mark-all-read">
<?= t('mark all as read') ?>
</a>
<a href="?action=unread&amp;order=updated&amp;direction=<?= $direction == 'asc' ? 'desc' : 'asc' ?>"><?= t('sort by date (%s)', $direction == 'desc' ? t('older') : t('most recent')) ?></a>
</li>
<li>
<a href="?action=mark-as-read" data-action="mark-all-read"><?= t('mark all as read') ?></a>
</li>
</ul>
</div>
@ -32,7 +33,7 @@
<?= Helper\escape(Helper\summary(strip_tags($item['content']), 50, 300)) ?>
</p>
<p>
<a href="?action=feed-items&amp;feed_id=<?= $item['feed_id'] ?>"><?= Helper\escape($item['feed_title']) ?></a> |
<a href="?action=feed-items&amp;feed_id=<?= $item['feed_id'] ?>" title="<?= t('Show only this subscription') ?>"><?= Helper\escape($item['feed_title']) ?></a> |
<span class="hide-mobile"><?= dt('%e %B %Y %k:%M', $item['updated']) ?> |</span>
<span class="hide-mobile">
@ -67,11 +68,11 @@
<nav id="items-paging">
<?php if ($offset > 0): ?>
<a id="previous-page" href="?action=unread&amp;offset=<?= ($offset - $items_per_page) ?>"> <?= t('Previous page') ?></a>
<a id="previous-page" href="?action=unread&amp;offset=<?= ($offset - $items_per_page) ?>&amp;order=<?= $order ?>&amp;direction=<?= $direction ?>"> <?= t('Previous page') ?></a>
<?php endif ?>
&nbsp;
<?php if (($nb_items - $offset) > $items_per_page): ?>
<a id="next-page" href="?action=unread&amp;offset=<?= ($offset + $items_per_page) ?>"><?= t('Next page') ?> ⇾</a>
<a id="next-page" href="?action=unread&amp;offset=<?= ($offset + $items_per_page) ?>&amp;order=<?= $order ?>&amp;direction=<?= $direction ?>"><?= t('Next page') ?> ⇾</a>
<?php endif ?>
</nav>

View File

@ -260,13 +260,10 @@ class Table
}
public function orderBy($column)
public function orderBy($column, $order = 'ASC')
{
if ($column[0] == '-') {
$order = 'DESC';
} else {
$order = 'ASC';
}
$order = strtoupper($order);
$order = $order === 'ASC' || $order === 'DESC' ? $order : 'ASC';
if ($this->sql_order === '') {
$this->sql_order = ' ORDER BY '.$this->db->escapeIdentifier($column).' '.$order;