4 require_once 'Pman.php';
6 class Pman_Xtuple_Print extends Pman
11 $au = $this->getAuthUser();
13 $this->jerr("access denied");
27 'template' => $_REQUEST['template']
29 if (isset($_REQUEST['param'])) {
30 $args['param'] = $_REQUEST['param'];
32 $this->toPdf($args, $_REQUEST['filename']);
37 * run rptrender and output the file..
39 * @param array $cfg configuration - param=> ... => loadfromdb=>
44 function toPdf($cfg, $name)
46 if (isset($cfg['template'])) {
47 $cfg['template'] = rtrim($cfg['template'],'-');
51 $do = DB_DataObject::Factory('invchead');
53 $dsn = $do->getDatabaseConnection()->dsn;
55 $office = substr($do->database(),-2);
58 $fn = tempnam(sys_get_temp_dir(),'print').'.pdf';
61 'databaseURL'=> 'psql://' . $dsn["hostspec"] .'/'. $dsn["database"] .':5432',
62 'username' => $dsn["username"],
63 'passwd' => $dsn["password"],
68 //'param' => "invchead_id:integer='{$this->invchead_id}'",
69 //'loadfromdb' => 'Invoice',
74 if (isset($cfg['template'])) {
75 $template = realpath(dirname(__FILE__).'/report_templates'). '/'.$cfg['template']. '.xml';
76 unset($cfg['template']);
78 // do we have a localized version???
79 $template_test = preg_replace('/\.xml$/', '-'.$office .'.xml', $template);
80 //$this->jerr($template_test);
81 if (file_exists($template_test)) {
82 $template = $template_test;
84 if (!file_exists($template)) {
86 // see if exists in the database..
87 $r = DB_DAtaObject::factory('report');
90 if (!$r->get('report_name', $ocfg['template'])) {
91 $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain');
95 $args['loadfromdb'] = escapeshellarg($ocfg['template']);
101 $args['loadfromdb'] = escapeshellarg($this->syncDB($cfg));
102 unset($cfg['template']);
103 // -loadfromdb={report_name}
105 $args = array_merge($cfg,$args);
108 require_once 'System.php';
109 $xvfb = System::which('xvfb-run');
111 $this->jerr("no xvfb-run found", array(), 'text/plain');
115 $rpt = System::which('rptrender');
117 $this->jerr("no report renderer", array(), 'text/plain');
119 $cmd[] = "$xvfb --auto-servernum";
125 foreach($args as $k=>$v) {
128 $cmd[] = '-'.$k . ($vv === true ? '' : '='. $vv);
132 $cmd[] = '-'.$k . ($v === true ? '' : '='. $v);
135 $cmd = implode(' ', $cmd) ;
136 //$cmd = implode(' ', $cmd) . ' ' . (strlen($ocfg['template']) ? escapeshellarg($template) : '');
137 if (isseT($_REQUEST['_debug'])) {
146 if (!file_exists($fn)) {
147 $this->jerr("File creation failed: " . $cmd, array(), 'text/plain');
150 require_once 'File/Convert.php';
151 $x = new File_Convert($fn, "application/pdf");
152 $x->convert("application/pdf");
153 $x->serve('attachment', $name .'.pdf', true); // delte after...
157 function syncDB($cfg)
159 $roo = HTML_FlexyFramework::get()->page;
161 $do = DB_DataObject::Factory('invchead');
163 $office = substr($do->database(),-2);
168 if (isset($cfg['template'])) {
170 $template = '/report_templates'. '/'.$cfg['template']. '.xml';
171 print_r(pathinfo($template));exit;
172 print_r(file_get_contents($template));exit;
174 unset($cfg['template']);
176 // do we have a localized version???
177 $template_test = preg_replace('/\.xml$/', '-'.$office .'.xml', $template);
178 //$this->jerr($template_test);
179 if (file_exists($template_test)) {
180 $template = $template_test;
183 // template now == full path with .xml at the end..
187 if (!file_exists($template)) {
189 // see if exists in the database..
190 $r = DB_DAtaObject::factory('report');
193 if (!$r->get('report_name', $ocfg['template'])) {
194 $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain');
197 return $ocfg['template'];
203 // we have a file, and we need to sync it with database.
205 $mt = filemtime($template);
207 $r = DB_DAtaObject::factory('report');
210 if (!$r->get('report_name', $ocfg['template'])) {
212 $r = DB_DAtaObject::factory('report');
215 'report_name' => $ocfg['template'],
217 'report_source' => file_get_contents($template),
220 'report_loaddate' => date('Y-m-d H:i:s', $mt)
223 return $ocfg['template'];
225 // see if it needs updating..
226 if (strtotime($r->report_loaddate) < $mt) {
229 'report_source' => file_get_contents($template),
232 'report_loaddate' => date('Y-m-d H:i:s', $mt)
236 return $ocfg['template'];
240 // system is newer than ours..
242 return $ocfg['template'];