<?php
/**
 * @package linea21.utils
 * @subpackage report_sdi
 * @author linea21 <info@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 * Define, compose and generate a SDI (Sustainable Development Indicators) PDF report
 */

/**
 * DisplayOneSdi()
 * Affiche un IDD + infos satellites
 *
 * @param int $pointer
 * @return bool true
 */
function DisplayOneSdi($pointer)
{
  global $sdi;
  global $pdf;
  global $report_settings;

  $pdf->AddPage();
  // lien interne si actif
  if ($report_settings['summary'] === true) $pdf->SetLink($pointer+1);
  DisplayGeneralsInfo($sdi[$pointer]);
  DisplayValues($sdi[$pointer]['sdii_id']);
  if ($report_settings['display_provider'] === true) DisplayProviderInfo($sdi[$pointer]);
  if ($report_settings['display_reglementation'] === true) DisplayReglementationInfo($sdi[$pointer]);
  if ($report_settings['display_evaluation'] === true) DisplayEvaluationInfo($sdi[$pointer]);

  return true;
}


function FormatSdiItem($libelle)
{
  global $pdf;
  
  $libelle="        ".$libelle;
  $pdf->SetFont(DOC_POLICE, SDI_ITEM_STYLE, SDI_ITEM_SIZE);
  $pdf->SetTextColorArray(getColor('SDI_ITEM_COLOR'));
  $pdf->MultiCell(0,DOC_HEIGHT,$libelle,0,'L',0);
  $pdf->SetTextColorArray(getColor('DOC_COLOR'));
  $pdf->SetFont(DOC_POLICE,DOC_STYLE,DOC_SIZE);
}

function FormatSdiInfo($libelle)
{
  global $pdf;
  
  $pdf->SetFillColorArray(getColor('BCKG_INFO'));
  $pdf->SetFont(DOC_POLICE, SDI_INFO_STYLE, SDI_INFO_SIZE);
  $pdf->SetTextColorArray(getColor('SDI_INFO_COLOR'));
  $pdf->MultiCell(0,DOC_HEIGHT+1,$libelle,0,'L',1);
  $pdf->SetTextColorArray(getColor('DOC_COLOR'));
  $pdf->SetFont(DOC_POLICE,DOC_STYLE,DOC_SIZE);
  $pdf->Ln(DOC_INTERVAL);
}

// Saut de paragraphe
function ParagraphBreak()
{
  global $pdf;
  
  $pdf->SetTextColorArray(getColor('DOC_COLOR'));
  $pdf->SetFont(DOC_POLICE,DOC_STYLE,DOC_SIZE);
  $pdf->Ln(PARAGRAPH_BREAK);
}

/**
 * DisplayGeneralsInfo()
 * Affiche les infos générales d'un IDD
 *
 * @param int $current_record
 * @return bool true
 */
