Add sort column to improve listing
[misc/hallinta-metro] / utils.php
index 5bd6ce4..8e53ad0 100644 (file)
--- a/utils.php
+++ b/utils.php
@@ -5,6 +5,18 @@ define('TEMPLATE', 'ordersatz.tex');
 define('FNAME_VARS', 'ordersatz_vars.tex');
 define('FNAME_ITEMS', 'ordersatz_items.tex');
 define('FAXNUMBER','2002-364');
+define('ERROR_SAFT','joey@finlandia');
+define('ERROR_MAIL','joey@finlandia.infodrom.org');
+
+function is_done($id)
+{
+  global $db;
+
+  $query = sprintf('SELECT count(*) AS count FROM metro_ordersatz WHERE id = %d AND done IS NULL', $id);
+  $sth = $db->query($query);
+  $row = $sth->fetch();
+  return $row['count'] == 0;
+}
 
 function mk_tempdir()
 {
@@ -147,7 +159,7 @@ function ordersatz_write($id, $dir, $send)
                 "JOIN metro_abteilung ON abteilung = metro_abteilung.id " .
                 "JOIN metro_vpe ON vpe = metro_vpe.id " .
                 "WHERE ordersatz = %d AND anzahl IS NOT NULL " .
-                "ORDER BY gang,metro_abteilung.name",
+                "ORDER BY sort,gang,metro_abteilung.name,bezeichnung",
                 $id);
 
   $result = query_db($sql);
@@ -164,20 +176,30 @@ function ordersatz_write($id, $dir, $send)
     if (strlen($row['hinweis']))
       $bez .= sprintf("\\newline\\textbf{%s}", latex_encode($row['hinweis']));
 
-    fwrite($f, sprintf('\order{%d}{%s}{%s %s}{%s}'."\n",
+    fwrite($f, sprintf('\order{%d}{%s}{%s %s}{%s}{%s}'."\n",
                       $row['anzahl'],
                       $row['artnr'] ? (int)$row['artnr'] : '',
-                      $row['menge'],
+                      latex_encode($row['menge']),
                       $bez,
-                      latex_encode($row['vpname'])));
+                      latex_encode($row['vpname']),
+                      latex_encode($row['gang'])));
   }
 
   fclose($f);
 
-  system("latex ordersatz > ordersatz.out 2>&1 && dvips ordersatz >> ordersatz.out 2>&1 && ps2pdf ordersatz.ps", $ret);
+  system("latex ordersatz > ordersatz.out 2>&1 && dvips ordersatz >> ordersatz.out 2>&1 && pdflatex ordersatz >> ordersatz.out", $ret);
 
-  if ($ret !== 0)
+  if ($ret !== 0) {
+    $fname = sprintf("ordersatz-%d-%d.tar.bz2", $id, time());
+    $cmd = sprintf("tar cfj %s ordersatz.tex ordersatz_vars.tex ordersatz_items.tex ordersatz.out && " .
+                  "mpack -s 'Probleme beim Compilieren des Ordersatzes %d' -d ordersatz.out %s %s && " .
+                  "sendfile -Q -c='Probleme beim Compilieren des Ordersatzes %d' %s %s",
+                  $fname,
+                  $id, $fname, ERROR_MAIL,
+                  $id, $fname, ERROR_SAFT);
+    system($cmd);
     return "Probleme beim Compilieren des Ordersatzes";
+  }
 
   if (!file_exists('ordersatz.ps'))
     return "Postscript-Datei nicht erstellt";
@@ -214,4 +236,4 @@ function ordersatz_write($id, $dir, $send)
   return true;
 }
 
-?>
\ No newline at end of file
+?>