summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlessandro Cosentino <cosenal@gmail.com>2012-05-23 13:02:27 -0400
committerAlessandro Cosentino <cosenal@gmail.com>2012-05-23 13:02:27 -0400
commit97f76e8cc62ede53709539595e835c4900f44d0c (patch)
tree90e9d84ac2fb3b04cda8561e7bc7f3b2f21d9612
parent5940e12a707e05aa458e96126da23e8bd8c3fdca (diff)
adds method to retrieve feed with all items from db
-rw-r--r--lib/feed.php16
-rw-r--r--lib/feedmapper.php10
-rw-r--r--lib/itemmapper.php34
-rw-r--r--templates/main.php3
4 files changed, 49 insertions, 14 deletions
diff --git a/lib/feed.php b/lib/feed.php
index ce1cc956a..e095cfa99 100644
--- a/lib/feed.php
+++ b/lib/feed.php
@@ -26,22 +26,26 @@
class OC_News_Feed {
private $url;
- private $feedid; //id of the feed in the database
+ private $id; //id of the feed in the database
private $spfeed; //encapsulate a SimplePie_Core object
private $items; //array that contains all the items of the feed
private $fetched;
- public function __construct($url){
+ public function __construct($url, $id = null){
$this->url = $url;
$this->spfeed = new SimplePie_Core();
$this->spfeed->set_feed_url( $url );
$this->spfeed->enable_cache( false );
$this->fetched = false;
+ if ($id !== null){
+ self::setId($id);
+ }
}
public function fetch(){
$this->spfeed->init();
$this->spfeed->handle_content_type();
+
$this->fetched = true;
}
@@ -50,11 +54,11 @@ class OC_News_Feed {
}
public function getId(){
- return $this->feedid;
+ return $this->id;
}
public function setId($id){
- $this->feedid = $id;
+ $this->id = $id;
}
public function getUrl(){
@@ -68,6 +72,10 @@ class OC_News_Feed {
return $this->spfeed->get_title();
}
+ public function setItems($items){
+ $this->items = $items;
+ }
+
public function getItems(){
if (!isset($this->items)){
if (!$this->isFetched()) {
diff --git a/lib/feedmapper.php b/lib/feedmapper.php
index d9b488595..12627b55d 100644
--- a/lib/feedmapper.php
+++ b/lib/feedmapper.php
@@ -39,7 +39,7 @@ class OC_News_FeedMapper {
$row = $result->fetchRow();
$url = $row['url'];
- $feed = new OC_News_Feed($url);
+ $feed = new OC_News_Feed($url, $id);
return $feed;
}
@@ -49,14 +49,18 @@ class OC_News_FeedMapper {
* @param id The id of the feed in the database table.
* @returns
*/
- public function findAllItems($id){
+ public function findWithItems($id){
$stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?');
$result = $stmt->execute(array($id));
$row = $result->fetchRow();
$url = $row['url'];
- $feed = new OC_News_Feed($url);
+ $feed = new OC_News_Feed($url, $id);
+ $itemMapper = new OC_News_ItemMapper($feed);
+ $items = $itemMapper->findAll();
+ $feed->setItems($items);
+
return $feed;
}
diff --git a/lib/itemmapper.php b/lib/itemmapper.php
index ed533f31d..ed8550376 100644
--- a/lib/itemmapper.php
+++ b/lib/itemmapper.php
@@ -37,14 +37,19 @@ class OC_News_ItemMapper {
* @brief Retrieve an item from the database
* @param id The id of the feed in the database table.
*/
- public function find($id){
- $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?');
- $result = $stmt->execute(array($id));
- $row = $result->fetchRow();
-
- $url = $row['url'];
- $title = $row['title'];
+ public function findAll(){
+ $feedid = $this->feed->getId();
+ $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feedid = ?');
+ $result = $stmt->execute(array($feedid));
+
+ $items = array();
+ while ($row = $result->fetchRow()) {
+ $url = $row['url'];
+ $title = $row['title'];
+ $items[] = new OC_News_Item($spitem);
+ }
+ return $items;
}
/**
@@ -80,4 +85,19 @@ class OC_News_ItemMapper {
$item->setId($itemid);
return $itemid;
}
+
+ /**
+ * @brief Retrieve an item from the database
+ * @param id The id of the feed in the database table.
+ */
+ public static function find($id){
+ $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?');
+ $result = $stmt->execute(array($id));
+ $row = $result->fetchRow();
+
+ $url = $row['url'];
+ $title = $row['title'];
+
+ }
+
} \ No newline at end of file
diff --git a/templates/main.php b/templates/main.php
index 88d1b9fb8..652f22f9b 100644
--- a/templates/main.php
+++ b/templates/main.php
@@ -7,6 +7,9 @@ $feed = $mapper->find($feed->getId());
echo "<br>" . $feed->getTitle() . "<br>";
$items = $feed->getItems();
+$feed2 = $mapper->findWithItems(45);
+echo "<br>" . $feed2->getTitle() . "<br>";
+
/*
$item = $feed->get_item(1);