Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Print.php
1 <?php
2
3
4 require_once 'Pman.php';
5
6 class Pman_Xtuple_Print extends Pman
7 {
8     function getAuth()
9     {
10         parent::getAuth();
11         $au = $this->getAuthUser();
12         if (!$au) {
13             $this->jerr("access denied");
14         }
15         
16     }
17     
18     
19     function get()
20     {
21         // accept
22         // param : 
23         // template
24         // filemanem
25         
26         $args = array(
27             'template' => $_REQUEST['template']
28         );
29         if (isset($_REQUEST['param'])) {
30             $args['param'] =  $_REQUEST['param'];
31         }
32         $this->toPdf($args, $_REQUEST['filename']);
33          
34     }
35     
36     /**
37      * run rptrender and output the file..
38      *
39      * @param array $cfg configuration - param=> ... => loadfromdb=>
40      * @param filename
41      * 
42      */
43     
44     function toPdf($cfg, $name)
45     {
46         if (isset($cfg['template'])) {
47             $cfg['template'] = rtrim($cfg['template'],'-');
48         }
49         $ocfg = $cfg;
50         
51         $do = DB_DataObject::Factory('invchead');
52         
53         $dsn  = $do->getDatabaseConnection()->dsn;
54         
55         $office = substr($do->database(),-2);
56         
57
58         $fn = tempnam(sys_get_temp_dir(),'print').'.pdf';
59         
60         $args = array(
61             'databaseURL'=> 'psql://' . $dsn["hostspec"] .'/'. $dsn["database"] .':5432',
62             'username' =>  $dsn["username"],
63             'passwd' => $dsn["password"],
64             'pdf' => true,
65             'outpdf' => $fn,
66             
67             
68             //'param' => "invchead_id:integer='{$this->invchead_id}'",
69             //'loadfromdb' => 'Invoice',
70             'close' => true
71         );
72          /*
73         $template  = '';
74         if (isset($cfg['template'])) {
75             $template = realpath(dirname(__FILE__).'/report_templates'). '/'.$cfg['template']. '.xml';
76             unset($cfg['template']);
77         }
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; 
83         }
84         if (!file_exists($template)) {
85             
86             // see if exists in the database..
87             $r = DB_DAtaObject::factory('report');
88             
89             
90             if (!$r->get('report_name', $ocfg['template'])) {
91                 $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain');
92             }
93             
94             
95             $args['loadfromdb'] = escapeshellarg($ocfg['template']);
96             $template = '';
97             
98         }
99         */
100          
101         $args['loadfromdb'] = escapeshellarg($this->syncDB($cfg));
102         unset($cfg['template']);
103         // -loadfromdb={report_name}
104         
105         $args = array_merge($cfg,$args);
106         
107         $cmd = array();
108         require_once 'System.php';
109         $xvfb = System::which('xvfb-run');
110         if (!$xvfb ) {
111             $this->jerr("no xvfb-run found", array(), 'text/plain');
112         }
113         
114         
115         $rpt = System::which('rptrender');
116         if (!$rpt) {
117             $this->jerr("no report renderer", array(), 'text/plain');
118         }
119         $cmd[] = "$xvfb --auto-servernum";
120
121         
122         
123         $cmd[] = $rpt;
124
125         foreach($args as $k=>$v) {
126             if (is_array($v)) {
127                 foreach($v as $vv) {
128                     $cmd[] = '-'.$k . ($vv === true   ? '' : '='. $vv);
129                 }
130                 continue;
131             }
132             $cmd[] = '-'.$k . ($v === true   ? '' : '='. $v);
133             
134         }
135         
136         $cmd = implode(' ', $cmd) ;
137         //$cmd = implode(' ', $cmd) . ' ' . (strlen($ocfg['template']) ? escapeshellarg($template) : '');
138         if (isseT($_REQUEST['_debug'])) {
139             echo $cmd;
140             exit;
141         }
142         
143 //        echo $cmd;exit;
144         $res = `$cmd`;
145         // var_dump( $res);
146         
147         if (!file_exists($fn)) {
148             $this->jerr("File creation failed:  " . $cmd, array(), 'text/plain');
149         }
150         
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...
155         exit;
156         
157     }
158     function syncDB($cfg) 
159     {
160         $dir = HTML_FlexyFramework::get()->Pman;
161         
162         $do = DB_DataObject::Factory('invchead');
163         
164         $office = substr($do->database(),-2);
165        
166         
167         $ocfg = $cfg;
168         $template  = '';
169         if (isset($cfg['template'])) {
170             
171             $template = realpath(dirname(__FILE__).'/report_templates'). '/'.$cfg['template']. '.xml';
172             
173             if(isset($dir['report_template_dir'])){
174                 $template = realpath($dir['report_template_dir']). '/'.$cfg['template']. '.xml';
175             }
176             
177             unset($cfg['template']);
178         }
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; 
184         }
185         
186         // template now == full path with .xml at the end..
187         
188         
189         
190         if (!file_exists($template)) {
191             
192             // see if exists in the database..
193             $r = DB_DAtaObject::factory('report');
194             
195             
196             if (!$r->get('report_name', $ocfg['template'])) {
197                 $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain');
198             }
199             
200             return $ocfg['template'];
201             
202             
203         }
204         
205         
206         // we have a file, and we need to sync it with database.
207         
208         $mt = filemtime($template);
209         
210         $r = DB_DAtaObject::factory('report');
211             
212             
213         if (!$r->get('report_name', $ocfg['template'])) {
214             // insert one..
215             $r = DB_DAtaObject::factory('report');
216             $r->setFrom(array(
217                 
218                 'report_name' => $ocfg['template'],
219                 
220                 'report_source' => file_get_contents($template),
221                 
222                 'report_grade' => 0,
223                 'report_loaddate' => date('Y-m-d H:i:s', $mt)
224             ));
225             $r->insert();
226             return $ocfg['template'];
227         } 
228         // see if it needs updating..
229         if (strtotime($r->report_loaddate) < $mt) {
230             $r->setFrom(array(
231                 
232                  'report_source' => file_get_contents($template),
233                 
234                 
235                 'report_loaddate' => date('Y-m-d H:i:s', $mt)
236             ));
237             $r->update();
238             
239             return $ocfg['template'];
240             
241             
242         }
243         // system is newer than ours..
244             
245         return $ocfg['template'];
246          
247         
248         
249     }
250     
251 }