summaryrefslogtreecommitdiff
path: root/app/models/Category.php
blob: e0b8f564df7d326a4b6957d5e616e018d0a5a238 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php

class Category extends Model {
	private $id = false;
	private $name;
	private $color;
	
	public function __construct ($name = '', $color = '#0062BE') {
		$this->_name ($name);
		$this->_color ($color);
	}
	
	public function id () {
		if (!$this->id) {
			return small_hash ($this->name . Configuration::selApplication ());
		} else {
			return $this->id;
		}
	}
	public function name () {
		return $this->name;
	}
	public function color () {
		return $this->color;
	}
	
	public function _id ($value) {
		$this->id = $value;
	}
	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_pdo {
	public function addCategory ($valuesTmp) {
		$sql = 'INSERT INTO category (id, name, color) VALUES(?, ?, ?)';
		$stm = $this->bd->prepare ($sql);

		$values = array (
			$valuesTmp['id'],
			$valuesTmp['name'],
			$valuesTmp['color'],
		);

		if ($stm && $stm->execute ($values)) {
			return true;
		} else {
			return false;
		}
	}
	
	public function updateCategory ($id, $valuesTmp) {
		$sql = 'UPDATE category SET name=?, color=? WHERE id=?';
		$stm = $this->bd->prepare ($sql);

		$values = array (
			$valuesTmp['name'],
			$valuesTmp['color'],
			$id
		);

		if ($stm && $stm->execute ($values)) {
			return true;
		} else {
			return false;
		}
	}
	
	public function deleteCategory ($id) {
		$sql = 'DELETE FROM category WHERE id=?';
		$stm = $this->bd->prepare ($sql); 

		$values = array ($id);

		if ($stm && $stm->execute ($values)) {
			return true;
		} else {
			return false;
		}
	}
	
	public function searchById ($id) {
		$sql = 'SELECT * FROM category WHERE id=?';
		$stm = $this->bd->prepare ($sql);
		
		$values = array ($id);
		
		$stm->execute ($values);
		$res = $stm->fetchAll (PDO::FETCH_ASSOC);
		$cat = HelperCategory::daoToCategory ($res);
		
		if (isset ($cat[0])) {
			return $cat[0];
		} else {
			return false;
		}
	}
	
	public function listCategories () {
		$sql = 'SELECT * FROM category';
		$stm = $this->bd->prepare ($sql); 
		$stm->execute ();

		return HelperCategory::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
	}
	
	public function count () {
		$sql = 'SELECT COUNT (*) AS count FROM category';
		$stm = $this->bd->prepare ($sql); 
		$stm->execute ();
		$res = $stm->fetchAll (PDO::FETCH_ASSOC);

		return $res[0]['count'];
	}
}

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

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

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

		return $list;
	}
}