Print.php
[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         $cmd = implode(' ', $cmd) ;
136         //$cmd = implode(' ', $cmd) . ' ' . (strlen($ocfg['template']) ? escapeshellarg($template) : '');
137         if (isseT($_REQUEST['_debug'])) {
138             echo $cmd;
139             exit;
140         }
141         
142        // echo $cmd;exit;
143         $res = `$cmd`;
144         // var_dump( $res);
145         
146         if (!file_exists($fn)) {
147             $this->jerr("File creation failed:  " . $cmd, array(), 'text/plain');
148         }
149         
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...
154         exit;
155         
156     }
157     function syncDB($cfg) 
158     {
159         $roo = HTML_FlexyFramework::get()->page;
160         
161         $do = DB_DataObject::Factory('invchead');
162         
163         $office = substr($do->database(),-2);
164        
165         
166         $ocfg = $cfg;
167         $template  = '';
168         if (isset($cfg['template'])) {
169             
170             $template = '/report_templates'. '/'.$cfg['template']. '.xml';
171             print_r(pathinfo($template));exit;
172             
173             print_r(file_get_contents($template));exit;
174             
175             unset($cfg['template']);
176         }
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; 
182         }
183         
184         // template now == full path with .xml at the end..
185         
186         
187         
188         if (!file_exists($template)) {
189             
190             // see if exists in the database..
191             $r = DB_DAtaObject::factory('report');
192             
193             
194             if (!$r->get('report_name', $ocfg['template'])) {
195                 $this->jerr("No template '{$ocfg['template']}' exists or $template or $template_test", array(), 'text/plain');
196             }
197             
198             return $ocfg['template'];
199             
200             
201         }
202         
203         
204         // we have a file, and we need to sync it with database.
205         
206         $mt = filemtime($template);
207         
208         $r = DB_DAtaObject::factory('report');
209             
210             
211         if (!$r->get('report_name', $ocfg['template'])) {
212             // insert one..
213             $r = DB_DAtaObject::factory('report');
214             $r->setFrom(array(
215                 
216                 'report_name' => $ocfg['template'],
217                 
218                 'report_source' => file_get_contents($template),
219                 
220                 'report_grade' => 0,
221                 'report_loaddate' => date('Y-m-d H:i:s', $mt)
222             ));
223             $r->insert();
224             return $ocfg['template'];
225         } 
226         // see if it needs updating..
227         if (strtotime($r->report_loaddate) < $mt) {
228             $r->setFrom(array(
229                 
230                  'report_source' => file_get_contents($template),
231                 
232                 
233                 'report_loaddate' => date('Y-m-d H:i:s', $mt)
234             ));
235             $r->update();
236             
237             return $ocfg['template'];
238             
239             
240         }
241         // system is newer than ours..
242             
243         return $ocfg['template'];
244          
245         
246         
247     }
248     
249 }