Category.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. class Category extends Model {
  3. private $id = false;
  4. private $name;
  5. private $color;
  6. private $feeds = null;
  7. public function __construct ($name = '', $color = '#0062BE') {
  8. $this->_name ($name);
  9. $this->_color ($color);
  10. }
  11. public function id () {
  12. if (!$this->id) {
  13. return small_hash ($this->name . Configuration::selApplication ());
  14. } else {
  15. return $this->id;
  16. }
  17. }
  18. public function name () {
  19. return $this->name;
  20. }
  21. public function color () {
  22. return $this->color;
  23. }
  24. public function nbFeed () {
  25. $catDAO = new CategoryDAO ();
  26. return $catDAO->countFeed ($this->id ());
  27. }
  28. public function nbNotRead () {
  29. $catDAO = new CategoryDAO ();
  30. return $catDAO->countNotRead ($this->id ());
  31. }
  32. public function feeds () {
  33. if (is_null ($this->feeds)) {
  34. $feedDAO = new FeedDAO ();
  35. return $feedDAO->listByCategory ($this->id ());
  36. } else {
  37. return $this->feeds;
  38. }
  39. }
  40. public function _id ($value) {
  41. $this->id = $value;
  42. }
  43. public function _name ($value) {
  44. $this->name = $value;
  45. }
  46. public function _color ($value) {
  47. if (preg_match ('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  48. $this->color = $value;
  49. } else {
  50. $this->color = '#0062BE';
  51. }
  52. }
  53. public function _feeds ($values) {
  54. if (!is_array ($values)) {
  55. $values = array ($values);
  56. }
  57. $this->feeds = $values;
  58. }
  59. }
  60. class CategoryDAO extends Model_pdo {
  61. public function addCategory ($valuesTmp) {
  62. $sql = 'INSERT INTO category (id, name, color) VALUES(?, ?, ?)';
  63. $stm = $this->bd->prepare ($sql);
  64. $values = array (
  65. $valuesTmp['id'],
  66. $valuesTmp['name'],
  67. $valuesTmp['color'],
  68. );
  69. if ($stm && $stm->execute ($values)) {
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75. public function updateCategory ($id, $valuesTmp) {
  76. $sql = 'UPDATE category SET name=?, color=? WHERE id=?';
  77. $stm = $this->bd->prepare ($sql);
  78. $values = array (
  79. $valuesTmp['name'],
  80. $valuesTmp['color'],
  81. $id
  82. );
  83. if ($stm && $stm->execute ($values)) {
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. public function deleteCategory ($id) {
  90. $sql = 'DELETE FROM category WHERE id=?';
  91. $stm = $this->bd->prepare ($sql);
  92. $values = array ($id);
  93. if ($stm && $stm->execute ($values)) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. public function searchById ($id) {
  100. $sql = 'SELECT * FROM category WHERE id=?';
  101. $stm = $this->bd->prepare ($sql);
  102. $values = array ($id);
  103. $stm->execute ($values);
  104. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  105. $cat = HelperCategory::daoToCategory ($res);
  106. if (isset ($cat[0])) {
  107. return $cat[0];
  108. } else {
  109. return false;
  110. }
  111. }
  112. public function listCategories () {
  113. $sql = 'SELECT * FROM category ORDER BY name';
  114. $stm = $this->bd->prepare ($sql);
  115. $stm->execute ();
  116. return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
  117. }
  118. public function count () {
  119. $sql = 'SELECT COUNT(*) AS count FROM category';
  120. $stm = $this->bd->prepare ($sql);
  121. $stm->execute ();
  122. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  123. return $res[0]['count'];
  124. }
  125. public function countFeed ($id) {
  126. $sql = 'SELECT COUNT(*) AS count FROM feed WHERE category=?';
  127. $stm = $this->bd->prepare ($sql);
  128. $values = array ($id);
  129. $stm->execute ($values);
  130. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  131. return $res[0]['count'];
  132. }
  133. public function countNotRead ($id) {
  134. $sql = 'SELECT COUNT(*) AS count FROM entry e INNER JOIN feed f ON e.id_feed = f.id WHERE category=? AND e.is_read=0';
  135. $stm = $this->bd->prepare ($sql);
  136. $values = array ($id);
  137. $stm->execute ($values);
  138. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  139. return $res[0]['count'];
  140. }
  141. }
  142. class HelperCategory {
  143. public static function daoToCategory ($listDAO) {
  144. $list = array ();
  145. if (!is_array ($listDAO)) {
  146. $listDAO = array ($listDAO);
  147. }
  148. foreach ($listDAO as $key => $dao) {
  149. $cat = new Category (
  150. $dao['name'],
  151. $dao['color']
  152. );
  153. $cat->_id ($dao['id']);
  154. $list[$key] = $cat;
  155. }
  156. return $list;
  157. }
  158. }