<?
####### PROJECTS ##########
/* a function to have all hours spended on a project */
function Total_heures($pid) {
	$sql2="
	SELECT SUM(hours) AS hours FROM pm_tasks_time 
	WHERE pm_tasks_time.project=" .$pid;
	#echo $sql2;
	#echo mysql_result($sql,$i,'hours');
	$sql2=mysql_query($sql2);
	$hours=mysql_result($sql2,0,'hours');
	echo $hours;
}

/* show tasks for current project */
function project_tasks_show($plib,$pid,$order,$statut,$operator,$anchor) {
echo '	<a name="'.$anchor .'"></a>
	<div class="pmTasks index" style="background-color: #F1CB84; padding-bottom: 13px;">
	<a name="taches"></a><h2>';
echo $plib;
echo '<a href="/intranet/pmcake/pm_tasks/add"><img src="/intranet/pmcake/img/toolbar/add.png" alt="Ajouter" title="Ajouter" /></a>';
//e($html->link($html->image('toolbar/add.png', array('alt' => 'Ajouter', 'title' => 'Ajouter')), array('controller' => 'pm_tasks', 'action'=>'add'), array('escape' => false)));
echo '
</h2>
	<table>
	<tr>
			<th>tâche</th>
			<th>statut</th>
			<th>priorité</th>
			<th>début</th>
			<th>délai</th>
			<th>milestone</th>
			<th class="actions" colspan="2">Actions</th>
	</tr>
';
	$sql="SELECT * FROM pm_tasks WHERE project=".$pid;

	if($_GET['statut']) {$statut = $_GET['statut'];}
		$statut = " AND status" .$operator .$statut;

	$sql.=$statut;
	$sql.= " ORDER BY " .$order;
	#echo $sql;
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }

$i=0;
while($i<mysql_num_rows($sql)){
			$class = null;
		if (intval($i/2) == ($i/2)) {
			$class = ' class="altrow"';
		}
	echo '			<tr' .$class .'>
		<td>';
	//echo $this->Html->link(mysql_result($sql,$i,'name'), array('controller' => 'pm_tasks', 'action' => 'view', mysql_result($sql,$i,'id')));
	#echo '<a href="/intranet/pmcake/pm_tasks/view/' .mysql_result($sql,$i,'id') .'">' .mysql_result($sql,$i,'name') .'</a>';
	
		echo '<a href="' .CHEMIN .'pm_tasks/edit/'.mysql_result($sql,$i,'id').'" class="tooltip">'.mysql_result($sql,$i,'name');
if(strlen(	mysql_result($sql,$i,'description'))>0) {
	echo '<em><span></span>'.nl2br(mysql_result($sql,$i,'description')).'</em>';
}
	echo '</a>';
	
	
	echo '</td>
				<td>';
		statut(mysql_result($sql,$i,'status'));
	echo '		</td>
		<td>';
	prioriteView( mysql_result($sql,$i,'priority'));
	echo '&nbsp;</td>
				<td>';
	echo mysql_result($sql,$i,'start_date');
	echo '&nbsp;</td>
		<td>';
	echo mysql_result($sql,$i,'due_date');
	echo '&nbsp;</td>
		<td>';
	echo mysql_result($sql,$i,'milestone');
	echo '&nbsp;</td>
		<td class="oktask">
	     <!-- ############ task ok ######### -->
			&#x2714;<input type="checkbox" title="OK?" onChange="task_ok(';
	echo mysql_result($sql,$i,'id');
	echo ',this.value)">
			<!-- http://fr.wikipedia.org/wiki/Coche_%28typographie%29 -->
		</td>
		<td class="actions">
		<!-- ################ PUSH DELAYS  ################  -->
		<form action="repousser" method="GET" name="';
	echo mysql_result($sql,$i,'id');
	echo '">
		<input type="hidden" name="identifiant" value="';
	echo mysql_result($sql,$i,'id');
	echo '">
		<input type="image" src="/pmcake/img/icons/bullet_arrow.gif" alt="Déplacer à demain" title="Déplacer à demain" name="demain" value="demain">
        <select name="ajout" class="micro" size="1" onChange="task_goto_URL(';
	echo mysql_result($sql,$i,'id');
	echo ',this.value)">';
	delais();
	echo '</select>';
	$idaction=mysql_result($sql,$i,'id');
	les_actions($idaction);
	echo '      </form>
			</td>
		</tr>';
		$i++;
		}
	echo '	</table>
		</div>
	';
}

/*function to get a scrolling list of projets and highlight the current project if exists*/
function projets_sel($pid) {
$sql="SELECT id, name, type FROM pm_projects ORDER BY name"; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}
$i=0;
echo "<option value=''>### projet ###" ."</option>";
while($i<mysql_num_rows($sql)){
	if(mysql_result($sql,$i,'type')=="p") {
		$css="projet_sel_perso";
	} else {
		$css="projet_sel";
		
	}
	echo "<option class=\"" .$css ."\" value=\"" .mysql_result($sql,$i,'id') ."\"";
	if($pid==mysql_result($sql,$i,'id')){
		echo " selected";
	}
	echo ">" .mysql_result($sql,$i,'name') ."</option>";
	$i++;
	}
}


/*function to print the name of a given project*/
function projet_nom_print($pid) {
$sql="SELECT name FROM pm_projects WHERE id=".$pid; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}

	echo mysql_result($sql,0,'name');
}

/*function to return the name of a given project*/
function projet_nom_return($pid) {
$sql="SELECT name FROM pm_projects WHERE id=".$pid; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}

	return utf8_encode(mysql_result($sql,0,'name'));
}

/*function to get a scrolling list of projets and highlight the current project if exists*/
function statut_sel($statut) {
	$statuts=array("Complet (client)", "Complet", "Non commencé", "Ouvert", "Suspendu",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	"Incubateur",
	 "",
	 "",
	 "",
	 "",
	"Références"
	);	
	for($i=0;$i<23;$i++) {
		$zestatut=$statuts[$i];
			if(strlen($zestatut)>1) {
				echo "<option value=\"" .$i ."\"";
				if($statut==$i){
					echo " selected";
				}
				echo ">" .$zestatut ."</option>";
			}
	}
}