function DisplayGeneralsInfo($current_record)
{
  global $pdf;
  global $lang;
  global $report_settings;

  // titre d'un indicateur
  $out_title = formatText($current_record['sdii_name']);
  $pdf->SetTextColorArray(getColor('SDI_TITLE_COLOR'));
  $pdf->SetFont(DOC_POLICE, SDI_TITLE_STYLE, SDI_TITLE_SIZE);
  $pdf->Bookmark(ucfirst(strtolower($out_title)));
  $pdf->Write(DOC_HEIGHT, $out_title);
  // si non publié
  if ($current_record['sdii_statut'] == 'D') {
    $pdf->SetTextColorArray(getColor('DOC_ADVISE_COLOR'));
    $pdf->SetFont(DOC_POLICE, '', SDI_TITLE_SIZE);
    $out_status = ' :: ' . strtolower($lang['statut']['draftpdf']);
    $pdf->Write(DOC_HEIGHT, $out_status);
    $pdf->SetFont(DOC_POLICE, SDI_TITLE_STYLE, SDI_TITLE_SIZE);
    $pdf->SetTextColorArray(getColor('DOC_COLOR'));
  }
  // si non attaché au tableau de bord
  if ($current_record['sdii_to_dashboard'] == 'N') {
  	$pdf->SetTextColorArray(getColor('DOC_ADVISE_COLOR'));
  	$pdf->SetFont(DOC_POLICE, '', SDI_TITLE_SIZE);
  	$out_attached_to_dashboard = ' (' . strtolower($lang['sdi']['not_attached_to_dashboard'].')');
  	$pdf->Write(DOC_HEIGHT, $out_attached_to_dashboard);
  	$pdf->SetFont(DOC_POLICE, SDI_TITLE_STYLE, SDI_TITLE_SIZE);
  	$pdf->SetTextColorArray(getColor('DOC_COLOR'));
  }
  
  $pdf->Ln(DOC_HEIGHT);
  // thème d'appartenance
  $out_theme = formatText('» ' . $lang['sdi']['theme'] . ' : ' . $current_record['theme_name'] . ' (' . $current_record['sdtheme_name'] . ')') . END_LINE;
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);
  $pdf->SetTextColorArray(getColor('SDI_TITLE_COLOR'));
  $pdf->Write(DOC_HEIGHT, $out_theme);
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
  $pdf->SetTextColorArray(getColor('DOC_COLOR'));
  // description
  $out_desc = formatText(strip_tags($current_record['sdii_description'])) . END_LINE;
  $pdf->Bookmark(ucfirst($lang['sdi']['description']), 1, -1);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_desc, 0, 'L');
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
  // objectif
  $out_goal = formatText(empty_nc(strip_tags($current_record['sdii_goal']))) . END_LINE;
  $out_goal_item = formatText(ucfirst($lang['sdi']['goal']));
  $pdf->Bookmark($out_goal_item, 1, -1);
  FormatSdiItem($out_goal_item);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_goal, 0, 'L');
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
  // conseil d'amélioration
  $out_consulting = formatText(empty_nc(strip_tags($current_record['sdii_consulting']))) . END_LINE;
  $out_consulting_item = formatText(ucfirst($lang['sdi']['consulting']));
  $pdf->Bookmark($out_consulting_item, 1, -1);
  FormatSdiItem($out_consulting_item);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_consulting, 0, 'L');
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);

  
  // informations de mesure
  $out_mesures_item = formatText(ucfirst($lang['sdi']['info_mesure']));
  $out_mesures_behavior = ($current_record['sdii_threshold_relative'] == 'Y' ? $lang['sdi']['threshold_relative_Y']: $lang['sdi']['threshold_relative_N']);
  $out_mesures_unit = formatText($lang['sdi']['unit']) . ' : ' . $current_record['sdii_unit'] . END_LINE;
  $pdf->Bookmark($out_mesures_item, 1, -1);
  FormatSdiItem($out_mesures_item);
  // unitée de mesure
  $pdf->SetFont(DOC_POLICE, EM_STYLE, MIN_SIZE);
  $pdf->Write(DOC_HEIGHT, $out_mesures_unit . END_LINE);
  // comportement
  $pdf->Write(DOC_HEIGHT, $out_mesures_behavior . END_LINE);
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);
  // tableau de valeurs
  DrawMesuresArray($current_record);
  $pdf->SetFont(DOC_POLICE, EM_STYLE, MIN_SIZE);
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
}

/**
 * DrawMesuresArray()
 * Dessine le tableau des valeurs IDD
 *
 * @param  $current_record
 * @return bool true
 */
function DrawMesuresArray($current_record)
{
  global $pdf;
  global $lang;

  $tbl = '
  <table cellspacing="0" cellpadding="5" border="1">
  <thead>
      <tr>
          <td>'.$lang['sdi']['max_value'].'</td>
          <td>'.$lang['sdi']['min_value'].'</td>
          <td>'.$lang['sdi']['initial_threshold_value'].'</td>
          <td>'.$lang['sdi']['frequency'].'</td>
      </tr>
  </thead>
      <tr>
          <td>'.empty_nc($current_record['sdii_max_value']).'</td>
          <td>'.empty_nc($current_record['sdii_min_value']).'</td>
          <td>'.empty_nc($current_record['sdii_threshold_value']).'</td>
          <td>'.$current_record['sdii_frequency'].'</td>
      </tr>
  </table>';
  
  $pdf->writeHTML($tbl, true, false, false, false, '');
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);

  return true;
}

