Handle artnr 4711 as special case items
[misc/hallinta-metro] / utils.php
index 9e5e005..1784acc 100644 (file)
--- a/utils.php
+++ b/utils.php
@@ -6,7 +6,17 @@ 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');
+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()
 {
@@ -26,19 +36,23 @@ function rm_tempdir($dir)
 
 function latexspecialchars($string)
 {
-  $map = array(
-              "#"=>"\\#",
-              "$"=>"\\$",
-              "%"=>"\\%",
-              "&"=>"\\&",
-              "~"=>"\\~{}",
-              "_"=>"\\_",
-              "^"=>"\\^{}",
-              "\\"=>"\\textbackslash",
-              "{"=>"\\{",
-              "}"=>"\\}",
-              );
-  return preg_replace("/([\^\%~\\\\#\$%&_\{\}])/e", "\$map['$1']", $string);
+  return preg_replace_callback("/([\^\%~\\\\#\$%&_\{\}])/",
+                              function($matches) {
+                                $map = array(
+                                             "#"=>"\\#",
+                                             "$"=>"\\$",
+                                             "%"=>"\\%",
+                                             "&"=>"\\&",
+                                             "~"=>"\\~{}",
+                                             "_"=>"\\_",
+                                             "^"=>"\\^{}",
+                                             "\\"=>"\\textbackslash",
+                                             "{"=>"\\{",
+                                             "}"=>"\\}",
+                                             );
+                                return $map[$matches[0]];
+                              },
+                              $string);
 }
 
 function latex_encode($text)
@@ -149,7 +163,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,bezeichnung",
+                "ORDER BY sort,gang,metro_abteilung.name,bezeichnung",
                 $id);
 
   $result = query_db($sql);
@@ -182,10 +196,10 @@ function ordersatz_write($id, $dir, $send)
   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 && " .
+                  "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, ERROR_MAIL, $fname,
+                  $id, $fname, ERROR_MAIL,
                   $id, $fname, ERROR_SAFT);
     system($cmd);
     return "Probleme beim Compilieren des Ordersatzes";
@@ -211,12 +225,12 @@ function ordersatz_write($id, $dir, $send)
 
     $message = sprintf("Hallo %s!\n\nDer angehängte Ordersatz wurde gerade an Metro Oldenburg verschickt.\n" .
                       "Eine Bestätigung über den Versand kommt per Mail.\n\n",
-                      $satz[0]['koch']);
+                      utf8_decode($satz[0]['koch']));
 
     mail_attach($satz[0]['email'],
-               mb_encode_mimeheader(utf8_decode('Ordersatz ' . $satz[0]['name']),'latin1'),
+               mb_encode_mimeheader('Ordersatz ' . $satz[0]['name'],'latin1'),
                $satz[0]['email'],
-               mb_encode_mimeheader(utf8_decode('Infodrom ' . $satz[0]['koch']),'latin1'),
+               mb_encode_mimeheader('Infodrom ' . $satz[0]['koch'],'latin1'),
                array($dir . '/ordersatz.pdf'),
                $message);