blob: d4b3453dc5e492aabc097649a851de51908548bf [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
Adrià Vilanova Martínez5af86512023-12-02 20:44:16 +01002/*
3 * hores
4 * Copyright (c) 2023 Adrià Vilanova Martínez
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public
17 * License along with this program.
18 * If not, see http://www.gnu.org/licenses/.
19 */
20
Copybara botbe50d492023-11-30 00:16:42 +010021class categories {
22 private static function parseEmails($string) {
23 $string = str_replace(" ", "", $string);
24 $emails = explode(",", $string);
25
26 foreach ($emails as $i => &$e) {
27 if (empty($e)) {
28 unset($emails[$i]);
29 } else {
30 if (filter_var($e, FILTER_VALIDATE_EMAIL) === false) return false;
31 }
32 }
33
34 return $emails;
35 }
36
37 public static function readableEmails($emails) {
38 $array = json_decode($emails, true);
39 if (json_last_error() != JSON_ERROR_NONE) return false;
40 return implode(", ", $array);
41 }
42
43 private static function canBeParent($id) {
44 if ($id == 0) return true;
45
46 $category = self::get($id);
47 return ($category !== false && $category["parent"] == 0);
48 }
49
50 public static function add($category, $stringEmails, $parent) {
51 global $con;
52
53 $emails = (empty($stringEmails) ? [] : self::parseEmails($stringEmails));
54 if ($emails === false) return false;
55
56 if (!self::canBeParent($parent)) return false;
57
58 $scategory = db::sanitize($category);
59 $semails = db::sanitize(json_encode($emails));
60 $sparent = (int)$parent;
61
62 return mysqli_query($con, "INSERT INTO categories (name, emails, parent) VALUES ('$scategory', '$semails', $sparent)");
63 }
64
65 public static function edit($id, $name, $stringEmails, $parent) {
66 global $con;
67
68 $emails = (empty($stringEmails) ? [] : self::parseEmails($stringEmails));
69 if ($emails === false) return false;
70
71 if (!self::canBeParent($parent)) return false;
72
73 $sid = (int)$id;
74 $sname = db::sanitize($name);
75 $semails = db::sanitize(json_encode($emails));
76 $sparent = (int)$parent;
77
78 return mysqli_query($con, "UPDATE categories SET name = '$sname', emails = '$semails', parent = $sparent WHERE id = $sid LIMIT 1");
79 }
80
81 public static function get($id) {
82 global $con;
83
84 $sid = (int)$id;
85
86 $query = mysqli_query($con, "SELECT * FROM categories WHERE id = $sid");
87
88 if (!mysqli_num_rows($query)) return false;
89
90 return mysqli_fetch_assoc($query);
91 }
92
93 private static function addChilds(&$row) {
94 global $con;
95
96 $query = mysqli_query($con, "SELECT * FROM categories WHERE parent = ".(int)$row["id"]);
97
98 $row["childs"] = [];
99 while ($child = mysqli_fetch_assoc($query)) {
100 $row["childs"][] = $child;
101 }
102 }
103
104 public static function getAll($simplified = true, $withparents = true, $includechilds = false) {
105 global $con, $conf;
106
107 $query = mysqli_query($con, "SELECT ".($simplified ? "id, name" : "c.id id, c.name name, c.parent parent, c.emails emails, p.name parentname")." FROM categories c".($simplified ? "" : " LEFT JOIN categories p ON c.parent = p.id").($withparents ? "" : " WHERE c.parent = 0")." ORDER BY ".($conf["debug"] ? "id" : "name")." ASC");
108
109 $categories = [];
110
111 while ($row = mysqli_fetch_assoc($query)) {
112 if (!$simplified && $includechilds) self::addChilds($row);
113
114 if ($simplified) $categories[$row["id"]] = $row["name"];
115 else $categories[] = $row;
116 }
117
118 return $categories;
119 }
120
121 public static function getAllWithWorkers() {
122 global $con;
123
124 $categories = self::getAll(false);
125
126 foreach ($categories as &$category) {
127 $category["workers"] = [];
128
129 $query = mysqli_query($con, "SELECT w.id FROM workers w LEFT JOIN people p ON w.person = p.id WHERE p.category = ".(int)$category["id"]);
130
131 while ($row = mysqli_fetch_assoc($query)) {
132 $category["workers"][] = $row["id"];
133 }
134 }
135
136 return $categories;
137 }
138
139 public static function getChildren() {
140 global $con, $conf;
141
142 $query = mysqli_query($con, "SELECT p.id parent, c.id child FROM categories c LEFT JOIN categories p ON c.parent = p.id WHERE c.parent != 0");
143
144 $childs = [];
145
146 while ($row = mysqli_fetch_assoc($query)) {
147 if (!isset($childs[$row["parent"]])) {
148 $childs[$row["parent"]] = [];
149 }
150 $childs[$row["parent"]][] = $row["child"];
151 }
152
153 return $childs;
154 }
155
156 public static function exists($id) {
157 global $con;
158
159 if ($id == -1) return true;
160
161 $query = mysqli_query($con, "SELECT id FROM categories WHERE id = ".(int)$id);
162
163 return (mysqli_num_rows($query) > 0);
164 }
165
166 public static function getIdByName($name) {
167 global $con;
168
169 if (strtolower($name) == "sin categoría") return -1;
170
171 $sname = db::sanitize($name);
172 $query = mysqli_query($con, "SELECT id FROM categories WHERE name = '$sname'");
173
174 if (!mysqli_num_rows($query)) return false;
175
176 $row = mysqli_fetch_assoc($query);
177
178 return $row["id"];
179 }
180}