From 4353c8ab583b7b10369290dec016883a35b2c185 Mon Sep 17 00:00:00 2001 From: Alessandro Cosentino Date: Tue, 15 May 2012 14:49:53 -0400 Subject: decoupled model class Feed and mapper class FeedMapper --- appinfo/app.php | 1 + lib/feed.php | 57 +++++++++++-------------------------------- lib/feedmapper.php | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ templates/main.php | 8 +++++- 4 files changed, 93 insertions(+), 44 deletions(-) create mode 100644 lib/feedmapper.php diff --git a/appinfo/app.php b/appinfo/app.php index 303b601b1..213295ac0 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -23,6 +23,7 @@ OC::$CLASSPATH['OC_News_Item'] = 'apps/news/lib/item.php'; OC::$CLASSPATH['OC_News_Feed'] = 'apps/news/lib/feed.php'; +OC::$CLASSPATH['OC_News_FeedMapper'] = 'apps/news/lib/feedmapper.php'; $l = new OC_l10n('news'); diff --git a/lib/feed.php b/lib/feed.php index cd38fb95a..bd2e3b1e4 100644 --- a/lib/feed.php +++ b/lib/feed.php @@ -20,60 +20,31 @@ * */ - /** * This class models a feed. */ -class OC_News_Feed extends SimplePie_Core{ +class OC_News_Feed { private $url; - private $feed_id; + private $spfeed; //we encapsulate a SimplePie_Core object public function __construct($url){ - parent::__construct(); $this->url = $url; - $this->set_item_class('OC_News_Item'); - $this->set_feed_url( $url ); - $this->enable_cache( false ); + $this->spfeed = new SimplePie_Core(); + $this->spfeed->set_item_class('OC_News_Item'); + $this->spfeed->set_feed_url( $url ); + $this->spfeed->enable_cache( false ); //FIXME: figure out if constructor is the right place for these - $this->init(); - $this->handle_content_type(); + $this->spfeed->init(); + $this->spfeed->handle_content_type(); } - - public function saveToDB() { - $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" ); - if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ - $_ut = "strftime('%s','now')"; - } elseif($CONFIG_DBTYPE == 'pgsql') { - $_ut = 'date_part(\'epoch\',now())::integer'; - } else { - $_ut = "UNIX_TIMESTAMP()"; - } - - //FIXME: Detect when user adds a known feed - $query = OCP\DB::prepare(" - INSERT INTO *PREFIX*news_feeds - (url, title, user_id, added, lastmodified) - VALUES (?, ?, ?, $_ut, $_ut) - "); - - $title = $this->get_title(); - if(empty($title)) { - $l = OC_L10N::get('news'); - $title = $l->t('no title'); - } + public function getUrl(){ + return $this->url; + } - $params=array( - htmlspecialchars_decode($this->url), - htmlspecialchars_decode($title), - OCP\USER::getUser() - ); - $query->execute($params); - - $feed_id = OCP\DB::insertid('*PREFIX*news_feeds'); - - return $feed_id; + public function getTitle(){ + return $this->spfeed->get_title(); } -} \ No newline at end of file +} diff --git a/lib/feedmapper.php b/lib/feedmapper.php new file mode 100644 index 000000000..e0c81a2b3 --- /dev/null +++ b/lib/feedmapper.php @@ -0,0 +1,71 @@ +. +* +*/ + +/** + * This class maps a feed to an entry in the feeds table of the database. + * It follows the Data Mapper pattern. + */ +class OC_News_FeedMapper { + + /** + * @brief Save the feed into the database + * @returns The id of the feed in the database table. + */ + public function insert(OC_News_Feed $feed) { + $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" ); + if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ + $_ut = "strftime('%s','now')"; + } elseif($CONFIG_DBTYPE == 'pgsql') { + $_ut = 'date_part(\'epoch\',now())::integer'; + } else { + $_ut = "UNIX_TIMESTAMP()"; + } + + //FIXME: Detect when user adds a known feed + //FIXME: specify folder where you want to add + $query = OCP\DB::prepare(" + INSERT INTO *PREFIX*news_feeds + (url, title, added, lastmodified) + VALUES (?, ?, $_ut, $_ut) + "); + + $title = $feed->getTitle(); + + if(empty($title)) { + $l = OC_L10N::get('news'); + $title = $l->t('no title'); + } + + $params=array( + htmlspecialchars_decode($feed->getUrl()), + htmlspecialchars_decode($title) + + //FIXME: user_id is going to move to the folder properties + //OCP\USER::getUser() + ); + $query->execute($params); + + $feed_id = OCP\DB::insertid('*PREFIX*news_feeds'); + + return $feed_id; + } +} \ No newline at end of file diff --git a/templates/main.php b/templates/main.php index 771d83b3e..f6eabb799 100644 --- a/templates/main.php +++ b/templates/main.php @@ -1,10 +1,15 @@ saveToDB(); +$mapper = new OC_News_FeedMapper(); +$feed_id = $mapper->insert($feed); +echo "
" . $feed->getTitle() . "
"; + +/* $item = $feed->get_item(1); + if ($item->isRead()) { echo $l->t('Read'); } @@ -24,3 +29,4 @@ if ($item->isRead()) { else { echo $l->t('Unread'); } +*/ \ No newline at end of file -- cgit v1.2.3