/**
 * DisplayProviderInfo()
 * Affiche les informations Fournisseurs d'un IDD
 *
 * @param  $current_record
 * @return bool true
 */
function DisplayProviderInfo($current_record)
{
  global $pdf;
  global $lang;

  $out_name = formatText($current_record['sdip_name']);
  if (!empty($current_record['sdip_service'])) $out_name .= ' - ' . formatText($current_record['sdip_service']);
  if (!empty($current_record['sdip_incharge'])) $out_name .= ' - ' . formatText($current_record['sdip_incharge']) . ' (' . $lang['sdi']['p_incharge'] . ')';
  $out_name .= END_LINE;
  $out_adress = formatText(empty_nc(strip_tags($current_record['sdip_address'])));
  $out_phone_fax = $lang['sdi']['p_phone'] . ' : ' . formatText(empty_nc($current_record['sdip_phone']));
  $out_phone_fax .= ' - ' . $lang['sdi']['p_fax'] . ' : ' . formatText(empty_nc($current_record['sdip_fax'])) . END_LINE;
  $out_email_item = $lang['sdi']['p_email'] . ' : ';
  $out_email = formatText($current_record['sdip_email']);
  $out_desc = formatText(empty_nc(strip_tags($current_record['sdip_description']))) . END_LINE;
  $out_provider_item = formatText(ucfirst($lang['sdi']['p_title'])) ;

  ParagraphBreak();
  $pdf->Bookmark($out_provider_item, 1, -1);
  FormatSdiInfo($out_provider_item);

  $firstcell_width = 60;

  $pdf->SetX($pdf->GetX() + $firstcell_width);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_name, 0, 'L');

  $pdf->SetX($pdf->GetX() + $firstcell_width);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_adress, 0, 'L');

  $pdf->SetX($pdf->GetX() + $firstcell_width);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_phone_fax, 0, 'L');

  $pdf->SetX($pdf->GetX() + $firstcell_width);
  $pdf->Cell($pdf->GetStringWidth($out_email_item), DOC_HEIGHT, $out_email_item);
  if (empty($out_email)) $pdf->Cell(0, DOC_HEIGHT, empty_nc($out_email));
  else $pdf->Cell(0, DOC_HEIGHT, $out_email, 0, 0, 'L', 0, 'mailto:' . $out_email);
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);

  return true;
}

/**
 * DisplayReglementationInfo()
 * Affiche la réglementation d'un IDD
 *
 * @param  $current_record
 * @return bool true
 */
function DisplayReglementationInfo($current_record)
{
  global $pdf;
  global $lang;

  $out_name = formatText(empty_nc($current_record['sdir_title'])) . END_LINE;
  $out_body = formatText(empty_nc(strip_tags($current_record['sdir_body']))) . END_LINE;
  $out_uri = formatText($current_record['sdir_mask_uri']);
  $out_uri_href = $current_record['sdir_referer_uri'];

  $out_name_item = $lang['sdi']['r_title'] . ' : ';
  $out_body_item = $lang['sdi']['r_body'] . ' : ';
  $out_uri_item = $lang['sdi']['r_referer_uri'] . ' : ';

  $out_reglementation_item = formatText(ucfirst($lang['sdi']['add_step4'])) ;

  ParagraphBreak();
  $pdf->Bookmark($out_reglementation_item, 1, -1);
  FormatSdiInfo($out_reglementation_item);

  $firstcell_width = 60;
  // nom de l'organisme fournisseur
  $pdf->SetFont(DOC_POLICE, EM_STYLE, DOC_SIZE);
  $pdf->Cell($firstcell_width, DOC_HEIGHT, $out_name_item, 0, 0, 'R');
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_name, 0, 'L');
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
  // détail reglementation
  $pdf->SetFont(DOC_POLICE, EM_STYLE, DOC_SIZE);
  $pdf->Cell($firstcell_width, DOC_HEIGHT, $out_body_item, 0, 0, 'R');
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);
  $pdf->MultiCell(0, DOC_HEIGHT, $out_body, 0, 'L');
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
  // lien internet reglementation
  $pdf->SetFont(DOC_POLICE, EM_STYLE, DOC_SIZE);
  $pdf->Cell($firstcell_width, DOC_HEIGHT, $out_uri_item, 0, 0, 'R');
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);
  if (!empty($out_uri_href)) {
    if (!empty($out_uri)) $pdf->Cell(0, DOC_HEIGHT, $out_uri, 0, 0, '', 0, $out_uri_href);
    //if (!empty($out_uri)) $pdf->Cell(0, DOC_HEIGHT, ' (', 0, 0);
    //$pdf->Cell(0, DOC_HEIGHT, $out_uri, 0, 0, '', 0, $out_uri_href);
    //if (!empty($out_uri)) $pdf->Cell(0, DOC_HEIGHT, ')', 0, 0);
  } else $pdf->Write(DOC_HEIGHT, empty_nc($out_uri));
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);

  return true;
}

