EntriesGetter.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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; //TODO: Update: Now done in SQL
  84. HelperEntry::$first = $this->first; //TODO: Update: Now done in SQL
  85. HelperEntry::$filter = $this->filter;
  86. $sqlLimit = (empty ($this->filter['words']) && empty ($this->filter['tags'])) ? $this->nb : ''; //Disable SQL LIMIT optimisation during search //TODO: Do better!
  87. switch ($this->type['type']) {
  88. case 'all':
  89. list ($this->entries, $this->next) = $entryDAO->listEntries (
  90. $this->state,
  91. $this->order,
  92. $this->first,
  93. $sqlLimit
  94. );
  95. break;
  96. case 'favoris':
  97. list ($this->entries, $this->next) = $entryDAO->listFavorites (
  98. $this->state,
  99. $this->order,
  100. $this->first,
  101. $sqlLimit
  102. );
  103. break;
  104. case 'c':
  105. list ($this->entries, $this->next) = $entryDAO->listByCategory (
  106. $this->type['id'],
  107. $this->state,
  108. $this->order,
  109. $this->first,
  110. $sqlLimit
  111. );
  112. break;
  113. case 'f':
  114. list ($this->entries, $this->next) = $entryDAO->listByFeed (
  115. $this->type['id'],
  116. $this->state,
  117. $this->order,
  118. $this->first,
  119. $sqlLimit
  120. );
  121. break;
  122. default:
  123. throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
  124. }
  125. }
  126. public function getPaginator () {
  127. $paginator = new RSSPaginator ($this->entries, $this->next);
  128. return $paginator;
  129. }
  130. }