Files
breadcrumb-the-shire/pages/admin/tenants/edit($id).php
2026-02-11 19:28:12 +01:00

83 lines
3.1 KiB
PHP

<?php
use MintyPHP\Buffer;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
use MintyPHP\Router;
use MintyPHP\Service\Tenant\TenantService;
use MintyPHP\Service\User\UserService;
use MintyPHP\Service\Settings\SettingService;
use MintyPHP\Service\Access\PermissionService;
Guard::requireLogin();
Guard::requirePermissionOrForbidden(PermissionService::TENANTS_VIEW);
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
if ($currentUserId > 0) {
PermissionService::getUserPermissions($currentUserId);
}
$uuid = trim((string) ($id ?? ''));
$tenant = $uuid !== '' ? TenantService::findByUuid($uuid) : null;
if (!$tenant) {
Flash::error('Tenant not found', 'admin/tenants', 'tenant_not_found');
Router::redirect('admin/tenants');
}
$tenantId = (int) ($tenant['id'] ?? 0);
$creatorId = (int) ($tenant['created_by'] ?? 0);
if ($creatorId > 0) {
$creator = UserService::findById($creatorId);
if ($creator) {
$creatorName = trim(($creator['first_name'] ?? '') . ' ' . ($creator['last_name'] ?? ''));
$tenant['created_by_label'] = $creatorName !== '' ? $creatorName : ($creator['email'] ?? '');
$tenant['created_by_uuid'] = $creator['uuid'] ?? null;
}
}
$modifierId = (int) ($tenant['modified_by'] ?? 0);
if ($modifierId > 0) {
$modifier = UserService::findById($modifierId);
if ($modifier) {
$modifierName = trim(($modifier['first_name'] ?? '') . ' ' . ($modifier['last_name'] ?? ''));
$tenant['modified_by_label'] = $modifierName !== '' ? $modifierName : ($modifier['email'] ?? '');
$tenant['modified_by_uuid'] = $modifier['uuid'] ?? null;
}
}
$statusChangedById = (int) ($tenant['status_changed_by'] ?? 0);
if ($statusChangedById > 0) {
$statusChanger = UserService::findById($statusChangedById);
if ($statusChanger) {
$statusChangerName = trim(($statusChanger['first_name'] ?? '') . ' ' . ($statusChanger['last_name'] ?? ''));
$tenant['status_changed_by_label'] = $statusChangerName !== '' ? $statusChangerName : ($statusChanger['email'] ?? '');
$tenant['status_changed_by_uuid'] = $statusChanger['uuid'] ?? null;
}
}
$errors = [];
$form = $tenant;
if (isset($_POST['description'])) {
if (!PermissionService::userHas($currentUserId, PermissionService::TENANTS_UPDATE)) {
Router::redirect('error/forbidden');
return;
}
$result = TenantService::updateFromAdmin($tenantId, $_POST, $currentUserId);
$form = $result['form'] ?? $form;
$errors = $result['errors'] ?? [];
if ($result['ok'] ?? false) {
if ($currentUserId > 0) {
\MintyPHP\Service\Auth\AuthService::loadTenantDataIntoSession($currentUserId);
}
$action = (string) ($_POST['action'] ?? 'save');
if ($action === 'save_close') {
Flash::success('Tenant updated', 'admin/tenants', 'tenant_updated');
Router::redirect('admin/tenants');
} else {
Flash::success('Tenant updated', "admin/tenants/edit/{$uuid}", 'tenant_updated');
Router::redirect("admin/tenants/edit/{$uuid}");
}
}
}
Buffer::set('title', t('Edit tenant'));