<?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 (!class_exists('auth')){
	include_once("../lib/lib_common.php");
	ReloadIndex('admin');
}
////////////

//////////// Check Droits utilisateur ////////////
if (!$l21auth->hasRight('dashboard')) ReloadIndex('admin');
////////////

include_once("../lib/input_helpers.php");
include_once("../dashboard/common.php");
$result=$sql_object->DBSelect($req_list_theme);

//$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_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'];
}

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


function getIndicators($themes, $indicators, $scale_id, $status, $sql_object) {

	global $rub_link;
	
	$sdi_object= new sdi;
	
	if($status=='all') $test = true;
	if($status=='attached') $test='Y';
	if($status=='not-attached') $test='N';
	$listing="";

	$link_add=$rub_link."&amp;todo=add&amp;id=";
	$link_det=$rub_link."&amp;todo=det&amp;id=";

	$current_sdtheme='';
	
	$a = array(); // to not display twice an indicator

	// themes loop
	for ($i=0;$i<count($themes);$i++){
		
		
		if ($themes[$i]['sdtheme_id'] != $current_sdtheme && $current_sdtheme <> '') {
			$listing.="</ul>\n</div>\n";
		}
		if ($themes[$i]['sdtheme_id'] != $current_sdtheme) {
			$current_sdtheme = $themes[$i]['sdtheme_id'];
			$listing.="<h3><a href=\"\">".formatText($themes[$i]['sdtheme_name'], '2HTML')."</a></h3>";
			$listing.="<div class=\"contboxgray\">\n";
			$listing.="<ul>\n";
		}

		// indicators loop
		for ($j=0;$j<count($indicators);$j++) {
			
			if ($themes[$i]['theme_id'] == $indicators[$j]['sdii_theme']) {
				// setting $threshold_value
				// By default initial value
				$threshold_value = $indicators[$j]['sdii_threshold_value'];
				// if set, we get the latest for the given scale
				$r = $sql_object -> DBSelect(SQL_getlastInsertByPublicationDate($indicators[$j]['sdii_id'], $scale_id));
				if(isset($r[0]['sdiv_value']) && !is_null($r[0]['sdiv_threshold'])) {
					$threshold_value = $r[0]['sdiv_threshold'];
				}

				if ($status == 'all' && $indicators[$j]['sdii_to_dashboard']=='N') {
					$cssclass=' not-attached';
				} else {
					$cssclass='';
				}
 				if ($indicators[$j]['sdii_to_dashboard'] == $test  && !in_array($indicators[$j]['sdii_id'], $a)) {
					$listing.="\t<li class=\"li2".$cssclass."\">\n";
					
					$listing.= getViz($sql_object, $scale_id, $indicators[$j], $threshold_value);
					
 					$listing.="\t<div class=\"dashboard-indicator-label\">\n";
					$listing.="\t\t<a href=\"".$link_det.$indicators[$j]['sdii_id']."&amp;scale_id=".$scale_id."\" title=\"".formatText($indicators[$j]['sdii_name'], '2HTML')."\" class=\"indic-name\">".formatTextli($indicators[$j]['sdii_name'])."</a>\n";
					if ($indicators[$j]['sdii_statut']=='D') $listing.="<span class=\"state\">"._t('statut','draft')."</span>\n";
					$listing.="\t</div>\n";
					$listing.="\t\t<a href=\"".$link_add.$indicators[$j]['sdii_id']."&amp;scale_id=".$scale_id."\" class=\"ico_add\" title=\""._t('dashboard','add')."\"></a>\n";
					
					$listing.="\t</li>\n";
					
					array_push($a, $indicators[$j]['sdii_id']); // we add the id into array for not displaying it twice
					
				}
			}
		}
		
	}

	$listing.="</ul>\n</div>\n";

	return $listing;

}

//affichage selectbox échelle
$resultscale = $sql_object -> DBSelect($req_list_scale);
$scale_selectbox="<h2>".formatText($resultscale2[0]['scale_denomination'], '2HTML')."</h2>";
$scale_selectbox.="<div class=\"filterselect\">";
$scale_selectbox.=ScaleSelectBox($resultscale, "scale", $scale_id, $link_list_scale);
$scale_selectbox.="</div>\n";

?>
<div id="chemin">
	<ul>
		<li><a href="index.php" class="chemin_home"><?php echo _t('way','home'); ?></a></li>
		<li><a href="<?php echo $rub_link; ?>" id="chemin_<?php echo $rub; ?>" class="chemin_rub"><?php echo _t('menu','dashboard');?> </a></li>
		<li><span><?php echo _t('dashboard','list'); ?> </span></li>
	</ul>
</div>
<!--end barre haute -->
<?php
include_once(THEME_ADMIN_PATH."quickicons.php");
?>
<script type="text/javascript">
$(document).ready(function() {
	
	$("#content ul").each(
			  function() {
			    var elem = $(this);
			    if (elem.children().length == 0) {
			      elem.append("<?php echo _t('divers', 'nodata'); ?>");
			    }
			  }
			);
				
	$( "#tabs" ).tabs();
	$(".dashboard-indicator-gauge .cursor").removeAttr("title");
	$(".infobox").hover(
		function() {
			$(this).next().fadeIn('fast');
		},
		function() {
			$(this).next().hide();
		}
	);
});
</script>
<div class="contentcontainer">
	<?php	include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));	?>
	<div id="content">
		<?php echo $scale_selectbox;	?>
		<br style="clear: both" />
		<div id="tabs">
			<ul>
				<li><a href="#tabs-1"><?php echo _t('dashboard', 'tab-attached'); ?></a></li>
				<li><a href="#tabs-2"><?php echo _t('dashboard', 'tab-not-attached'); ?></a></li>
				<li><a href="#tabs-3"><?php echo _t('dashboard', 'tab-all'); ?></a></li>
			</ul>
			<div id="tabs-1">
				<?php echo getIndicators($result, $result_sdi, $scale_id, 'attached', $sql_object); ?>
			</div>
			<div id="tabs-2">
				<?php echo getIndicators($result, $result_sdi, $scale_id, 'not-attached', $sql_object); ?>
			</div>
			<div id="tabs-3">
				<?php echo getIndicators($result, $result_sdi, $scale_id, 'all', $sql_object); ?>
			</div>
		</div>
	</div>
	<?php include_once(override($dir.'/help.php', THEME_ADMIN_PATH)); ?>
</div>