<?php

// +-------------------------------------------------+
// © 2002-2012 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: cms_module_animationslist_datasource_animations_by_section_categories.class.php,v 1.1.4.2 2026/02/17 14:24:23 qvarin Exp $

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

class cms_module_animationslist_datasource_animations_by_section_categories extends cms_module_common_datasource_animations_list
{
    public function __construct($id = 0)
    {
        parent::__construct($id);
        $this->paging = true;
    }

    /*
     * On défini les sélecteurs utilisable pour cette source de donnée
     */
    public function get_available_selectors()
    {
        return [
            "cms_module_common_selector_section",
            "cms_module_common_selector_env_var",
        ];
    }

    public function get_form()
    {
        $form = parent::get_form();
        if (!isset($this->parameters['operator_between_authorities'])) {
            $this->parameters['operator_between_authorities'] = 'or';
        }
        $form .= '
        <div class="row">
            <div class="colonne3"><label for="'.$this->get_form_value_name('operator_between_authorities').'">'.$this->format_text($this->msg['cms_module_common_datasource_operator_between_authorities']).'</label></div>
            <div class="colonne_suite">
                '.$this->format_text($this->msg['cms_module_common_datasource_operator_between_authorities_or']).' <input type="radio" '.($this->parameters['operator_between_authorities'] == 'or' ? 'checked="checked"' : '').' name="'.$this->get_form_value_name('operator_between_authorities').'" value="or"/>
                '.$this->format_text($this->msg['cms_module_common_datasource_operator_between_authorities_and']).' <input type="radio" '.($this->parameters['operator_between_authorities'] == 'and' ? 'checked="checked"' : '').' name="'.$this->get_form_value_name('operator_between_authorities').'" value="and"/>
            </div>
        </div>';

        return $form;
    }

    public function save_form()
    {
        $this->parameters['operator_between_authorities'] = $this->get_value_from_form('operator_between_authorities');
        return parent::save_form();
    }

    /*
     * Récupération des données de la source...
     */
    public function get_datas()
    {
        $selector = $this->get_selected_selector();
        if ($selector) {
            if (!isset($this->parameters['operator_between_authorities'])) {
                $this->parameters['operator_between_authorities'] = 'or';
            }

            $animations = [];
            $result = pmb_mysql_query($this->buildQuery());
            if ($result && pmb_mysql_num_rows($result) > 0) {
                while ($row = pmb_mysql_fetch_assoc($result)) {
                    $animations[] = $row['id_animation'];
                }
            }

            $animations = $this->filter_datas('animations', $animations);
            $animations = $this->sort_animations($animations);

            // Pagination
            $return = [];
            if ($this->paging && isset($this->parameters['paging_activate']) && $this->parameters['paging_activate'] == "on") {
                $return["paging"] = $this->inject_paginator($animations);
                $return['animations'] = $this->cut_paging_list($animations, $return["paging"]);
            } elseif ($this->parameters["nb_max_elements"] > 0) {
                $return["animations"] = array_slice($animations, 0, $this->parameters["nb_max_elements"]);
            } else {
                $return["animations"] = $animations;
            }

            return $return;
        }
        return false;
    }

    protected function getDefaultClause()
	{
        $selector = $this->get_selected_selector();
        if (!$selector) {
            return [];
        }

        $num_section = intval($selector->get_value());
        switch ($this->parameters["operator_between_authorities"]) {
            case 'and':
                $query = 'SELECT distinct num_noeud FROM cms_sections_descriptors WHERE num_section = "' . $num_section . '"';
                $result = pmb_mysql_query($query);

                $clause = [];
                if ($result && (pmb_mysql_num_rows($result) > 0)) {
                    while ($row = pmb_mysql_fetch_assoc($result)) {
                        $clause[] = 'anim_animation_categories.num_noeud = "' . $row['num_noeud'] . '"';
                    }
                }
                return ['(' . implode(' AND ', $clause) . ')'];

            case 'or':
            default:
                return [
                    'cms_sections_descriptors.num_section = "' . $num_section . '"',
                ];
        }
	}

	protected function getDefaultJoin()
	{
        switch ($this->parameters["operator_between_authorities"]) {
            case 'and':
                return [
                    'JOIN anim_animation_categories ON anim_animation_categories.num_animation=anim_animations.id_animation',
                ];

            case 'or':
            default:
                return [
                    'JOIN anim_animation_categories ON anim_animation_categories.num_animation=anim_animations.id_animation',
                    'JOIN cms_sections_descriptors ON anim_animation_categories.num_noeud=cms_sections_descriptors.num_noeud',
                ];
        }

	}
}
