diff options
| author | 2012-10-21 18:47:57 +0200 | |
|---|---|---|
| committer | 2012-10-21 18:47:57 +0200 | |
| commit | fb57be5a5af3a2fb46b2dbf2b503ffe78eb5cf49 (patch) | |
| tree | 9440fc7846d8a56a7005b9ef029669c96ad959aa /app/models | |
First commit
Diffstat (limited to 'app/models')
| -rwxr-xr-x | app/models/Entry.php | 178 | ||||
| -rw-r--r-- | app/models/Feed.php | 148 | ||||
| -rwxr-xr-x | app/models/RSSConfiguration.php | 76 |
3 files changed, 402 insertions, 0 deletions
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 @@ +<?php + +class Entry extends Model { + private $guid; + private $title; + private $author; + private $content; + private $link; + private $date; + private $is_read; + private $is_favorite; + + public function __construct ($guid = '', $title = '', $author = '', $content = '', + $link = '', $pubdate = 0, $is_read = false, $is_favorite = false) { + $this->_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 @@ +<?php + +class Feed extends Model { + private $url; + private $categories; + private $entries_list; + + public function __construct ($url = null) { + $this->_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 @@ +<?php + +class RSSConfiguration extends Model { + private $posts_per_page; + private $default_view; + private $display_posts; + + public function __construct () { + $confDAO = new RSSConfigurationDAO (); + $this->_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); + } +} |
