PL
games/xencenter-xenadmin-xcp-jumphp / src / Feature / Organize / OrganizeProperties.php · commit fb1b7c9
<?phpnamespace App\Feature\Organize;use App\Model\Connection;use JumPHP\Gui\ComboBox;use JumPHP\Gui\Form;use JumPHP\Gui\TextBox;/** * The Tags and Folder fields of the Properties dialog (forms/PropertiesForm.form.yaml, panel "organize"): the tags as a comma list, * the folder chosen in a list or typed as a new one. PropertiesForm calls params() / fill() / values() / apply(). */final class OrganizeProperties{    public const NONE = '(top level)';    /** @return array<string, mixed> what the dialog needs about the object */    public static function params(Connection $c, string $ref): array    {        $session = \App\Model\Session::$current;        $folders = $session !== null ? Organizer::folders($session) : [];        return ['tags' => Organizer::tagsOf($c->cache, $ref), 'folder' => Organizer::folderOf($c->cache, $ref), 'folders' => $folders];    }    /** @param array<string, mixed> $params */    public static function fill(Form $form, array $params): void    {        /** @var TextBox $tags */        $tags = $form->widget('tags');        /** @var ComboBox $folder */        $folder = $form->widget('folder');        /** @var TextBox $newFolder */        $newFolder = $form->widget('newFolder');        $tags->text = implode(', ', array_map('strval', (array)($params['tags'] ?? [])));        $folder->items = [self::NONE, ...array_map('strval', (array)($params['folders'] ?? []))];        $folder->selected = max(0, (int)array_search((string)($params['folder'] ?? '') ?: self::NONE, $folder->items, true));        $newFolder->text = '';    }    /** @return array{newTags: list<string>, newFolder: string} what the fields say */    public static function values(Form $form): array    {        /** @var TextBox $tags */        $tags = $form->widget('tags');        /** @var ComboBox $folder */        $folder = $form->widget('folder');        /** @var TextBox $newFolder */        $newFolder = $form->widget('newFolder');        $chosen = trim($newFolder->text) !== '' ? $newFolder->text : (string)($folder->value() ?? '');        return ['newTags' => Organizer::parseTags($tags->text), 'newFolder' => $chosen === self::NONE ? '' : Organizer::normalize($chosen)];    }    /** Sends what changed. @param array<string, mixed> $p what PropertiesForm closed with */    public static function apply(Connection $c, string $ref, array $p): void    {        if (!array_key_exists('newTags', $p) || $c->cache->get($ref) === null) {            return;        }        $tags = array_values(array_map('strval', (array)$p['newTags']));        $now = Organizer::tagsOf($c->cache, $ref);        if (array_diff($tags, $now) !== [] || array_diff($now, $tags) !== []) {            Organizer::setTags($c, $ref, $tags)->then(fn () => null, fn () => null);        }        if ((string)$p['newFolder'] !== Organizer::folderOf($c->cache, $ref)) {            Organizer::setFolder($c, $ref, (string)$p['newFolder'])->then(fn () => null, fn () => null);        }    }}