summaryrefslogtreecommitdiff
path: root/app/models/EntriesGetter.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2013-05-05 12:58:27 +0200
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2013-05-05 12:58:27 +0200
commit99e8fc0ad67bf7ff14f53fd141fe57b96a107648 (patch)
tree78b07eaa7fdc9bde1bf2ecd11e65f22afdcc8944 /app/models/EntriesGetter.php
parent23dd73ddeec2473c3e8a3c517317e074ac53a1d8 (diff)
parent22e9fb02f838e2b6ac8c5dd504fad0446c5807dc (diff)
Merge branch 'dev' into releases
Diffstat (limited to 'app/models/EntriesGetter.php')
-rw-r--r--app/models/EntriesGetter.php144
1 files changed, 144 insertions, 0 deletions
diff --git a/app/models/EntriesGetter.php b/app/models/EntriesGetter.php
new file mode 100644
index 000000000..ca92804a7
--- /dev/null
+++ b/app/models/EntriesGetter.php
@@ -0,0 +1,144 @@
+<?php
+
+class EntriesGetter {
+ private $type = array (
+ 'type' => 'all',
+ 'id' => 'all'
+ );
+ private $state = 'all';
+ private $filter = array (
+ 'words' => array (),
+ 'tags' => array (),
+ );
+ private $order = 'high_to_low';
+ private $entries = array ();
+
+ private $nb = 1;
+ private $first = '';
+ private $next = '';
+
+ public function __construct ($type, $state, $filter, $order, $nb, $first = '') {
+ $this->_type ($type);
+ $this->_state ($state);
+ $this->_filter ($filter);
+ $this->_order ($order);
+ $this->nb = $nb;
+ $this->first = $first;
+ }
+
+ public function type () {
+ return $this->type;
+ }
+ public function state () {
+ return $this->state;
+ }
+ public function filter () {
+ return $this->filter;
+ }
+ public function order () {
+ return $this->order;
+ }
+ public function entries () {
+ return $this->entries;
+ }
+
+ public function _type ($value) {
+ if (!is_array ($value) ||
+ !isset ($value['type']) ||
+ !isset ($value['id'])) {
+ throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
+ }
+
+ $type = $value['type'];
+ $id = $value['id'];
+
+ if ($type != 'all' && $type != 'favoris' && $type != 'public' && $type != 'c' && $type != 'f') {
+ throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
+ }
+
+ if (($type == 'all' || $type == 'favoris' || $type == 'public') &&
+ ($type != $id)) {
+ throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
+ }
+
+ $this->type = $value;
+ }
+ public function _state ($value) {
+ if ($value != 'all' && $value != 'not_read' && $value != 'read') {
+ throw new EntriesGetterException ('Bad state line ' . __LINE__ . ' in file ' . __FILE__);
+ }
+
+ $this->state = $value;
+ }
+ public function _filter ($value) {
+ $value = trim ($value);
+ $terms = explode (' ', $value);
+
+ foreach ($terms as $word) {
+ if (!empty ($word) && $word[0] == '#' && isset ($word[1])) {
+ $tag = substr ($word, 1);
+ $this->filter['tags'][$tag] = $tag;
+ } elseif (!empty ($word)) {
+ $this->filter['words'][$word] = $word;
+ }
+ }
+ }
+ public function _order ($value) {
+ if ($value != 'high_to_low' && $value != 'low_to_high') {
+ throw new EntriesGetterException ('Bad order line ' . __LINE__ . ' in file ' . __FILE__);
+ }
+
+ $this->order = $value;
+ }
+
+ public function execute () {
+ $entryDAO = new EntryDAO ();
+
+ HelperEntry::$nb = $this->nb;
+ HelperEntry::$first = $this->first;
+ HelperEntry::$filter = $this->filter;
+
+ switch ($this->type['type']) {
+ case 'all':
+ list ($this->entries, $this->next) = $entryDAO->listEntries (
+ $this->state,
+ $this->order
+ );
+ break;
+ case 'favoris':
+ list ($this->entries, $this->next) = $entryDAO->listFavorites (
+ $this->state,
+ $this->order
+ );
+ break;
+ case 'public':
+ list ($this->entries, $this->next) = $entryDAO->listPublic (
+ $this->state,
+ $this->order
+ );
+ break;
+ case 'c':
+ list ($this->entries, $this->next) = $entryDAO->listByCategory (
+ $this->type['id'],
+ $this->state,
+ $this->order
+ );
+ break;
+ case 'f':
+ list ($this->entries, $this->next) = $entryDAO->listByFeed (
+ $this->type['id'],
+ $this->state,
+ $this->order
+ );
+ break;
+ default:
+ throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
+ }
+ }
+
+ public function getPaginator () {
+ $paginator = new RSSPaginator ($this->entries, $this->next);
+
+ return $paginator;
+ }
+}