<?php 
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: demandes_controller.class.php,v 1.1.12.1 2025/12/18 08:54:03 dgoron Exp $

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

global $class_path;
require_once($class_path."/demandes.class.php");

class demandes_controller extends lists_controller {
	
	protected static $model_class_name = 'demandes';
	protected static $list_ui_class_name = 'list_demandes_ui';
	
	public static function proceed($id=0) {
	    global $act, $chk, $state, $num_linked_notice;
		
		switch ($act) {
			case 'new':
			    $model_instance = static::get_model_instance($id);
			    $num_linked_notice = intval($num_linked_notice);
			    if ($num_linked_notice) {
			        $model_instance->set_num_linked_notice($num_linked_notice);
			    }
				$model_instance->show_modif_form();
				break;
			case 'save':
				static::proceed_save($id);
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'search':
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'suppr':
				if (!empty($id)) {
				    $model_instance = static::get_model_instance($id);
					demandes::delete($model_instance);
				} elseif(!empty($chk)) {
					$chk = explode(",", $chk);
					$nb_chk = count($chk);
					for ($i = 0; $i < $nb_chk; $i++) {
						$dmde = new demandes($chk[$i]);
						demandes::delete($dmde);
					}
				}
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'suppr_noti':
				$requete = "SELECT num_notice FROM demandes WHERE id_demande IN (".implode(",", $chk).") AND num_notice!=0";
				$result = pmb_mysql_query($requete);
				if (pmb_mysql_num_rows($result) > 0) {
				    $model_instance = static::get_model_instance($id);
					$model_instance->suppr_notice_form();
				} else {
					if (!empty($id)) {
					    $model_instance = static::get_model_instance($id);
						demandes::delete($model_instance);
					} elseif(!empty($chk)) {
						if (!is_array($chk)) {
							$chk = explode(",", $chk);
						}
						$nb_chk = count($chk);
						for ($i = 0; $i < $nb_chk; $i++) {
							$dmde = new demandes($chk[$i]);
							demandes::delete($dmde);
						}
					}
					$list_ui_instance = static::get_list_ui_instance();
					print $list_ui_instance->get_display_list();
				}
				break;
			case 'change_state':
			    if (!empty($chk) && is_countable($chk)) {
					$nb_chk = count($chk);
					for ($i = 0; $i < $nb_chk; $i++) {
						$dde = new demandes($chk[$i]);
						$dde->change_state($state);
					}
				} else {
				    $model_instance = static::get_model_instance($id);
					$model_instance->change_state($state);
					$model_instance->fetch_data($id);
				}
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'affecter':
			    $model_instance = static::get_model_instance($id);
				$model_instance->attribuer();
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			default:
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
		}
	}
}
