<?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.16.2 2026/08/14 08:40:20 dbellamy 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, $class_path, $base_path, $include_path;
	    global $demandes_display_mode;
		
		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 'modif':
				$model_instance = static::get_model_instance($id);
				$model_instance->show_modif_form();
				break;
			case 'save':
				//L'enregistrement n'est pas confie a proceed_save(), son identifiant n'etant connu qu'apres l'insertion.
				$model_instance = static::get_model_instance($id);
				$model_instance->set_properties_from_form();
				$model_instance->save();
				//Demande creee depuis la liste en accordeon : elle recoit aussitot une action, comme en Opac
				//Sans  action, une demande n'a pas de fil de discussion
				if(empty($id) && $demandes_display_mode == 2) {
					static::create_first_action($model_instance);
				}
				$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 = static::get_checked_ids($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':
				$chk = static::get_checked_ids($chk);
				$result = false;
				if (!empty($chk)) {
					$requete = "SELECT num_notice FROM demandes WHERE id_demande IN (".implode(",", $chk).") AND num_notice!=0";
					$result = pmb_mysql_query($requete);
				}
				if ($result && 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)) {
						$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':
				$state = intval($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':
				$chk = static::get_checked_ids($chk);
			    $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;
			case 'create_notice':
				//Creation de la notice de la demande depuis la liste :
				$model_instance = static::get_model_instance($id);
				$model_instance->create_notice();
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'delete_notice':
				//Suppression de la notice de la demande.
				$model_instance = static::get_model_instance($id);
				$model_instance->delete_notice();
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'notice':
				//Formulaire de catalogage de la notice de la demande
				$model_instance = static::get_model_instance($id);
				$model_instance->show_notice_form();
				break;
			case 'upd_notice':
				//Enregistrement de la notice catalogue
				include($base_path."/demandes/update_notice.inc.php");
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'attach':
				//Ecran de choix des documents numeriques a rattacher a la notice
				$model_instance = static::get_model_instance($id);
				$model_instance->show_docnum_to_attach();
				break;
			case 'save_attach':
				//Rattachement des documents choisis
				$model_instance = static::get_model_instance($id);
				$model_instance->attach_docnum();
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
			case 'rapport':
				//Ecran de composition du rapport
				require_once($class_path."/rapport.class.php");
				$rapport = new rapport_demandes($id);
				$rapport->showRapport();
				break;
			default:
				$list_ui_instance = static::get_list_ui_instance();
				print $list_ui_instance->get_display_list();
				break;
		}
	}

	/**
	 * Cree l'action initiale d'une demande qui vient d'etre enregistree.
	 *
	 * Une demande sans action n'a pas de fil de discussion : les notes et les
	 * pieces jointes se rattachent a une action, jamais a la demande elle-meme.
	 * L'Opac cree donc cette action des la creation dans son affichage en liste
	 * la liste en accordeon de la gestion fait de meme.
	 *
	 * Le type et le statut sont ceux que le workflow declare par defaut
	 * 
	 * @param demandes $demande Demande enregistree, son identifiant renseigne
	 * @return void
	 */
	protected static function create_first_action($demande) {
		global $PMBuserid;

		if(empty($demande->id_demande)) {
			return;
		}
		$query = "select 1 from demandes_actions where num_demande=".intval($demande->id_demande);
		$result = pmb_mysql_query($query);
		if(pmb_mysql_num_rows($result)) {
			return;
		}

		$today = date('Y-m-d');
		$action = new demandes_actions();
		$action->num_demande = $demande->id_demande;
		//Type par defaut du workflow des actions, celui que retient deja la demande (Question / Reponse).
		$action->type_action = (!empty($demande->first_action) ? $demande->first_action : 1);
		//Statut par defaut du meme workflow, a defaut le premier declare.
		$action->statut_action = 1;
		if(!empty($action->list_statut)) {
			$first_statut = 0;
			foreach ($action->list_statut as $statut) {
				if(empty($first_statut)) {
					$first_statut = $statut['id'];
				}
				if(!empty($statut['default'])) {
					$first_statut = $statut['id'];
					break;
				}
			}
			if(!empty($first_statut)) {
				$action->statut_action = $first_statut;
			}
		}
		$action->date_action = $today;
		$action->deadline_action = $today;
		$action->sujet_action = $demande->titre_demande;
		$action->detail_action = $demande->sujet_demande;
		//Cote gestion, l'auteur est l'utilisateur PMB : actions_type_user a 0,
		$action->actions_num_user = $PMBuserid;
		$action->actions_type_user = 0;
		$action->save();
	}

	protected static function get_checked_ids($chk) {
		if (!is_array($chk)) {
			$chk = (strlen(strval($chk)) ? explode(',', $chk) : array());
		}
		$ids = array();
		foreach ($chk as $value) {
			$id = intval($value);
			if ($id) {
				$ids[] = $id;
			}
		}
		return $ids;
	}
}
