I get these labels for a doctor's office and they have to be printed. Printing is done via a custom C# Outlook right-click calendar menu add-in and so the local printers aren't used. Also, this PHP setup was established long before my time and isn't getting rewritten for the client.
The ArrivalHelper.php
system calls the script below, "PrintersMgmt.php" and then calls PinnaclePDO.php
to connect to a MySQL instance running on an Ubuntu server to find an office location (in this instance locID is 2) and then finds the printer in that MySQL table and what server (cc-fp2, in this instance) that hosts it and sends that information back to print.
I need to find a way to do this better. I thought about using JavaScript to open a print dialog box, but I have no idea how to implement that here. Can someone give a PHP noob some pointers?
<?php
class PrintersMgmt {
const PRINT_ALL_LABELS = 0x1;
const PRINT_ONE_LABEL = 0x10;
const PRINT_PASSPORT = 0x100;
const PRINT_REFERRAL = 0x1000;
const PRINTER_TYPE_LETTER = 1;
const PRINTER_TYPE_LABEL = 2;
const
ARRIVAL_URL = 'http://localhost/arrival.php';
const QUERY_GET_PRINTER_BY_LOC_TYPE =
'SELECT * FROM LocationPrinters
WHERE locID = :loc AND printerType = :type
ORDER BY printerPriority
LIMIT 1';
private $dbh;
public function __construct() {
$this->dbh = new PinnaclePDO();
}
public function getByLocationType( $loc, $type ) {
$stmt = $this->dbh->prepare( self::QUERY_GET_PRINTER_BY_LOC_TYPE );
$stmt->bindValue( ':loc', $loc );
$stmt->bindValue( ':type', $type );
$stmt->execute();
return $stmt->fetch();
}
public function printPassport( $locId, $hin, $date, $userName, $method ) {
$printFlags = $this->getPrintFlags($method);
$referral = ( ! ($printFlags & self::PRINT_REFERRAL) == 0 );
$passport = ( ! ($printFlags & self::PRINT_PASSPORT) == 0 );
$printer = $this->getByLocationType( $locId, 1 );
if ( $printer ) {
$cmd = '.\firefox.exe';
$args = sprintf(
' -print "%s?method=%s&hin=%s&user=%s&referral=%d&passport=%d"' .
' -printprinter "\\\\' . $printer[ 'printerHost' ] . '\\' . $printer[ 'printerName' ] .'"',
self::ARRIVAL_URL, 'listconfirmed',
$hin, urlencode( $userName ), $referral, $passport
);
if ( isset( $_GET[ 'debug' ] ) ) {
print "Print with args: '$cmd $args'<br/>";
}
chdir( "C:/Program Files/Mozilla Firefox2/" );
shell_exec($cmd.$args);
}
return true;
}
public function printLabel( $locId, $hin, $date, $method ) {
$printFlags = $this->getPrintFlags($method);
$printer = $this->getByLocationType( $locId, 2 );
$number = ( ( $printFlags & self::PRINT_ONE_LABEL ) == 0 ) ? 'all' : 'one' ;
$cmd = 'LabelPrinter.exe';
$args = sprintf(
' /print %s %s "%s" %s',
$hin, $date, $printer[ 'printerName' ], $number );
if ( isset( $_GET[ 'debug' ] ) ) {
print sprintf( 'Printing \'%s\' labels \'%s %s\'<br/>',
$number, $cmd, $args );
} else {
chdir( "C:/Program Files/(truncated)/(truncated) Tools" );
shell_exec( $cmd . $args);
}
return true;
}
public function getPrintFlags( $arriveMethod ) {
$printFlags = 0x1101;
switch ( $arriveMethod ) {
case 'arrive':
$printFlags =
( self::PRINT_ALL_LABELS
| self::PRINT_PASSPORT
| self::PRINT_REFERRAL );
break;
case 'reprintpassport':
$printFlags = self::PRINT_PASSPORT;
break;
case 'reprintreferral':
$printFlags = self::PRINT_REFERRAL;
break;
case 'reprintlabel':
$printFlags = self::PRINT_ONE_LABEL;
break;
default:
$printFlags = null;
}
return $printFlags;
}
}
?>