Improve LaTeX code and add corridor number for all items
[misc/hallinta-metro] / utils.php
index 3476ccf..016f15b 100644 (file)
--- a/utils.php
+++ b/utils.php
@@ -22,11 +22,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 +141,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,16 +158,19 @@ 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}{%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($bez),
-                      latex_encode($row['vpname'])));
+                      latex_encode($row['menge']),
+                      $bez,
+                      latex_encode($row['vpname']),
+                      latex_encode($row['gang'])));
   }
 
   fclose($f);
@@ -187,7 +205,7 @@ function ordersatz_write($id, $dir, $send)
     mail_attach($satz[0]['email'],
                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);
 
@@ -197,4 +215,4 @@ function ordersatz_write($id, $dir, $send)
   return true;
 }
 
-?>
\ No newline at end of file
+?>