-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbartender.class.php
101 lines (90 loc) · 2.93 KB
/
bartender.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
class BarTenderException extends Exception {}
class BarTender {
private $label;
private $printer;
private $label_job_count;
private $xml;
/**
* @param string $label A full path (including filename,) to a BarTender Label (*.xxx) file.
* @param string $printer The name of the printer to which the label will be sent to print.
*/
public function __construct($label, $printer) {
$this->setLabel($label);
$this->setPrinter($printer);
}
/**
* @param string $printer The name of the printer to which the label will be sent to print.
* @return boolean True on success, Exception thrown on failure
* @throws BarTenderException Thrown when $printer is not a string
*/
public function setPrinter($printer) {
if(!is_string($printer)) {
throw new BarTenderException('Printer passed must be a string, encountered `' . gettype($printer) . '` instead');
}
$this->printer = $printer;
return True;
}
/**
* @param string $label A full path (including filename,) to a BarTender Label (*.xxx) file.
* @return boolean True on success, Exception thrown on failure
* @throws BarTenderException Thrown when $label is not a string
*/
public function setLabel($label) {
if(!is_string($label)) {
throw new BarTenderException('Label passed must be a string, encountered `' . gettype($label) . '` instead');
}
$this->label = $label;
return True;
}
/**
* @return string The label to be printed
*/
public function getLabel() {
return $this->label;
}
/**
* @return string The name of the printer to which the label will be sent to print.
*/
public function getPrinter() {
return $this->printer;
}
public function generateToReturn($label_jobs) {
return $this->_generate($label_jobs);
}
public function generateToFile($label_jobs, $filename) {
$xml = $this->_generate($label_jobs);
$file_pointer = fopen($filename, 'wb');
fwrite($file_pointer, $xml);
fclose($file_pointer);
}
private function _generate($label_jobs) {
$this->label_job_count = 0;
$xml = '<?xml version="1.0" encoding="utf-8"?>' . "\r\n";
$xml .= '<XMLScript Version="2.0">' . "\r\n";
foreach($label_jobs as $label_job) {
$this->label_job_count++;
$xml .= "\r\n";
$xml .= $this->_generateEntry($label_job);
}
$xml .= '</XMLScript>';
return $xml;
}
private function _generateEntry($label_job) {
$xml = '';
$xml .= ' <Command Name="Job' . $this->label_job_count . '">' . "\r\n";
$xml .= ' <Print>' . "\r\n";
$xml .= ' <Format>' . $this->label . '</Format>' . "\r\n";
$xml .= ' <PrintSetup>' . "\r\n";
$xml .= ' <Printer>' . $this->printer . '</Printer>' . "\r\n";
$xml .= ' </PrintSetup>' . "\r\n";
foreach($label_job as $var_name => $var_value) {
$xml .= ' <NamedSubString Name="' . $var_name . '">' . "\r\n";
$xml .= ' <Value>' . $var_value . '</Value>' . "\r\n";
$xml .= ' </NamedSubString>' . "\r\n";
}
$xml .= ' </Print>' . "\r\n";
$xml .= ' </Command>' . "\r\n";
return $xml;
}
}