Use custom style for buttons
[misc/hallinta-metro] / utils.php
index a9ae57e..7ad39ac 100644 (file)
--- a/utils.php
+++ b/utils.php
@@ -5,6 +5,8 @@ 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');
 
 function mk_tempdir()
 {
@@ -22,11 +24,26 @@ function rm_tempdir($dir)
   system(sprintf("rm -rf %s", $dir));
 }
 
+function latexspecialchars($string)
+{
+  $map = array(
+              "#"=>"\\#",
+              "$"=>"\\$",
+              "%"=>"\\%",
+              "&"=>"\\&",
+              "~"=>"\\~{}",
+              "_"=>"\\_",
+              "^"=>"\\^{}",
+              "\\"=>"\\textbackslash",
+              "{"=>"\\{",
+              "}"=>"\\}",
+              );
+  return preg_replace("/([\^\%~\\\\#\$%&_\{\}])/e", "\$map['$1']", $string);
+}
+
 function latex_encode($text)
 {
-  return str_replace(array("\\",'%'),
-                    array('\\','\%'),
-                    utf8_decode($text));
+  return latexspecialchars(utf8_decode($text));
 }
 
 function ordersatz_archive($id, $title, $filename)
@@ -126,13 +143,13 @@ function ordersatz_write($id, $dir, $send)
   fwrite($f, sprintf('\newcommand{\datum}{%d.%d.%d}'."\n", $d[2], $d[1], $d[0]));
   fclose($f);
 
-  $sql = sprintf("SELECT artnr,gang,metro_vpe.short AS vpname,menge,bezeichnung,hersteller,anzahl " .
+  $sql = sprintf("SELECT artnr,gang,metro_vpe.short AS vpname,menge,bezeichnung,hinweis,hersteller,anzahl " .
                 "FROM metro_ordersatz_pos " .
                 "JOIN metro_artikel ON artikel = metro_artikel.id " .
                 "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 gang,metro_abteilung.name,bezeichnung",
                 $id);
 
   $result = query_db($sql);
@@ -143,24 +160,36 @@ function ordersatz_write($id, $dir, $send)
   $f = fopen(FNAME_ITEMS, 'w');
 
   foreach ($result as $row) {
-    $bez = $row['bezeichnung'];
+    $bez = latex_encode($row['bezeichnung']);
     if (strlen($row['hersteller']))
-      $bez .= ' (' . $row['hersteller'] . ')';
+      $bez .= ' (' . latex_encode($row['hersteller']) . ')';
+    if (strlen($row['hinweis']))
+      $bez .= sprintf("\\newline\\textbf{%s}", latex_encode($row['hinweis']));
 
-    fwrite($f, sprintf('\order{%d}{%d}{%s %s}{%s}'."\n",
+    fwrite($f, sprintf('\order{%d}{%s}{%s %s}{%s}{%s}'."\n",
                       $row['anzahl'],
-                      $row['artnr'],
-                      $row['menge'],
-                      latex_encode($bez),
-                      latex_encode($row['vpname'])));
+                      $row['artnr'] ? (int)$row['artnr'] : '',
+                      latex_encode($row['menge']),
+                      $bez,
+                      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);
 
-  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 && " .
+                  "mail -s 'Probleme beim Compilieren des Ordersatzes %d' %s < %s && " .
+                  "sendfile -Q -c='Probleme beim Compilieren des Ordersatzes %d' %s %s",
+                  $fname,
+                  $id, ERROR_MAIL, $fname
+                  $id, $fname, ERROR_SAFT);
+    system($cmd);
     return "Probleme beim Compilieren des Ordersatzes";
+  }
 
   if (!file_exists('ordersatz.ps'))
     return "Postscript-Datei nicht erstellt";
@@ -171,6 +200,9 @@ function ordersatz_write($id, $dir, $send)
     if ($ret !== true)
       return $ret;
 
+    if (empty($satz[0]['email']))
+      $satz[0]['email'] = 'joey@infodrom.org';
+
     $cmd = sprintf("faxspool -f %s %s %s > /dev/null 2>&1",
                   $satz[0]['email'],
                   FAXNUMBER,
@@ -182,9 +214,9 @@ function ordersatz_write($id, $dir, $send)
                       $satz[0]['koch']);
 
     mail_attach($satz[0]['email'],
-               'Ordersatz ' . $satz[0]['name'],
+               mb_encode_mimeheader(utf8_decode('Ordersatz ' . $satz[0]['name']),'latin1'),
                $satz[0]['email'],
-               'Infodrom ' . $satz[0]['koch'],
+               mb_encode_mimeheader(utf8_decode('Infodrom ' . $satz[0]['koch']),'latin1'),
                array($dir . '/ordersatz.pdf'),
                $message);
 
@@ -194,4 +226,4 @@ function ordersatz_write($id, $dir, $send)
   return true;
 }
 
-?>
\ No newline at end of file
+?>