<?php
// +-------------------------------------------------+
// © 2002-2012 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: cms_module_common_selector_section_by_origine_cp.class.php,v 1.2.2.2 2026/03/10 14:34:22 gneveu Exp $

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

class cms_module_common_selector_section_by_origine_cp extends cms_module_common_selector_section_by_value_cp
{
    public function __construct($id=0)
    {
        parent::__construct($id);
        $this->cms_module_common_selector_type_editorial_type="section";
    }

	public function get_value()
	{
		if (isset($this->value)) {
			return $this->value;
		}

        $this->value = 0;

        $classSubSelector = $this->parameters['sub_selector'] ?? "";
        if (empty($classSubSelector) || !class_exists($classSubSelector)) {
			return $this->value;
        }

        $subSelector = new $classSubSelector($this->get_sub_selector_id($this->parameters['sub_selector']));
		$genericType = 0;

		$query = "SELECT id_editorial_type FROM cms_editorial_types
			WHERE editorial_type_element = '{$this->cms_module_common_selector_type_editorial_type}_generic'";
		$result = pmb_mysql_query($query);
		if (pmb_mysql_num_rows($result)) {
			$genericType = pmb_mysql_result($result, 0, 0);
		    $genericType = intval($genericType);
		}
		$type = intval($this->parameters["type_editorial"]);
		$fieldId = intval($this->parameters['type_editorial_field']);
		$query = "SELECT datatype FROM cms_editorial_custom
			WHERE idchamp={$fieldId} AND num_type in ({$genericType}, {$type})";
		$result = pmb_mysql_query($query);
		if (pmb_mysql_num_rows($result)) {
			$datatype = pmb_mysql_result($result, 0, 0);

			$query = "SELECT  cms_editorial_custom_{$datatype} FROM cms_editorial_custom_values
				WHERE cms_editorial_custom_champ = {$fieldId}
                AND  cms_editorial_custom_origine = '{$subSelector->get_value()}'";
			$result = pmb_mysql_query($query);

			if (pmb_mysql_num_rows($result)) {
				$idRubrique = pmb_mysql_result($result, 0, 0);
				$this->value = intval($idRubrique);
			}
		}

		return $this->value;
	}
}