/*function to get a scrolling list of projets and highlight the current project if exists*/
function statut_radio($id,$statut) {
	$statuts=array("Complet (client)", "Complet", "Non commencé", "Ouvert", "Suspendu",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	 "",
	"Incubateur",
	 "",
	 "",
	 "",
	 "",
	"Références"
	);	
	for($i=0;$i<count($statuts);$i++) {
		$zestatut=$statuts[$i];
			if(strlen($zestatut)>1&&$zestatut!="Complet (client)") {
				echo "<span>
<input type=\"radio\" name=\"radiovalue\" value=\"" .$i ."\"";

				echo " onChange=\"task_change_status(".$id.",".$i.")\"";
				if($statut==$i){
					echo " checked";
				}
				echo " / >&nbsp;" .$zestatut;
								echo "</span>";
			}
	}
}
/* function to extract specified status */
function statut($id) {
	$statut=""; $lestyle="";
	if($id==0) {
		$statut="Complet (client)";
	}elseif($id==1) {
		$statut="Complet";
	}elseif($id==2) {
		$statut="Non commencé";
	}elseif($id==3) {
		$statut="Ouvert";
	}elseif($id==4) {
		$statut="Suspendu";
	}elseif($id==5) {
	}elseif($id==6) {
	}elseif($id==7) {
	}elseif($id==17) { //café papo 17
		$statut="Incubateur"; //dreams, boîte à idées
		$lestyle='" style="width: 20px"';
	}elseif($id==22) { //22 vla les flics
		$statut="Références"; //references	
		$lestyle='" style="width: 30px"';
	}
	
	echo '<img src="/pmcake/img/gfx_status/' .$id .'.gif" alt="' .$statut .'" title="' .$statut .'" ' .$lestyle .' />';

	}
	/* function to return specified status */
function statutreturn($id) {
	$statut=""; $lestyle="";
	if($id==0) {
		$statut="Complet (client)";
	}elseif($id==1) {
		$statut="Complet";
	}elseif($id==2) {
		$statut="Non commencé";
	}elseif($id==3) {
		$statut="Ouvert";
	}elseif($id==4) {
		$statut="Suspendu";
	}elseif($id==5) {
	}elseif($id==6) {
	}elseif($id==7) {
	}elseif($id==17) { //café papo 17
		$statut="Incubateur"; //dreams, boîte à idées
		$lestyle='" style="width: 20px"';
	}elseif($id==22) { //22 vla les flics
		$statut="Références"; //references	
		$lestyle='" style="width: 20px"';
	}
	
	return '<img src="/pmcake/img/gfx_status/' .$id .'.gif" alt="' .$statut .'" title="' .$statut .'" ' .$lestyle .' />';

	}
	
####### TASKS #########
/*function to get a scrolling list of tasks and select the current task if exists*/
function tasks_sel($tid) {
$sql="SELECT id, name FROM pm_tasks ORDER BY name"; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}
$i=0;
while($i<mysql_num_rows($sql)){
	echo "<option value=\"" .mysql_result($sql,$i,'id') ."\"";
	if($tid==mysql_result($sql,$i,'id')){
		echo " selected";
	}
	echo ">" .mysql_result($sql,$i,'name') ."</option>";
	$i++;
	}
}

function completion($completion) {
	$pourcent=array(0,10,20,30,40,50,60,70,80,90,100);
	for($i=0;$i<11;$i++) {	
		echo "<option value=\"" .$pourcent[$i] ."\"";
		if($i==$completion) {
			echo " selected";
			}
		echo ">" .$pourcent[$i] ."%</option>";
	}
}
	
function priorite($priorite) {
	$prioritelib=array("Vide","Très faible","Faible","Moyenne","Elevée","Très élevée");
	$prioritecolor=array(  "white","white"           , "white"     ,"white"       ,"#FFC0CB","#FF6C7F");
	for($i=0;$i<6;$i++) {	
		echo "<option value=\"" .$i ."\"";
		if($i==$priorite) {
			echo " selected";
			}
			echo " style=\"background-color: "  .$prioritecolor[$i] ."\"";
		echo ">" .$prioritelib[$i] ."</option>";
	}
}

	
function prioriteView($i) {
	$prioritelib=array("Vide","Très faible","Faible","Moyenne","Elevée","Très élevée");
	$prioritecolor=array(  "white","white"           , "white"     ,"white"       ,"#FFC0CB","#FF6C7F");

		echo "<span style=\"background-color: "  .$prioritecolor[$i] ."\">" .$prioritelib[$i] ."</span>";
}
	
/*function to print the name of a given task*/
function task_nom_print($pid) {
$sql="SELECT name FROM pm_tasks WHERE id=".$pid; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}
	echo mysql_result($sql,0,'name');
}
/*time spent on a task*/
function total_hours_task($task_id) {
		$sql2="
			SELECT SUM(hours) AS hours FROM pm_tasks_time 
			WHERE task=" .$task_id;
			#echo $sql2;
			#echo mysql_result($sql,$i,'hours');
			$sql2=mysql_query($sql2);
			$hours=mysql_result($sql2,0,'hours');
			echo $hours;
}
#############################################################################

