<?php
if (!isset($version)) $version = '2.6';
?>
<html>
<head>
<title>Gnome <?php print $version; ?> Translation Showdown</title>
<!-- "relative" in div.graph is essential, because it changes the parent coordinate system, so we can use "absolute" later on -->
<style language="css">
body {
  background:#F8F8F1;
  margin: 0px;
  margin-right: 180px;
}
table {
  background: white;
}

tr.odd {
  background: #E0E0E0;
}
tr.even {
  background: #F8F8F1;
}

td {
  text-align: right;
  padding-left: 8px;
  padding-right: 8px;
}

td.lang {
  text-align: left;
}

th {
  background: #446688;
  color: white;
}

div.graph {
  height: 10pt;
  width: 240px;
  border: 1px solid black; 
  position: relative;
}

div.translated {
  position: absolute; 
  height:100%; 
  left: 00%; 
  background: #448844;
}

div.goodchange {
  position: absolute; 
  height:100%; 
  background: #55DD55;
}

div.fuzzy {
  position: absolute; 
  height:100%; 
  background: #4444AA;
}

div.untranslated {
  position: absolute; 
  height:100%; 
  background: #FF4444;
}

div.legend {
  width: 100%;
  color: black;
  text-align: center;
  padding-top: 3px;
  padding-bottom: 3px;
  font-weight: bold;
  border: 1px solid black;
  border-bottom: 0px;
}
</style>
</head>
<body>
<div style="right: 180px; left: 0px; position:relative; border: 0px solid black; padding:20px;">
<?php

  $db = mysql_connect("localhost","danilo_nejaki","m0J4loz1Nka");
  mysql_selectdb("dnl_prevod");

$gw = 240; // width of the final graph

function readin($ime) {
   global $HTTP_COOKIE_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS;
 
   $temp=$HTTP_COOKIE_VARS[$ime];
   if ($HTTP_POST_VARS[$ime]) $temp=$HTTP_POST_VARS[$ime];
   if ($HTTP_GET_VARS[$ime]) $temp=$HTTP_GET_VARS[$ime];
   $temp=str_replace("'","\'",$temp);
   return $temp;
}

