<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: rapport_ajax.inc.php,v 1.8 2021/08/06 11:50:06 dgoron Exp $

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

global $quoifaire;
global $idnote, $id, $id_item;

switch($quoifaire){
	case 'add_note';
		add_note($idnote);
	break;
	case 'up_order':
		update_order();	
	break;
	case 'show_addcom':
		show_addcom($id);	
	break;
	case 'update_com':
		update_contenu($id);
	break;
	case 'del_item':
		del_item($id_item);
	break;
}

/*
 * Ajout d'une note au rapport
 */
function add_note($idnote=0){
	global $idtype, $idobject, $comment, $ordre_cible;
	
	$iddemande = $idobject;
	$commentaire = trim($comment);
	
	if($ordre_cible){
		$ordre = $ordre_cible;
	} else {
		$req = "select max(ordre)+1 from rapport_demandes where num_demande='".$iddemande."'";
		$res = pmb_mysql_query($req);
		$ordre = pmb_mysql_result($res,0,0);
	}
	
	$req = "insert into rapport_demandes set 
		contenu='".$commentaire."',
		num_demande='".$iddemande."',
		num_note='".$idnote."',
		ordre = '".$ordre."',
		type='".$idtype."'
		";
	pmb_mysql_query($req);
	
	if($ordre_cible) update_order(pmb_mysql_insert_id());
	
	$req = "select rd.id_item, rd.contenu, rd.ordre, rd.type, rd.num_note, sujet_action from rapport_demandes rd left join demandes_notes on num_note=id_note left join demandes_actions on num_action=id_action where rd.num_demande='".$iddemande."' order by ordre";
	$res = pmb_mysql_query($req);
	$display = "";
	while(($item = pmb_mysql_fetch_object($res))){
		$titre = substr($item->contenu,0,15)."...";	
		$style="";
		if(!$item->num_note){
			//Ajout manuel
			switch ($item->type) {
				case '1':
					//Titre
					$style = "style='background-color:#DECDEC' titre='yes'";
					$content= $item->contenu;
				break;
				case '0':
					//Commmentaire
					$content= "* ".$item->contenu;
				break;
			}
		} else $content= $item->contenu;	
		$ordre = $item->ordre;
		
		if($item->sujet_action)
			$contenu = "<u>".$item->sujet_action."</u> : ".$content;
		else $contenu = $content;
			
		$drag = "<span id=\"rap_handle_$item->id_item\" style='padding-left:7px'  ><img src=\"".get_url_icon('notice_drag.png')."\" /></span>";
		$del = "<span id=\"rap_del_$item->id_item\" style='padding-left:7px' onclick='delete_item($item->id_item);' ><img src='".get_url_icon('cross.png')."' style='cursor:pointer;width:10px;vertical-align:middle;' /></span>";
		$modif = "<span id=\"rap_modif_$item->id_item\" style='padding-left:7px;' onclick='modif_item($item->id_item);' ><img src='".get_url_icon('b_edit.png')."' style='cursor:pointer;width:10px;vertical-align:middle;'/></span>";
		$display .= "
					<div class='row' $style id='rap_drag_$item->id_item' draggable=\"yes\" dragtype=\"rapport\" dragtext=\"$titre\" dragicon='".get_url_icon('icone_drag_notice.png')."'
						handler=\"rap_handle_$item->id_item\" recepttype=\"rapport\" recept=\"yes\" highlight=\"rap_highlight\" downlight=\"rap_downlight\" iditem='$item->id_item' order='$ordre'>".$contenu.$drag.$modif.$del."</div>			
				";
	}

	ajax_http_send_response($display);
}


/*
 * Mise à jour de l'ordre des notes
 */
function update_order($idinsert=0){
	global $idsource,$ordre_source,$ordre_cible;

	if(($ordre_source > $ordre_cible) && !$idinsert) {
		$req = "update rapport_demandes set ordre='".$ordre_cible."' where id_item='".$idsource."'";
		pmb_mysql_query($req);
		$req = "update rapport_demandes set ordre=ordre+1 where (ordre <= '".$ordre_source."' and ordre >='".$ordre_cible."') and id_item!='".$idsource."' ";
		pmb_mysql_query($req);
	} else if(($ordre_source < $ordre_cible) && !$idinsert){
		$req = "update rapport_demandes set ordre='".($ordre_cible-1)."' where id_item='".$idsource."'";
		pmb_mysql_query($req);
		$req = "update rapport_demandes set ordre=ordre-1 where (ordre >= '".$ordre_source."' and ordre <='".($ordre_cible-1)."') and id_item!='".$idsource."' ";
		pmb_mysql_query($req);
	} else if($idinsert && $ordre_cible && $idinsert){
		//Insertion d'un élément nouveau dans la liste donc on a idinsert et pas idsource
		$req = "update rapport_demandes set ordre='".$ordre_cible."' where id_item='".$idinsert."'";
		pmb_mysql_query($req);
		$req = "update rapport_demandes set ordre=ordre+1 where ordre >='".$ordre_cible."' and id_item!='".$idinsert."' ";
		pmb_mysql_query($req);
	}
	
}