/* MAIN function to extract the list of current tasks for a given category / period */
function print_tasks($catlib,$quand) {
		$datenow=date("Y-m-d");
	if($catlib=="prof") {
		$projtyp=" AND proj.type !='p'";
	} elseif($catlib=="perso") {
		$projtyp=" AND proj.type ='p'";	
	}
	if($quand=="auj") {
		$quandSQL=" AND (tas.due_date <= '".date("Y-m-d")."'"; 
	}elseif($quand=="demain") {
		$quandSQL=" AND (tas.due_date <= DATE_ADD('$datenow', INTERVAL 1 DAY) AND (tas.due_date > '$datenow') ";
	}
	global $sql, $idc;
	$sql="
	SELECT * FROM pm_tasks AS tas, pm_projects as proj 
	WHERE tas.status > 1 
	AND tas.status < 17". $quandSQL
	." AND tas.due_date <> '--') 
	AND tas.project=proj.id " .$projtyp;
		if($quand=="futur") {
		$sql="
		SELECT * FROM pm_tasks AS tas, pm_projects as proj 
		WHERE tas.status > 1 
		AND tas.status < 17 
		AND (tas.due_date > DATE_ADD('".$datenow."', INTERVAL 2 DAY)) AND tas.due_date <> '--' 
		AND tas.project=proj.id 
		ORDER BY tas.due_date ASC 
		LIMIT 0, 30";
	//echo nl2br($sql); exit;
	}
	/** */
	
	
	#echo nl2br($sql)."<br>"; //tests
	$sql=mysql_query($sql);
	if(!$sql) {
		echo "SQL error: " .mysql_error(); exit;
	}
	$i=0;
	echo '<div id="' .$catlib .'"><h2><a name="' .$catlib .'"></a>'. ucfirst($catlib) .'</h2><form id="'.$catlib.'">';
	
	//some special css classes to hide/show
	if($quand=="demain") {
echo '<small><a onclick="montrecache1();"><img src="/intranet/pmcake/img/icons/open_tab.gif"></a></small>
<div id="tomorrowContainer" style="display: none;">';
	}elseif($quand=="futur") {
#echo '<div id="clickme2" style="display: block; position: relative; top: 18px; right: 20px;"><small><a onclick="montrecache2();"><img src="/intranet/pmcake/img/icons/open_tab.gif"></a></small></div><div id="futureContainer" style="display: none;">';
echo '<small><a onclick="montrecache2();"><img src="/intranet/pmcake/img/icons/open_tab.gif"></a></small>
<div id="futureContainer" style="display: none;">';
	}
	echo '<h3>Total tâches en cours: #'.mysql_num_rows($sql).'</h3>
			<table cellpadding="0" cellspacing="0">
			<tr>
			<!--
	<th style="padding: 0px 3px;">
	<input id="_TechSelectAll_" type="hidden" value="0" name="data[_Tech][selectAll]">
	<input id="_TechSelectAll" type="checkbox" value="1" style="margin-bottom: 8px;" name="data[_Tech][selectAll]" class="checkAll" onclick="checkAll(document.getElementById(\'prof\'), \'cb-element\', this.checked);">
	</th>
	-->
				<th>project</th>
				<th>priority</th>
				<th>status</th>
			<!--	<th>owner</th>-->
				<th>name</th>
				<th>due_date</th>
				<th>milestone</th>
				<th class="actions" colspan="2">Actions</th>
			</tr>
	';

	$i=0;
	while($i<mysql_num_rows($sql)){
		$class = null;
		if (intval($i/2) == ($i/2)) {
			$class = ' class="altrow"';
		}
	echo "<tr" .$class;
	echo " id=\"tr" .mysql_result($sql,$i,'id')."\"";
	echo  ">";
	//experimental not working (select all and apply a common action
	#echo '<input id="User1Id" class="model_id" type="checkbox" value="1" name="data[User][1][id]">';
	#echo '<td><input id="PmTask' .mysql_result($sql,$i,'id').'Id" class="cb-element" type="checkbox" value="' .mysql_result($sql,$i,'id').'" name="data[PmTask][' .mysql_result($sql,$i,'id').'][id]"></td>';
	echo "<td>";
	if(mysql_result($sql,$i,'proj.type')=="p") {
		perso_list();
	}
	echo '<a href="/intranet/pmcake/pm_projects/view/'.mysql_result($sql,$i,'proj.id').'">'.mysql_result($sql,$i,'proj.name').'</a>';
	echo "</td>";
	echo "<td>";
	prioriteView(mysql_result($sql,$i,'priority'));
	echo "</td>";
	echo "<td>";
	statut(mysql_result($sql,$i,'status'));
	echo  "</td>";
	#echo "<td>" .mysql_result($sql,$i,'owner') ."</td>";
#	echo '<td><a href="' .CHEMIN .'pm_tasks/edit/'.mysql_result($sql,$i,'id').'" onmouseOver="task_detail('.mysql_result($sql,$i,'id').')">'.mysql_result($sql,$i,'name') .'</a>';
	echo '<td><a href="' .CHEMIN .'pm_tasks/edit/'.mysql_result($sql,$i,'id').'" class="tooltip">'.mysql_result($sql,$i,'name');
if(strlen(	mysql_result($sql,$i,'description'))>0) {
	echo '<em><span></span>'.nl2br(mysql_result($sql,$i,'description')).'</em>';
}
	echo '</a>';
	echo "</td>";
#	echo '<div id="detail'.mysql_result($sql,$i,'id').'" style="display: none; position: relative; top: 3px; left: 10px">'.nl2br(mysql_result($sql,$i,'description')) .'</div>';
	echo "<td>";
	dateSQL2fr(mysql_result($sql,$i,'due_date'));
	echo "<br /><em style=\"font-size: smaller\">(";
	dateSQL2frSmall(mysql_result($sql,$i,'start_date'));
	echo ")</em> ";
	echo "</td>";
	echo "<td>" .mysql_result($sql,$i,'milestone') ."</td>";
	echo "<td>";
	################ BEGIN PUSH DELAYS  ################
	$idc=mysql_result($sql,$i,'id');
	push_delays($idc);
	echo '<td><a href="/intranet/pmcake/pm_tasks/view/' .$idc .'" alt="Voir" title="Voir"><img src="/intranet/pmcake/img/toolbar/loupe.png" alt="Voir" /></a><a href="/intranet/pmcake/pm_tasks/edit/' .$idc .'" alt="Modifier" title="Modifier"><img src="/intranet/pmcake/img/toolbar/editor.png" alt="Modifier" /></a><a href="/intranet/pmcake/pm_tasks/delete/' .$idc .'" alt="Effacer" title="Effacer" onclick="return confirm(\'Voulez-vous vraiment effacer cet élément ?\');"><img src="/intranet/pmcake/img/toolbar/drop.png" alt="effacer" /></a></td></tr>';
	 ################ END PUSH DELAYS  ################  
	/* ACTIONS END */
		echo "</tr>\n\n";
		$i++;
	}
	echo '
	</table>
<!--
<div class="choose_action">
Action à effectuer sur les éléments sélectionnés&nbsp;
<?php
push_all_delays($catlib);
?>
</div>
!-->
</form>';
	if($quand=="demain"||$quand=="futur") {
		echo "</div>";
	}

echo '</div>
<!-- CLOSE '.$catlib.' TASKS -->
	';
}

