Category.php 2.8 KB

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