EntriesGetter.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. class EntriesGetter {
  3. private $type = array (
  4. 'type' => 'all',
  5. 'id' => 'all'
  6. );
  7. private $state = 'all';
  8. private $filter = array (
  9. 'words' => array (),
  10. 'tags' => array (),
  11. );
  12. private $order = 'high_to_low';
  13. private $entries = array ();
  14. private $nb = 1;
  15. private $first = '';
  16. private $next = '';
  17. public function __construct ($type, $state, $filter, $order, $nb, $first = '') {
  18. $this->_type ($type);
  19. $this->_state ($state);
  20. $this->_filter ($filter);
  21. $this->_order ($order);
  22. $this->nb = $nb;
  23. $this->first = $first;
  24. }
  25. public function type () {
  26. return $this->type;
  27. }
  28. public function state () {
  29. return $this->state;
  30. }
  31. public function filter () {
  32. return $this->filter;
  33. }
  34. public function order () {
  35. return $this->order;
  36. }
  37. public function entries () {
  38. return $this->entries;
  39. }
  40. public function _type ($value) {
  41. if (!is_array ($value) ||
  42. !isset ($value['type']) ||
  43. !isset ($value['id'])) {
  44. throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
  45. }
  46. $type = $value['type'];
  47. $id = $value['id'];
  48. if ($type != 'all' && $type != 'favoris' && $type != 'public' && $type != 'c' && $type != 'f') {
  49. throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
  50. }
  51. if (($type == 'all' || $type == 'favoris' || $type == 'public') &&
  52. ($type != $id)) {
  53. throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
  54. }
  55. $this->type = $value;
  56. }
  57. public function _state ($value) {
  58. if ($value != 'all' && $value != 'not_read' && $value != 'read') {
  59. throw new EntriesGetterException ('Bad state line ' . __LINE__ . ' in file ' . __FILE__);
  60. }
  61. $this->state = $value;
  62. }
  63. public function _filter ($value) {
  64. $value = trim ($value);
  65. $terms = explode (' ', $value);
  66. foreach ($terms as $word) {
  67. if (!empty ($word) && $word[0] == '#' && isset ($word[1])) {
  68. $tag = substr ($word, 1);
  69. $this->filter['tags'][$tag] = $tag;
  70. } elseif (!empty ($word)) {
  71. $this->filter['words'][$word] = $word;
  72. }
  73. }
  74. }
  75. public function _order ($value) {
  76. if ($value != 'high_to_low' && $value != 'low_to_high') {
  77. throw new EntriesGetterException ('Bad order line ' . __LINE__ . ' in file ' . __FILE__);
  78. }
  79. $this->order = $value;
  80. }
  81. public function execute () {
  82. $entryDAO = new EntryDAO ();
  83. HelperEntry::$nb = $this->nb;
  84. HelperEntry::$first = $this->first;
  85. HelperEntry::$filter = $this->filter;
  86. switch ($this->type['type']) {
  87. case 'all':
  88. list ($this->entries, $this->next) = $entryDAO->listEntries (
  89. $this->state,
  90. $this->order
  91. );
  92. break;
  93. case 'favoris':
  94. list ($this->entries, $this->next) = $entryDAO->listFavorites (
  95. $this->state,
  96. $this->order
  97. );
  98. break;
  99. case 'public':
  100. list ($this->entries, $this->next) = $entryDAO->listPublic (
  101. $this->state,
  102. $this->order
  103. );
  104. break;
  105. case 'c':
  106. list ($this->entries, $this->next) = $entryDAO->listByCategory (
  107. $this->type['id'],
  108. $this->state,
  109. $this->order
  110. );
  111. break;
  112. case 'f':
  113. list ($this->entries, $this->next) = $entryDAO->listByFeed (
  114. $this->type['id'],
  115. $this->state,
  116. $this->order
  117. );
  118. break;
  119. default:
  120. throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
  121. }
  122. }
  123. public function getPaginator () {
  124. $paginator = new RSSPaginator ($this->entries, $this->next);
  125. return $paginator;
  126. }
  127. }