This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Category.php
101 lines (90 loc) · 2.56 KB
/
Category.php
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
<?php
/**
* Class Category
* Handles category db manipulation
*
*/
class Category {
/**
* @var $db - medoo database object
*/
private $db;
/**
* @param $db - medoo database object
*/
public function __construct($db) {
$this->db = $db;
}
/**
* Gets all the category records
*
* @param null $where
* @return
*/
public function getCategories($where = null) {
return $this->db->select('categories',['ID','parentID','name'],$where);
}
/**
* Add the form data to db
*
* @param $name - user name
* @param $categs - user selected categories
* @param $agreed - user agreed to terms
* @return array|bool - db data or false
*/
public function store($name, $categs, $agreed) {
$data = [];
$user = $this->db->insert('users', ['name' => $name, 'agreed' => $agreed]);
$data['user'] = $user;
$data['categories'] = [];
foreach($categs as $cat) {
if(!$this->db->insert('user_categories', ['userID' => $user, 'categoryID' => $cat]))
return false;
array_push($data['categories'],$cat);
}
return $data;
}
/**
* Update user name
*
* @param $id - id which user to update
* @param $name - change the name to this
* @return bool - success or fail
*/
public function updateUserName($id, $name) {
if($this->db->update('users',['name' => $name],['ID' => $id])) {
return true;
}
return false;
}
/**
* Loops through given categories and inserts them to db
*
* @param $data - user data
* @param $categs - categories to add
* @return bool
*/
public function insertCategories($data, $categs) {
foreach($categs as $cat) {
if(!$this->db->insert('user_categories', ['userID' => $data['user'], 'categoryID' => $cat])) {
return false;
}
}
return true;
}
/**
* Loops through given categories and deletes them from db
*
* @param $data - user data
* @param $categs - categories to delete
* @return bool
*/
public function deleteCategories($data, $categs) {
foreach($categs as $cat) {
if(!$this->db->delete('user_categories', ['AND' => ['userID' => $data['user'], 'categoryID' => $cat]])) {
return false;
}
}
return true;
}
}