Category.php 2.5 KB

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