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