mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-06 19:19:19 +02:00
Add admin form field partials and apply to add/edit forms
Four reusable PHP partials extracted to templates/partials/form/:
- text-field.php — single-line input (text/number/url); wraps input+hint in div,
skips the inner wrapper when no hint is present. Supports $type,
$placeholder, $required, $attrs, $hint, $id overrides.
- select-field.php — <select> with leading empty option; matches $selected against
option id OR option name string (handles view-sourced data where
orientation/ap/finality come back as name strings, not FK ids).
- checkbox-list.php — checkbox group (languages, formats); renders .admin-checkbox-list
with typed-string comparison so int ids from DB match string values.
- file-field.php — file input with accept/multiple/hint; appends [] to name when
$multiple is true.
Both add.php and edit.php rewritten to use the partials:
- ~15 repeated text-field divs collapsed to single-line include calls
- ~6 repeated select divs collapsed to single-line include calls
- 4 checkbox-list blocks collapsed to 2 calls each
- 3 file input blocks collapsed to single-line include calls
- Textarea fields (synopsis, context_note) kept inline — no partial for <textarea>
- Banner preview block in edit.php kept inline — conditional UI not generalised
Line count: add.php 251→93 (-158), edit.php 289→171 (-118)
This commit is contained in:
29
templates/partials/form/checkbox-list.php
Normal file
29
templates/partials/form/checkbox-list.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Checkbox list partial — renders a group of checkboxes (e.g. languages, formats).
|
||||
*
|
||||
* Variables consumed:
|
||||
* string $name — input name attribute (will be posted as array: name[])
|
||||
* string $label — group label (rendered as plain <label>, not associated with any single input)
|
||||
* array $options — each element must have 'id' and 'name' keys
|
||||
* array $checked — array of 'id' values that are currently checked
|
||||
*/
|
||||
|
||||
$checked = $checked ?? [];
|
||||
?>
|
||||
<div>
|
||||
<label><?= htmlspecialchars($label) ?></label>
|
||||
<div class="admin-checkbox-list">
|
||||
<?php foreach ($options as $opt): ?>
|
||||
<label class="admin-checkbox-label">
|
||||
<input type="checkbox"
|
||||
name="<?= htmlspecialchars($name) ?>[]"
|
||||
value="<?= htmlspecialchars((string)$opt['id']) ?>"
|
||||
<?= in_array((string)$opt['id'], array_map('strval', $checked)) ? 'checked' : '' ?>>
|
||||
<?= htmlspecialchars($opt['name']) ?>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
unset($checked);
|
||||
33
templates/partials/form/file-field.php
Normal file
33
templates/partials/form/file-field.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* File input partial.
|
||||
*
|
||||
* Variables consumed:
|
||||
* string $name — input name attribute (used for id too unless $id set)
|
||||
* string $label — visible label text
|
||||
* string $accept — MIME types / extensions for the accept attribute (e.g. 'image/jpeg,image/png')
|
||||
* string|null $hint — optional hint shown in <small> below the input
|
||||
* bool $multiple — whether to allow multiple file selection; default false
|
||||
* string|null $id — override the id attribute (defaults to $name)
|
||||
*/
|
||||
|
||||
$accept = $accept ?? '';
|
||||
$hint = $hint ?? null;
|
||||
$multiple = $multiple ?? false;
|
||||
$id = $id ?? $name;
|
||||
?>
|
||||
<div>
|
||||
<label for="<?= htmlspecialchars($id) ?>"><?= htmlspecialchars($label) ?></label>
|
||||
<div class="admin-file-input">
|
||||
<input type="file"
|
||||
id="<?= htmlspecialchars($id) ?>"
|
||||
name="<?= htmlspecialchars($name) ?><?= $multiple ? '[]' : '' ?>"
|
||||
<?= $accept ? 'accept="' . htmlspecialchars($accept) . '"' : '' ?>
|
||||
<?= $multiple ? 'multiple' : '' ?>>
|
||||
<?php if ($hint): ?>
|
||||
<small><?= htmlspecialchars($hint) ?></small>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
unset($accept, $hint, $multiple, $id);
|
||||
49
templates/partials/form/select-field.php
Normal file
49
templates/partials/form/select-field.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Select field partial.
|
||||
*
|
||||
* Variables consumed:
|
||||
* string $name — select name attribute (also used for id)
|
||||
* string $label — visible label text
|
||||
* array $options — each element must have 'id' and 'name' keys;
|
||||
* may optionally have 'code' for display suffix
|
||||
* mixed $selected — currently selected value (compared to option 'id');
|
||||
* pass null or '' for no selection
|
||||
* bool $required — whether the field is required; default false
|
||||
* string $placeholder — text for the leading empty <option>; default ''
|
||||
* set to null to suppress the empty option entirely
|
||||
* string|null $id — override the id attribute (defaults to $name)
|
||||
* string|null $hint — optional hint shown in <small> below the select
|
||||
*/
|
||||
|
||||
$required = $required ?? false;
|
||||
$placeholder = array_key_exists('placeholder', get_defined_vars()) ? $placeholder : '';
|
||||
$id = $id ?? $name;
|
||||
$hint = $hint ?? null;
|
||||
?>
|
||||
<div>
|
||||
<label for="<?= htmlspecialchars($id) ?>"><?= htmlspecialchars($label) ?></label>
|
||||
<select id="<?= htmlspecialchars($id) ?>"
|
||||
name="<?= htmlspecialchars($name) ?>"
|
||||
<?= $required ? 'required' : '' ?>>
|
||||
<?php if ($placeholder !== null): ?>
|
||||
<option value=""><?= htmlspecialchars($placeholder) ?></option>
|
||||
<?php endif; ?>
|
||||
<?php foreach ($options as $opt): ?>
|
||||
<?php
|
||||
// Match by id (numeric FK) or by name string (when the view returns the name).
|
||||
$isSelected = ((string)$selected === (string)$opt['id'])
|
||||
|| ($selected !== null && $selected !== '' && isset($opt['name']) && (string)$selected === (string)$opt['name']);
|
||||
?>
|
||||
<option value="<?= htmlspecialchars((string)$opt['id']) ?>"
|
||||
<?= $isSelected ? 'selected' : '' ?>>
|
||||
<?= htmlspecialchars($opt['name']) ?><?php if (!empty($opt['code'])): ?> (<?= htmlspecialchars($opt['code']) ?>)<?php endif; ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<?php if ($hint): ?>
|
||||
<small><?= htmlspecialchars($hint) ?></small>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
unset($required, $placeholder, $id, $hint);
|
||||
56
templates/partials/form/text-field.php
Normal file
56
templates/partials/form/text-field.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Text field partial — single-line text / number / url / email input.
|
||||
*
|
||||
* Variables consumed:
|
||||
* string $name — input name attribute (also used for id)
|
||||
* string $label — visible label text
|
||||
* string $value — current value (already htmlspecialchars'd by caller, or raw)
|
||||
* string $type — input type; default 'text'
|
||||
* bool $required — whether the field is required; default false
|
||||
* string $placeholder — placeholder text; default ''
|
||||
* string|null $hint — optional hint shown in <small> below the input
|
||||
* string|null $id — override the id attribute (defaults to $name)
|
||||
* array $attrs — extra HTML attributes as key=>value pairs (e.g. min/max for number)
|
||||
*
|
||||
* The partial does NOT call htmlspecialchars on $value — the caller is responsible.
|
||||
*/
|
||||
|
||||
$type = $type ?? 'text';
|
||||
$required = $required ?? false;
|
||||
$placeholder = $placeholder ?? '';
|
||||
$hint = $hint ?? null;
|
||||
$id = $id ?? $name;
|
||||
$attrs = $attrs ?? [];
|
||||
|
||||
$attrStr = '';
|
||||
foreach ($attrs as $k => $v) {
|
||||
$attrStr .= ' ' . htmlspecialchars($k) . '="' . htmlspecialchars((string)$v) . '"';
|
||||
}
|
||||
?>
|
||||
<div>
|
||||
<label for="<?= htmlspecialchars($id) ?>"><?= htmlspecialchars($label) ?></label>
|
||||
<?php if ($hint): ?>
|
||||
<div>
|
||||
<input type="<?= htmlspecialchars($type) ?>"
|
||||
id="<?= htmlspecialchars($id) ?>"
|
||||
name="<?= htmlspecialchars($name) ?>"
|
||||
value="<?= $value ?>"
|
||||
<?= $required ? 'required' : '' ?>
|
||||
<?= $placeholder ? 'placeholder="' . htmlspecialchars($placeholder) . '"' : '' ?>
|
||||
<?= $attrStr ?>>
|
||||
<small><?= htmlspecialchars($hint) ?></small>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<input type="<?= htmlspecialchars($type) ?>"
|
||||
id="<?= htmlspecialchars($id) ?>"
|
||||
name="<?= htmlspecialchars($name) ?>"
|
||||
value="<?= $value ?>"
|
||||
<?= $required ? 'required' : '' ?>
|
||||
<?= $placeholder ? 'placeholder="' . htmlspecialchars($placeholder) . '"' : '' ?>
|
||||
<?= $attrStr ?>>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
// Reset consumed variables so includes in a loop don't bleed state.
|
||||
unset($type, $required, $placeholder, $hint, $id, $attrs, $attrStr, $k, $v);
|
||||
Reference in New Issue
Block a user