Category.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 . time () . 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 searchByName ($name) {
  113. $sql = 'SELECT * FROM category WHERE name=?';
  114. $stm = $this->bd->prepare ($sql);
  115. $values = array ($name);
  116. $stm->execute ($values);
  117. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  118. $cat = HelperCategory::daoToCategory ($res);
  119. if (isset ($cat[0])) {
  120. return $cat[0];
  121. } else {
  122. return false;
  123. }
  124. }
  125. public function listCategories () {
  126. $sql = 'SELECT * FROM category ORDER BY name';
  127. $stm = $this->bd->prepare ($sql);
  128. $stm->execute ();
  129. return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
  130. }
  131. public function checkDefault () {
  132. $def_cat = $this->searchById ('000000');
  133. if (!$def_cat) {
  134. $cat = new Category ('Sans catégorie');
  135. $cat->_id ('000000');
  136. $values = array (
  137. 'id' => $cat->id (),
  138. 'name' => $cat->name (),
  139. 'color' => $cat->color ()
  140. );
  141. $this->addCategory ($values);
  142. }
  143. }
  144. public function count () {
  145. $sql = 'SELECT COUNT(*) AS count FROM category';
  146. $stm = $this->bd->prepare ($sql);
  147. $stm->execute ();
  148. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  149. return $res[0]['count'];
  150. }
  151. public function countFeed ($id) {
  152. $sql = 'SELECT COUNT(*) AS count FROM feed WHERE category=?';
  153. $stm = $this->bd->prepare ($sql);
  154. $values = array ($id);
  155. $stm->execute ($values);
  156. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  157. return $res[0]['count'];
  158. }
  159. public function countNotRead ($id) {
  160. $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';
  161. $stm = $this->bd->prepare ($sql);
  162. $values = array ($id);
  163. $stm->execute ($values);
  164. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  165. return $res[0]['count'];
  166. }
  167. }
  168. class HelperCategory {
  169. public static function daoToCategory ($listDAO) {
  170. $list = array ();
  171. if (!is_array ($listDAO)) {
  172. $listDAO = array ($listDAO);
  173. }
  174. foreach ($listDAO as $key => $dao) {
  175. $cat = new Category (
  176. $dao['name'],
  177. $dao['color']
  178. );
  179. $cat->_id ($dao['id']);
  180. $list[$key] = $cat;
  181. }
  182. return $list;
  183. }
  184. }