DK2/src/util/components.php

90 lines
2.5 KiB
PHP

<?php
/**
* Render the logout button's form.
*/
function c_logout_button()
{
return render('components/logout_button');
}
/**
* Render the character bar. Relies on there being a character in the session. Without one, this will return an empty
* string.
*/
function c_char_bar()
{
if (char() === false) return '';
return render('components/char_bar', ['char' => char()]);
}
/**
* Render the left sidebar navigation menu. Provide the active tab to highlight it. Will retrieve the current
* tab from the GLOBALS.
*/
function c_left_nav()
{
return render('components/left_nav');
}
/**
* Render the debug query log.
*/
function c_debug_query_log()
{
return render('components/debug_query_log');
}
/**
* Render the character select radio buttons.
*/
function c_char_select_box($id, $char)
{
return render('components/char_select_box', ['id' => $id, 'char' => $char]);
}
/**
* Render an alert with a given type and message.
*/
function c_alert($type, $message)
{
$a = $type !== 'danger' ? ' auto-close="5000"' : '';
return "<div class=\"alert $type\"$a><div>$message</div> <a alert-close>&times;</a></div>";
}
/**
* Renders a danger alert with form errors, if there are any. Add an optional placement id.
*/
function c_form_errors($placement = '')
{
$errors = $GLOBALS[($placement !== '' ? "form-errors-$placement" : 'form-errors')] ?? false;
if ($errors === false) return '';
$html = '';
foreach ($errors as $field)
if (!empty($field))
foreach ($field as $message) $html .= !empty($message) ? "<p>$message</p>" : '';
return c_alert('danger', $html);
}
/**
* Generate a text form field. This component will automatically add the error class if there are errors for this field,
* depending on the form-errors GLOBAL. Pass an optional form ID to target a specific form GLOBAL.
*/
function c_form_field($type, $name, $placeholder, $required = false, $autocomplete = "off", $formId = '')
{
$errors = $GLOBALS[($formId !== '' ? "form-errors-$formId" : 'form-errors')] ?? false;
$html = "<input type=\"$type\" name=\"$name\" id=\"$name\" placeholder=\"$placeholder\"";
if ($required) $html .= ' required';
if (isset($_POST[$name]) && $type !== 'password') $html .= ' value="' . $_POST[$name] . '"';
$html .= $errors !== false && !empty($errors[$name]) ? ' class="form control error"' : ' class="form control"';
return $html . " autocomplete=\"$autocomplete\">";
}
/**
* Render the stopwatch debug component.
*/
function c_debug_stopwatch()
{
return render('components/debug_stopwatch');
}