/**
 * DisplayEvaluationInfo()
 * Affiche le tableau de critères d'évaluation
 *
 * @param int $current_record
 * @return bool true
 */
function DisplayEvaluationInfo($current_record)
{
  global $pdf;
  global $lang;

  $out_eval_item = formatText(ucfirst($lang['sdi']['e_title'])) ;

  ParagraphBreak();
  $pdf->Bookmark($out_eval_item, 1, -1);
  FormatSdiInfo($out_eval_item);
  
  
  $tbl = '
      <table cellspacing="0" cellpadding="5" border="1">
      <thead>
          <tr>
              <td>'.$lang['sdi']['e_scale_compare'].'</td>
              <td>'.$lang['sdi']['e_fiability'].'</td>
              <td>'.$lang['sdi']['e_accessibility'].'</td>
              <td>'.$lang['sdi']['e_lisibility'].'</td>
              <td>'.$lang['sdi']['e_relevance'].'</td>
              <td>'.$lang['sdi']['e_global_performance'].'</td>
          </tr>
      </thead>
        <tr>
        	<td>'.$current_record['sdie_scale_compare'].'</td>
            <td>'.$current_record['sdie_fiability'].'</td>
            <td>'.$current_record['sdie_accessibility'].'</td>
            <td>'.$current_record['sdie_lisibility'].'</td>
            <td>'.$current_record['sdie_relevance'].'</td>
            <td>'.$current_record['sdie_global_performance'].'</td>
      	</tr>
      </table>';
  
  $pdf->SetFont(DOC_POLICE, ARRAY_DATA_STYLE, ARRAY_DATA_SIZE);
  $pdf->writeHTML($tbl, true, false, false, false, '');
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);

  return true;
}

/**
 * DisplayValues()
 * Affiche les valeurs d'un IDD.
 *
 * @param int $sdi_id
 * @return bool true
 **/
function DisplayValues($sdi_id)
{
  global $pdf;
  global $lang;
  global $report_settings;
  global $scale_array;
  global $sql_object;

  if ($report_settings['graphic_values'] === true && $report_settings['table_values'] === true) $mode = 'TWICE';
  elseif ($report_settings['graphic_values'] === true || $report_settings['table_values'] === true) $mode = 'ONE';
  else return false;

  $out_values_item = ucfirst($lang['dashboard']['value']);
  ParagraphBreak();
  $pdf->Bookmark($out_values_item, 1, -1);
  FormatSdiInfo($out_values_item);

  $scale_no_values = array(); // recipient no values
  $scale_insuffisant_values = array(); // recipient insuffisant values graph
  for($i = 0; $i < count($scale_array); $i++) {
    if ($report_settings['graphic_values'] === true) {
      if (DisplayGraphicValues($sdi_id, $scale_array[$i], GetStatus($report_settings['status']), $mode) === false) array_push($scale_insuffisant_values, $scale_array[$i]);
    }

    if ($report_settings['table_values'] === true) {
      if (DisplayTableValues($sdi_id, $scale_array[$i], GetStatus($report_settings['status']), $mode) === false) array_push($scale_no_values, $scale_array[$i]);
    }
  }
  DisplayNoValuesFor($scale_no_values, $lang['dashboard']['rap_no_values_for']);
  DisplayNoValuesFor($scale_insuffisant_values, $lang['dashboard']['rap_insuffisant_values'], $mode, $scale_no_values);

  return true;
}

