<?php
// +-------------------------------------------------+
// 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: cms_module_common_selector_animation_by_status.class.php,v 1.1.4.2 2026/06/24 07:45:46 tsamson Exp $

use Pmb\Animations\Orm\AnimationStatusOrm;

if (stristr($_SERVER['REQUEST_URI'], ".class.php")) {
    die("no access");
}

class cms_module_common_selector_animation_by_status extends cms_module_common_selector
{
    /**
     * @param int $id
     */
    public function __construct($id = 0)
    {
        parent::__construct($id);
    }

    /**
     * @return string
     */
    public function get_form()
    {
        $form = parent::get_form();
        $form .= "
            <div class='row'>
                <select name='" . $this->get_form_value_name("status_ids") . "[]' multiple='yes'>
                    " . $this->get_options_status() . "
                </select>
            </div>";

        return $form;
    }

    /**
     * @return bool
     */
    public function save_form()
    {
        $this->parameters = $this->get_value_from_form("status_ids");

        return parent::save_form();
    }

    /**
     * Retourne les identifiants de statut selectionnes
     *
     * @return array
     */
    public function get_value()
    {
        if (!$this->value) {
            $this->value = $this->parameters;
        }

        return $this->value;
    }

    /**
     * Genere les options HTML du select a partir de la table anim_status
     *
     * @return string
     */
    private function get_options_status()
    {
        $options = "";
        $statuses = AnimationStatusOrm::findAll();
        $selected_ids = is_array($this->parameters) ? $this->parameters : [];

        foreach ($statuses as $status) {
            $selected = in_array($status->id_status, $selected_ids) ? "selected='selected'" : "";
            $options .= "<option value='" . (int) $status->id_status . "' " . $selected . ">"
                . htmlspecialchars($status->label, ENT_QUOTES) . "</option>";
        }

        return $options;
    }
}
