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);
136 $cmd = implode(' ', $cmd) ;
137 //$cmd = implode(' ', $cmd) . ' ' . (strlen($ocfg['template']) ? escapeshellarg($template) : '');
138 if (isseT($_REQUEST['_debug'])) {
147 if (!file_exists($fn)) {
148 $this->jerr("File creation failed: " . $cmd, array(), 'text/plain');
151 require_once 'File/Convert.php';
152 $x = new File_Convert($fn, "application/pdf");
153 $x->convert("application/pdf");
154 $x->serve('attachment', $name .'.pdf', true); // delte after...
158 function syncDB($cfg)
160 $dir = HTML_FlexyFramework::get()->Pman;
162 $do = DB_DataObject::Factory('invchead');
164 $office = substr($do->database(),-2);
169 if (isset($cfg['template'])) {
171 $template = realpath(dirname(__FILE__).'/report_templates'). '/'.$cfg['template']. '.xml';
173 if(isset($dir['report_template_dir'])){
174 $template = realpath($dir['report_template_dir']). '/'.$cfg['template']. '.xml';
177 unset($cfg['template']);
179 // do we have a localized version???
180 $template_test = preg_replace('/\.xml$/', '-'.$office .'.xml', $template);
181 //$this->jerr($template_test);
182 if (file_exists($template_test)) {
183 $template = $template_test;
186 // template now == full path with .xml at the end..
190 if (!file_exists($template)) {
192 // see if exists in the database..
193 $r = DB_DAtaObject::factory('report');
196 if (!$r->get('report_name', $ocfg['template'])) {
197 $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain');
200 return $ocfg['template'];
206 // we have a file, and we need to sync it with database.
208 $mt = filemtime($template);
210 $r = DB_DAtaObject::factory('report');
213 if (!$r->get('report_name', $ocfg['template'])) {
215 $r = DB_DAtaObject::factory('report');
218 'report_name' => $ocfg['template'],
220 'report_source' => file_get_contents($template),
223 'report_loaddate' => date('Y-m-d H:i:s', $mt)
226 return $ocfg['template'];
228 // see if it needs updating..
229 if (strtotime($r->report_loaddate) < $mt) {
232 'report_source' => file_get_contents($template),
235 'report_loaddate' => date('Y-m-d H:i:s', $mt)
239 return $ocfg['template'];
243 // system is newer than ours..
245 return $ocfg['template'];