/**
 * GetStatus()
 * Formattage du statut pour requête SQL
 *
 * @param string $current_status
 * @return string $status
 **/
function GetStatus($current_status)
{
  switch ($current_status) {
    case 'ALL':
      $status = 'SCA';
      break;
    case 'PUBLIC':
      $status = 'SCP';
      break;
  }
  return $status;
}

/**
 * DisplayNoValuesFor()
 * Affiche les échelles ne contenant pas de valeurs ou insuffisamment
 *
 * @param array $array_scale
 * @param string $message
 * @param integer $mode
 * @param integer $array
 * @return bool true
 **/
function DisplayNoValuesFor($array_scale, $message, $mode = -1, $array = -1)
{
  global $pdf;
  global $lang;
  global $scale_denomination_array;
  $content = '';
  $sep = '';
  // si mode TWICE et deuxième tableau fourni. On le soustrait au premier.
  if ($array != -1 && $mode == 'TWICE') $array_scale = array_values(array_diff($array_scale, $array));

  if (count($array_scale) == 0) return true;

  for($i = 0; $i < count($array_scale); $i++) {
    $content .= $sep . $scale_denomination_array[$array_scale[$i]];
    $sep = ', ';
  }
  if (empty($content)) return true;
  $margin = 0;
  $pdf->SetX($pdf->GetX() + $margin);
  $pdf->SetFont(DOC_POLICE, EM_STYLE, DOC_SIZE);
  $pdf->Cell(0, DOC_HEIGHT, $message, 0, 1, 'L');
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);
  $margin = 10;
  $pdf->SetTextColorArray(getColor('DOC_ADVISE_COLOR'));
  $pdf->SetX($pdf->GetX() + $margin);
  $pdf->MultiCell(0, DOC_HEIGHT, $content, 0, 'L');
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
  $pdf->SetTextColorArray(getColor('DOC_COLOR'));

  return true;
}

/**
 * DisplayGraphicValues()
 * Gènère et affiche les valeurs sous forme graphique
 *
 * @param int $sdi_id
 * @param int $current_scale
 * @param string $status
 * @param string $mode
 * @return bool true
 **/
