<?php
// +-------------------------------------------------+
// © 2002-2012 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: cms_module_animationslist_datasource_animations_by_cp_section.class.php,v 1.2 2026/02/18 09:14:49 gneveu Exp $
use Pmb\Animations\Orm\AnimationOrm;

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

class cms_module_animationslist_datasource_animations_by_cp_section extends cms_module_common_datasource_animations_list
{

    public function __construct($id = 0)
    {
        parent::__construct($id);
        $this->sortable = true;
        $this->limitable = true;
        $this->paging = true;
    }

    /*
     * On défini les sélecteurs utilisable pour cette source de donnée
     */
    public function get_available_selectors()
    {
        return array(
            "cms_module_common_selector_section_by_value_cp"
        );
    }

    /*
     * Récupération des données de la source...
     */
    public function get_datas()
    {
        $selector = $this->get_selected_selector();
        if (! empty($selector) && $selector->get_value()) {
            $query = $this->buildQuery();
            if (empty($query)) {
                return false;
            }
            $query = sprintf($query, $selector->get_value());

		    $animations = [];
            $result = pmb_mysql_query($query);
            if (pmb_mysql_num_rows($result)) {
                while ($row = pmb_mysql_fetch_assoc($result)) {
                    $animations[] = intval($row['id_animation']);
                }
                pmb_mysql_free_result($result);
                $animations = $this->filter_datas('animations', $animations);
            }

            if (!is_countable($animations) || !count($animations)) {
                return false;
            }

            $data = $this->sort_animations($animations);
            $data['title'] = "";

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

            return $data;
        }
        return false;
    }

    protected function getDefaultJoin()
    {
        $joins = [];
        $joins[] = "JOIN anim_animation_locations ON anim_animation_locations.num_animation = anim_animations.id_animation";
        $joins = array_merge($joins, parent::getDefaultJoin());
        return $joins;
    }

    protected function getDefaultClause()
    {
        $clause = [];
        $clause = parent::getDefaultClause();
        $clause[] = "anim_animation_locations.num_location = %s";
        return $clause;
    }
}