/* extract the tasks for a given projects, to select the parent_task_id for a given task £*/
function parent_tasks($pid,$task_id) {
	//no project (new task)
	if($pid=="") {
	echo "<option value=\"0\">--- no parent ---</option>";	
	} else {
$sql="SELECT id, name FROM pm_tasks WHERE project=".$pid." AND status>2 ORDER BY name"; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}
$i=0;
echo "<option value=\"0\">--- no parent ---</option>";
while($i<mysql_num_rows($sql)){
	echo "<option value=\"" .mysql_result($sql,$i,'id') ."\"";
	if($task_id==mysql_result($sql,$i,'id')){
		echo " selected";
	}
	echo ">" .mysql_result($sql,$i,'name') ."</option>";
	$i++;
	}
}
}

/* extract the parent task for a given task */
function parent_task($task_id) {
		//no project (new task)
	$sql="SELECT parent_id FROM pm_tasks WHERE id=".$task_id; 
	$sql=mysql_query($sql);
	if(!$sql) {
		echo "SQL error: " .mysql_error(); exit;
	}
	$task_id=mysql_result($sql,0,'parent_id');
	if($task_id>0) {
	$sql="SELECT id, name FROM pm_tasks WHERE id=".$task_id ." ORDER BY due_date"; 
	echo "<tr><td>";
	//echo "<br>".$sql ."<br>"; //tests
	#do and check sql
	$sql=mysql_query($sql);
	if(!$sql) {
		echo "SQL error: " .mysql_error(); exit;
	}
	$i=0;
	if(mysql_num_rows($sql)>0) {
		echo "<h2>Parent</h2>";
		//echo "<ol>";
	while($i<mysql_num_rows($sql)){
		echo "<li><a href=\"" .mysql_result($sql,$i,'id') ."\">" .mysql_result($sql,$i,'name') ."</a></li>";
		$i++;
		}
		//echo "</ol>";

	}
	echo "</td></tr>";
	/**/
	}
}

/* extract the children tasks for a given task */
function children_tasks($task_id) {
	$sql="SELECT id, name FROM pm_tasks WHERE parent_id=".$task_id ." ORDER BY due_date"; 
	//echo "<br>".$sql ."<br>"; //tests
	#do and check sql
	$sql=mysql_query($sql);
	if(!$sql) {
		echo "SQL error: " .mysql_error(); exit;
	}
	$i=0;
	if(mysql_num_rows($sql)>0) {
		echo "<tr><td>";
	echo "<h2>Children Tasks</h2>";
		echo "<ol>";
	while($i<mysql_num_rows($sql)){
		echo "<li><a href=\"" .mysql_result($sql,$i,'id') ."\">" .mysql_result($sql,$i,'name') ."</a></li>";
		$i++;
		}
		echo "</ol>";


	echo "</td></tr>";	
	}
}

/* total hours today */
function task_total_today($s_edate2) {
	
	$sql="
	SELECT * FROM pm_tasks_time, pm_projects 
	WHERE pm_tasks_time.date 
	LIKE '$s_edate2' 
	AND pm_tasks_time.project=pm_projects.id 
	AND pm_projects.type NOT LIKE 'p' 
	GROUP BY pm_projects.name

	";

	#echo nl2br($sql)."<br>";
	$sql=mysql_query($sql);
	if(!$sql) {
		echo "SQL error: " .mysql_error(); exit;
	}

	#echo $sql;
	echo "Total ";
	dateSQL2fr($s_edate2);
	echo ": ";
	$i=0;
	$total=0;
	while($i<mysql_num_rows($sql)){
	$pmid=mysql_result($sql,$i,'pm_tasks_time.project');
	$sql2="
	SELECT SUM(hours) AS hours FROM pm_tasks_time 
	WHERE pm_tasks_time.date 
	LIKE '$s_edate2' 
	AND pm_tasks_time.project=$pmid
	";
	$sql2=mysql_query($sql2);
	$hours=mysql_result($sql2,0,'hours');
	$total=$total+$hours;

	$i++;
	}
	echo $total;
	echo "h";
	
}

/* resume task for a given date */
function task_resume($s_edate2) {
	$sql="
	SELECT * FROM pm_tasks_time, pm_projects 
	WHERE pm_tasks_time.date 
	LIKE '$s_edate2' 
	AND pm_tasks_time.project=pm_projects.id 
	AND pm_projects.type NOT LIKE 'p' 
	GROUP BY pm_projects.name

	";

	#echo nl2br($sql)."<br>";
	$sql=mysql_query($sql);
	if(!$sql) {
		echo "SQL error: " .mysql_error(); exit;
	}

	#echo $sql;
	echo "<h2>Totaux pour le: ";
	dateSQL2fr($s_edate2);
	echo "</h2><table>";
	$i=0;
	$total=0;
	while($i<mysql_num_rows($sql)){
	echo "<tr><td style=\"padding: 3px\">";
	echo mysql_result($sql,$i,'name');
	$pmid=mysql_result($sql,$i,'pm_tasks_time.project');
	echo "</td><td>";
	$sql2="
	SELECT SUM(hours) AS hours FROM pm_tasks_time 
	WHERE pm_tasks_time.date 
	LIKE '$s_edate2' 
	AND pm_tasks_time.project=$pmid
	";
	#echo $sql2;
	#echo mysql_result($sql,$i,'hours');
	$sql2=mysql_query($sql2);
	$hours=mysql_result($sql2,0,'hours');
	echo $hours;
	#echo mysql_result($sql,$i,'hours');

	#$total=$total+mysql_result($sql,$i,'hours');
	$total=$total+$hours;

	echo "</td></tr>";
	$i++;
	}
	echo "<tr><td style=\"padding: 3px\"><b>TOTAL</b></td><td style=\"padding: 3px\">" .$total ."</td></tr>";
	echo "</table><br>";
}

