From fb57be5a5af3a2fb46b2dbf2b503ffe78eb5cf49 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Sun, 21 Oct 2012 18:47:57 +0200 Subject: First commit --- app/App_FrontController.php | 29 ++++++ app/configuration/application.ini | 9 ++ app/configuration/routes.php | 5 + app/controllers/configureController.php | 33 ++++++ app/controllers/entryController.php | 58 +++++++++++ app/controllers/errorController.php | 26 +++++ app/controllers/feedController.php | 89 ++++++++++++++++ app/controllers/indexController.php | 34 ++++++ app/layout/aside.phtml | 35 +++++++ app/layout/layout.phtml | 17 +++ app/models/Entry.php | 178 ++++++++++++++++++++++++++++++++ app/models/Feed.php | 148 ++++++++++++++++++++++++++ app/models/RSSConfiguration.php | 76 ++++++++++++++ app/views/configure/categorize.phtml | 3 + app/views/configure/display.phtml | 28 +++++ app/views/configure/flux.phtml | 3 + app/views/error/index.phtml | 13 +++ app/views/helpers/pagination.phtml | 34 ++++++ app/views/index/index.phtml | 49 +++++++++ 19 files changed, 867 insertions(+) create mode 100644 app/App_FrontController.php create mode 100644 app/configuration/application.ini create mode 100644 app/configuration/routes.php create mode 100755 app/controllers/configureController.php create mode 100755 app/controllers/entryController.php create mode 100644 app/controllers/errorController.php create mode 100755 app/controllers/feedController.php create mode 100755 app/controllers/indexController.php create mode 100644 app/layout/aside.phtml create mode 100644 app/layout/layout.phtml create mode 100755 app/models/Entry.php create mode 100644 app/models/Feed.php create mode 100755 app/models/RSSConfiguration.php create mode 100644 app/views/configure/categorize.phtml create mode 100644 app/views/configure/display.phtml create mode 100644 app/views/configure/flux.phtml create mode 100644 app/views/error/index.phtml create mode 100755 app/views/helpers/pagination.phtml create mode 100644 app/views/index/index.phtml (limited to 'app') diff --git a/app/App_FrontController.php b/app/App_FrontController.php new file mode 100644 index 000000000..5a2b27eed --- /dev/null +++ b/app/App_FrontController.php @@ -0,0 +1,29 @@ + +*/ +require ('FrontController.php'); + +class App_FrontController extends FrontController { + public function init () { + $this->loadLibs (); + $this->loadModels (); + + Session::init (); + + View::prependStyle (Url::display ('/theme/base.css')); + View::_param ('conf', Session::param ('conf', new RSSConfiguration ())); + } + + private function loadLibs () { + require (LIB_PATH . '/lib_rss.php'); + require (LIB_PATH . '/lib_simplepie.php'); + } + + private function loadModels () { + include (APP_PATH . '/models/RSSConfiguration.php'); + include (APP_PATH . '/models/Feed.php'); + include (APP_PATH . '/models/Entry.php'); + } +} diff --git a/app/configuration/application.ini b/app/configuration/application.ini new file mode 100644 index 000000000..75911aef5 --- /dev/null +++ b/app/configuration/application.ini @@ -0,0 +1,9 @@ +[general] +environment = "development" +use_url_rewriting = false +sel_application = "flux rss lalala ~~~" + +base_url = "/~marien/rss/public" +title = "Flux RSS" +language = "fr" +cache_enabled = false ; n'influe pas sur le cache de SimplePie diff --git a/app/configuration/routes.php b/app/configuration/routes.php new file mode 100644 index 000000000..0efec7ba7 --- /dev/null +++ b/app/configuration/routes.php @@ -0,0 +1,5 @@ +view->conf->_postsPerPage (intval ($nb)); + $this->view->conf->_defaultView ($view); + $this->view->conf->_displayPosts ($display); + + $values = array ( + 'posts_per_page' => $this->view->conf->postsPerPage (), + 'default_view' => $this->view->conf->defaultView (), + 'display_posts' => $this->view->conf->displayPosts () + ); + + $confDAO = new RSSConfigurationDAO (); + $confDAO->save ($values); + Session::_param ('conf', $this->view->conf); + } + } +} diff --git a/app/controllers/entryController.php b/app/controllers/entryController.php new file mode 100755 index 000000000..e180cc7f1 --- /dev/null +++ b/app/controllers/entryController.php @@ -0,0 +1,58 @@ +listNotReadEntries (); + } else { + $entry = $entryDAO->searchById ($id); + $entries = $entry !== false ? array ($entry) : array (); + } + + foreach ($entries as $entry) { + $values = array ( + 'is_read' => $is_read, + ); + + $entryDAO->updateEntry ($entry->id (), $values); + } + + Request::forward (array (), true); + } + + public function bookmarkAction () { + $id = Request::param ('id'); + $is_fav = Request::param ('is_favorite'); + + if ($is_fav) { + $is_fav = true; + } else { + $is_fav = false; + } + + $entryDAO = new EntryDAO (); + if ($id != false) { + $entry = $entryDAO->searchById ($id); + + if ($entry != false) { + $values = array ( + 'is_favorite' => $is_fav, + ); + + $entryDAO->updateEntry ($entry->id (), $values); + } + } + + Request::forward (array (), true); + } +} diff --git a/app/controllers/errorController.php b/app/controllers/errorController.php new file mode 100644 index 000000000..3b6f202ab --- /dev/null +++ b/app/controllers/errorController.php @@ -0,0 +1,26 @@ +view->code = 'Error 403 - Forbidden'; + break; + case 404: + $this->view->code = 'Error 404 - Not found'; + break; + case 500: + $this->view->code = 'Error 500 - Internal Server Error'; + break; + case 503: + $this->view->code = 'Error 503 - Service Unavailable'; + break; + default: + $this->view->code = 'Error 404 - Not found'; + } + + $this->view->logs = Request::param ('logs'); + } +} diff --git a/app/controllers/feedController.php b/app/controllers/feedController.php new file mode 100755 index 000000000..3b4a90b64 --- /dev/null +++ b/app/controllers/feedController.php @@ -0,0 +1,89 @@ +loadEntries (); + $feed_entries = array (); + + if ($entries !== false) { + $entryDAO = new EntryDAO (); + + foreach ($entries as $entry) { + $values = array ( + 'id' => $entry->id (), + 'guid' => $entry->guid (), + 'title' => $entry->title (), + 'author' => $entry->author (), + 'content' => $entry->content (), + 'link' => $entry->link (), + 'date' => $entry->date (true), + 'is_read' => $entry->isRead (), + 'is_favorite' => $entry->isFavorite (), + ); + $entryDAO->addEntry ($values); + + $feed_entries[] = $entry->id (); + } + } + + $feedDAO = new FeedDAO (); + $values = array ( + 'id' => $feed->id (), + 'url' => $feed->url (), + 'categories' => $feed->categories (), + 'entries' => $feed_entries + ); + $feedDAO->addFeed ($values); + } catch (Exception $e) { + // TODO ajouter une erreur : url non valide + } + + Request::forward (array (), true); + } + } + + public function actualizeAction () { + $feedDAO = new FeedDAO (); + $entryDAO = new EntryDAO (); + + $feeds = $feedDAO->listFeeds (); + + foreach ($feeds as $feed) { + $entries = $feed->loadEntries (); + $feed_entries = $feed->entries (); + + if ($entries !== false) { + foreach ($entries as $entry) { + $values = array ( + 'id' => $entry->id (), + 'guid' => $entry->guid (), + 'title' => $entry->title (), + 'author' => $entry->author (), + 'content' => $entry->content (), + 'link' => $entry->link (), + 'date' => $entry->date (true), + 'is_read' => $entry->isRead (), + 'is_favorite' => $entry->isFavorite (), + ); + $entryDAO->addEntry ($values); + + if (!in_array ($entry->id (), $feed_entries)) { + $feed_entries[] = $entry->id (); + } + } + } + + $values = array ( + 'entries' => $feed_entries + ); + $feedDAO->updateFeed ($values); + } + + Request::forward (array (), true); + } +} diff --git a/app/controllers/indexController.php b/app/controllers/indexController.php new file mode 100755 index 000000000..3fda234ca --- /dev/null +++ b/app/controllers/indexController.php @@ -0,0 +1,34 @@ +view->conf->defaultView ()); + if ($mode == 'not_read') { + $entries = $entryDAO->listNotReadEntries (); + } elseif ($mode == 'all') { + $entries = $entryDAO->listEntries (); + } + + usort ($entries, 'sortEntriesByDate'); + + //gestion pagination + $page = Request::param ('page', 1); + $this->view->entryPaginator = new Paginator ($entries); + $this->view->entryPaginator->_nbItemsPerPage ($this->view->conf->postsPerPage ()); + $this->view->entryPaginator->_currentPage ($page); + } + + public function changeModeAction () { + $mode = Request::param ('mode'); + + if ($mode == 'not_read') { + Session::_param ('mode', 'not_read'); + } else { + Session::_param ('mode', 'all'); + } + + Request::forward (array (), true); + } +} diff --git a/app/layout/aside.phtml b/app/layout/aside.phtml new file mode 100644 index 000000000..6e2e4965d --- /dev/null +++ b/app/layout/aside.phtml @@ -0,0 +1,35 @@ +
+
+ + +
+ + +
+ + +
+ +
+ diff --git a/app/layout/layout.phtml b/app/layout/layout.phtml new file mode 100644 index 000000000..ad798c369 --- /dev/null +++ b/app/layout/layout.phtml @@ -0,0 +1,17 @@ + + + + + + + + +
+ partial ('aside'); ?> + +
+ render (); ?> +
+
+ + diff --git a/app/models/Entry.php b/app/models/Entry.php new file mode 100755 index 000000000..85e04cb4e --- /dev/null +++ b/app/models/Entry.php @@ -0,0 +1,178 @@ +_guid ($guid); + $this->_title ($title); + $this->_author ($author); + $this->_content ($content); + $this->_link ($link); + $this->_date ($pubdate); + $this->_isRead ($is_read); + $this->_isFavorite ($is_favorite); + } + + public function id () { + return small_hash ($this->guid . Configuration::selApplication ()); + } + public function guid () { + return $this->guid; + } + public function title () { + return $this->title; + } + public function author () { + return $this->author; + } + public function content () { + return $this->content; + } + public function link () { + return $this->link; + } + public function date ($raw = false) { + if ($raw) { + return $this->date; + } else { + return timestamptodate ($this->date); + } + } + public function isRead () { + return $this->is_read; + } + public function isFavorite () { + return $this->is_favorite; + } + + public function _guid ($value) { + $this->guid = $value; + } + public function _title ($value) { + $this->title = $value; + } + public function _author ($value) { + $this->author = $value; + } + public function _content ($value) { + $this->content = $value; + } + public function _link ($value) { + $this->link = $value; + } + public function _date ($value) { + $this->date = $value; + } + public function _isRead ($value) { + $this->is_read = $value; + } + public function _isFavorite ($value) { + $this->is_favorite = $value; + } +} + +class EntryDAO extends Model_array { + public function __construct () { + parent::__construct (PUBLIC_PATH . '/data/db/Entries.array.php'); + } + + public function addEntry ($values) { + $id = $values['id']; + unset ($values['id']); + + if (!isset ($this->array[$id])) { + $this->array[$id] = array (); + + foreach ($values as $key => $value) { + $this->array[$id][$key] = $value; + } + + $this->writeFile ($this->array); + } else { + return false; + } + } + + public function updateEntry ($id, $values) { + foreach ($values as $key => $value) { + $this->array[$id][$key] = $value; + } + + $this->writeFile($this->array); + } + + public function searchById ($id) { + $list = HelperEntry::daoToEntry ($this->array); + + if (isset ($list[$id])) { + return $list[$id]; + } else { + return false; + } + } + + public function listEntries () { + $list = $this->array; + + if (!is_array ($list)) { + $list = array (); + } + + return HelperEntry::daoToEntry ($list); + } + + public function listNotReadEntries () { + $list = $this->array; + $list_not_read = array (); + + if (!is_array ($list)) { + $list = array (); + } + + foreach ($list as $key => $entry) { + if (!$entry['is_read']) { + $list_not_read[$key] = $entry; + } + } + + return HelperEntry::daoToEntry ($list_not_read); + } + + public function count () { + return count ($this->array); + } +} + +class HelperEntry { + public static function daoToEntry ($listDAO) { + $list = array (); + + if (!is_array ($listDAO)) { + $listDAO = array ($listDAO); + } + + foreach ($listDAO as $key => $dao) { + $list[$key] = new Entry ( + $dao['guid'], + $dao['title'], + $dao['author'], + $dao['content'], + $dao['link'], + $dao['date'], + $dao['is_read'], + $dao['is_favorite'] + ); + } + + return $list; + } +} diff --git a/app/models/Feed.php b/app/models/Feed.php new file mode 100644 index 000000000..583a7fef5 --- /dev/null +++ b/app/models/Feed.php @@ -0,0 +1,148 @@ +_url ($url); + $this->_categories (array ()); + $this->_entries (array ()); + } + + public function id () { + return small_hash ($this->url . Configuration::selApplication ()); + } + public function url () { + return $this->url; + } + public function categories () { + return $this->categories; + } + public function entries () { + return $this->entries_list; + } + + public function _url ($value) { + if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) { + $this->url = $value; + } else { + throw new Exception (); + } + } + public function _categories ($value) { + if (!is_array ($value)) { + $value = array ($value); + } + + $this->categories = $value; + } + public function _entries ($value) { + if (!is_array ($value)) { + $value = array ($value); + } + + $this->entries_list = $value; + } + + public function loadEntries () { + if (!is_null ($this->url)) { + $feed = new SimplePie (); + $feed->set_feed_url ($this->url); + $feed->set_cache_location (CACHE_PATH); + $feed->init (); + + $entries = array (); + if ($feed->data) { + foreach ($feed->get_items () as $item) { + $title = $item->get_title (); + $author = $item->get_author (); + $content = $item->get_content (); + $link = $item->get_permalink (); + $date = strtotime ($item->get_date ()); + + $entry = new Entry ( + $item->get_id (), + !is_null ($title) ? $title : '', + !is_null ($author) ? $author->name : '', + !is_null ($content) ? $content : '', + !is_null ($link) ? $link : '', + $date ? $date : time () + ); + + $entries[$entry->id ()] = $entry; + } + + return $entries; + } else { + return false; + } + } else { + return false; + } + } +} + +class FeedDAO extends Model_array { + public function __construct () { + parent::__construct (PUBLIC_PATH . '/data/db/Feeds.array.php'); + } + + public function addFeed ($values) { + $id = $values['id']; + unset ($values['id']); + + if (!isset ($this->array[$id])) { + $this->array[$id] = array (); + + foreach ($values as $key => $value) { + $this->array[$id][$key] = $value; + } + + $this->writeFile ($this->array); + } else { + return false; + } + } + + public function updateFeed ($id, $values) { + foreach ($values as $key => $value) { + $this->array[$id][$key] = $value; + } + + $this->writeFile($this->array); + } + + public function listFeeds () { + $list = $this->array; + + if (!is_array ($list)) { + $list = array (); + } + + return HelperFeed::daoToFeed ($list); + } + + public function count () { + return count ($this->array); + } +} + +class HelperFeed { + public static function daoToFeed ($listDAO) { + $list = array (); + + if (!is_array ($listDAO)) { + $listDAO = array ($listDAO); + } + + foreach ($listDAO as $key => $dao) { + $list[$key] = new Feed ($dao['url']); + $list[$key]->_categories ($dao['categories']); + $list[$key]->_entries ($dao['entries']); + } + + return $list; + } +} diff --git a/app/models/RSSConfiguration.php b/app/models/RSSConfiguration.php new file mode 100755 index 000000000..f42f1283c --- /dev/null +++ b/app/models/RSSConfiguration.php @@ -0,0 +1,76 @@ +_postsPerPage ($confDAO->posts_per_page); + $this->_defaultView ($confDAO->default_view); + $this->_displayPosts ($confDAO->display_posts); + } + + public function postsPerPage () { + return $this->posts_per_page; + } + public function defaultView () { + return $this->default_view; + } + public function displayPosts () { + return $this->display_posts; + } + + public function _postsPerPage ($value) { + if (is_int ($value)) { + $this->posts_per_page = $value; + } else { + $this->posts_per_page = 10; + } + } + public function _defaultView ($value) { + if ($value == 'not_read') { + $this->default_view = 'not_read'; + } else { + $this->default_view = 'all'; + } + } + public function _displayPosts ($value) { + if ($value == 'yes') { + $this->display_posts = 'yes'; + } else { + $this->display_posts = 'no'; + } + } +} + +class RSSConfigurationDAO extends Model_array { + public $posts_per_page = 10; + public $default_view = 'all'; + public $display_posts = 'no'; + + public function __construct () { + parent::__construct (PUBLIC_PATH . '/data/db/Configuration.array.php'); + + if (isset ($this->array['posts_per_page'])) { + $this->posts_per_page = $this->array['posts_per_page']; + } + if (isset ($this->array['default_view'])) { + $this->default_view = $this->array['default_view']; + } + if (isset ($this->array['display_posts'])) { + $this->display_posts = $this->array['display_posts']; + } + } + + public function save ($values) { + $this->array[0] = array (); + + foreach ($values as $key => $value) { + $this->array[0][$key] = $value; + } + + $this->writeFile($this->array); + } +} diff --git a/app/views/configure/categorize.phtml b/app/views/configure/categorize.phtml new file mode 100644 index 000000000..126f6b48a --- /dev/null +++ b/app/views/configure/categorize.phtml @@ -0,0 +1,3 @@ +
+ Fonctionnalité non implémentée (pour le moment) +
diff --git a/app/views/configure/display.phtml b/app/views/configure/display.phtml new file mode 100644 index 000000000..23c41712b --- /dev/null +++ b/app/views/configure/display.phtml @@ -0,0 +1,28 @@ +
+
+

