getMessage(); return $message ?? ''; } } function render_info_or_json(bool $json, string $message, int $httpCode = 200, string $template = 'errors.%d'): string { $error = $httpCode >= 400; http_response_code($httpCode); if($json) { return json_encode([($error ? 'error' : 'message') => $message, 'success' => $error]); } return render_info($message, $httpCode, $template); } function html_link(string $url, ?string $content = null, $attributes = []): string { $content = $content ?? $url; $attributes = array_merge( is_string($attributes) ? ['class' => $attributes] : $attributes, ['href' => $url] ); if(mb_strpos($url, '://') !== false) { $attributes['target'] = '_blank'; $attributes['rel'] = 'noreferrer noopener'; } $html = ' $value) { $value = str_replace('"', '\"', $value); $html .= " {$name}=\"{$value}\""; } $html .= ">{$content}"; return $html; } function html_colour(?int $colour, $attribs = '--user-colour'): string { $colour = $colour ?? colour_none(); if(is_string($attribs)) { $attribs = [ $attribs => '%s', ]; } if(!$attribs) { $attribs = [ 'color' => '%s', '--user-colour' => '%s', ]; } $css = ''; $value = colour_get_css($colour); foreach($attribs as $name => $format) { $css .= $name . ':' . sprintf($format, $value) . ';'; } return $css; } function html_avatar(int $userId, int $resolution, string $altText = '', array $attributes = []): string { $attributes['src'] = url('user-avatar', ['user' => $userId, 'res' => $resolution * 2]); $attributes['alt'] = $altText; $attributes['class'] = trim('avatar ' . ($attributes['class'] ?? '')); if(!isset($attributes['width'])) $attributes['width'] = $resolution; if(!isset($attributes['height'])) $attributes['height'] = $resolution; return html_tag('img', $attributes); } function html_tag(string $name, array $atrributes = [], ?bool $close = null, string $content = ''): string { $html = '<' . $name; foreach($atrributes as $key => $value) { $html .= ' ' . $key; if(!empty($value)) $html .= '="' . $value . '"'; } if($close === false) $html .= '/'; $html .= '>'; if($close === true) $html .= $content . ''; return $html; }