/* detail task for a given date */
function task_detail($s_edate2) {
	######### DETAIL #########
$sql="
SELECT * FROM pm_tasks_time,pm_projects,pm_tasks
WHERE pm_tasks_time.date 
LIKE '$s_edate2' 
AND pm_tasks_time.project=pm_projects.id
AND pm_tasks.id=pm_tasks_time.task 
AND pm_projects.type NOT LIKE 'p' 
ORDER BY pm_tasks_time.created

";
#echo $sql;
echo "<hr><h2>D&eacute;tail</h2>
<table>
<tr>
<th>project</th><th>task</th><th>created</th><th>modified</th><th>comments</th><th>hours</th>
</tr>";
$sql=mysql_query($sql);
$i=0;
$total=0;
while($i<mysql_num_rows($sql)){
	
	if(intval($i/2)==($i/2)) {
		$classe="";
	}else{
		$classe="pair";
	}
	
echo "<tr><td class=\"" .$classe ."\">";
echo mysql_result($sql,$i,'pm_projects.name');
echo "</td><td class=\"" .$classe ."\">";
echo mysql_result($sql,$i,'pm_tasks.name');
echo " <a href=\"" .CHEMIN ."pm_tasks_times/edit/" .mysql_result($sql,$i,'pm_tasks_time.id') ."\">";
echo "<img src=\"" .CHEMIN ."img/icons/chronometre.png\" alt=\"modifier heures\" style=\"width: 25px\">";
echo "</a>";
echo " <a href=\"" .CHEMIN ."pm_tasks/edit/" .mysql_result($sql,$i,'pm_tasks.id') ."\">";
echo "<img src=\"" .CHEMIN ."img/toolbar/editor.png\" alt=\"voir tâche\" style=\"width: 25px\">";
echo " <a href=\"" .CHEMIN ."pm_tasks/view/" .mysql_result($sql,$i,'pm_tasks.id') ."\">";
echo "<img src=\"" .CHEMIN ."img/toolbar/loupe.png\" alt=\"voir tâche\" style=\"width: 25px\">";
echo "</a></td><td class=\"" .$classe ."\">";
echo mysql_result($sql,$i,'pm_tasks_time.created');
echo "</td><td class=\"" .$classe ."\">";
echo mysql_result($sql,$i,'pm_tasks_time.modified');
echo "</td><td class=\"" .$classe ."\">";
echo substr(mysql_result($sql,$i,'pm_tasks_time.comments'), 0,100);
echo "</td><td class=\"" .$classe ."\">";
echo mysql_result($sql,$i,'pm_tasks_time.hours');
echo "</td></tr>";
$i++;
}
echo "</table>";

}

####### CLIENTS #########
/*function to get a scrolling list of projets and highlight the current project if exists*/
function clients_sel($pid) {
$sql="SELECT id, name FROM pm_organizations ORDER BY name"; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}
$i=0;
while($i<mysql_num_rows($sql)){
	echo "<option value=\"" .mysql_result($sql,$i,'id') ."\"";
	if($pid==mysql_result($sql,$i,'id')){
		echo " selected";
	}
	echo ">" .mysql_result($sql,$i,'name') ."</option>";
	$i++;
	}
}
/* function to extract specified usagers/clients */
function clients($id) {
	$sql="SELECT * FROM pm_organizations WHERE id=".$id;
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }
	echo "<a href=\"" .CHEMIN."/pm_organizations/view/".mysql_result($sql,0,'id') ."\">" .mysql_result($sql,0,'name') ."</a>";
	}

/* list of projets for a given client */
function client_projets($id) {
	$sql="SELECT id, name FROM pm_projects  WHERE organization=".$id;
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }
	
	$i=0;
	echo "<ul>";
	while($i<mysql_num_rows($sql)){
		echo "<li><a href=\"" .CHEMIN."/pm_projects/view/".mysql_result($sql,$i,'id') ."\">" .mysql_result($sql,0,'name') ."</a></li>";
		$i++;
		}
	echo "</ul>";	
}

/* list of all tasks for a given client */
function client_tasks($id) {
	$datenow = date("Y-m-d");
	$sql="
	SELECT * FROM pm_tasks AS tas, pm_projects as proj 
	WHERE tas.project=proj.id
	AND proj.organization=".$id ." 
	ORDER BY tas.due_date ASC
	";
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }
	
	$i=0;
	echo "<ul class=\"puce_cache\">";
	while($i<mysql_num_rows($sql)){
		echo "<li>";
		statut(mysql_result($sql,$i,'tas.status'));
		echo "&nbsp;<a href=\"" .CHEMIN."/pm_tasks/view/".mysql_result($sql,$i,'tas.id') ."\">" .mysql_result($sql,$i,'tas.name') ."</a>";
		echo "&nbsp;<span style=\"font-style: italic; font-size: smaller;\">(" .mysql_result($sql,$i,'tas.due_date') .")</span>";
		echo "</li>";
		$i++;
		}
	echo "</ul>";	
}

######### USERS ###########
/* function to extract specified member (owners) */
function membres($id) {
	$sql="SELECT * FROM pm_members WHERE id=".$id;
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }
	echo "<a href=\"../pm_members/view/".mysql_result($sql,0,'id') ."\">" .mysql_result($sql,0,'login') ."</a>";
	}
	
/*function to get a scrolling list of projets and highlight the current project if exists*/
function membres_sel($pid) {
$sql="SELECT id, name FROM pm_members ORDER BY name"; 
#do and check sql
$sql=mysql_query($sql);
if(!$sql) {
	echo "SQL error: " .mysql_error(); exit;
}
$i=0;
while($i<mysql_num_rows($sql)){
	echo "<option value=\"" .mysql_result($sql,$i,'id') ."\"";
	if($pid==mysql_result($sql,$i,'id')){
		echo " selected";
	}
	echo ">" .mysql_result($sql,$i,'name') ."</option>";
	$i++;
	}
}
######### FILES ###########
/* function to print specified filetypes */
function typefichier($id) {
	echo '<img src="/pmcake/img/filetypes/' .$id .'.gif" alt="' .$id .'" />';
}

######### MySQL ###########

/* function to clean strings before insert or modify SQL */
function normaliser($string) {
//	return trim(addslashes($string));
	return addslashes($string);
}

######### HTML tools #######
/* keep record of every transactions for logs history */
function ecritlog() {	
	$db=connect_db();
	$db_name=db_name();
	mysql_select_db($db_name,$db);
	$sql ="INSERT INTO `pm_urls_logs` (
	`id` ,
	`time` ,
	`url`
	)
	VALUES (
	NULL , '" .date("Y-m-d H:i:s") ."', '" .$_SERVER["REQUEST_URI"] ."'
	);
	";
	#echo $sql; exit;
	#do and check sql
	//don't run if image or so
	if(!preg_match("/\.gif$/",$_SERVER["REQUEST_URI"])) {
		$sql=mysql_query($sql);

		if(!$sql) {
			echo "SQL error: " .mysql_error(); exit;
		}
	}
}


