Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | class categories { |
| 3 | private static function parseEmails($string) { |
| 4 | $string = str_replace(" ", "", $string); |
| 5 | $emails = explode(",", $string); |
| 6 | |
| 7 | foreach ($emails as $i => &$e) { |
| 8 | if (empty($e)) { |
| 9 | unset($emails[$i]); |
| 10 | } else { |
| 11 | if (filter_var($e, FILTER_VALIDATE_EMAIL) === false) return false; |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | return $emails; |
| 16 | } |
| 17 | |
| 18 | public static function readableEmails($emails) { |
| 19 | $array = json_decode($emails, true); |
| 20 | if (json_last_error() != JSON_ERROR_NONE) return false; |
| 21 | return implode(", ", $array); |
| 22 | } |
| 23 | |
| 24 | private static function canBeParent($id) { |
| 25 | if ($id == 0) return true; |
| 26 | |
| 27 | $category = self::get($id); |
| 28 | return ($category !== false && $category["parent"] == 0); |
| 29 | } |
| 30 | |
| 31 | public static function add($category, $stringEmails, $parent) { |
| 32 | global $con; |
| 33 | |
| 34 | $emails = (empty($stringEmails) ? [] : self::parseEmails($stringEmails)); |
| 35 | if ($emails === false) return false; |
| 36 | |
| 37 | if (!self::canBeParent($parent)) return false; |
| 38 | |
| 39 | $scategory = db::sanitize($category); |
| 40 | $semails = db::sanitize(json_encode($emails)); |
| 41 | $sparent = (int)$parent; |
| 42 | |
| 43 | return mysqli_query($con, "INSERT INTO categories (name, emails, parent) VALUES ('$scategory', '$semails', $sparent)"); |
| 44 | } |
| 45 | |
| 46 | public static function edit($id, $name, $stringEmails, $parent) { |
| 47 | global $con; |
| 48 | |
| 49 | $emails = (empty($stringEmails) ? [] : self::parseEmails($stringEmails)); |
| 50 | if ($emails === false) return false; |
| 51 | |
| 52 | if (!self::canBeParent($parent)) return false; |
| 53 | |
| 54 | $sid = (int)$id; |
| 55 | $sname = db::sanitize($name); |
| 56 | $semails = db::sanitize(json_encode($emails)); |
| 57 | $sparent = (int)$parent; |
| 58 | |
| 59 | return mysqli_query($con, "UPDATE categories SET name = '$sname', emails = '$semails', parent = $sparent WHERE id = $sid LIMIT 1"); |
| 60 | } |
| 61 | |
| 62 | public static function get($id) { |
| 63 | global $con; |
| 64 | |
| 65 | $sid = (int)$id; |
| 66 | |
| 67 | $query = mysqli_query($con, "SELECT * FROM categories WHERE id = $sid"); |
| 68 | |
| 69 | if (!mysqli_num_rows($query)) return false; |
| 70 | |
| 71 | return mysqli_fetch_assoc($query); |
| 72 | } |
| 73 | |
| 74 | private static function addChilds(&$row) { |
| 75 | global $con; |
| 76 | |
| 77 | $query = mysqli_query($con, "SELECT * FROM categories WHERE parent = ".(int)$row["id"]); |
| 78 | |
| 79 | $row["childs"] = []; |
| 80 | while ($child = mysqli_fetch_assoc($query)) { |
| 81 | $row["childs"][] = $child; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public static function getAll($simplified = true, $withparents = true, $includechilds = false) { |
| 86 | global $con, $conf; |
| 87 | |
| 88 | $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"); |
| 89 | |
| 90 | $categories = []; |
| 91 | |
| 92 | while ($row = mysqli_fetch_assoc($query)) { |
| 93 | if (!$simplified && $includechilds) self::addChilds($row); |
| 94 | |
| 95 | if ($simplified) $categories[$row["id"]] = $row["name"]; |
| 96 | else $categories[] = $row; |
| 97 | } |
| 98 | |
| 99 | return $categories; |
| 100 | } |
| 101 | |
| 102 | public static function getAllWithWorkers() { |
| 103 | global $con; |
| 104 | |
| 105 | $categories = self::getAll(false); |
| 106 | |
| 107 | foreach ($categories as &$category) { |
| 108 | $category["workers"] = []; |
| 109 | |
| 110 | $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"]); |
| 111 | |
| 112 | while ($row = mysqli_fetch_assoc($query)) { |
| 113 | $category["workers"][] = $row["id"]; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return $categories; |
| 118 | } |
| 119 | |
| 120 | public static function getChildren() { |
| 121 | global $con, $conf; |
| 122 | |
| 123 | $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"); |
| 124 | |
| 125 | $childs = []; |
| 126 | |
| 127 | while ($row = mysqli_fetch_assoc($query)) { |
| 128 | if (!isset($childs[$row["parent"]])) { |
| 129 | $childs[$row["parent"]] = []; |
| 130 | } |
| 131 | $childs[$row["parent"]][] = $row["child"]; |
| 132 | } |
| 133 | |
| 134 | return $childs; |
| 135 | } |
| 136 | |
| 137 | public static function exists($id) { |
| 138 | global $con; |
| 139 | |
| 140 | if ($id == -1) return true; |
| 141 | |
| 142 | $query = mysqli_query($con, "SELECT id FROM categories WHERE id = ".(int)$id); |
| 143 | |
| 144 | return (mysqli_num_rows($query) > 0); |
| 145 | } |
| 146 | |
| 147 | public static function getIdByName($name) { |
| 148 | global $con; |
| 149 | |
| 150 | if (strtolower($name) == "sin categorÃa") return -1; |
| 151 | |
| 152 | $sname = db::sanitize($name); |
| 153 | $query = mysqli_query($con, "SELECT id FROM categories WHERE name = '$sname'"); |
| 154 | |
| 155 | if (!mysqli_num_rows($query)) return false; |
| 156 | |
| 157 | $row = mysqli_fetch_assoc($query); |
| 158 | |
| 159 | return $row["id"]; |
| 160 | } |
| 161 | } |