<?php
/**
 * @package linea21.modules
 * @subpackage dashboard
 * @author linea21 <info@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

//////////// Check Inclusion de pages //////////// 
if (!function_exists('AuthenthificationProcess')){ 
include_once("../lib/lib_common.php");
ReloadIndex('admin');
}
////////////

//$scale_id=1;
include_once("../lib/input_helpers.php");
$result=$sql_object->DBSelect($req_list_theme);
$sdi_object= new sdi;
//$result_sdi=$sdi_object->GetListSdi($filter="R", $valueparam=0, $sql_object);
$req_sdi=SQL_getListSdi($filter="R", $valueparam=0);
$result_sdi = $sql_object -> DBSelect($req_sdi);

$link_sup=$rub_link."&amp;todo=sup&amp;id=";
$link_add=$rub_link."&amp;todo=add&amp;id=";
$link_det=$rub_link."&amp;todo=det&amp;id=";
$link_list_scale=$rub_link."&amp;todo=list&amp;scale_id=";

if (!isset($_GET['scale_id']) && !isset($_POST['scale_id'])) {
    $scale_id = 1;
} else {
    if (isset($_POST['scale_id'])) $scale_id = $_POST['scale_id'];
    if (isset($_GET['scale_id'])) $scale_id = $_GET['scale_id'];
} 

$req_scale=SQL_getonescale($scale_id);
$resultscale2 = $sql_object -> DBSelect($req_scale);

$listing="";
$current_sdtheme='';
for ($i=0;$i<count($result);$i++){
	if ($result[$i]['sdtheme_id']!=$current_sdtheme && $current_sdtheme<>'') {
	$listing.="</ul>\n";
	$listing.="</div>\n";
	}
	if ($result[$i]['sdtheme_id']!=$current_sdtheme) {
		$current_sdtheme=$result[$i]['sdtheme_id'];
		$listing.="<div class=\"h2sdtheme\"><a href=\"\">".formatText($result[$i]['sdtheme_name'], '2HTML')."</a></div>";
		$listing.="<div class=\"sdtheme\">\n";
		$listing.="<ul>\n";
	}
	for ($j=0;$j<count($result_sdi);$j++){
	if ($result[$i]['theme_id']==$result_sdi[$j]['sdii_theme']) {
	$listing.="\t<li class=\"li2\">\n";
	$aff_bare=aff_barre($scale_id, $result_sdi[$j]['sdii_id'], $result_sdi[$j]['sdii_max_value'], $result_sdi[$j]['sdii_min_value'], $result_sdi[$j]['sdii_threshold_value'], $sdi_object, $sql_object, $lang, $result_sdi[$j]['sdii_unit'], $result_sdi[$j]['sdii_threshold_relative']);
	$listing.=$aff_bare[1];
	$listing.="\t<div>\n";
	$listing.="\t\t<a href=\"".$link_det.$result_sdi[$j]['sdii_id']."&amp;scale_id=".$scale_id."\" title=\"".formatText($result_sdi[$j]['sdii_name'], '2HTML')."\">".formatTextli($result_sdi[$j]['sdii_name'])."</a>\n";
	if ($result_sdi[$j]['sdii_statut']=='D') $listing.="<span class=\"state\">"._t('statut','draft')."</span>\n";
	$listing.="\t\t<a href=\"".$link_add.$result_sdi[$j]['sdii_id']."&amp;scale_id=".$scale_id."\" class=\"ico_add\" title=\""._t('dashboard','add')."\"></a>\n";
	$listing.="\t</div>\n";
	$listing.="\t</li>\n";   
	$barre_globale[$j]=$aff_bare[0];
	}
	}
}
	$listing.="</ul>\n";
	$listing.="</div>\n";

	//calcul de la valeur de la barre globale
	$value_globale="";
	$z="";
	$i="";
	if(isset($barre_globale)) {
	for ($z=0;$z<count($barre_globale);$z++){
		if ($barre_globale[$z]>0) {
			$value_globale+=$barre_globale[$z];
			$i++;
		}
	}
	}
	if ($i<>0) {
	$value_globale=$value_globale/$i;
	$value_globale=round($value_globale, 1);
	$value_curs=111-$value_globale;   
	}
	
	if ($i==0) {
	   $listing2="<div class=\"globalvalue\">";
	       $listing2.="<div class=\"imagebarre\">\n<span class=\"globalvaluetext\">"._t('dashboard','global_value')."</span><img src=\"" .THEME_ADMIN_PATH. "images/barre_gris.gif\" alt=\""._t('dashboard','novalue')."\" />\n</div>\n";
	$listing2.="</div><br class=\"brendstep\" />";
	}
	else {
	$listing2="<div class=\"globalvalue\">";
$listing2.="<div class=\"imagebarre\"><span >"._t('dashboard','global_value')."</span> <img src=\"" .THEME_ADMIN_PATH. "images/barre.gif\" alt=\""._t('dashboard','global_value')."\" />";
$listing2.="<img src=\"" .THEME_ADMIN_PATH. "images/cursor.gif\" alt=\""._t('dashboard','global_value').": ".$value_globale.
"\n"._t('dashboard','barre')."\" class=\"cursor\" style=\"left:-".$value_curs."px;\"/>";
$listing2.="\t<span class=\"value\"> (".(int)$value_globale.")</span></div>\n";
$listing2.="</div><br class=\"brendstep\" />";
	}
	//affichage selectbox échelle
	
$resultscale = $sql_object -> DBSelect($req_list_scale);
$listing3="<h2>".formatText($resultscale2[0]['scale_denomination'], '2HTML')."</h2>";
$listing3.="<div class=\"listselect\">";
$listing3.=ScaleSelectBox($resultscale, "scale", $scale_id, $link_list_scale);
$listing3.="</div>\n";	
	
?>
<div id="chemin">
	<ul>
		<li>
			<a href="index.php" class="chemin_home"><? echo _t('way','home'); ?></a>
		</li>
		<li>
			   <a href="<?php echo $rub_link; ?>" id="chemin_<? echo $rub; ?>" class="chemin_rub"><? echo _t('menu','dashboard');?></a>
		</li>
		<li>
			<span><? echo _t('dashboard','list'); ?></span>
		</li>
	</ul>
</div>
  <!--end barre haute -->
<?php
function value_barre ($value, $max_value, $min_value, $threshold_value, $threshold_relative){
if ($max_value==0) $max_value=0.000001;
if ($min_value==0) $min_value=0.000001;
if ($threshold_relative=='Y') {
$unitup=50/($max_value-$threshold_value);
$unitdown=50/($threshold_value-$min_value);
if ($value>$threshold_value) $result=50+(($value-$threshold_value)*$unitup);
else $result=50-(($threshold_value-$value)*$unitdown);
}
else{
if(($max_value - $threshold_value) <> 0) $unitdown=50/($max_value-$threshold_value);
if(($threshold_value - $min_value) <> 0) $unitup=50/($threshold_value-$min_value);
if ($value>$threshold_value) $result=50-(($value-$threshold_value)*$unitdown);
else $result=50+(($threshold_value-$value)*$unitup);
}
return (int)$result;
}
function aff_barre($scale_id, $id, $max_value, $min_value, $threshold_value, $sdi_object, $sql_object, $lang, $unit, $threshold_relative){
//$req_sdiav=SQL_getInfoSdi("SCA", $scale_id, $id, $sql_object);

$req_sdiav=SQL_getAllValue("SCA", $scale_id, $id);	
$result_value = $sql_object -> DBSelect($req_sdiav);


if ($result_value<>false){
$value_cursor1=value_barre ($result_value[0]['sdiv_value'], $max_value, $min_value, $threshold_value, $threshold_relative);
$value_cursor=111- $value_cursor1;
}
else $value_cursor1="";
if ($result_value==false) {
    $listing="<div class=\"imagebarre\">\n<img src=\"" .THEME_ADMIN_PATH. "images/barre_gris.gif\" title=\""._t('dashboard','novalue')."\" />\n</div>\n";
}
else{
$listing="<div class=\"imagebarre\"><a href=\"index.php?rub=dashboard&amp;todo=det&amp;id=".$id."&amp;scale_id=".$scale_id."\" class=\"barre\"><img src=\"" .THEME_ADMIN_PATH. "images/barre.gif\" alt=\""._t('dashboard','value').": ".$result_value[0]['sdiv_value']." ".$unit.
"\n "._t('sdi','min_value').": ".$min_value." ".$unit.
"\n "._t('sdi','threshold_value').": ".$threshold_value." ".$unit.
"\n "._t('sdi','max_value').": ".$max_value." ".$unit.
"\"  /><span>"._t('dashboard','value').": ".$result_value[0]['sdiv_value']." ".$unit.
"<br /> "._t('sdi','min_value').": ".$min_value." ".$unit.
"<br /> "._t('sdi','threshold_value').": ".$threshold_value." ".$unit.
"<br /> "._t('sdi','max_value').": ".$max_value." ".$unit.
"</span></a>";
$listing.="<img src=\"" .THEME_ADMIN_PATH. "images/cursor.gif\" title=\""._t('dashboard','value').": ".$result_value[0]['sdiv_value'].
" "._t('dashboard','barre')."\" class=\"cursor\" style=\"left:-".$value_cursor."px;\"/>";
if (isset($result_value[1]['sdiv_value'])) {
if ($result_value[0]['sdiv_value']>=$result_value[1]['sdiv_value']) $listing.="<img src=\"" .THEME_ADMIN_PATH. "images/ico_asc.gif\" alt=\""._t('dashboard','value_tendance')."\" />";
else $listing.="<img src=\"" .THEME_ADMIN_PATH. "images/ico_desc.gif\" alt=\""._t('dashboard','value_tendance')."\" />";
}
$listing.="\t<span class=\"value\"> (".(int)$result_value[0]['sdiv_value'].")</span></div>\n";
}
$result=array($value_cursor1, $listing);
return 	$result;
}
include_once(THEME_ADMIN_PATH."quickicons.php");
?>
<div class="contentcontainer">
<?php	include_once($dir."/menurub.php");	?>
<div id="content">
<?
echo $listing3;
echo $listing2;
echo $listing;
?>
</div>
<?php include_once($dir."/help.php"); ?>
</div>
  