function DisplayGraphicValues($sdi_id, $current_scale, $status, $mode)
{
  global $pdf;
  global $sql_object;
  global $scale_denomination_array;

  $values = $sql_object->DBSelect(SQL_getAllValue($status, $current_scale, $sdi_id));
  // renversement des valeurs pour affichage chronologique
  if(is_array($values)) $values = @array_reverse ($values, false);

  $result_sdii = $sql_object->DBSelect(SQL_getInfoSdi($sdi_id));

  if (count($values) < 3) return false;
  // on checke si le graph tient sur la page sinon Saut de page
  // $pdf->CheckPageBreak((GRAPH_HEIGHT * 0.36) + DOC_INTERVAL);
  // Affichage du titre seulement si $values>= 3
  $out_scale_item = $scale_denomination_array[$current_scale];
  //$pdf->Bookmark($out_scale_item, 2, -1);
  FormatSdiItem($out_scale_item);
  $pdf->SetY($pdf->GetY() + DOC_INTERVAL);

  // stockage de l'ordonnée initiale
  $_y = $pdf->GetY();

  // positionnement en fonction du mode.

  if ($mode == 'TWICE') $_x = 104;
  else $_x = 50;

  /* pChart library inclusions */
	include_once("../class/system/pChart/class/pData.class.php");
	include_once("../class/system/pChart/class/pDraw.class.php");
	include_once("../class/system/pChart/class/pImage.class.php");

  $unit = $result_sdii[0]['sdii_unit'];
  $full_title=formatText($result_sdii[0]['sdii_name']);
  $title=cutText($full_title, 50, 0);

  $is_null_threshold = true; // is the threshold null or not? to know if we display it
  for ($i = 0; $i < count($values); $i++) {
    $ydata[$i] = $values[$i]['sdiv_value'];
    
    // setting $threshold_value
    // By default initial value
    $ydata2[$i]=$result_sdii[0]['sdii_threshold_value'];
    // if set, we get it from the table
    if(!is_null($values[$i]['sdiv_threshold'])) {
    	$ydata2[$i]=$values[$i]['sdiv_threshold'];
    }
    // if at least one numeric value is there test is false
    if(is_numeric($ydata2[$i])) $is_null_threshold = false;
    
    list($day, $month, $year)=explode('-', $values[$i]['date_p']);
    $months[$i] = $day . '-' .$month. '-'. substr($year, 2, 2);
  }
  
  /* Create and populate the pData object */
  $MyData = new pData();
  //  $MyData->loadPalette("../class/system/pChart/palettes/blind.color",TRUE);
  $MyData->setAxisName(0,$unit);
  $MyData->addPoints($ydata,$full_title);
  $MyData->addPoints($ydata2,"thresholds");
  $MyData->addPoints($months,"Dates");
  $MyData->setSerieDescription($full_title,$unit);
  $MyData->setAbscissa("Dates");
  
  $thresholdsSettings = array("R"=>254,"G"=>148,"B"=>64);
  $MyData->setPalette("thresholds",$thresholdsSettings);
  $valuesSettings = array("R"=>207,"G"=>228,"B"=>252);
  $MyData->setPalette("$full_title",$valuesSettings);
  
  /* Create the pChart object */
  $myPicture = new pImage(GRAPH_WIDTH,GRAPH_HEIGHT,$MyData);

  /* Turn of Antialiasing */
	$myPicture->Antialias = TRUE;
	
	/* Add a border to the picture */
	// $myPicture->drawRectangle(0,0,499,349,array("R"=>180,"G"=>180,"B"=>180));
	
	/* Set the default font */
	$myPicture->setFontProperties(array("FontName"=>"../class/system/pChart/fonts/verdana.ttf","FontSize"=>7,"R"=>80,"G"=>80,"B"=>80));
	
	/* Define the chart area */
	$myPicture->setGraphArea(50,20,GRAPH_WIDTH-20,GRAPH_HEIGHT-50);
	
	
	/* Draw the scale */
	$scaleSettings = array(
			// grid settings
			"GridR"=>160,"GridG"=>160,"GridB"=>160,"GridTicks"=>1, "DrawSubTicks"=>TRUE,
			// axis settings
			"AxisR"=>80,"AxisG"=>80,"AxisB"=>80,
			"CycleBackground"=>TRUE, 'LabelRotation'=>50,
			"DrawArrows"=>FALSE);
	
	$myPicture->drawScale($scaleSettings);
	
	
	/* Write the chart legend */
	//$myPicture->drawLegend(50,5,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
	
	
	/* Draw the chart */
	$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10)); /* Turn on shadow computing */
	$settings = array(
			"InnerBorderR"=>80, "InnerBorderG"=>80, "InnerBorderB"=>80,
			"BorderR"=>80, "BorderG"=>80, "BorderB"=>80,
			// color and gradient settings
			"DisplayColor"=>DISPLAY_MANUAL,
			"Gradient"=>TRUE,"GradientStartR"=>207,"GradientStartG"=>228,"GradientStartB"=>252,
			"GradientEndR"=>146, "GradientEndG"=>174, "GradientEndB"=>206,"GradientAlpha"=>100,
			// label settings
			"DisplayValues"=>FALSE, "DisplayPos"=>LABEL_POS_OUTSIDE,"DisplayR"=>115,"DisplayG"=>115,"DisplayB"=>115,"DisplayOrientation"=>ORIENTATION_AUTO,
			"DisplayShadow"=>FALSE,
			"Draw0Line"=>FALSE,
			//"Rounded"=>TRUE, // not working with gradient
			"Surrounding"=>-30,
			"InnerSurrounding"=>0);
	
	$MyData->setSerieDrawable($full_title,TRUE);
	$MyData->setSerieDrawable("thresholds",FALSE);
	$myPicture->drawBarChart($settings);
	
	/* Draw the line and plot chart */
	$myPicture->setShadow(FALSE); /* Turn off shadow computing */
	// if at least one threshold is there we display the serie on graph
	if(!$is_null_threshold) {
		$MyData->setSerieDrawable("thresholds",TRUE);
	}
	$MyData->setSerieDrawable($full_title,FALSE);
	$myPicture->drawSplineChart();
	$myPicture->drawPlotChart();
	
	/* Render the picture */
	if(!file_exists('../tmp/report')) mkdir('../tmp/report');

	$myPicture->render('../tmp/report/'.$sdi_id.'_'.$current_scale.'.png');
	
	/**

  $group = new PlotGroup;
  $group->setSpace(5, 5, 5, 0);
  $group->setBackgroundColor(
  new Color(244, 244, 244)
  );
  $group->grid->hideVertical();
  $group->grid->SetColor(new Color(0, 0, 0, 80));
  $group->grid->setType(LINE_DOTTED );
  $group->setPadding(40, 15, 20, 50);

  $bplot = new BarPlot($ydata);
  //$bplot->setSpace(4, 4, 4, 0);
  //$bplot->setPadding(15, 15, 5, 50);
  $bplot->SetBarGradient(
  new LinearGradient(
  new Color(207, 228, 252, 0),
  new Color(146, 174, 206, 0),
  90
  )
  );

  $group->axis->left->title->set($unit);
  $group->axis->left->title->setFont(new Tuffy(10));
  $group->axis->left->title->move(-4, 0);
  $group->axis->left->setTitleAlignment(LABEL_TOP);

  $bplot->label->border->SetColor(new Color(20, 20, 20, 20));
  $bplot->label->setPadding(3, 1, 1, 0);
  $bplot->barBorder->SetColor(new Color(161, 182, 206, 0));

  $group->axis->bottom->setLabelText($months);
  $group->axis->bottom->label->setAngle(55);
  $group->axis->bottom->label->setFont(new Tuffy(7));

  $group->add($bplot);


  $plot = new LinePlot($ydata2, LINEPLOT_MIDDLE);
  $plot->setFillColor(new Color(254, 148, 64, 50));
  $plot->SetColor(new Color(254, 148, 64, 0));
  $plot->mark->setType(MARK_SQUARE);
  $plot->mark->setSize(5);
  $plot->mark->setFill(new Color(246, 149, 55, 20));
  $plot->mark->border->show();
  $plot->mark->border->SetColor(new Color(254, 148, 64, 0));

  $group->add($plot);
  $graph->add($group);
  if(!file_exists('tmp')) mkdir('tmp');
  $graph->draw('tmp/'.$sdi_id.'_'.$current_scale.'.png');
  */
	
  // /////////////////////////////
  // Affichage
  $pdf->Image('../tmp/report/'.$sdi_id.'_'.$current_scale.'.png', $_x, $_y, 0, 0);
  // conversion px -> mm + replacement en ordonnée
  if ($mode == 'TWICE') $pdf->SetY($_y);
  else $pdf->SetY($_y + (GRAPH_HEIGHT * 0.36)); //$pdf->SetY($_y + (GRAPH_HEIGHT * 0.36) + DOC_INTERVAL);
  return true;
}

