diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2012-10-13 02:21:36 +0200 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2012-10-13 02:21:47 +0200 |
commit | dfcf639f2f7b6eaf82c48dd503f7c98ce9d2155e (patch) | |
tree | 603e257c5a60ec7b126b2e8716383d83052e26a8 | |
parent | 8fd373e8d5194e315f840fe654b7031979910e88 (diff) |
refactored index php into controllers
-rw-r--r-- | controllers/controller.php | 92 | ||||
-rw-r--r-- | controllers/news.controller.php | 108 | ||||
-rw-r--r-- | index.php | 70 | ||||
-rw-r--r-- | lib/feed.php | 6 | ||||
-rw-r--r-- | lib/feedmapper.php | 13 | ||||
-rw-r--r-- | lib/foldermapper.php | 8 |
6 files changed, 229 insertions, 68 deletions
diff --git a/controllers/controller.php b/controllers/controller.php new file mode 100644 index 000000000..89037b263 --- /dev/null +++ b/controllers/controller.php @@ -0,0 +1,92 @@ +<?php +/** +* ownCloud - News app +* +* @author Bernhard Posselt +* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +* +* This file is licensed under the Affero General Public License version 3 or later. +* See the COPYING-README file +* +*/ + +namespace OCA\News; + +class Controller { + + protected $userId; + protected $trans; + + + public function __construct(){ + $this->userId = \OCP\USER::getUser(); + $this->trans = \OC_L10N::get('news'); + } + + + protected function addScript($name){ + \OCP\Util::addScript('news', $name); + } + + + protected function addStyle($name){ + \OCP\Util::addStyle('news', $name); + } + + + protected function add3rdPartyScript($name){ + \OCP\Util::addScript('news/3rdparty', $name); + } + + + protected function add3rdPartyStyle($name){ + \OCP\Util::addStyle('news/3rdparty', $name); + } + + + /** + * Shortcut for setting a user defined value + * @param $key the key under which the value is being stored + * @param $value the value that you want to store + */ + protected function setUserValue($key, $value){ + \OCP\Config::setUserValue($this->userId, 'news', $key, $value); + } + + + /** + * Shortcut for getting a user defined value + * @param $key the key under which the value is being stored + */ + protected function getUserValue($key){ + return \OCP\Config::getUserValue($this->userId, 'news', $key); + } + + + /** + * Binds variables to the template and prints it + * The following values are always assigned: userId + * @param $arguments an array with arguments in $templateVar => $content + * @param $template the name of the template + * @param $fullPage if true, it will render a full page, otherwise only a part + * defaults to true + */ + protected function render($template, $arguments=array(), $fullPage=true){ + + if($fullPage){ + $template = new \OCP\Template('news', $template, 'user'); + } else { + $template = new \OCP\Template('news', $template); + } + + foreach($arguments as $key => $value){ + $template->assign($key, $value); + } + + $template->assign('userId', $this->userId); + $template->printPage(); + } + +} + +?>
\ No newline at end of file diff --git a/controllers/news.controller.php b/controllers/news.controller.php new file mode 100644 index 000000000..f01022104 --- /dev/null +++ b/controllers/news.controller.php @@ -0,0 +1,108 @@ +<?php +/** +* ownCloud - News app +* +* @author Bernhard Posselt +* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +* +* This file is licensed under the Affero General Public License version 3 or later. +* See the COPYING-README file +* +*/ + +namespace OCA\News; + + +class FeedType { + const FEED = 0; + const FOLDER = 1; + const STARRED = 2; + const SUBSCRIPTIONS = 3; +} + + +class NewsController extends Controller { + + + /** + * Decides wether to show the feedpage or the firstrun page + */ + public function index(){ + $feedMapper = new FeedMapper($this->userId); + + if($feedMapper->feedCount() > 0){ + $this->feedPage(); + } else { + $this->firstRun(); + } + } + + + public function firstRun(){ + $this->addScript('news'); + $this->addScript('firstrun'); + $this->addStyle('firstrun'); + $this->render('firstrun'); + } + + + public function feedPage(){ + $this->addScript('main'); + $this->addScript('news'); + $this->addScript('menu'); + $this->addScript('items'); + $this->add3rdPartyScript('jquery.timeago'); + + $this->addStyle('news'); + $this->addStyle('settings'); + + $folderMapper = new FolderMapper($this->userId); + $feedMapper = new FeedMapper($this->userId); + + // always show the last viewed feed on reload + $lastViewedId = $this->getUserValue('lastViewedFeed'); + $lastViewedType = $this->getUserValue('lastViewedFeedType'); + + if( $lastViewedId === null || $lastViewedType === null) { + $lastViewedId = $feedMapper->mostRecent(); + } else { + // check if the last selected feed or folder exists + if( ( + $feedMapper->findById($lastViewedId) === null && + $lastViewedType === FeedType::FEED + ) || + ( + $folderMapper->findById($lastViewedId) === null && + $lastViewedType === FeedType::FOLDER + ) ){ + $lastViewedId = $feedMapper->mostRecent(); + } + } + + $feeds = $folderMapper->childrenOfWithFeeds(0); + $folderForest = $folderMapper->childrenOf(0); //retrieve all the folders + + $params = array( + 'allfeeds' => $feeds, + 'folderforest' => $folderForest, + 'lastViewedId' => $lastViewedType, + 'lastViewedType' => $lastViewedType, + // for compability, remove this after refactoring + 'feedid' => $lastViewedId, + 'feedtype' => $lastViewedType, + ); + + $this->render('main', $params); + } + + + public function javascriptTests(){ + $this->add3rdPartyScript('jasmine-1.2.0/jasmine.js'); + $this->add3rdPartyStyle('jasmine-1.2.0/jasmine.css'); + $this->render('javascript.tests'); + } + + +} + +?>
\ No newline at end of file @@ -11,72 +11,18 @@ * */ -// Check if we are a user -OCP\User::checkLoggedIn(); +require_once('controllers/controller.php'); +require_once('controllers/news.controller.php'); +OCP\User::checkLoggedIn(); OCP\App::checkAppEnabled('news'); OCP\App::setActiveNavigationEntry('news'); -$l = OC_L10N::get('news'); - -$userid = OCP\USER::getUser(); - -$foldermapper = new OCA\News\FolderMapper($userid); - -$allfeeds = $foldermapper->childrenOfWithFeeds(0); //$foldermapper->populate($folder); -$folderforest = $foldermapper->childrenOf(0); //retrieve all the folders - -$feedid = 0; -$feedtype = 0; +$controller = new OCA\News\NewsController(); +// routes if(isset($_GET['jstest'])){ - OCP\Util::addScript('news/3rdparty', 'jasmine-1.2.0/jasmine.js'); - OCP\Util::addScript('news/3rdparty', 'jasmine-1.2.0/jasmine-html.js'); - OCP\Util::addStyle('news/3rdparty','jasmine-1.2.0/jasmine.css'); - $tmpl = new OCP\Template('news', 'javascript.tests'); - $tmpl->printPage(); + $controller->javascriptTests(); } else { - - if ($allfeeds) { - - OCP\Util::addScript('news','main'); - OCP\Util::addScript('news','news'); - OCP\Util::addScript('news','menu'); - OCP\Util::addScript('news','items'); - OCP\Util::addScript('news/3rdparty', 'jquery.timeago'); - - OCP\Util::addStyle('news','news'); - OCP\Util::addStyle('news','settings'); - - $feedid = isset( $_GET['feedid'] ) ? $_GET['feedid'] : null; - if ($feedid == null) { - $feedmapper = new OCA\News\FeedMapper(OCP\USER::getUser($userid)); - $lastViewedId = OCP\Config::getUserValue($userid, 'news', 'lastViewedFeed'); - $lastViewedType = OCP\Config::getUserValue($userid, 'news', 'lastViewedFeedType'); - if( $lastViewedId == null || $lastViewedType == null) { - $feedid = $feedmapper->mostRecent(); - } else { - $feedid = $lastViewedId; - $feedtype = $lastViewedType; - // check if feed exists in table - if($feedmapper->findById($feedid) === null) { - $feedid = $feedmapper->mostRecent(); - } - } - } - $tmpl = new OCP\Template( 'news', 'main', 'user' ); - $tmpl->assign('allfeeds', $allfeeds); - $tmpl->assign('folderforest', $folderforest); - $tmpl->assign('feedid', $feedid); - $tmpl->assign('feedtype', $feedtype); - $tmpl->printPage(); - - } else { - - OCP\Util::addScript('news','news'); - OCP\Util::addScript('news','firstrun'); - OCP\Util::addStyle('news','firstrun'); - $tmpl = new OCP\Template( 'news', 'firstrun', 'user' ); - $tmpl->printPage(); - } -} + $controller->index(); +}
\ No newline at end of file diff --git a/lib/feed.php b/lib/feed.php index 9a9226d73..4f82404f6 100644 --- a/lib/feed.php +++ b/lib/feed.php @@ -12,12 +12,6 @@ namespace OCA\News; -class FeedType { - const FEED = 0; - const FOLDER = 1; - const STARRED = 2; - const SUBSCRIPTIONS = 3; -} /** * This class models a feed. diff --git a/lib/feedmapper.php b/lib/feedmapper.php index 16804a1a7..35847acdf 100644 --- a/lib/feedmapper.php +++ b/lib/feedmapper.php @@ -72,6 +72,19 @@ class FeedMapper { return $feeds; } + + /** + * @brief returns the number of feeds that a user has + * @returns the number of feeds that a user has + */ + public function feedCount() { + $query = 'SELECT COUNT(*) AS size FROM ' . self::tableName . ' WHERE user_id = ?'; + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute(array($this->userid))->fetchRow(); + return $result['size']; + } + + /** * @brief Retrieve a feed from the database * @param id The id of the feed in the database table. diff --git a/lib/foldermapper.php b/lib/foldermapper.php index 17fcbbf7b..a30759d2d 100644 --- a/lib/foldermapper.php +++ b/lib/foldermapper.php @@ -83,6 +83,14 @@ class FolderMapper { /** + * This is being used for consistency + */ + public function findById($id){ + return $this->find($id); + } + + + /** * @brief Retrieve a folder from the database * @param id The id of the folder in the database table. * @returns an instance of OC_News_Folder |