summaryrefslogtreecommitdiff
path: root/app/models/Category.php
blob: 5b5d45b15f13cb8ca3520feb9ece3fc7304ea8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

class Category extends Model {
	private $id;
	private $name;
	private $color;
	
	public function __construct ($name = '', $color = '#0062BE') {
		$this->_name ($name);
		$this->_color ($color);
	}
	
	public function id () {
		return small_hash ($this->name . Configuration::selApplication ());
	}
	public function name () {
		return $this->name;
	}
	public function color () {
		return $this->color;
	}
	
	public function _name ($value) {
		$this->name = $value;
	}
	public function _color ($value) {
		if (preg_match ('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
			$this->color = $value;
		} else {
			$this->color = '#0062BE';
		}
	}
}

class CategoryDAO extends Model_array {
	public function __construct () {
		parent::__construct (PUBLIC_PATH . '/data/db/Categories.array.php');
	}
	
	public function addCategory ($values) {
		$id = $values['id'];
		unset ($values['id']);
	
		if (!isset ($this->array[$id])) {
			$this->array[$id] = array ();
		
			foreach ($values as $key => $value) {
				$this->array[$id][$key] = $value;
			}
		} else {
			return false;
		}
	}
	
	public function updateCategory ($id, $values) {
		foreach ($values as $key => $value) {
			$this->array[$id][$key] = $value;
		}
	}
	
	public function deleteCategory ($id) {
		if (isset ($this->array[$id])) {
			unset ($this->array[$id]);
		}
	}
	
	public function searchById ($id) {
		$list = HelperCategory::daoToCategory ($this->array);
		
		if (isset ($list[$id])) {
			return $list[$id];
		} else {
			return false;
		}
	}
	
	public function listCategories () {
		$list = $this->array;
		
		if (!is_array ($list)) {
			$list = array ();
		}
		
		return HelperCategory::daoToCategory ($list);
	}
	
	public function count () {
		return count ($this->array);
	}
	
	public function save () {
		$this->writeFile ($this->array);
	}
}

class HelperCategory {
	public static function daoToCategory ($listDAO) {
		$list = array ();

		if (!is_array ($listDAO)) {
			$listDAO = array ($listDAO);
		}

		foreach ($listDAO as $key => $dao) {
			$list[$key] = new Category (
				$dao['name'],
				$dao['color']
			);
		}

		return $list;
	}
}