ZH
apps/winzik / src / Form / FlsForm.php · commit 6576d6f
<?phpnamespace App\Form;use App\Model\Text;use JumPHP\Gui\Button;use JumPHP\Gui\Label;use JumPHP\Gui\ListBox;use JumPHP\Gui\TextBox;use JumPHP\Native\FilePicker;use JumPHP\Native\Native;/** * Saving or loading a play list as a .FLS ("[FLS]" then one file per line), the CommonDialog of Form2 in 2001. * The lists are kept in var/playlists; "Parcourir…" uses the system's file chooser for any other place. * Params: save (true: save, false: load). * * @property Label $heading * @property ListBox $lists * @property TextBox $name * @property Button $browse * @property Label $status * @property Button $cancel * @property Button $ok */final class FlsForm extends WinZikDialog{    private bool $save = true;    /** @var list<string> */    private array $paths = [];    protected function loaded(array $params = []): void    {        $this->save = (bool)($params['save'] ?? true);        $this->heading->text = $this->save ? 'Sauver la playlist (.FLS)' : 'Charger une playlist (.FLS)';        $this->ok->text = $this->save ? 'Sauver' : 'Charger';        $this->status->text = '';        $dir = $this->wz->storagePath('playlists');        @mkdir($dir, 0777, true);        $this->paths = array_values(array_filter(array_map(fn (string $f) => $dir . '/' . $f, scandir($dir) ?: []),            fn (string $f) => is_file($f) && Text::extension($f) === 'fls'));        $this->lists->items = array_map(fn (string $f) => basename($f), $this->paths);        $this->name->text = $this->save ? 'winzik.fls' : '';        $this->browse->enabled = Native::supports($this->save ? 'save' : 'open');        if (!$this->browse->enabled) {            $this->hideOnPhone('browse');      // the phone: no file chooser of its own here, the name typed is enough        }        if ($this->paths === [] && !$this->save) {            $this->status->text = 'Aucune liste sauvée pour l\'instant.';        }        $this->setFocus($this->save ? $this->name : $this->lists);    }    public function onListsChange(ListBox $sender, int $index, string $item): void    {        $this->name->text = $item;    }    public function onListsActivate(ListBox $sender, int $index, string $item): void    {        $this->name->text = $item;        $this->onOkClick($this->ok);    }    public function onNameSubmit(TextBox $sender): void    {        $this->onOkClick($this->ok);    }    public function onBrowseClick(Button $sender): void    {        $task = $this->save            ? FilePicker::save(['file_name' => 'winzik.fls', 'filters' => ['Fichier liste (FLS)' => ['fls']]])            : FilePicker::open(['filters' => ['Fichier liste (FLS)' => ['fls']]]);        $task->then(fn (mixed $file) => is_string($file) && $file !== '' && $this->run($file));    }    public function onOkClick(Button $sender): void    {        $name = basename(trim($this->name->text));        if ($name === '') {            $this->status->text = 'Un nom de fichier, svp.';            return;        }        $this->run($this->wz->storagePath('playlists/' . $name));    }    private function run(string $file): bool    {        if ($this->save) {            $ok = $this->wz->saveFls($file);            $this->status->text = $ok ? 'Liste sauvée : ' . basename($file) : 'Impossible d\'écrire ' . $file;        } else {            $ok = $this->wz->loadFls($file);            $this->status->text = $ok ? '' : 'Ce n\'est pas un fichier liste (FLS) : ' . basename($file);        }        if ($ok) {            $this->wz->notice($this->save ? 'Liste sauvée' : 'Liste chargée', 3);            $this->close();        }        return $ok;    }    public function onCancelClick(Button $sender): void    {        $this->close();    }}