/*
 * Affiche le formulaire de saisie d'un titre ou d'une note
 */
function show_addcom($id=0){
	global $msg;
	
	$contenu="";
	if($id){
		$req = "select contenu from rapport_demandes where id_item='".$id."'";
		$res = pmb_mysql_query($req);
		$rap = pmb_mysql_fetch_object($res);
		
		$contenu = $rap->contenu;
	}
	
	$display = "
		<div class='row'>
			<textarea name='comment' id='comment' rows='3' cols='50'/>".$contenu."</textarea>
		</div>
		<div class='row'>
			<input type='button'  id='cancel_com' name='cancel_com' class='bouton' value='".$msg[76]."' />
			<input type='button' id='save_com' name='save_com' class='bouton' value='".$msg[77]."' />
		</div>
	";
	
	ajax_http_send_response($display);
}

/*
 * Suppression d'un élément du rapport
 */
function del_item($id=0){
	if($id){
		$req = "delete from rapport_demandes where id_item='".$id."'";
		pmb_mysql_query($req); 
	}
}

/*
 * Mis à jour de la note du rapport
 */
function update_contenu($id){
	global $comment, $idobject;
	
	if($id){
		$req = "update rapport_demandes set contenu='".$comment."' where id_item='".$id."'";
		pmb_mysql_query($req);
	
		//$req = "select id_item, contenu, ordre, type, num_note from rapport_demandes where num_demande='".$idobject."' order by ordre";
		$req = "select rd.id_item, rd.contenu, rd.ordre, rd.type, rd.num_note, sujet_action from rapport_demandes rd left join demandes_notes on num_note=id_note left join demandes_actions on num_action=id_action where rd.num_demande='".$idobject."' order by ordre";
		$res = pmb_mysql_query($req);
		$display = "";
		while(($item = pmb_mysql_fetch_object($res))){
			$titre = substr($item->contenu,0,15)."...";	
			$style="";
			if(!$item->num_note){
				//Ajout manuel
				switch ($item->type) {
					case '1':
						//Titre
						$style = "style='background-color:#DECDEC' titre='yes'";
						$content= $item->contenu;
					break;
					case '0':
						//Commmentaire
						$content= "* ".$item->contenu;
					break;
				}
			} else $content= $item->contenu;	
			$ordre = $item->ordre;
			
			if($item->sujet_action)
				$contenu = "<u>".$item->sujet_action."</u> : ".$content;
			else $contenu = $content;
			
			$drag = "<span id=\"rap_handle_$item->id_item\" style='padding-left:7px'  ><img src=\"".get_url_icon('notice_drag.png')."\" /></span>";
			$del = "<span id=\"rap_del_$item->id_item\" style='padding-left:7px' onclick='delete_item($item->id_item);' ><img src='".get_url_icon('cross.png')."' style='cursor:pointer;width:10px;vertical-align:middle;' /></span>";
			$modif = "<span id=\"rap_modif_$item->id_item\" style='padding-left:7px;' onclick='modif_item($item->id_item);' ><img src='".get_url_icon('b_edit.png')."' style='cursor:pointer;width:10px;vertical-align:middle;'/></span>";
			$display .= "
						<div class='row' $style id='rap_drag_$item->id_item' draggable=\"yes\" dragtype=\"rapport\" dragtext=\"$titre\" dragicon='".get_url_icon('icone_drag_notice.png')."'
							handler=\"rap_handle_$item->id_item\" recepttype=\"rapport\" recept=\"yes\" highlight=\"rap_highlight\" downlight=\"rap_downlight\" iditem='$item->id_item' order='$ordre'>".$contenu.$drag.$modif.$del."</div>			
					";
		}
		ajax_http_send_response($display);
	}				
}
		
?>