- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks - Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists - Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services - Add Microsoft OIDC SSO, API token management, and user lifecycle features - Add swagger-ui vendor integration and OpenAPI spec - Add production Docker setup and bin/ scripts - Update composer dependencies, config, templates, and frontend assets throughout Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
128 lines
4.2 KiB
PHP
128 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Repository\CustomField;
|
|
|
|
use MintyPHP\DB;
|
|
|
|
class TenantCustomFieldOptionRepository
|
|
{
|
|
private static function unwrapList($rows): array
|
|
{
|
|
if (!is_array($rows)) {
|
|
return [];
|
|
}
|
|
$list = [];
|
|
foreach ($rows as $row) {
|
|
$item = $row['tenant_custom_field_options'] ?? null;
|
|
if (is_array($item)) {
|
|
$list[] = $item;
|
|
}
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
public static function listByDefinitionIds(array $definitionIds, bool $onlyActive = true): array
|
|
{
|
|
$definitionIds = array_values(array_unique(array_map('intval', $definitionIds)));
|
|
$definitionIds = array_values(array_filter($definitionIds, static fn ($id) => $id > 0));
|
|
if (!$definitionIds) {
|
|
return [];
|
|
}
|
|
$query = 'select id, definition_id, option_key, label, active, sort_order, created, modified ' .
|
|
'from tenant_custom_field_options where definition_id in (???)';
|
|
$params = [$definitionIds];
|
|
if ($onlyActive) {
|
|
$query .= ' and active = 1';
|
|
}
|
|
$query .= ' order by definition_id asc, sort_order asc, label asc, id asc';
|
|
return self::unwrapList(call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $params)));
|
|
}
|
|
|
|
public static function replaceForDefinition(int $definitionId, array $options): bool
|
|
{
|
|
if ($definitionId <= 0) {
|
|
return false;
|
|
}
|
|
$existing = self::listByDefinitionIds([$definitionId], false);
|
|
$existingByKey = [];
|
|
foreach ($existing as $row) {
|
|
$key = (string) ($row['option_key'] ?? '');
|
|
if ($key !== '') {
|
|
$existingByKey[$key] = $row;
|
|
}
|
|
}
|
|
|
|
$keepIds = [];
|
|
foreach ($options as $option) {
|
|
if (!is_array($option)) {
|
|
continue;
|
|
}
|
|
$optionKey = trim((string) ($option['option_key'] ?? ''));
|
|
$label = trim((string) ($option['label'] ?? ''));
|
|
if ($optionKey === '' || $label === '') {
|
|
continue;
|
|
}
|
|
$active = !empty($option['active']) ? 1 : 0;
|
|
$sortOrder = (int) ($option['sort_order'] ?? 100);
|
|
|
|
if (isset($existingByKey[$optionKey]['id'])) {
|
|
$optionId = (int) $existingByKey[$optionKey]['id'];
|
|
$keepIds[] = $optionId;
|
|
$updated = DB::update(
|
|
'update tenant_custom_field_options set label = ?, active = ?, sort_order = ? where id = ?',
|
|
$label,
|
|
(string) $active,
|
|
(string) $sortOrder,
|
|
(string) $optionId
|
|
);
|
|
if ($updated === false) {
|
|
return false;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
$insertId = DB::insert(
|
|
'insert into tenant_custom_field_options (definition_id, option_key, label, active, sort_order, created) values (?,?,?,?,?,NOW())',
|
|
(string) $definitionId,
|
|
$optionKey,
|
|
$label,
|
|
(string) $active,
|
|
(string) $sortOrder
|
|
);
|
|
if ($insertId === false) {
|
|
return false;
|
|
}
|
|
if ($insertId) {
|
|
$keepIds[] = (int) $insertId;
|
|
}
|
|
}
|
|
|
|
if (!$keepIds) {
|
|
$deleted = DB::delete('delete from tenant_custom_field_options where definition_id = ?', (string) $definitionId);
|
|
if ($deleted === false) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
$deleted = DB::delete(
|
|
'delete from tenant_custom_field_options where definition_id = ? and id not in (???)',
|
|
(string) $definitionId,
|
|
$keepIds
|
|
);
|
|
if ($deleted === false) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static function deleteByDefinitionId(int $definitionId): bool
|
|
{
|
|
if ($definitionId <= 0) {
|
|
return false;
|
|
}
|
|
$result = DB::delete('delete from tenant_custom_field_options where definition_id = ?', (string) $definitionId);
|
|
return $result !== false;
|
|
}
|
|
}
|