ultimix
report.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
26  class report_1_0_0{
27 
38  var $CachedMultyFS = false;
39  var $Database = false;
40  var $PageJS = false;
41  var $ReportUtilities = false;
42  var $Security = false;
43  var $String = false;
44  var $Utilities = false;
45 
56  var $Templates = array();
57 
68  var $Output = '';
69 
84  function __construct()
85  {
86  try
87  {
88  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
89  $this->Database = get_package( 'database' , 'last' , __FILE__ );
90  $this->PageJS = get_package( 'page::page_js' , 'last' , __FILE__ );
91  $this->ReportUtilities = get_package( 'report::report_utilities' , 'last' , __FILE__ );
92  $this->Security = get_package( 'security' , 'last' , __FILE__ );
93  $this->String = get_package( 'string' , 'last' , __FILE__ );
94  $this->Utilities = get_package( 'utilities' , 'last' , __FILE__ );
95  }
96  catch( Exception $e )
97  {
98  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
99  }
100  }
101 
120  function pre_generation( $Options )
121  {
122  try
123  {
124  $PackagePath = _get_package_relative_path_ex( 'report' , '1.0.0' );
125  $this->PageJS->add_javascript( "{http_host}/$PackagePath/include/js/report.js" );
126 
127  $Lang = get_package( 'lang' , 'last' , __FILE__ );
128  $Lang->include_strings_js( 'report' );
129  }
130  catch( Exception $e )
131  {
132  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
133  }
134  }
135 
158  function compile_report_template_list( &$Settings )
159  {
160  try
161  {
162  $Reports = $this->ReportUtilities->get_reports_list( $Settings );
163 
164  $Ids = array();
165  $Titles = array();
166 
167  foreach( $Reports as $i => $r )
168  {
169  $Ids [] = $i;
170  $Titles [] = basename( $r , '.rep' );
171  }
172 
173  $Subfolder = $Settings->get_setting( 'subfolder' , '' );
174  if( $Subfolder != '' )
175  {
176  $Subfolder = '<input type="hidden" name="subfolder" value="'.$Subfolder.'">';
177  }
178 
179  $Code = "$Subfolder{select:class=width_320 flat;".$Settings->get_all_settings().';first='.
180  implode( '|' , $Ids ).';second='.implode( '|' , $Titles ).'}';
181 
182  return( $Code );
183  }
184  catch( Exception $e )
185  {
186  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
187  }
188  }
189 
216  function compile_report_template( &$Settings , $Data )
217  {
218  try
219  {
220  $Name = $Settings->get_setting( 'name' );
221 
222  $this->Templates [ $Name ] = $Data;
223  }
224  catch( Exception $e )
225  {
226  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
227  }
228  }
229 
252  private function get_raw_template( &$Settings )
253  {
254  try
255  {
256  $Name = $Settings->get_setting( 'name' , false );
257 
258  if( $Name === false )
259  {
260  $TemplateName = $Settings->get_setting( 'template_package_name' );
261  $TemplateVersion = $Settings->get_setting( 'template_package_version' , 'last' );
262  $TemplateName = $Settings->get_setting( 'template' );
263 
264  $Path = _get_package_path_ex( $TemplateName , $TemplateVersion )."/res/templates/$TemplateName";
265  $RawTemplate = $this->CachedMultyFS->file_get_contents( $Path );
266  }
267  else
268  {
269  $RawTemplate = $this->Templates[ $Name ];
270  }
271 
272  return( $RawTemplate );
273  }
274  catch( Exception $e )
275  {
276  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
277  }
278  }
279 
302  function compile_report_query( &$Settings )
303  {
304  try
305  {
306  $RawTemplate = $this->get_raw_template( $Settings );
307 
308  $Records = $this->Utilities->get_records();
309 
310  $Code = '';
311 
312  foreach( $Records as $k => $r )
313  {
314  $Template = $RawTemplate;
315  $Template = $this->String->print_record( $Template , $r );
316  $Template = str_replace(
317  array( '{rowid}' , '{recid}' , '{odd_factor}' ) ,
318  array( $k + 1 , $k , $k % 2 ? 'odd' : 'even' ) , $Template
319  );
320 
321  $Code .= $Template;
322  }
323 
324  return( $Code );
325  }
326  catch( Exception $e )
327  {
328  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
329  }
330  }
331 
354  function compile_report_button( &$Settings )
355  {
356  try
357  {
358  $Settings->set_undefined( 'id' , md5( microtime( true ) ) );
359  $Settings->set_undefined( 'package_version' , 'last' );
360 
361  $Code = $this->CachedMultyFS->get_template( __FILE__ , 'report_button.tpl' );
362 
363  $Code = $this->String->print_record( $Code , $Settings->get_raw_settings() );
364 
365  return( $Code );
366  }
367  catch( Exception $e )
368  {
369  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
370  }
371  }
372 
403  function get_report_template( $PackageName , $PackageVersion , $ReportTemplate , $Subfolder )
404  {
405  try
406  {
407  $ReportsPath = _get_package_path_ex( $PackageName , $PackageVersion )."/res/reports/";
408 
409  if( $Subfolder != '' )
410  {
411  $ReportsPath .= $Subfolder.'/';
412  }
413 
414  /* getting list of reports */
415  $Reports = $this->Utilities->get_files_from_directory( $ReportsPath , '/\.rep/' );
416  $Report = $Reports[ $ReportTemplate ];
417  return( $this->CachedMultyFS->file_get_contents( $Report ) );
418  }
419  catch( Exception $e )
420  {
421  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
422  }
423  }
424 
443  private function prepare_report_template( &$Package )
444  {
445  try
446  {
447  if( $this->Security->get_gp( 'report_template' , 'integer' , false ) !== false )
448  {
449  /* it means that the function 'report' has returned data, not the generated report */
450  $ReportTemplate = $this->get_report_template(
451  $this->Security->get_gp( 'report_package_name' , 'string' ) ,
452  $this->Security->get_gp( 'report_package_version' , 'string' , 'last' ) ,
453  $this->Security->get_gp( 'report_template' , 'integer' ) ,
454  $this->Security->get_gp( 'subfolder' , 'command' , '' )
455  );
456  $Package->Output = $this->String->print_record( $ReportTemplate , $Package->Output );
457  }
458  }
459  catch( Exception $e )
460  {
461  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
462  }
463  }
464 
465  // TODO add report generating tutorial
466 
493  private function compile_report( &$Package , $FunctionName , &$Options )
494  {
495  try
496  {
497  call_user_func( array( $Package , $FunctionName ) , $Options );
498 
499  $this->prepare_report_template( $Package );
500 
501  $Extension = $this->Security->get_gp( 'output' , 'string' , 'txt' );
502  $Extension = $Options->get_setting( 'output' , $Extension );
503 
504  header( 'HTTP/1.0 200 OK' );
505  header( 'Content-type: application/octet-stream' );
506  header( 'Content-Length: '.strlen( $Package->Output ) );
507  header( 'Content-Disposition: attachment; filename="'.date( 'YmdHid' ).".$Extension".'"' );
508  header( 'Connection: close' );
509 
510  $this->Output = $Package->Output;
511  }
512  catch( Exception $e )
513  {
514  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
515  }
516  }
517 
540  private function get_report_generator( &$Options )
541  {
542  try
543  {
544  $PackageName = $this->Security->get_gp( 'report_package_name' , 'string' , false );
545  $PackageName = $Options->get_setting( 'report_package_name' , $PackageName );
546 
547  if( $PackageName === false )
548  {
549  return( false );
550  }
551 
552  $PackageVersion = $this->Security->get_gp( 'report_package_version' , 'string' , 'last' );
553  $PackageVersion = $Options->get_setting( 'report_package_version' , $PackageVersion );
554 
555  $Package = get_package( $PackageName , $PackageVersion , __FILE__ );
556 
557  return( $Package );
558  }
559  catch( Exception $e )
560  {
561  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
562  }
563  }
564 
583  function generate_report( &$Options )
584  {
585  try
586  {
587  $Package = $this->get_report_generator( $Options );
588 
589  if( $Package === false )
590  {
591  return;
592  }
593 
594  // TODO close reports with permits
595 
596  $FunctionName = $this->Security->get_gp( 'func_name' , 'string' , 'report' );
597  $FunctionName = $Options->get_setting( 'func_name' , $FunctionName );
598 
599  if( method_exists( $Package , $FunctionName ) )
600  {
601  $this->compile_report( $Package , $FunctionName , $Options );
602  return;
603  }
604 
605  $Message = "Method \"$FunctionName\" does not exists in the package \"".get_class( $Package )."\"";
606  throw( new Exception( $Message ) );
607  }
608  catch( Exception $e )
609  {
610  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
611  }
612  }
613 
636  function view( &$Options )
637  {
638  try
639  {
640  $this->generate_report( $Options );
641 
642  return( $this->Output );
643  }
644  catch( Exception $e )
645  {
646  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
647  }
648  }
649  }
650 ?>