Adds ability to generate graphs for custom areas

This commit adds the ability to generate individual PNG and SVG graphs
for custom areas, defined as the union of several "Àrees Bàsiques de
Salut" (ABS) in the config/customAreas.php file.

Change-Id: I27f34b4a8f520a38c55eed224554b470c5ba2938
diff --git a/cron/generateCustomData.php b/cron/generateCustomData.php
new file mode 100644
index 0000000..5be2bc0
--- /dev/null
+++ b/cron/generateCustomData.php
@@ -0,0 +1,63 @@
+<?php
+if (php_sapi_name() != "cli")
+  exit();
+
+require_once(__DIR__."/../config/config.php");
+require_once(__DIR__."/includes/generation.php");
+
+// Funció que retorna el nombre d'habitants a les ABS donades via una
+// API de la Generalitat de Catalunya
+function habitants($abs) {
+  $data = query("SELECT sum(poblacio_oficial) AS habitants
+  WHERE
+    abs_codi in(".implode(",", array_map(function($abs) { return "'".$abs."'"; }, $abs)).") AND
+    any = 2020", "ftq4-h9vk");
+
+  return ($data[0]["habitants"] ?? null);
+}
+
+if (isset($conf["customAreas"])) {
+  // A cada ciutat
+  foreach ($conf["customAreas"] as $area) {
+    // Si no hi ha cap ABS configurada no fem res
+    if (count($area["abs"]) == 0) {
+      echo "[Warning] There aren't any ABS configured for ".$area["name"].".\n";
+      continue;
+    }
+
+    // Demanem una llista del nombre de casos cada dia
+    $data = query("SELECT data, sum(numcasos) AS sum_numcasos
+    WHERE
+      resultatcoviddescripcio = 'Positiu PCR' AND
+      abscodi in(".implode(",", array_map(function($abs) { return "'".$abs."'"; }, $area["abs"])).")
+    GROUP BY data
+    ORDER BY data ASC
+    LIMIT 50000");
+
+    // Obtenim el nombre d'habitants a les ABS
+    $habitants = habitants($area["abs"]);
+    if ($habitants === null) {
+      echo "[Fatal error] Failed getting population for ".$area["name"].".\n";
+      continue;
+    }
+
+    // Generem les dades
+    $summary = generateSummary($data, $habitants);
+
+    // Les escribim en un fitxer
+    $file = tmpfile();
+    $fileName = stream_get_meta_data($file)['uri'];
+
+    foreach ($summary as $row)
+      fwrite($file, $row["data"]." ".$row["ia14"]." ".$row["rho7"]."\n");
+
+    // Cridem al gnuplot perquè generi la gràfica
+    shell_exec("gnuplot -c generateCustomGraph.gnu \"".escapeshellcmd($area["name"])."\" \"".escapeshellcmd($area["codename"])."\" \"".escapeshellcmd($fileName)."\"");
+    rename("/tmp/covid19graphgenerator-area-".$area["codename"]."-graph.png", __DIR__."/../output/area-".$area["codename"]."-graph.png");
+    rename("/tmp/covid19graphgenerator-area-".$area["codename"]."-graph.svg", __DIR__."/../output/area-".$area["codename"]."-graph.svg");
+
+    fclose($file);
+  }
+} else {
+  echo "[Warning] The config/customAreas.php file doesn't define the customAreas field.";
+}