pica.php

Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of the web-based "Application Form for New Users" 
00004 // of University Library Braunschweig, Germany
00005 //
00006 // Copyright (C) 2004 University Library Braunschweig, Germany
00007 
00018 
00019 require_once('../text.php');
00020 require_once('../util.php');
00021 
00032 
00033 function export_to_pica($dblink, $id, $barcode) {
00034         global $text;
00035         global $fields;
00036         global $iln;
00037         global $pica_export_command;
00038 
00039 // default values 
00040 
00041         $pica = array(
00042                 "a000" => $iln,         // ILN
00043                 "a100" => "U",          // Update       
00044                 "a104" => "0",          // user status (loan allowed)
00045                 "a111" => "1",          // Nummer der Mahnadresse
00046                 "a200" => "U",          // Update
00047                 "a201" => "001",        // Abteilungsgruppen-Nummer
00048                 "a300" => "U",          // Update
00049                 "a301" => "1",          // 1: extern, 0: intern
00050                 "b400" => "U"           // Update
00051         ); 
00052 
00053 // query database, assign results to $pica[] array
00054 
00055         foreach ($fields as $f) {
00056 
00057                 $q = (isset($f["pica_query"])) ? $f["pica_query"] : $f["query"];
00058                 $r = db_query_mysql($dblink, $q, array("@id@" => $id));
00059 
00060                 $k = (isset($f["pica_field"])) ? $f["pica_field"] : "" ;
00061 
00062                 if ((!empty($k)) and (!empty($r)))  
00063                         $pica[$k] .=  $r[1][0] . " ";
00064         }
00065 
00066 // strip trailing blanks
00067 
00068         foreach ($fields as $f) {
00069                 $k = (isset($f["pica_field"])) ? $f["pica_field"] : "" ;
00070                 if (!empty($k))
00071                         $pica[$k] = preg_replace("/ $/","", $pica[$k]);
00072         }
00073 
00074 // do postprocessing
00075 
00076 // date of creation
00077         $pica["a001"] = strftime('%d-%m-%Y');   
00078 
00079 // barcode
00080 
00081         if (!empty($barcode))
00082                 $pica["a102"] = $barcode;
00083 
00084 // create serial number
00085 
00086         if (!isset($pica["a114"]) or ($pica["a114"] == "")) {
00087                 $qs = array(
00088                         "lock tables serial_number write ", 
00089                         "update serial_number set number=0, " .
00090                         " time=CURDATE() where YEAR(time) != YEAR(CURDATE()) ",
00091                         " update serial_number set number=number+1 ",
00092                         " select DATE_FORMAT(time,'%y'), number " . 
00093                         " from serial_number "
00094                 );
00095 
00096                 foreach ($qs as $q) 
00097                         $r = db_query_mysql($dblink,$q, array());
00098 
00099                 if (!empty($r))   {
00100                         $pica["a114"]  = $r[1][0] . '/' . $r[1][1];
00101                 }
00102 
00103                 db_query_mysql($dblink,"unlock tables", array());
00104 
00105         }
00106 
00107 
00108 // fix registration number (student id, or Nota Bene ID) 
00109 
00110         $q = $fields["usertype"]["query"];
00111         $r = db_query_mysql($dblink, $q, array('@id@' => $id));
00112 
00113         if (!empty($r))   {
00114                 $usertype = $r[1][0];
00115 
00116                 if ($usertype == 2)
00117                         $pica["a101"] = "FH" . $pica["a101"]; 
00118                 else if ($usertype == 3)
00119                         $pica["a101"] = "HBK" . $pica["a101"]; 
00120                 else if ($usertype >= 4)
00121                         $pica["a101"] = "NB" . 
00122                                 strtr($pica["a114"],array( "/" => "" )) ; 
00123         }
00124 
00125 // check reg no
00126 
00127 if (!check_registration_number($pica["a101"])) {
00128         $kw = array ( 
00129                 '@notabene@' => "",  
00130                 '@bodyattr@' => "",
00131                 '@url-de@' => "index.php?lang=de", 
00132                 '@url-en@' => "index.php?lang=en",
00133                 '@regno@' => $pica['a101']); 
00134 
00135         print_header($kw);
00136         print strtr($text['conflict_regno'],$kw);
00137         print_footer($kw);
00138         exit(0);
00139 }
00140 
00141 // write serial number into database
00142 
00143 $q = $fields["notabene"]["update"];
00144 $kw = array( '@id@' => $id, '@val@' => $pica["a114"]);
00145 
00146 db_query_mysql($dblink, $q, $kw);
00147 
00148 
00149 // determine whether we've got a second address
00150 // create header if necessary
00151 
00152         $second_address = FALSE;
00153 
00154         foreach ( $pica as $k => $v ) {
00155                 if (strpos($k,"b3") === 0 and ($v != "") )  {
00156                         $second_address = TRUE;
00157                 }
00158         }
00159 
00160         if ($second_address) {
00161                 $pica["b300"] = "U";
00162                 $pica["b301"] = "2";
00163         }
00164 
00165 // create export file
00166 
00167         ksort($pica);           // sort by key
00168         reset($pica);
00169 
00170         $export = "";
00171 
00172         foreach ($pica as $k => $v) {
00173                 $k = preg_replace("/^[a-zA-Z]/", "", $k);
00174                 if ($v != "")
00175                         $export .= "$k $v\n";
00176         }
00177 
00178 
00179 // do character translation for german umlauts
00180 
00181         $export = strtr($export,"ÄÖÜäöüß", "\301\302\303\321\322\323\276");
00182                 
00183 // run export command
00184 
00185 // print "<pre>$export</pre>";
00186         ($handle = popen($pica_export_command, "w")) or 
00187                 error_msg("could not run command: $pica_export_command");
00188         fwrite($handle, $export) or error_msg("pipe broken");   
00189         return pclose($handle); 
00190 }
00191 
00192 
00213 
00214 function check_registration_number($regnum)  {
00215 
00216 global $LBSServer;
00217 global $LBSDB;
00218 global $LBSUser;
00219 global $LBSPw;
00220 global $iln;
00221 
00222 ($lnk = @ sybase_pconnect($LBSServer,$LBSUser,$LBSPw)) or error_msg("SYBASE: pconnect failed");
00223 
00224 (@ sybase_select_db($LBSDB,$lnk)) or error_msg("SYBASE: database $LBSDB not found");
00225 
00226 $q = "select * from borrower where " . "registration_number = '@regnum@' and iln = $iln";
00227 // . "registration_number = '@regnum@'";
00228 
00229 
00230 $regnum = strtr($regnum, array( "'" => "''", "%" => "" ));
00231 $q = strtr($q, array( "@regnum@" => $regnum));
00232 
00233 // print $q;
00234 
00235 ( $res = sybase_query($q, $lnk) ) or error_msg("SYBASE: database query failed");
00236 
00237 return (($res) and (sybase_num_rows($res) > 0)) ? FALSE : TRUE;
00238 }
00239 
00254 
00255 function check_for_doubles($dblink, $id)  {
00256 global $fields;
00257 
00258 global $LBSServer;
00259 global $LBSDB;
00260 global $LBSUser;
00261 global $LBSPw;
00262 global $iln;
00263 
00264 
00265 $birthday = "0000-00-00";
00266 
00267 $q =  $fields["last_name"]["query"];
00268 $r = db_query_mysql($dblink, $q, array('@id@' => $id));
00269 
00270 $name = (isset($r[1][0])) ? $r[1][0] : "";
00271 
00272 $q =  $fields["birthday"]["query"];
00273 $r = db_query_mysql($dblink, $q, array('@id@' => $id));
00274 
00275 $birthday = (isset($r[1][0])) ? $r[1][0] : "";
00276 
00277 if (empty($name) or empty($birthday))
00278         return FALSE;
00279 
00280 list ($year,$month,$day)  = split('-', $birthday);
00281 
00282 
00283 $months_by_name = array (
00284         "01" => "Jan", "02" => "Feb", "03" => "Mar", "04" => "Apr",
00285         "05" => "May", "06" => "Jun", "07" => "Jul", "08" => "Aug",
00286         "09" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec" );
00287 
00288 $users = array();
00289 
00290 ($lnk = @ sybase_pconnect($LBSServer,$LBSUser,$LBSPw)) or error_msg("SYBASE: pconnect failed");
00291 
00292 (@ sybase_select_db($LBSDB,$lnk)) or error_msg("SYBASE: database $LBSDB not found");
00293 
00294 
00295 // extract last name
00296 
00297 $users = array();
00298 
00299 $q = "select name,first_name_initials_prefix,gender,date_of_birth," . 
00300      "borrower_bar,borrower_status from borrower where " .  
00301      "LOWER(name) LIKE '%@name@%' and " .
00302      "date_of_birth = '@birth@' and iln = $iln";
00303 
00304 $birth = $months_by_name[$month] . " " . $day . " " . $year;
00305 $names = explode(" ", $name);
00306 $name  = $names[count($names) - 1];
00307 $name = strtr($name, array( "'" => "''", "%" => "" ));
00308 
00309 // umlaut translation
00310 $name = strtolower(strtr($name,"ÄÖÜäöüß", "_______"));
00311 
00312 $q = strtr($q, array( "@name@" => $name, "@birth@" => $birth ));
00313 
00314 ( $erg = sybase_query($q, $lnk) ) or error_msg("SYBASE: database query failed");
00315 
00316 
00317 if ($erg) {
00318      $i = 0;
00319         
00320       while ($row = sybase_fetch_array($erg)) {
00321 
00322                 // umlaut back-translation
00323 
00324                 $row["name"] = strtr($row["name"],
00325                                      "\301\302\303\321\322\323\276",
00326                                      "ÄÖÜäöüß"
00327                                 );
00328                 $row["first_name_initials_prefix"] = strtr(
00329                                      $row["first_name_initials_prefix"],
00330                                      "\301\302\303\321\322\323\276",
00331                                      "ÄÖÜäöüß"
00332                                  );
00333 
00334                 $users[$i] = $row;
00335                 $i++;
00336       }
00337 }
00338 
00339 
00340 sybase_close($lnk);
00341 
00342 return $users;
00343 }
00344 
00345 ?>

Generated on Mon Aug 28 09:46:09 2006 for anmeldung by  doxygen 1.4.7