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;
173 print_r(file_get_contents($template));exit;
175 unset($cfg['template']);
177 // do we have a localized version???
178 $template_test = preg_replace('/\.xml$/', '-'.$office .'.xml', $template);
179 //$this->jerr($template_test);
180 if (file_exists($template_test)) {
181 $template = $template_test;
184 // template now == full path with .xml at the end..
188 if (!file_exists($template)) {
190 // see if exists in the database..
191 $r = DB_DAtaObject::factory('report');
194 if (!$r->get('report_name', $ocfg['template'])) {
195 $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain');
198 return $ocfg['template'];
204 // we have a file, and we need to sync it with database.
206 $mt = filemtime($template);
208 $r = DB_DAtaObject::factory('report');
211 if (!$r->get('report_name', $ocfg['template'])) {
213 $r = DB_DAtaObject::factory('report');
216 'report_name' => $ocfg['template'],
218 'report_source' => file_get_contents($template),
221 'report_loaddate' => date('Y-m-d H:i:s', $mt)
224 return $ocfg['template'];
226 // see if it needs updating..
227 if (strtotime($r->report_loaddate) < $mt) {
230 'report_source' => file_get_contents($template),
233 'report_loaddate' => date('Y-m-d H:i:s', $mt)
237 return $ocfg['template'];
241 // system is newer than ours..
243 return $ocfg['template'];