From 4b154de4526d25fe810f1f371d9707789994e858 Mon Sep 17 00:00:00 2001 From: Alessandro Cosentino Date: Sat, 7 Jul 2012 21:52:41 -0400 Subject: [PATCH] mark as read item working now --- ajax/deletefeed.php | 2 +- ajax/markitem.php | 13 ++++---- lib/item.php | 7 ++++- lib/itemmapper.php | 64 ++++++++++++++++++++++++++-------------- templates/part.items.php | 2 +- 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/ajax/deletefeed.php b/ajax/deletefeed.php index a6f74f583..d31a9417d 100644 --- a/ajax/deletefeed.php +++ b/ajax/deletefeed.php @@ -17,7 +17,7 @@ OCP\JSON::callCheck(); $userid = OCP\USER::getUser(); -$feedid = trim($_POST['feedid']); +$feedid = $_POST['feedid']; $feedmapper = new OC_News_FeedMapper(); $success = $feedmapper->deleteById($feedid); diff --git a/ajax/markitem.php b/ajax/markitem.php index ea54d79e3..4d0a54fae 100644 --- a/ajax/markitem.php +++ b/ajax/markitem.php @@ -15,20 +15,21 @@ OCP\JSON::checkLoggedIn(); OCP\JSON::checkAppEnabled('news'); OCP\JSON::callCheck(); -$itemid = trim($_POST['itemid']); +$itemid = $_POST['itemid']; $itemmapper = new OC_News_ItemMapper(); $item = $itemmapper->find($itemid); -$feedid = $itemmapper->save($feed, 0); +$item->setRead(); +$success = $itemmapper->update($item); $l = OC_L10N::get('news'); -if(!$feedid) { - OCP\JSON::error(array('data' => array('message' => $l->t('Error adding folder.')))); - OCP\Util::writeLog('news','ajax/newfeed.php: Error adding feed: '.$_POST['feedurl'], OCP\Util::ERROR); +if(!$success) { + OCP\JSON::error(array('data' => array('message' => $l->t('Error marking item as read.')))); + OCP\Util::writeLog('news','ajax/markitem.php: Error marking item as read: '.$_POST['itemid'], OCP\Util::ERROR); exit(); } //TODO: replace the following with a real success case. see contact/ajax/createaddressbook.php for inspirations -OCP\JSON::success(array('data' => array('message' => $l->t('Feed added!')))); +OCP\JSON::success(array('data' => array('message' => $l->t('Item updated!')))); diff --git a/lib/item.php b/lib/item.php index ff3655e04..64ae368c3 100644 --- a/lib/item.php +++ b/lib/item.php @@ -36,7 +36,12 @@ class OC_News_Item { $this->url = $url; $this->guid = $guid; $this->body = $body; - $this->status |= StatusFlag::Unread; + if ($id == null) { + $this->status |= StatusFlag::Unread; + } + else { + $this->id = $id; + } } public function getGuid(){ diff --git a/lib/itemmapper.php b/lib/itemmapper.php index db2dd0056..95b88f6b3 100644 --- a/lib/itemmapper.php +++ b/lib/itemmapper.php @@ -18,6 +18,19 @@ class OC_News_ItemMapper { const tableName = '*PREFIX*news_items'; + public function fromRow($row){ + $url = $row['url']; + $title = $row['title']; + $guid = $row['guid']; + $status = $row['status']; + $body = $row['body']; + $id = $row['id']; + $item = new OC_News_Item($url, $title, $guid, $body, $id); + $item->setStatus($status); + + return $item; + } + /** * @brief Retrieve all the item corresponding to a feed from the database * @param feedid The id of the feed in the database table. @@ -28,13 +41,7 @@ class OC_News_ItemMapper { $items = array(); while ($row = $result->fetchRow()) { - $url = $row['url']; - $title = $row['title']; - $guid = $row['guid']; - $status = $row['status']; - $body = $row['body']; - $item = new OC_News_Item($url, $title, $guid, $body); - $item->setStatus($status); + $item = $this->fromRow($row); $items[] = $item; } @@ -56,6 +63,30 @@ class OC_News_ItemMapper { return $id; } + /** + * @brief Update the item after its status has changed + * @returns The item whose status has changed. + */ + public function update(OC_News_Item $item){ + + $itemid = $item->getId(); + $status = $item->getStatus(); + + $stmt = OCP\DB::prepare(' + UPDATE ' . self::tableName . + ' SET status = ? + WHERE id = ? + '); + + $params=array( + $status, + $itemid + ); + $stmt->execute($params); + + return true; + } + /** * @brief Save the feed and all its items into the database * @returns The id of the feed in the database table. @@ -99,20 +130,8 @@ class OC_News_ItemMapper { $itemid = OCP\DB::insertid(self::tableName); } - // update item: its status might have changed - // TODO: maybe make this a new function else { - $stmt = OCP\DB::prepare(' - UPDATE ' . self::tableName . - ' SET status = ? - WHERE id = ? - '); - - $params=array( - $status, - $itemid - ); - $stmt->execute($params); + update($item); } $item->setId($itemid); return $itemid; @@ -127,8 +146,9 @@ class OC_News_ItemMapper { $result = $stmt->execute(array($id)); $row = $result->fetchRow(); - $url = $row['url']; - $title = $row['title']; + $item = $this->fromRow($row); + + return $item; } diff --git a/templates/part.items.php b/templates/part.items.php index d1336b838..33bffc8ee 100644 --- a/templates/part.items.php +++ b/templates/part.items.php @@ -14,7 +14,7 @@ foreach($items as $item) { echo '
' . $title . '
'; } else { - echo '
' . $title . '
'; + echo '
' . $title . '
'; } echo '
' . $item->getBody() . '
'; }