/*convert SQL date time to french date*/
function dateSQL2fr($date) {
	$date=explode(" ", $date);
	$hour=$date[1];
	$date=$date[0];
	$date=explode("-", $date);
//	$date=mktime(0,0,0,$date[2],$date[1],$date[0]);
	$date=mktime(0,0,0,$date[1],$date[2],$date[0]);
	echo strftime("%a, %d-%m-%Y", $date);
}	

/*convert SQL long date time to french date*/
function dateSQLlong2fr($date) {
	$timestamp=strtotime($date);
	//echo $date;
	$date_mod= date('D, d-m-Y H:i',$timestamp);
	$today1=dateen2fr($date_mod);
	echo $today1;
}	
/*same but no day name (shorter)*/
function dateSQL2frSmall($date) {
	$date=explode(" ", $date);
	$hour=$date[1];
	$date=$date[0];
	$date=explode("-", $date);
	
	$date=mktime(0,0,0,$date[1],$date[2],$date[0]);
	echo strftime("%d-%m-%Y", $date);
}	

/*convert english short date to french short date*/
function dateen2fr($today1) {
#mois en francais - attention à le faire dans ce sens car Mar(s) < Mardi !
$today1 = preg_replace("/Jan/", "Janvier", $today1);
$today1 = preg_replace("/Feb/", "Février", $today1);
$today1 = preg_replace("/Mar/", "Mars", $today1);
$today1 = preg_replace("/Apr/", "Avril", $today1);
$today1 = preg_replace("/May/", "Mai", $today1);
$today1 = preg_replace("/Jun/", "Juin", $today1);
$today1 = preg_replace("/Jul/", "Juillet", $today1);
$today1 = preg_replace("/Aug/", "Août", $today1);
$today1 = preg_replace("/Sept/", "Septembre", $today1);
$today1 = preg_replace("/Oct/", "Octobre", $today1);
$today1 = preg_replace("/Nov/", "Novembre", $today1);
$today1 = preg_replace("/Dec/", "Décembre", $today1);

$today1 = preg_replace("/Mon/", "Lundi", $today1);
$today1 = preg_replace("/Tue/", "Mardi", $today1);
$today1 = preg_replace("/Wed/", "Mercredi", $today1);
$today1 = preg_replace("/Thu/", "Jeudi", $today1);
$today1 = preg_replace("/Fri/", "Vendredi", $today1);
$today1 = preg_replace("/Sat/", "Samedi", $today1);
$today1 = preg_replace("/Sun/", "Dimanche", $today1);

//$today1=preg_replace("/-/"," ", $today1);

return $today1;
}

/* function to extract urls from variables */
function urlise($chaine) { 
	#echo "test urlize: <br>" .$chaine ."<hr>";
	#$chaine=ereg_replace("(http://)(([[:punct:]]|[[:alnum:]]=?)*)","<a href=\"\\0\">\\0</a>",$chaine);
	$chaine = preg_replace("/(https:\/\/)(([[:punct:]]|[[:alnum:]]=?)*)/","<a target=\"_blank\" href=\"\\0\">\\0</a>",$chaine);
	$chaine=preg_replace("/(http:\/\/)(([[:punct:]]|[[:alnum:]]=?)*)/","<a target=\"_blank\" href=\"\\0\">\\0</a>",$chaine);
	//now replace emails
	if(!preg_match("/[a-zA-Z0-9]*\.[a-zA-Z0-9]*@/",$chaine)){
	#$chaine = ereg_replace('[-a-zA-Z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_/-])*','<a href="mailto:\\0">\\0</a>',$chaine);
	#$chaine = preg_replace('/[-a-zA-Z0-9!#$%&\'*+/=?^_`{|}~]+@([.]?[a-zA-Z0-9_\/-])*/','<a href="mailto:\\0">\\0</a>',$chaine);
	}else {
	$chaine = preg_replace('/[-a-zA-Z0-9]*\.?[-a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~]+@([.]?[a-zA-Z0-9_\/-])*/','<a href="mailto:\\0">\\0</a>',$chaine);	
	}

	echo nl2br($chaine);
}
function melto($chaine) { 
	#echo "test urlize: <br>" .$chaine ."<hr>";
	#$chaine=ereg_replace("(http://)(([[:punct:]]|[[:alnum:]]=?)*)","<a href=\"\\0\">\\0</a>",$chaine);
	$chaine = "<a href=\"mailto:" .$chaine ."\">".$chaine ."</a>";
	echo $chaine;
}
######### BAD ###########
/* todo bad : used mysql connection, to remove - settings are in config/core.php 
 * */
function connect_db()	{
//radeff todo change on another server
$login=LOGINMYSQL;                              // user name for you database
$pass=PASSWORDMYSQL;                                   // pass word to the database if you dont have a password 
	$db=mysql_connect("localhost",$login,$pass) or  die("Unable  to  select  database");
		
	return $db;
	
	}

function db_name()	{
$database_name=DBMYSQL;                     //name of the database

	$db_name=$database_name;
	
	return $db_name;
	
	}
	
/* a function to distinct private projets / tasks */
/* other available images: fleur.jpg redstar.jpg  redStar.png redstar.png * */	

function perso_list() {
		echo '<img src="/pmcake/img/icons/redstar.png" alt="Perso" title="Perso" style="width: 10px"/>&nbsp;';
}

/* options for delaying tasks */
function delais() {	
		echo '<option value="">--- repousser ---</option>';
		$sql="SELECT * FROM pm_delays ORDER BY days";
		$sql=mysql_query($sql);
		if(!$sql) {
			echo "SQL error: " .mysql_error(); exit;
		}
		$i=0;
		while($i<mysql_num_rows($sql)){
			echo "<option value=\"" .mysql_result($sql,$i,'days') ."\">repousser " .mysql_result($sql,$i,'lib')."</option>";
			$i++;
			}
}

/* options for tasks hours */
function ajoutheure() {	
		$sql="SELECT * FROM pm_times ORDER BY hours";
		$sql=mysql_query($sql);
		if(!$sql) {
			echo "SQL error: " .mysql_error(); exit;
		}
		$i=0;
		while($i<mysql_num_rows($sql)){
			echo "<option value=\"" .mysql_result($sql,$i,'hours') ."\">" .mysql_result($sql,$i,'lib')."</option>";
			$i++;
			}
}

