Category.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. class Category extends Model {
  3. private $id;
  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. return small_hash ($this->name . Configuration::selApplication ());
  12. }
  13. public function name () {
  14. return $this->name;
  15. }
  16. public function color () {
  17. return $this->color;
  18. }
  19. public function _name ($value) {
  20. $this->name = $value;
  21. }
  22. public function _color ($value) {
  23. if (preg_match ('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
  24. $this->color = $value;
  25. } else {
  26. $this->color = '#0062BE';
  27. }
  28. }
  29. }
  30. class CategoryDAO extends Model_array {
  31. public function __construct () {
  32. parent::__construct (PUBLIC_PATH . '/data/db/Categories.array.php');
  33. }
  34. public function addCategory ($values) {
  35. $id = $values['id'];
  36. unset ($values['id']);
  37. if (!isset ($this->array[$id])) {
  38. $this->array[$id] = array ();
  39. foreach ($values as $key => $value) {
  40. $this->array[$id][$key] = $value;
  41. }
  42. } else {
  43. return false;
  44. }
  45. }
  46. public function updateCategory ($id, $values) {
  47. foreach ($values as $key => $value) {
  48. $this->array[$id][$key] = $value;
  49. }
  50. }
  51. public function deleteCategory ($id) {
  52. if (isset ($this->array[$id])) {
  53. unset ($this->array[$id]);
  54. }
  55. }
  56. public function searchById ($id) {
  57. $list = HelperCategory::daoToCategory ($this->array);
  58. if (isset ($list[$id])) {
  59. return $list[$id];
  60. } else {
  61. return false;
  62. }
  63. }
  64. public function listCategories () {
  65. $list = $this->array;
  66. if (!is_array ($list)) {
  67. $list = array ();
  68. }
  69. return HelperCategory::daoToCategory ($list);
  70. }
  71. public function count () {
  72. return count ($this->array);
  73. }
  74. public function save () {
  75. $this->writeFile ($this->array);
  76. }
  77. }
  78. class HelperCategory {
  79. public static function daoToCategory ($listDAO) {
  80. $list = array ();
  81. if (!is_array ($listDAO)) {
  82. $listDAO = array ($listDAO);
  83. }
  84. foreach ($listDAO as $key => $dao) {
  85. $list[$key] = new Category (
  86. $dao['name'],
  87. $dao['color']
  88. );
  89. }
  90. return $list;
  91. }
  92. }