Category.php 3.1 KB

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