/* print actions */
function les_actions($idaction) {
	$les_actions="
	e($html->link($html->image('toolbar/add.png', array('alt' => 'Ajouter', 'title' => 'Ajouter')), array('action'=>'add'), array('escape' => false)));
	e($html->link($html->image('toolbar/editor.png', array('alt' => 'Modifier')), array('action'=>'edit', $idaction), array('alt' => 'Modifier', 'title' => 'Modifier', 'escape' => false)));
	$delete_text = isset($delete_text) ? $delete_text : ___d('alaxos', 'do you really want to delete this item ?', true);
	e($html->link($html->image('toolbar/drop.png', array('alt' => __d('alaxos', 'delete', true))), array('action' => 'delete', $idaction), array('alt' => ___d('alaxos', 'delete', true), 'title' => ___d('alaxos', 'delete', true), 'escape' => false), $delete_text));
	e($html->link($html->image('toolbar/list.png', array('alt' => __d('alaxos', 'list', true))), array('action' => 'index'), array('alt' => ___d('alaxos', 'list', true), 'title' => ___d('alaxos', 'list', true), 'escape' => false)));
";
return $les_actions;
}

/* report the delay for a given task */
function push_delays($idc) {
echo "<a href=\"mel?id='" .$idc ."\">@</a> 
	<form action=\"repousser\" method=\"get\" name=\"" .$idc ."\">
	<input type=\"hidden\" name=\"identifiant\" value=\"" .$idc ."\" />

	<a href=\"javascript:demain(" .$idc .")\"><img src=\"/pmcake/img/icons/bullet_arrow.gif\" alt=\"Déplacer à demain\" title=\"Déplacer à demain\"></a>
	
        <select name=\"ajout\" class=\"micro\" size=\"1\" onchange=\"repousser(" .$idc .",this.value)\" id=\"sel" .$idc ."\">";
delais();
echo "        </select>
      <!-- ############ task ok ######### -->
 	&nbsp;<input style=\"background: orange\" type=\"checkbox\" title=\"OK?\" onchange=\"task_ok(" .$idc .",this.value)\" />
      </form>
      </td>
      <td>";
}

/* report the delay for many tasks */
function push_all_delays($idc) {
/*echo "<form action=\"repousser\" method=\"get\" name=\"" .$idc ."\">
	<input type=\"hidden\" name=\"identifiant\" value=\"" .$idc ."\" />

	<a href=\"javascript:demain(" .$idc .")\"><img src=\"/pmcake/img/icons/bullet_arrow.gif\" alt=\"Déplacer à demain\" title=\"Déplacer à demain\"></a>
	
        <select name=\"ajout\" class=\"micro\" size=\"1\" onchange=\"repousser(" .$idc .",this.value)\" id=\"sel" .$idc ."\">";
delais();
echo "        </select>
     
      </form>
      </td>
      <td>";*/
}



function champiprevisualiser($id = null) {
		echo '<img width="100" src="' .CHEMINPHOTOS .'/'.$id.'"></em><span></span>';
	}
	
	
########################## UPLOADER #########################################	
			/**
 * Upload a file to a specified destination
 * 
 * @param string $path Path of original file
 * @param string $source Temp file
 * @param string $dest Destination path
 * @access public 
 */
function uploadFile($path, $source, $dest)
{
        #$source = str_replace('\\', '/', $source);;
       /* if (!is_dir($path)) {
            createDir($path);
        }*/
        $destination = $path . '/' . $dest;
        move_uploaded_file($source, $destination)||die("unable to mv tmp file and create");
    
}

/**
 * Folder creation
 * 
 * @param string $path Path to the new directory
 * @access public 
 */
function createDir($path)
{
#            @mkdir('../webroot/files/' . $path, 0777)||die("unable to create dir");
            @mkdir('../webroot/files/' . $path, 0777);
}


//Link checker source http://www.phptoys.com/tutorial/create-link-checker.html
function getPage($link){
   
   if ($fp = fopen($link, 'r')) {
      $content = '';
        
      while ($line = fread($fp, 1024)) {
         $content .= $line;
      }
   }

   //echo $content;  
   if(strlen($content)<1) {
	echo "<pre>Broken link: " .$link ."</pre>";
	echo $content;
	}
}
function gethead($url) {
  // Gets url ready to use
   $info  = @parse_url( $url );

  // Opens socket
  if(preg_match("/^https/",$url)) {
  $socket=443;
}else{
  $socket=80;
}
  $fp    = @fsockopen( $info["host"],  $socket,  $errno,  $errstr,  10 );

  // Makes sure the socket is open or returns false
  if ( !$fp ) {
     return false;
  } else {

     // Checks the path is not empty
     if( empty( $info["path"] ) ) {

        // If it is empty it fills it
        $info["path"] = "/";
     }
     $query = ""; 

     // Checks if there is a query string in the url
     if( isset( $info["query"] ) ) {

          // If there is a query string it adds a ? to the front of it
          $query = "?".$info["query"]."";
     }

     // Sets the headers to send
      $out  = "HEAD ".$info["path"]."".$query." HTTP/1.0\r\n";
     $out .= "Host: ".$info["host"]."\r\n";
     $out .= "Connection: close \r\n" ;
     $out .= "User-Agent: link_checker/1.1\r\n\r\n";

     // writes the headers out
     fwrite( $fp,  $out );
     $html = '';

     // Reads what gets sent back
     while ( !feof( $fp ) ) {
          $html .= fread( $fp,  200 );
     }

     // Closes socket
     fclose( $fp );
  }
  //return $html;
  //echo $html;
   $headers = explode( "\r\n",  $html );
  unset( $header );
  for( $i=0;isset( $headers[$i] );$i++ ) {

    // Checks if the header is the status header
    if( preg_match( "/HTTP\/[0-9A-Za-z +]/i" , $headers[$i] ) ) {

      // If it is save the status
      $status = preg_replace( "/http\/[0-9]\.[0-9]/i", "", $headers[$i] );
    }
  }
  //return $status; 
  $status=trim($status);
  if($status!="200 OK") {
	 echo "<span style=\"background-color: #FFC8C8; padding: 3px;\">".$status."</span>";
}
} 

