blob: 676d129bffff43e1cc8c178a25859c7ca1046774 [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 visual {
22 const VIEW_ADMIN = 0;
23 const VIEW_WORKER = 1;
24
25 // Old:
26 /*const YES = "✓";
27 const NO = "✗";*/
28
29 // New:
30 const YES = "✓";
31 const NO = "X";
32
33 public static function snackbar($msg, $timeout = 10000, $printHTML = true) {
34 if ($printHTML) echo '<div class="mdl-snackbar mdl-js-snackbar">
35 <div class="mdl-snackbar__text"></div>
36 <button type="button" class="mdl-snackbar__action"></button>
37 </div>';
38 echo '<script>
39 window.addEventListener("load", function() {
40 var notification = document.querySelector(".mdl-js-snackbar");
41 notification.MaterialSnackbar.showSnackbar(
42 {
43 message: "'.security::htmlsafe($msg).'",
44 timeout: '.(int)$timeout.'
45 }
46 );
47 });
48 </script>';
49 }
50
51 public static function smartSnackbar($msgs, $timeout = 10000, $printHTML = true) {
52 global $_GET;
53
54 if (!isset($_GET["msg"])) return;
55
56 foreach ($msgs as $msg) {
57 if ($_GET["msg"] == $msg[0]) {
58 self::snackbar($msg[1], $timeout, $printHTML);
59 return;
60 }
61 }
62 }
63
64 public static function debugJson($array) {
65 return security::htmlsafe(json_encode($array, JSON_PRETTY_PRINT));
66 }
67
68 public static function includeHead() {
69 include("includes/head.php");
70 }
71
72 public static function includeNav() {
73 global $conf, $mdHeaderRowMore, $mdHeaderMore, $mdHeaderRowBefore;
74
75 $activeView = security::getActiveView();
76 switch ($activeView) {
77 case self::VIEW_ADMIN:
78 include("includes/adminnav.php");
79 break;
80
81 case self::VIEW_WORKER:
82 include("includes/workernav.php");
83 break;
84
85 default:
86 exit();
87 }
88 }
89
90 public static function backBtn($url) {
91 return '<a class="backbtn mdl-button mdl-js-button mdl-button--icon mdl-js-ripple-effect" href="'.$url.'"><i id="auto_backbtn" class="material-icons">arrow_back</i></a><div class="mdl-tooltip" for="auto_backbtn">Atrás</div><div style="width: 16px;"></div>';
92 }
93
94 public static function printDebug($function, $return, $always=false, $notjson=false) {
95 global $conf;
96
97 if ($always || $conf["debug"])
98 echo '<details class="debug margintop">
99 <summary>Debug:</summary>
100 <p><b>'.security::htmlsafe($function).'</b></p>
101 <div class="overflow-wrapper"><pre>'.($notjson ? security::htmlsafe(print_r($return, true)) : self::debugJson($return)).'</pre></div>
102 </details>';
103 }
104
105 public static function renderPagination($rows, $page, $limit = 10, $showLimitLink = false, $alreadyHasParameters = false, $limitChange = false, $highlightedPage = false) {
106 global $_GET;
107
108 $numPages = ($limit == 0 ? 1 : ceil($rows/$limit));
109 if ($numPages > 1) {
110 $currentPage = ((isset($_GET["page"]) && $_GET["page"] <= $numPages && $_GET["page"] >= 1) ? $_GET["page"] : 1);
111
112 echo '<div class="pagination">';
113 for ($i = 1; $i <= $numPages; $i++) {
114 echo ($i != $currentPage ? '<a class="page'.($i == $highlightedPage ? " mdl-color-text--green" : "").'" href="'.security::htmlsafe($page).($alreadyHasParameters ? "&" : "?").'page='.(int)$i.($showLimitLink ? '&limit='.(int)$limit : '').'">'.(int)$i.'</a> ' : '<b class="page">'.(int)$i.'</b> ');
115 }
116 echo '</div>';
117 }
118
119 if ($limitChange !== false) {
120 ?>
121 <div class="limit-change-container">Ver <select id="limit-change">
122 <?php
123 if (isset($limitChange["options"])) {
124 if (!in_array($limit, $limitChange["options"])) {
125 echo "<option value=\"".(int)$limit."\" selected>".(int)$limit."</option>";
126 }
127 foreach ($limitChange["options"] as $option) {
128 echo "<option value=\"".(int)$option."\"".($option == $limit ? " selected" : "").">".(int)$option."</option>";
129 }
130 }
131 ?>
132 </select> <?=security::htmlsafe($limitChange["elementName"])?> por página.</div>
133 <?php
134 }
135 }
136
137 public static function padNum($num, $length) {
138 return str_pad($num, $length, "0", STR_PAD_LEFT);
139 }
140
141 public static function isMDColor() {
142 global $conf;
143
144 return (($conf["backgroundColor"][0] ?? "") != "#");
145 }
146
147 public static function printBodyTag() {
148 global $conf;
149
150 $conf["backgroundColorIsDark"];
151 echo "<body ".(!visual::isMDColor() ? "style=\"background-color: ".security::htmlsafe($conf["backgroundColor"]).";\"" : "")."class=\"".(visual::isMDColor() ? "mdl-color--".security::htmlsafe($conf["backgroundColor"]) : "").($conf["backgroundColorIsDark"] ? " dark-background" : "")."\">";
152 }
153
154 // WARNING: We will not sanitize $msg, so sanitize it before calling this function!
155 public static function addTooltip($id, $msg) {
156 global $_tooltips;
157
158 if (!isset($_tooltips)) $_tooltips = "";
159 $_tooltips .= '<div class="mdl-tooltip" for="'.security::htmlsafe($id).'">'.$msg.'</div>';
160 }
161
162 public static function renderTooltips() {
163 global $_tooltips;
164
165 echo ($_tooltips ?? "");
166 }
167
168 private static function addMsgToUrl($url, $msg = false) {
169 if ($msg === false) return $url;
170 return $url.(preg_match("/\?/", $url) == 1 ? "&" : "?")."msg=".urlencode($msg);
171 }
172
173 public static function getContinueUrl($defaultUrl, $msg = false, $method = "GET") {
174 global $_GET, $_POST;
175
176 $url = "";
177
178 switch ($method) {
179 case "GET":
180 if (!isset($_GET["continue"])) return self::addMsgToUrl($defaultUrl, $msg);
181 $url = (string)$_GET["continue"];
182 break;
183
184 case "POST":
185 if (!isset($_POST["continue"])) return self::addMsgToUrl($defaultUrl, $msg);
186 $url = (string)$_POST["continue"];
187 break;
188
189 default:
190 return self::addMsgToUrl($defaultUrl, $msg);
191 }
192
193 if (!preg_match("/^[^\/\\\\]*$/", $url)) return self::addMsgToUrl($defaultUrl, $msg);
194
195 if ($msg !== false) $url = self::addMsgToUrl($url, $msg);
196
197 return $url;
198 }
199
200 public static function addContinueInput($url = false) {
201 global $_GET, $_POST;
202
203 if ($url === false) {
204 if (isset($_GET["continue"])) $url = $_GET["continue"];
205 elseif (isset($_POST["continue"])) $url = $_POST["continue"];
206 else return;
207 }
208
209 echo '<input type="hidden" name="continue" value="'.security::htmlsafe($url).'">';
210 }
211}