summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--appinfo/database.xml9
-rw-r--r--appinfo/version2
-rw-r--r--index.php2
-rw-r--r--js/jquery.timeago.js156
-rw-r--r--js/news.js2
-rw-r--r--lib/item.php16
-rw-r--r--lib/itemmapper.php17
-rw-r--r--lib/utils.php32
-rw-r--r--templates/part.items.php3
9 files changed, 221 insertions, 18 deletions
diff --git a/appinfo/database.xml b/appinfo/database.xml
index 1d5cef5b9..af5e25668 100644
--- a/appinfo/database.xml
+++ b/appinfo/database.xml
@@ -162,11 +162,10 @@
<notnull>false</notnull>
</field>
<field>
- <name>date</name>
- <type>integer</type>
- <default></default>
- <notnull>false</notnull>
- <unsigned>true</unsigned>
+ <name>pub_date</name>
+ <type>timestamp</type>
+ <default>0000-00-00 00:00:00</default>
+ <notnull>false</notnull>
</field>
<field>
<name>body</name>
diff --git a/appinfo/version b/appinfo/version
index c7930257d..986084f36 100644
--- a/appinfo/version
+++ b/appinfo/version
@@ -1 +1 @@
-7 \ No newline at end of file
+7.1 \ No newline at end of file
diff --git a/index.php b/index.php
index 8e64d48af..310e68741 100644
--- a/index.php
+++ b/index.php
@@ -18,6 +18,8 @@ OCP\App::checkAppEnabled('news');
OCP\App::setActiveNavigationEntry('news');
OCP\Util::addScript('news','news');
+OCP\Util::addScript('news','jquery.timeago');
+
OCP\Util::addStyle('news','news');
OCP\Util::addStyle('news','settings');
diff --git a/js/jquery.timeago.js b/js/jquery.timeago.js
new file mode 100644
index 000000000..bc2e368be
--- /dev/null
+++ b/js/jquery.timeago.js
@@ -0,0 +1,156 @@
+/**
+ * Timeago is a jQuery plugin that makes it easy to support automatically
+ * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
+ *
+ * @name timeago
+ * @version 0.11.4
+ * @requires jQuery v1.2.3+
+ * @author Ryan McGeary
+ * @license MIT License - http://www.opensource.org/licenses/mit-license.php
+ *
+ * For usage and examples, visit:
+ * http://timeago.yarp.com/
+ *
+ * Copyright (c) 2008-2012, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org)
+ */
+(function($) {
+ $.timeago = function(timestamp) {
+ if (timestamp instanceof Date) {
+ return inWords(timestamp);
+ } else if (typeof timestamp === "string") {
+ return inWords($.timeago.parse(timestamp));
+ } else if (typeof timestamp === "number") {
+ return inWords(new Date(timestamp));
+ } else {
+ return inWords($.timeago.datetime(timestamp));
+ }
+ };
+ var $t = $.timeago;
+
+ $.extend($.timeago, {
+ settings: {
+ refreshMillis: 60000,
+ allowFuture: false,
+ strings: {
+ prefixAgo: null,
+ prefixFromNow: null,
+ suffixAgo: "ago",
+ suffixFromNow: "from now",
+ seconds: "less than a minute",
+ minute: "about a minute",
+ minutes: "%d minutes",
+ hour: "about an hour",
+ hours: "about %d hours",
+ day: "a day",
+ days: "%d days",
+ month: "about a month",
+ months: "%d months",
+ year: "about a year",
+ years: "%d years",
+ wordSeparator: " ",
+ numbers: []
+ }
+ },
+ inWords: function(distanceMillis) {
+ var $l = this.settings.strings;
+ var prefix = $l.prefixAgo;
+ var suffix = $l.suffixAgo;
+ if (this.settings.allowFuture) {
+ if (distanceMillis < 0) {
+ prefix = $l.prefixFromNow;
+ suffix = $l.suffixFromNow;
+ }
+ }
+
+ var seconds = Math.abs(distanceMillis) / 1000;
+ var minutes = seconds / 60;
+ var hours = minutes / 60;
+ var days = hours / 24;
+ var years = days / 365;
+
+ function substitute(stringOrFunction, number) {
+ var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
+ var value = ($l.numbers && $l.numbers[number]) || number;
+ return string.replace(/%d/i, value);
+ }
+
+ var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
+ seconds < 90 && substitute($l.minute, 1) ||
+ minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
+ minutes < 90 && substitute($l.hour, 1) ||
+ hours < 24 && substitute($l.hours, Math.round(hours)) ||
+ hours < 42 && substitute($l.day, 1) ||
+ days < 30 && substitute($l.days, Math.round(days)) ||
+ days < 45 && substitute($l.month, 1) ||
+ days < 365 && substitute($l.months, Math.round(days / 30)) ||
+ years < 1.5 && substitute($l.year, 1) ||
+ substitute($l.years, Math.round(years));
+
+ var separator = $l.wordSeparator === undefined ? " " : $l.wordSeparator;
+ return $.trim([prefix, words, suffix].join(separator));
+ },
+ parse: function(iso8601) {
+ var s = $.trim(iso8601);
+ s = s.replace(/\.\d+/,""); // remove milliseconds
+ s = s.replace(/-/,"/").replace(/-/,"/");
+ s = s.replace(/T/," ").replace(/Z/," UTC");
+ s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
+ return new Date(s);
+ },
+ datetime: function(elem) {
+ var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
+ return $t.parse(iso8601);
+ },
+ isTime: function(elem) {
+ // jQuery's `is()` doesn't play well with HTML5 in IE
+ return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
+ }
+ });
+
+ $.fn.timeago = function() {
+ var self = this;
+ self.each(refresh);
+
+ var $s = $t.settings;
+ if ($s.refreshMillis > 0) {
+ setInterval(function() { self.each(refresh); }, $s.refreshMillis);
+ }
+ return self;
+ };
+
+ function refresh() {
+ var data = prepareData(this);
+ if (!isNaN(data.datetime)) {
+ $(this).text(inWords(data.datetime));
+ }
+ return this;
+ }
+
+ function prepareData(element) {
+ element = $(element);
+ if (!element.data("timeago")) {
+ element.data("timeago", { datetime: $t.datetime(element) });
+ var text = $.trim(element.text());
+ if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
+ element.attr("title", text);
+ }
+ }
+ return element.data("timeago");
+ }
+
+ function inWords(date) {
+ var inwords = $t.inWords(distance(date))
+ if (inwords == 'a day ago') {
+ return 'yesterday';
+ }
+ return $t.inWords(distance(date));
+ }
+
+ function distance(date) {
+ return (new Date().getTime() - date.getTime());
+ }
+
+ // fix for IE6 suckage
+ document.createElement("abbr");
+ document.createElement("time");
+}(jQuery));
diff --git a/js/news.js b/js/news.js
index 6fb67e21d..651df7c62 100644
--- a/js/news.js
+++ b/js/news.js
@@ -692,6 +692,8 @@ $(document).ready(function(){
})
});
+
+ $("time.timeago").timeago();
});
diff --git a/lib/item.php b/lib/item.php
index acef41c92..e64eaa044 100644
--- a/lib/item.php
+++ b/lib/item.php
@@ -33,7 +33,8 @@ class Item {
private $status; //a bit-field set with status flags
private $id; //id of the item in the database table
private $author;
-
+ private $date; //date is stored in the Unix format
+
public function __construct($url, $title, $guid, $body, $id = null){
$this->title = $title;
$this->url = $url;
@@ -98,6 +99,10 @@ class Item {
public function setStatus($status){
$this->status = $status;
}
+
+ /* change the following method with set/get magic methods
+ * http://www.php.net/manual/en/language.oop5.overloading.php#object.get
+ */
public function getTitle(){
return $this->title;
@@ -130,4 +135,13 @@ class Item {
public function setAuthor($author){
$this->author = $author;
}
+
+ public function getDate(){
+ return $this->date;
+ }
+
+ //TODO: check if the parameter is in the Unix format
+ public function setDate($date){
+ $this->date = $date;
+ }
}
diff --git a/lib/itemmapper.php b/lib/itemmapper.php
index 95488b14f..dcce2a640 100644
--- a/lib/itemmapper.php
+++ b/lib/itemmapper.php
@@ -37,14 +37,12 @@ class ItemMapper {
$url = $row['url'];
$title = $row['title'];
$guid = $row['guid'];
- $status = $row['status'];
$body = $row['body'];
$id = $row['id'];
$item = new Item($url, $title, $guid, $body, $id);
- $item->setStatus($status);
- $date = $row['date'];
- $author = $row['author'];
- $item->setAuthor($author);
+ $item->setStatus($row['status']);
+ $item->setAuthor($row['author']);
+ $item->setDate(Utils::dbtimestampToUnixtime($row['pub_date']));
return $item;
}
@@ -54,7 +52,7 @@ class ItemMapper {
* @param feedid The id of the feed in the database table.
*/
public function findAll($feedid){
- $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feed_id = ?');
+ $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feed_id = ? ORDER BY pub_date DESC');
$result = $stmt->execute(array($feedid));
$items = array();
@@ -153,8 +151,8 @@ class ItemMapper {
$stmt = \OCP\DB::prepare('
INSERT INTO ' . self::tableName .
- '(url, title, body, guid, guid_hash, feed_id, status)
- VALUES (?, ?, ?, ?, ?, ?, ?)
+ '(url, title, body, guid, guid_hash, pub_date, feed_id, status)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
');
if(empty($title)) {
@@ -167,12 +165,15 @@ class ItemMapper {
$body = $l->t('no body');
}
+ $pub_date = Utils::unixtimeToDbtimestamp($item->getDate());
+
$params=array(
$item->getUrl(),
$title,
$body,
$guid,
$guid_hash,
+ $pub_date,
$feedid,
$status
);
diff --git a/lib/utils.php b/lib/utils.php
index 7ad9f1929..1ecc92245 100644
--- a/lib/utils.php
+++ b/lib/utils.php
@@ -17,7 +17,27 @@ namespace OCA\News;
require_once('news/3rdparty/SimplePie/autoloader.php');
class Utils {
-
+
+ /**
+ * @brief Transform a date from UNIX timestamp format to MDB2 timestamp format
+ * @param dbtimestamp
+ * @returns
+ */
+ public static function unixtimeToDbtimestamp($unixtime) {
+ $dt = \DateTime::createFromFormat('U', $unixtime);
+ return $dt->format('Y-m-d H:i:s');
+ }
+
+ /**
+ * @brief Transform a date from MDB2 timestamp format to UNIX timestamp format
+ * @param dbtimestamp
+ * @returns
+ */
+ public static function dbtimestampToUnixtime($dbtimestamp) {
+ $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $dbtimestamp);
+ return $dt->format('U');
+ }
+
/**
* @brief Fetch a feed from remote
* @param url remote url of the feed
@@ -32,7 +52,7 @@ class Utils {
return null;
}
- //I understand this try-catch sucks, but SimplePie gives weird errors sometimes
+ //temporary try-catch to bypass SimplePie bugs
try {
$spfeed->handle_content_type();
$title = $spfeed->get_title();
@@ -44,11 +64,17 @@ class Utils {
$itemTitle = $spitem->get_title();
$itemGUID = $spitem->get_id();
$itemBody = $spitem->get_content();
- $itemAuthor = $spitem->get_author();
$item = new Item($itemUrl, $itemTitle, $itemGUID, $itemBody);
+
+ $itemAuthor = $spitem->get_author();
if ($itemAuthor !== null) {
$item->setAuthor($itemAuthor->get_name());
}
+
+ //date in Item is stored in UNIX timestamp format
+ $itemDate = $spitem->get_date('U');
+ $item->setDate($itemDate);
+
$items[] = $item;
}
}
diff --git a/templates/part.items.php b/templates/part.items.php
index 399274e30..16b7d5b67 100644
--- a/templates/part.items.php
+++ b/templates/part.items.php
@@ -43,6 +43,9 @@ foreach($items as $item) {
echo '<h1 class="item_title"><a target="_blank" href="' . $item->getUrl() . '">' . $item->getTitle() . '</a></h1>';
echo '<h2 class="item_author">' . $l->t('from') . ' ' . parse_url($item->getUrl(), PHP_URL_HOST) . '</h2>';
+
+ echo '<h2 class="item_date"><time class="timeago" datetime="' .
+ date('c', $item->getDate()) . '">' . date('F j, Y, g:i a', $item->getDate()) . '</time>' . '</h2>';
echo '<div class="body">' . $item->getBody() . '</div>';