function contacts_groupes() {
	$sql="SELECT DISTINCT(Category) AS lib FROM contacts WHERE Category NOT LIKE '' GROUP BY Category";
	$sql=mysql_query($sql);
		if(!$sql) {
			echo "SQL error: " .mysql_error(); exit;
		}
		$i=0;
		?><option value="">*** catégorie ***</option><?
		while($i<mysql_num_rows($sql)){
			echo "<option>" .mysql_result($sql,$i,'lib')."</option>";
			$i++;
			}
}

/* look calendar */
/* ################# task ################# */ 

function extrait_donnees($annee_selectionne,$mois_aff,$jour_aff) {
	echo "<strong>".$jour_aff."</strong>";

	if($mois_aff<10) {
		$mois_aff1="0".$mois_aff;
	}
	$sql="SELECT * FROM pm_tasks WHERE due_date LIKE '".$annee_selectionne."-".$mois_aff1."-".$jour_aff."'";
	#echo "<br>" .$sql; //tests
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }

	$i=0;

	echo "<table>";
	while($i<mysql_num_rows($sql)){
	if(intval($i/2)==($i/2)) {
	$couleur="#f5f5f5";
	} else {
		$couleur="#F3F3C6";
	}
		echo "<tr><td style=\"background-color: " .$couleur ."\"><a href=\"" .CHEMIN ."pm_tasks/view/" .mysql_result($sql,$i,'id');
		echo "\" title=\"";
		echo strstr(mysql_result($sql,$i,'description'),0,20);
		echo "\"><span class=\"petit\">";
		echo mysql_result($sql,$i,'name');
		echo "</span></a>";
		echo "<br />";
		statut(mysql_result($sql,$i,'status'));
		echo "<br />";
		?>

		<form action="repousser" method="GET" name="<? echo mysql_result($sql,$i,'id');?>">
		
		<input type="hidden" name="identifiant" value="<? echo mysql_result($sql,$i,'id');?>">
		<input type='image' src="/pmcake/img/icons/bullet_arrow.gif" alt="Déplacer à demain" title="Déplacer à demain" name="demain" value="demain">
			<select name="ajout" class="petit" size="1" onChange="goto_URL(<? echo mysql_result($sql,$i,'id');?>,this.value)">
				<? delais(); ?>
			</select>
			<!--<input type="submit" class="micro">--></form>
	</td>	
		<?
		echo "</tr>";
		$i++;
		} 
	echo "</table>";
}
/* ################# calendar ################# */ 

function calendrier($annee_selectionne,$mois_aff,$jour_aff) {
	echo "<strong>".$jour_aff."</strong>";
if(!is_numeric($mois_aff)) {
	$mois_aff1=date("m");
} else {
	$mois_aff1=$mois_aff;
}	
	if($mois_aff<10) {
		$mois_aff1="0".$mois_aff;
	}
	$sql="SELECT * FROM events, event_types WHERE 
	(start LIKE '".$annee_selectionne."-".$mois_aff1."-".$jour_aff."%'
	OR end LIKE '".$annee_selectionne."-".$mois_aff1."-".$jour_aff."%') 
	AND event_type_id=event_types.id   
	ORDER BY events.start";
	//echo "<br>" .$sql; //tests
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }

	$i=0;

	echo "<table>";
	while($i<mysql_num_rows($sql)){
		$couleur=mysql_result($sql,$i,'color');
		echo "<tr><td style=\"background-color: " .$couleur ."\"><a href=\"" .CHEMIN ."events/edit/" .mysql_result($sql,$i,'events.id');
		echo "\" title=\"";
		echo mysql_result($sql,$i,'title');
		echo '" class="tooltip">';
				echo mysql_result($sql,$i,'title');

		if(strlen(mysql_result($sql,$i,'details'))>0) {
	echo '<em><span></span>'.nl2br(mysql_result($sql,$i,'details')).'</em>';
}
			echo "</a>";
		echo "<br />";
		statut(mysql_result($sql,$i,'end'));
		echo "<br />";
		?>
	</td>	
		<?
		echo "</tr>";
		$i++;
		} 
	echo "</table>";
}
/* extract events for a specific day */
function extraitjour($jour_aff) {
	$sql="SELECT * FROM events, event_types WHERE 
	(start LIKE '".$jour_aff."%'
	OR end LIKE '".$jour_aff ."%') 
	AND event_type_id=event_types.id   
	ORDER BY events.start";
	//echo "<br>" .$sql; //tests
	$sql=mysql_query($sql);
	if(!$sql) { echo "SQL error: " .mysql_error(); }

	$i=0;

	echo "<table>";
	while($i<mysql_num_rows($sql)){
		$couleur=mysql_result($sql,$i,'color');
		echo "<tr><td style=\"background-color: " .$couleur ."\"><a href=\"" .CHEMIN ."events/edit/" .mysql_result($sql,$i,'events.id');
		echo "\" title=\"";
		echo mysql_result($sql,$i,'title');
		echo '" class="tooltip">';
				//echo preg_replace("^...","",mysql_result($sql,$i,'start'));
				$heure=preg_replace("/$jour_aff/","",mysql_result($sql,$i,'start'));
				$heure=preg_replace("/:..$/","",$heure);
				echo $heure;
				echo " ";
				echo mysql_result($sql,$i,'title');

		if(strlen(mysql_result($sql,$i,'details'))>0) {
	echo '<em><span></span>'.nl2br(mysql_result($sql,$i,'details')).'</em>';
}
			echo "</a>";
		echo "<br />";
		statut(mysql_result($sql,$i,'end'));
		echo "<br />";
		?>
	</td>	
		<?
		echo "</tr>";
		$i++;
		} 
	echo "</table>";
}
 function getFirstMonday($week, $year)
{
	// Si le 1er janvier tombe semaine 53 de l'année précédent on doit tenir compte d'une semaine en plus
	if (date("W",mktime(0,0,0,1,1,$year)) == 53)
	{
		$week++;
	}	
#	$startdate =  strtotime('+' . ($week-1) . ' week',mktime(0,0,0,1,1,$year));
	$startdate =  strtotime('+' . ($week) . ' week',mktime(0,0,0,1,1,$year));
	return strtotime('last monday',$startdate);
}

?>