/**
 * DisplayTableValues()
 * Affiche un tableau de valeurs a une échelle donnée
 *
 * @param int $sdi_id
 * @param int $current_scale
 * @param string $status
 * @param string $mode
 * @return bool true
 **/
function DisplayTableValues($sdi_id, $current_scale, $status, $mode)
{
  global $pdf;
  global $lang;
  global $sql_object;
  global $scale_denomination_array;
  
  $values ='';
  $comments='';
  $j = 1;

  $_y = $pdf->GetY();
  
  
  // setting $default_threshold value
  // By default initial value
  $result_sdii = $sql_object -> DBSelect(SQL_getInfoSdi($sdi_id));
  $default_threshold = $result_sdii[0]['sdii_threshold_value'];
  
  $sdi_values = $sql_object->DBSelect(SQL_getAllValue($status, $current_scale, $sdi_id));
  
  if (count($sdi_values) <= 1 && $sdi_values[0] == false) return false;
  
  // we prepare values for displaying
  for($i = 0; $i < count($sdi_values); $i++) {
    
  	$current_value = formatText($sdi_values[$i]['sdiv_value']);
  	
  	// if set, we get it from the table
  	if(!is_null($sdi_values[$i]['sdiv_threshold'])) {
  		$threshold = $sdi_values[$i]['sdiv_threshold'];
  	} else {
  		$threshold = $default_threshold;
  	}
  	
  	if ($sdi_values[$i]['sdiv_statut'] == 'D') $current_value .= ' *';
  	if (!empty($sdi_values[$i]['sdiv_comment'])) {
  	  if($sdi_values[$i]['sdiv_comment_display']=='Y') {
  	    $comment_status = _t('dashboard', 'public');
  	  } else {
  	    $comment_status = _t('dashboard', 'private');
  	  }
  	  $bullet = ' <sup>['.$j.']</sup>';
  	  $current_value .= $bullet;
  	  $comments .= $bullet. ' '.formatText(strip_tags($sdi_values[$i]['sdiv_comment'])) .' ('.$comment_status.')<br />';
  	  $j++;
  	}
  	$current_date = formatText($sdi_values[$i]['date_p']);
  	$values .= '
            <tr>
                <td>'.$current_value.'</td>
                <td>'.$threshold.'</td>
                <td>'.$current_date.'</td>
            </tr>';
  }

  

  if ($mode != 'TWICE' || (count($sdi_values) >= 1 && count($sdi_values) < 3)) {
    // Affichage du titre seulement si affichage des valeurs en standalone
    $out_scale_item = $scale_denomination_array[$current_scale];
    //$pdf->Bookmark($out_scale_item, 2, -1);
    FormatSdiItem($out_scale_item);
    $pdf->SetY($pdf->GetY() + DOC_INTERVAL);
  }

  $pdf->Ln(DOC_INTERVAL);
  if ($mode == 'TWICE') $_margin = 20;
  else $_margin = 60;

  $pdf->SetX($_margin);
 
  
  $tbl = '
    <table cellspacing="0" cellpadding="5" border="1" style="width:40%">
    <thead>
        <tr>
            <td>'.$lang['dashboard']['name_pluriel'].'</td>
            <td>'.$lang['dashboard']['threshold_value'].'</td>
            <td>'.$lang['statut']['published_on'].'</td>
        </tr>
    </thead>
    '.$values.'
    </table>';
  
  $pdf->writeHTML($tbl, true, false, false, false, '');
  
  $pdf->SetX($_margin);
  $pdf->SetFont(DOC_POLICE, EM_STYLE, MIN_SIZE);
  $pdf->MultiCell(0, DOC_HEIGHT, '* ' . strtolower($lang['statut']['draftpdf']), 0, 'L', 0);
  $pdf->SetX($_margin);
  if(!empty($comments))  $pdf->writeHTMLCell(0, DOC_HEIGHT, $pdf->GetX(), $pdf->GetY(), $comments, 0, 1);
  $pdf->SetFont(DOC_POLICE, DOC_STYLE, DOC_SIZE);
  $pdf->Ln(DOC_INTERVAL * 2);
  // conversion px -> mm + replacement en ordonnées
  if ($mode == 'TWICE' && count($sdi_values) >= 3 && $pdf->GetY() < ($_y + (GRAPH_HEIGHT * 0.36))) $pdf->SetY($_y + (GRAPH_HEIGHT * 0.36) + DOC_INTERVAL);

  return true;
}

?>