if (strtolower($_SERVER['REQUEST_METHOD']) == 'post' or readin('end')) {
  //if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  $startdate = readin('start');
  $enddate = readin('end');
  if (!$startdate) $startdate=$enddate;
  print "<h1>Gnome $version desktop translation statistics</h1>\n";
  print "Comparing translation status from $startdate to $enddate<br>\n";

  $q = mysql_query("select ".
		   " stats.id, description, date, version, translated, fuzzy, untranslated".
		   " from stats, langs".
		   " where stats.id=langs.id and date='$enddate' and version='$version'".
		   " order by (translated/(translated+fuzzy+untranslated)) desc, translated desc, description asc");

  $XMLoutput = "<translation-stats>\n";
  $output = "<table>\n".
    "<tr><th>rank</th><th>Language</th><th>State</th><!-- <th>Fuzzy</th><th>Untranslated</th> -->".
    "<th>Change</th><th>Cool graph</th></tr>\n";

  $rank = 0; $ptrans = 0;
  while ($q and $row = mysql_fetch_array($q)) {
    $prevstate = $ptrans;
    $rank++;
    $total = $row['translated'] + $row['fuzzy'] + $row['untranslated'];
    $ptrans = round(10000*$row['translated']/$total)/100;
    $pfuzzy = round(10000*$row['fuzzy']/$total)/100;
    $puntrans = round(10000*$row['untranslated']/$total)/100;

    $optrans = 0;
    $qq = mysql_query("select * from stats where date='$startdate' and version='$version' and id='$row[id]'");
    if ($qq and $oldrow = mysql_fetch_array($qq)) {
      $oldtotal = $oldrow['translated'] + $oldrow['fuzzy'] + $oldrow['untranslated'];
      $optrans = number_format(round(10000*$oldrow['translated']/$oldtotal)/100,2);
    }
    $pchange = round(100*($ptrans-$optrans))/100;
    if ($pchange>0) {
      $schange = "up ". number_format($pchange,2) . "%";
    } elseif ($pchange<0) {
      $schange = "down ". number_format(-$pchange,2). "%";
    } else {
      $schange = "no change";
    }
    
    // building a graph
    $dagraph = "<div class=\"graph\">\n";
    $curstart = 0;
    $curwidth = 0;
    if ($pchange>0) {
      $soldtrans = round(($ptrans - $pchange)*$gw/100);
      $send = round($pchange*$gw/100);
      $dagraph .= "  <div class=\"translated\" style=\"width: $soldtrans"."px;\"></div>\n".
	"  <div class=\"goodchange\" style=\"left: $soldtrans"."px; width: $send"."px;\"></div>\n";
      $curwidth = $soldtrans+$send;
    } else {
      $curwidth = round($ptrans*$gw/100);
      $dagraph .= "  <div class=\"translated\" style=\"width: $curwidth"."px;\"></div>\n";
    }

    if ($pfuzzy>0) {
      $swidth = round($pfuzzy*$gw/100);
      $dagraph .= "  <div class=\"fuzzy\" style=\"width: $swidth"."px; left: $curwidth"."px;\"></div>\n";
      $curwidth += $swidth;
    }

    if ($puntrans>0) {
      $swidth = round($puntrans*$gw/100);
      if ($swidth<($gw-$curwidth)) $swidth=$gw-$curwidth;
      if ($swidth>($gw-$curwidth)) $swidth=$gw-$curwidth;
      $dagraph .= "  <div class=\"untranslated\" style=\"width: $swidth"."px; left: $curwidth"."px;\"></div>\n";
      $curwidth += $swidth;
    }
    $dagraph .= "</div>";

    if (!$row['description']) $row['description'] = $row['id'];

    $ptrans = number_format($ptrans, 2) . "%";
    $xptrans = number_format($ptrans, 2);
    $pfuzzy = number_format($pfuzzy, 2) . "%";
    $puntrans = number_format($puntrans, 2) . "%";

    if ($rank % 2 == 0) $style = 'even';
    else $style = 'odd';
    if ($prevstate == $ptrans) $norank = '';
    else $norank = $rank.'.';
    $output .= 
      "<tr class=\"$style\"><td>$norank</td><td class=\"lang\">$row[description]</td>\n".
      "    <td>$ptrans <!-- ($row[translated]) --></td>\n".
      "    <!--<td>$pfuzzy  ($row[fuzzy])</td>-->\n".
      "    <!--<td>$puntrans  ($row[untranslated])</td>-->\n".
      "    <td>$schange</td>\n".
      "    <td>$dagraph</td>\n".
      "</tr>\n";
    $xpchange = number_format($pchange, 2);
    $XMLoutput .= "  <language rank=\"$norank\" translated=\"$xptrans\" changed=\"$xpchange\">$row[description]</language>\n";
  }
  $XMLoutput .= "</translation-stats>\n";
  $XMLoutput = str_replace("&aring;", "a", $XMLoutput);
  $output .= "</table>\n";
  print $output;
  $email = readin('email');
  if ($email) {
    print "<hr>Mailed translation report to: $email";

    mail("$email","Translation stats for $startdate to $enddate",$XMLoutput, "From: danilo@gnome.org");
  }
} else {
  $stselect = "<select size=\"4\" name=\"start\">\n";
  $endselect = "<select size=\"4\" name=\"end\">\n";
  $q = mysql_query("select distinct(date) date from stats where version='$version'");

  while ($q and $row = mysql_fetch_array($q)) {
    $stselect .= "  <option value=\"$row[date]\">$row[date]</option>\n";
    $endselect .= "  <option value=\"$row[date]\">$row[date]</option>\n";
  }
  $stselect .= "</select>\n";
  $endselect .= "</select>\n";

  $form = "<form method=\"post\">\n<table><tr><td>\n$stselect</td><td>\n$endselect</td></tr>\n".
    "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"Gimme\"></td></tr>\n".
    "</table>\n</form>\n";
  print $form;
}

if (strtolower($_SERVER['REQUEST_METHOD']) == 'post' or readin('end')) {

?>
<div style="width: 140px; position: fixed; right: 20px; bottom: 20px;">
<h2>Legend</h2>
<div class="legend" style="background: #448844;">translated messages</div>
<div class="legend" style="background: #55DD55;">change for better</div>
<div class="legend" style="background: #4444AA;">fuzzy messages</div>
<div class="legend" style="background: #FF4444; border-bottom: 1px solid black;">untranslated messages</div>
</div>
<?php
    }
?>
</div>
</body>
</html>
