104 lines
2.3 KiB
PHP
104 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Glabels;
|
|
|
|
use SimpleXMLElement;
|
|
|
|
class LabelMerger
|
|
{
|
|
/** @var GlabelsLabel $label */
|
|
private $label;
|
|
/** @var array $rows */
|
|
private $rows = [];
|
|
|
|
private $fields = [];
|
|
|
|
public function __construct(GLabelsLabel $label)
|
|
{
|
|
$this->label = $label;
|
|
try {
|
|
$this->fields = $label->getMergeFields();
|
|
} catch (\Exception $e) {
|
|
$this->fields = [];
|
|
}
|
|
}
|
|
|
|
public function getSupportedFields()
|
|
{
|
|
return $this->fields;
|
|
}
|
|
|
|
public function addRow(array $data)
|
|
{
|
|
if (count($this->fields) > 0) {
|
|
$row = [];
|
|
foreach ($this->fields as $field) {
|
|
$row[] = array_key_exists($field, $data)?$data[$field]:null;
|
|
}
|
|
$this->rows[] = $row;
|
|
} else {
|
|
$this->rows[] = $data;
|
|
}
|
|
}
|
|
|
|
public function mergeToPdf(string $output)
|
|
{
|
|
$tempfile = sprintf("/tmp/%s.tmp", uniqid("php_glabels_"));
|
|
|
|
$fd = fopen($tempfile, "w");
|
|
if (count($this->fields)>0) {
|
|
fputcsv($fd, $this->fields);
|
|
} else {
|
|
fputcsv($fd, array_keys(reset($this->rows)));
|
|
}
|
|
foreach ($this->rows as $row) {
|
|
fputcsv($fd, $row);
|
|
}
|
|
fclose($fd);
|
|
|
|
$filename = $this->label->getFilename();
|
|
$mergeCmdline = sprintf(
|
|
"glabels-3-batch -i %s -o %s %s",
|
|
escapeshellarg($tempfile),
|
|
escapeshellarg($output),
|
|
escapeshellarg($filename)
|
|
);
|
|
|
|
file_put_contents("/tmp/label.cmd", $mergeCmdline);
|
|
|
|
exec($mergeCmdline, $out, $ret);
|
|
if ($ret != 0) {
|
|
throw new GlabelsException("Merge failed");
|
|
}
|
|
|
|
unlink($tempfile);
|
|
|
|
/*
|
|
$template = $this->label->getTemplateInformation();
|
|
|
|
$args = [
|
|
"-density 300",
|
|
escapeshellarg($tempout)
|
|
];
|
|
if ($template['rotate']) {
|
|
$args[] = "-rotate 270";
|
|
}
|
|
$args[] = escapeshellarg($output);
|
|
|
|
$convertCmdline = sprintf("convert %s", join(" ",$args));
|
|
|
|
echo "\$ ".$convertCmdline."\n";
|
|
|
|
exec($convertCmdline, $out, $ret);
|
|
|
|
if ($ret != 0) {
|
|
throw GlabelsException::ConvertCommandFailed();
|
|
}
|
|
|
|
unlink($tempout);
|
|
*/
|
|
}
|
|
|
|
}
|
|
|