v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- if (count(get_included_files()) === 1) {
- http_response_code(403);
- exit;
- }
-
- if (!Auth::hasRoleAtLeast(Auth::ROLE_STORE_OWNER)) {
- echo '<p>You do not have permission to manage categories.</p>';
- return;
- }
-
- $db = Database::site();
- $categories = $db->query('SELECT * FROM categories ORDER BY sort_order ASC, name ASC')->fetchAll();
- foreach ($categories as &$category) {
- if (!array_key_exists('show_in_menu', $category)) {
- $category['show_in_menu'] = 1;
- }
- }
- unset($category);
-
- ?>
- <div class="dash-header-row">
- <h1 class="dash-title"><?= Icons::icon("categories", "icon icon-lg") ?>Categories</h1>
- <button type="button" class="dash-btn dash-btn-primary" id="category-new-btn"><?= Icons::icon('plus', 'icon icon-sm') ?>New category</button>
- </div>
-
- <table class="dash-table">
- <thead>
- <tr>
- <th><?= Icons::icon('heading', 'icon icon-sm') ?>Name</th>
- <th><?= Icons::icon('hash', 'icon icon-sm') ?>Slug</th>
- <th><?= Icons::icon('menu', 'icon icon-sm') ?>In menu</th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($categories as $category): ?>
- <tr>
- <td><?= htmlspecialchars($category['name']) ?></td>
- <td><?= htmlspecialchars($category['slug']) ?></td>
- <td>
- <form method="post" style="display:inline;">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="toggle_category_menu">
- <input type="hidden" name="category_id" value="<?= (int) $category['id'] ?>">
- <button type="submit" class="dash-btn-small"><?= Icons::icon($category['show_in_menu'] ? 'check' : 'x', 'icon icon-sm') ?><?= $category['show_in_menu'] ? 'Shown' : 'Hidden' ?></button>
- </form>
- </td>
- <td class="dash-table-actions">
- <button type="button" class="dash-btn-small js-edit-category"
- data-id="<?= (int) $category['id'] ?>"
- data-name="<?= htmlspecialchars($category['name'], ENT_QUOTES) ?>"
- data-description="<?= htmlspecialchars((string) $category['description'], ENT_QUOTES) ?>"
- data-parent="<?= (int) ($category['parent_id'] ?? 0) ?>"
- data-show-in-menu="<?= (int) $category['show_in_menu'] ?>"><?= Icons::icon('edit', 'icon icon-sm') ?>Edit</button>
- <?php if (Languages::enabled()): ?>
- <a href="translate.php?type=category&id=<?= (int) $category['id'] ?>" class="dash-btn-small"><?= Icons::icon('translate', 'icon icon-sm') ?>Translate</a>
- <?php endif; ?>
- <form method="post" style="display:inline;" onsubmit="return confirm('Delete this category? Products in it will become uncategorized.');">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="delete_category">
- <input type="hidden" name="category_id" value="<?= (int) $category['id'] ?>">
- <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- <?php if (empty($categories)): ?>
- <tr>
- <td colspan="4">No categories yet.</td>
- </tr>
- <?php endif; ?>
- </tbody>
- </table>
-
- <div id="category-form-wrapper" class="sidebar-box" style="max-width:520px;margin-top:20px;display:none;">
- <h2 class="dash-subtitle" style="margin-top:0;"><?= Icons::icon('plus', 'icon icon-sm') ?><span id="category-form-title">New category</span></h2>
- <form method="post" id="category-form">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="save_category">
- <input type="hidden" name="category_id" id="category_id_input" value="">
-
- <label>Name</label>
- <input type="text" name="name" id="category_name_input" required>
-
- <label>Description</label>
- <textarea name="description" id="category_description_input" rows="2"></textarea>
-
- <label>Parent category</label>
- <select name="parent_id" id="category_parent_input">
- <option value="">— none —</option>
- <?php foreach ($categories as $category): ?>
- <option value="<?= (int) $category['id'] ?>"><?= htmlspecialchars($category['name']) ?></option>
- <?php endforeach; ?>
- </select>
-
- <label><input type="checkbox" name="show_in_menu" id="category_show_in_menu_input" checked> Show in site menu</label>
-
- <div class="publish-actions">
- <button type="submit" class="dash-btn dash-btn-primary"><?= Icons::icon('check', 'icon icon-sm') ?>Save category</button>
- <button type="button" class="dash-btn" id="category-cancel-btn"><?= Icons::icon('x', 'icon icon-sm') ?>Cancel</button>
- </div>
- </form>
- </div>
-
- <script>
- (function() {
- var wrapper = document.getElementById('category-form-wrapper');
- var formTitle = document.getElementById('category-form-title');
- var form = document.getElementById('category-form');
-
- function resetForm() {
- form.reset();
- document.getElementById('category_id_input').value = '';
- document.getElementById('category_show_in_menu_input').checked = true;
- formTitle.textContent = 'New category';
- }
-
- function showForm() {
- wrapper.style.display = '';
- wrapper.scrollIntoView({
- behavior: 'smooth',
- block: 'nearest'
- });
- }
-
- document.getElementById('category-new-btn').addEventListener('click', function() {
- resetForm();
- showForm();
- });
-
- document.getElementById('category-cancel-btn').addEventListener('click', function() {
- wrapper.style.display = 'none';
- resetForm();
- });
-
- document.querySelectorAll('.js-edit-category').forEach(function(button) {
- button.addEventListener('click', function() {
- document.getElementById('category_id_input').value = this.dataset.id;
- document.getElementById('category_name_input').value = this.dataset.name;
- document.getElementById('category_description_input').value = this.dataset.description;
- document.getElementById('category_parent_input').value = this.dataset.parent === '0' ? '' : this.dataset.parent;
- document.getElementById('category_show_in_menu_input').checked = this.dataset.showInMenu === '1';
- formTitle.textContent = 'Edit category';
- showForm();
- });
- });
- })();
- </script>