Configuration de l'affichage

+ + + + + +
+ conf->defaultView () == 'all' ? ' checked="checked"' : ''; ?> /> + +
+ conf->defaultView () == 'not_read' ? ' checked="checked"' : ''; ?> /> + +
+ + +
+ conf->displayPosts () ? ' checked="checked"' : ''; ?> /> + +
+ conf->displayPosts () ? ' checked="checked"' : ''; ?> /> + +
+ + +
+
diff --git a/app/views/configure/flux.phtml b/app/views/configure/flux.phtml new file mode 100644 index 000000000..126f6b48a --- /dev/null +++ b/app/views/configure/flux.phtml @@ -0,0 +1,3 @@ +
+ Fonctionnalité non implémentée (pour le moment) +
diff --git a/app/views/error/index.phtml b/app/views/error/index.phtml new file mode 100644 index 000000000..ffa156068 --- /dev/null +++ b/app/views/error/index.phtml @@ -0,0 +1,13 @@ +

+ +

code; ?>

+ +logs)) { ?> + + + +

diff --git a/app/views/helpers/pagination.phtml b/app/views/helpers/pagination.phtml new file mode 100755 index 000000000..fe9feb0e8 --- /dev/null +++ b/app/views/helpers/pagination.phtml @@ -0,0 +1,34 @@ + + +nbPage > 1) { ?> + + diff --git a/app/views/index/index.phtml b/app/views/index/index.phtml new file mode 100644 index 000000000..2dfdb5064 --- /dev/null +++ b/app/views/index/index.phtml @@ -0,0 +1,49 @@ +
+ Tout marquer comme luTout afficher + + -->Afficher les non lus + +
+ +
+entryPaginator->items (); ?> + + entryPaginator->render ('pagination.phtml', 'page'); ?> + + +
+
author (); ?> a écrit le date (); ?>,
+ +

title (); ?>

+
content (); ?>
+ + +
+ + + entryPaginator->render ('pagination.phtml', 'page'); ?> + +
+

+ Il n'y a aucun flux à afficher. + + Afficher tous les articles ? + +

+
+ +
-- cgit v1.2.3