Category.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. class FreshRSS_Category extends Minz_Model {
  3. /**
  4. * @var int
  5. */
  6. private $id = 0;
  7. private $name;
  8. private $nbFeed = -1;
  9. private $nbNotRead = -1;
  10. private $feeds = null;
  11. private $hasFeedsWithError = false;
  12. private $isDefault = false;
  13. private $attributes = [];
  14. public function __construct(string $name = '', $feeds = null) {
  15. $this->_name($name);
  16. if (isset($feeds)) {
  17. $this->_feeds($feeds);
  18. $this->nbFeed = 0;
  19. $this->nbNotRead = 0;
  20. foreach ($feeds as $feed) {
  21. $this->nbFeed++;
  22. $this->nbNotRead += $feed->nbNotRead();
  23. $this->hasFeedsWithError |= $feed->inError();
  24. }
  25. }
  26. }
  27. public function id(): int {
  28. return $this->id;
  29. }
  30. public function name(): string {
  31. return $this->name;
  32. }
  33. public function isDefault(): bool {
  34. return $this->isDefault;
  35. }
  36. public function nbFeed(): int {
  37. if ($this->nbFeed < 0) {
  38. $catDAO = FreshRSS_Factory::createCategoryDao();
  39. $this->nbFeed = $catDAO->countFeed($this->id());
  40. }
  41. return $this->nbFeed;
  42. }
  43. public function nbNotRead(): int {
  44. if ($this->nbNotRead < 0) {
  45. $catDAO = FreshRSS_Factory::createCategoryDao();
  46. $this->nbNotRead = $catDAO->countNotRead($this->id());
  47. }
  48. return $this->nbNotRead;
  49. }
  50. public function feeds(): array {
  51. if ($this->feeds === null) {
  52. $feedDAO = FreshRSS_Factory::createFeedDao();
  53. $this->feeds = $feedDAO->listByCategory($this->id());
  54. $this->nbFeed = 0;
  55. $this->nbNotRead = 0;
  56. foreach ($this->feeds as $feed) {
  57. $this->nbFeed++;
  58. $this->nbNotRead += $feed->nbNotRead();
  59. $this->hasFeedsWithError |= $feed->inError();
  60. }
  61. }
  62. usort($this->feeds, function ($a, $b) {
  63. return strnatcasecmp($a->name(), $b->name());
  64. });
  65. return $this->feeds;
  66. }
  67. public function hasFeedsWithError() {
  68. return $this->hasFeedsWithError;
  69. }
  70. public function attributes($key = '') {
  71. if ($key == '') {
  72. return $this->attributes;
  73. } else {
  74. return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
  75. }
  76. }
  77. public function _id($id) {
  78. $this->id = intval($id);
  79. if ($id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
  80. $this->_name(_t('gen.short.default_category'));
  81. }
  82. }
  83. public function _name($value) {
  84. $this->name = mb_strcut(trim($value), 0, 255, 'UTF-8');
  85. }
  86. public function _isDefault($value) {
  87. $this->isDefault = $value;
  88. }
  89. public function _feeds($values) {
  90. if (!is_array($values)) {
  91. $values = array($values);
  92. }
  93. $this->feeds = $values;
  94. }
  95. public function _attributes($key, $value) {
  96. if ('' == $key) {
  97. if (is_string($value)) {
  98. $value = json_decode($value, true);
  99. }
  100. if (is_array($value)) {
  101. $this->attributes = $value;
  102. }
  103. } elseif (null === $value) {
  104. unset($this->attributes[$key]);
  105. } else {
  106. $this->attributes[$key] = $value;
  107. }
  108. }
  109. }