diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/feedmapper.php | 61 | ||||
-rw-r--r-- | lib/folder.php | 10 | ||||
-rw-r--r-- | lib/foldermapper.php | 60 | ||||
-rw-r--r-- | lib/utils.php (renamed from lib/fetcher.php) | 0 |
4 files changed, 75 insertions, 56 deletions
diff --git a/lib/feedmapper.php b/lib/feedmapper.php index c0e0b03a5..7b148b142 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -63,11 +63,28 @@ class OC_News_FeedMapper { } /** + * @brief Find the id of a feed and all its items from the database + * @param url url of the feed + * @return id of the feed corresponding to the url passed as parameters + null - if there is no such feed + */ + public function findIdFromUrl($url){ + $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE url = ?'); + $result = $stmt->execute(array($url)); + $row = $result->fetchRow(); + $id = null; + if ($row != null){ + $id = $row['id']; + } + return $id; + } + + /** * @brief Save the feed and all its items into the database * @param feed the feed to be saved * @returns The id of the feed in the database table. */ - public function insert(OC_News_Feed $feed){ + public function insert(OC_News_Feed $feed, $folderid){ $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" ); if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ $_ut = "strftime('%s','now')"; @@ -76,32 +93,36 @@ class OC_News_FeedMapper { } 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 ' . self::tableName . - '(url, title, added, lastmodified) - VALUES (?, ?, $_ut, $_ut) - '); - + $title = $feed->getTitle(); + $url = htmlspecialchars_decode($feed->getUrl()); 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); - - $feedid = OCP\DB::insertid(self::tableName); + //FIXME: Detect when feed contains already a database id + + $feedid = $this->findIdFromUrl($url); + if ($feedid == null){ + $query = OCP\DB::prepare(' + INSERT INTO ' . self::tableName . + '(url, title, folderid, added, lastmodified) + VALUES (?, ?, ?, ?, ?) + '); + + $params=array( + $url, + htmlspecialchars_decode($title), + $folderid, + $_ut, + $_ut + ); + $query->execute($params); + + $feedid = OCP\DB::insertid(self::tableName); + } $feed->setId($feedid); $itemMapper = new OC_News_ItemMapper($feed); diff --git a/lib/folder.php b/lib/folder.php index a7036e8a7..f97960c7b 100644 --- a/lib/folder.php +++ b/lib/folder.php @@ -35,6 +35,7 @@ class OC_News_Folder { $this->name = $name; $this->parent = $parent; $this->feeds = array(); + $this->id = 0; } public function getName(){ @@ -50,7 +51,14 @@ class OC_News_Folder { } public function setId($id){ - $this->$id = $id; + $this->id = $id; + } + + public function getParentId(){ + if ($this->parent == null){ + return 0; + } + return $this->parent->getId(); } public function addFeed(OC_News_Feed $feed){ diff --git a/lib/foldermapper.php b/lib/foldermapper.php index 23ced832c..92f53c35c 100644 --- a/lib/foldermapper.php +++ b/lib/foldermapper.php @@ -63,52 +63,42 @@ class OC_News_FolderMapper { } /** - * @brief Save the feed and all its items into the database - * @param feed the feed to be saved - * @returns The id of the feed in the database table. + * @brief Store the folder and all its feeds into the database + * @param folder the folder to be saved + * @returns The id of the folder 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 + public function insert(OC_News_Folder $folder){ $query = OCP\DB::prepare(' INSERT INTO ' . self::tableName . - '(url, title, added, lastmodified) - VALUES (?, ?, $_ut, $_ut) + '(name, parentid, userid) + VALUES (?, ?, ?) '); - $title = $feed->getTitle(); + $name = $folder->getName(); - if(empty($title)) { + if(empty($name)) { $l = OC_L10N::get('news'); - $title = $l->t('no title'); + $name = $l->t('no name'); } + $parentid = $folder->getParentId(); + $params=array( - htmlspecialchars_decode($feed->getUrl()), - htmlspecialchars_decode($title) - - //FIXME: user_id is going to move to the folder properties - //OCP\USER::getUser() + htmlspecialchars_decode($name), + $parentid, + OCP\USER::getUser() ); $query->execute($params); - - $feedid = OCP\DB::insertid(self::tableName); - $feed->setId($feedid); + $folderid = OCP\DB::insertid(self::tableName); - $itemMapper = new OC_News_ItemMapper($feed); - - $items = $feed->getItems(); - foreach($items as $item){ - $itemMapper->insert($item); - } - return $feedid; + $folder->setId($folderid); + +// $folder->getFeeds(); +// $feedMapper = new OC_News_FeedMapper($feed); +// $items = $feed->getItems(); +// foreach($items as $item){ +// $itemMapper->insert($item); +// } +// return $folderid; } +}
\ No newline at end of file diff --git a/lib/fetcher.php b/lib/utils.php index e5ea27626..e5ea27626 100644 --- a/lib/fetcher.php +++ b/lib/utils.php |