网页上生成报表模块的功能

这个功能实在 b2cube 项目的 app/c/report.php 文件里面

<?php
class report extends base{
  function __construct()
  {
    parent::__construct();
    $this->check();
  }

  function index( $condition = '' , $value = '')
  {
    $param['data'] = $data = load('m/config_m')->get(" and `type`='report'");
    $this->display('v/report/list',$param);
  }

  function view($uuid = 0)
  {
    $sm = load('m/config_m')->get_one($uuid);
    $data = _decode($sm['data']);
    $form = $data['item'];
    $report = array();
    // $data['report'];
    $param['form'] = $form;
    $param['show_form'] = true;
    $param['fields'] = $this->build_form( $form,$_POST);


    if($_POST['report']=='preview'){
      if(is_ajax_request()){
        $_POST['data']['report'] = 'view';
        echo _encode(array('rtu'=>'report/view/'.$uuid.'/','msg'=>'预览报表','data'=>$_POST['data']));
        return;
      }
    }

    if($_POST){
      if($_POST['report']=='down')$_POST = array_merge($_POST,$_POST['data']);
      $report = $this->report_data($data['report']);
      $param['out'] = $out = $this->html_report($report);
      if($_POST['report']=='down'){
        html2docx($out,$sm['label']);
        exit();
      }
    }
    $this->display('v/report/add',$param);
  }


  function report_data($yaml)
  {
/* Yaml 格式
-
  h1
  content
-
  table
  entity:uuid

-
  list
  entity:filed:val

*/
    foreach($yaml as $sec){
      $list = $unlist = $ndt = array();
      $tag = $sec[0];
      switch($tag){
        case 'table':
          $label = load('m/config_m')->key($sec[1]);
          $data = load('m/entity_m')->get_one($_POST[$sec[2]]);
          $dt = _decode($data['data']);
          if($sec[3]['unlist']){
            $unlist = explode(',',$sec[3]['unlist']);
            foreach($label['item'] as $k=>$v){
              if(!in_array($v['id'],$unlist)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }
          elseif($sec[3]['list'])
          {
            $list = explode(',',$sec[3]['list']);
            foreach($label['item'] as $k=>$v){
              if(in_array($v['id'],$list)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }
          $report1[] = array('table',array($label['item'],$dt));
          break;

        case 'list':
          $data  = $header = array();
          $label = load('m/config_m')->key($sec[1]);
          $rs = load('m/entity_m')->get_by_index($sec[1],$sec[2],$_POST[$sec[3]]);
          if($sec[4]['unlist']){
            $unlist = explode(',',$sec[4]['unlist']);
            foreach($label['item'] as $k=>$v){
              if(!in_array($v['id'],$unlist)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }
          elseif($sec[4]['list'])
          {
            $list = explode(',',$sec[4]['list']);
            foreach($label['item'] as $k=>$v){
              if(in_array($v['id'],$list)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }

          foreach($label['item'] as  $k => $label){
           switch($label['type']){
             case 'section':
               break;
             case 'system':
               $k = $k.'_label';
               $header[$k] = $label['name'];
               break;
             default:
               $header[$k] = $label['name'];
           }
          }

          $data[] = $header;
          foreach($rs as $line){
           $dt = _decode($line['data']);
           $tr = array();
           foreach($header as $k=>$label){
             $tr[] = $dt[$k];
           }
           $data[] = $tr;
          }
          $report1[] = array('list',$data);
          break;
        default:
           $report1[] = array($tag,$sec[1]);
     }
   }
    return $report1;
  }

  /*
    htmlt report :
    根据 数组数据 生成 html 格式报表
  */

  function html_report($report)
  {
    foreach($report as $r){
      $tag = $r[0];
      switch($tag){
        case 'h1':
        case 'h2':
        case 'h3':
        case 'h4':
          $out.= "<$tag>".$r[1]."</$tag>";
          break;
        case 'table':
          $data = $r[1];
          $label = $data[0];
          $dt = $data[1];
          $out.='<table class="print-table" >';
          foreach($label as $k=>$r){
            if($r['type']=='section')$out.='<tr><th colspan="2" >'.$r['name'].'</th></tr>';
            else $out.='<tr><td class="label" >'.$r['name'].'</td><td>'.$dt[$k].'</td></tr>';
          }
          $out.='</table>';
          break;
        case 'list':
          $data = $r[1];
          $no = '序号';
          $out.='<table class="print-table" >';
          foreach($data as $dt){
            $out.='<tr><td class="no" >'.$no.'</td>';
            foreach($dt as $d){
              $out.='<td>'.$d.'</td>';
            }
            $out.='</tr>';
            $no = ($no == '序号')?1:$no+1;
          }
          $out.='</table>';
          break;
        case 'textbox':
          $out.='<table class="print-table" ><tr><td> &nbsp; </td></tr></table>';
          break;
        case 'text':
          $out.='<div>'.$r[1].'</div>';
          break;
      }
    }
    return $out;
  }
}


function html2docx($doc,$filename = 'report')
{
  $from = array(
    '<table'
    ,'<th colspan="2" >',
    '<td>',
    '<td class="label" >',
    '<td class="no" >',
    '</td>','</th>','<h1>','</h1>','<h2>','</h2>','<h3>','</h3>','<h4>','</h4>',
  );

  $to = array(
    '<table width="650" '
    ,'<th colspan="2" bgcolor="#ffffff" style="border: 0.50pt solid #000001; padding: 0.05cm 0.19cm" ><p style="margin-bottom:0px;" >'
    ,'<td bgcolor="#ffffff" style="border: 0.50pt solid #000001; padding: 0.05cm 0.19cm" ><p style="margin-bottom:0px;" >'
    ,'<td bgcolor="#ffffff" width="120" style="border: 0.50pt solid #000001; padding: 0.05cm 0.19cm" ><p style="margin-bottom:0px;" >'
    ,'<td bgcolor="#ffffff" width="20" style="border: 0.50pt solid #000001; padding: 0.05cm 0.19cm" ><p style="margin-bottom:0px;" >'
    ,'</p></td>','</p></th>',
    '<h1><font size="4" style="font-size: 16pt">','</font></h1>',
    '<h2><font size="2" style="font-size: 12pt">','</font></h2>'
    ,'<h3><font size="2" style="font-size: 11pt">','</font></h3>'
    ,'<h4><font size="2" style="font-size: 10pt">','</font></h4>'
  );
  $doc = str_replace($from,$to,$doc);
  $out = view('v/report/html_temp',array('content'=>$doc),true);
  file_put_contents(APP.'cache/word.html',$out);
  exec("nice unoconv -f docx ".APP.'cache/word.html');

  header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment;Filename=$filename.docx");
  //header("Content-Length: ".APP.'cache/word.docx');
  echo file_get_contents(APP.'cache/word.docx');
  exit();
}

这里的功能算是相当的复杂,自己以前都没有做过的。

还有这个在后台的配置的功能选项。(其实我觉得这两个功能是相互关系的,配置决定表单界面的生成结果)

我的任务就是在 vc800 项目当中整合 “基金” 和 “配置” 两个项目,关键性的功能放进 vc800 里面。