Category.php 3.6 KB

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