b2core 框架的首页视图显示机制

 在 app/c/__construct.php 文件中渲染出了视图,决定首页是 cms.php,并且不显示在 URL 上面。base 是所有控制器的基础类。

<?php
class m1 extends m{
  function __construct($table)
  {
    global $db;
    $this->db = $db;
    $this->table = $table;
    $sid =  $GLOBALS['sid']?addslashes($GLOBALS['sid']):'demo';
    $this->filter = " 1 and sid='$sid'";
    $this->key = 'uuid';  //uuid maybe a better way
  }

  function add($elem = FALSE)
  {
    if(!$elem) $elem = $_POST;
    $elem['sid'] = $GLOBALS['sid'];
    return parent::add($elem);
  }

}

class base extends c{

  var $u;
  function __construct()
  {
  }

  function check()
  {
    $this->u = load('m/usr_m')->check();
    if(!$this->u){
      redirect('?/usr/login','请登录系统');
    }

    if(!$this->u['sid'] && seg(1)!='space'){
      redirect('?/space','请选择空间');
    }

    $GLOBALS['u'] = $this->u;
    $GLOBALS['sid'] = $this->u['sid'];
    return $this->u;
  }

  function build_form($structure, $val )
  {
    foreach($structure as $k => $sort){
      $sort['kid'] = $k;
      $sort['val'] = $val[$k];
      if(in_array($sort['type'],array('system','system_fill'))) $sort['val_label'] = $val[$k.'_label'];
      $line.= view('v/config/mod_form',$sort,true);
    }
    return $line;
  }

  function build_view($structure, $val , $table = true)
  {
    if($table)$line = '<table class="pepm-table" ><tr>';
    $t = 0;
    foreach($structure as $k=>$sort){
      $sort['width']=$sort['width']?:1;
      $width = in_array($sort['type'],array('data','section'))?3:$sort['width'];
      $sort['id'] = $k;
      $sort['val'] = $val[$k];
      if(in_array($sort['type'],array('system','system_fill'))) $sort['val_label'] = $val[$k.'_label'];

      if($table){
        if( $t+$width > 3){
          for($i=$t;$i<3;$i++)$line.= '<td colspan=2 ></td>';
          $line.= '</tr><tr>';
          $t = 0;
        }
        //$line.= '<td>'.$t.'</td>';
        $line.= view('v/config/mod_view_table',$sort,true);
        $t+=$width;
        $GLOBALS['t'] = $t;
      }
      else $line.= view('v/config/mod_view',$sort,true);
    }

    if($table){
        for($i=$t;$i<3;$i++)$line.= '<td colspan=2 ></td>';
        $line.= '</tr></table>';
    }
    return $line;
  }

  function ext($msg_404 = '模块不存在')
  {
    show_404( $msg_404 );
  }

  function display($view,$param)
  {
    $p['content'] = view($view,$param,true);
    if(is_ajax_request()){
      $tmp = 'ajax';
    }
    else{
      $tmp = 'layout';
      $r = load('m/config_m')->get(" and `key`= 'menu'");
      $p['menu'] = $r[0]['data'];
    }
    view('v/'.$tmp,$p);
  }

  function cms($view,$param)
  {
    $p['content'] = view($view,$param,true);
    if(is_ajax_request()){
      $tmp = 'ajax';
    }
    else{
      $tmp = 'cms';
      //$r = load('m/config_m')->get(" and `key`= 'menu'");
      $p['menu'] = array(
        'home'=>'首页',
        'news'=>'产业新闻',
        'park'=>'园区展示',
        'annou'=>'定增扩产',
        'deal'=>'融资情报',
        'proj'=>'项目管理',
        'solution'=>'招商方案'

      );
      //$r[0]['data'];
    }
    view('v/'.$tmp,$p);
  }

}


function _encode($arr)
{
  return json_encode($arr,JSON_UNESCAPED_UNICODE);
  // 上方为使用 php5.4 以上时,系统自带编码函数,速度很快。
}

function _decode($str, $method=0xff)
{
  //$str = htmlspecialchars_decode($str, ENT_QUOTES | ENT_HTML5);
  return json_decode($str,true);
}

function static_uri()
{
  return '/';
}

function redirect($url , $msg ='',$ext_msg = '' , $sec = 1) //跳转
{
  if($url == -1) $url = $_SERVER['HTTP_REFERER'];
  $url = $url ?: WWW;
  //!empty($url) && $url[0] != '/' && $url = '/' . $url;
  if (strpos($url, 'http://') !== FALSE) $url = ltrim($url, '/');
  if(!$sec) header("location:$url");
  $param = array('url'=>$url, 'msg'=>$msg, 'ext_msg'=>$ext_msg , 'sec'=>$sec);
  //view('v/redirect',$param);
  //exit();
  $p['content'] = view('v/redirect',$param,true);
  if(is_ajax_request())die($p['content']);
  view('v/temp',$p);
  exit;
}

function is_ajax_request()
{
  return ($_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest');
}

function format_number($num)
{
  $tmp  = explode('.',$num);
  $deci = strlen($tmp[1]);
  if($deci > 2 ) $deci = 2;
  return number_format($num,$deci);
}

function rval($val,$sort)
{
  switch($sort['type'])
  {
    case 'system':
      $val = _decode($val);
      return $val['name'];
    default:
      return $val;
  }
}

function u($uid)
{
  $u =  load('m/usr_m')->get($uid);
  return $u['uname'];
}

function b2($comment)
{
  if(!DEBUG)return;
  echo '<li>';
  echo $comment;
  echo '</li>';
  return;
}

function gval($data,$label,$definition)
{
  foreach($definition as $k=>$v)
  {
    if($v['id'] == $label){
      return $data[$k];
    }
  }
}

function human_filesize($bytes, $decimals = 2) {
  $sz = 'BKMGTP';
  $factor = floor((strlen($bytes) - 1) / 3);
  return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}

function ajax_return($data)
{
  // ini_set('zlib.output_compression', 'On');
  header('Content-Type:application/json; charset=utf-8');

  // IE EDGE 缓存问题
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  header("Pragma: no-cache");

  exit(_encode($data));
}

/* 生成图形数据 */

//deal filter fund= fund , join company
// count entity
function chartdata( $entity = 'company', $count = '', $sort = 'industry',$filter = '',$join = '' )
{
  // join 为 有关联 对象的字段 . 还是关联表 ?  目前为 关联字段, 如 deal_c_inv 的 company  字段有关联 .
  if($filter){
    // 通过  entity_index 检索
    $key = key($filter);
    $val = array_pop($filter);
    $query = " and uuid in ( select eid from entity_index where type='$entity' and field='$key' and val='$val' ) ";
    $companies = load('m/entity_m')->get($query);
  }
  else  $companies = load('m/entity_m')->get(" and type='$entity' ");
  $definition = load('m/config_m')->key($entity);
  $dd = $definition['item'];
  // after jo
  if($join){
    foreach($dd as $k => $it)
    {
        if( trim($it['id']) == trim($join) ){
          // 获取关联字段 .
          $join_key = $k;
          //
          $join_table = $it['ext']['entity'];
          //print_r($it);
          break;

        }
    }
    $join_definition = load('m/config_m')->key($join_table);
    $dd = array_merge($dd , $join_definition['item']);

  }

  foreach($companies as $company){
    $cdata = _decode($company['data']);

    if($join){
      $joinuuid = $cdata[$join_key];
      $joindata = load('m/entity_m')->get_one($joinuuid);
      if($joindata )$cdata = array_merge($cdata , _decode($joindata['data']));
    }

    $industry = gval($cdata,$sort,$dd);
    if($count) $arra[$industry]= $arra[$industry]+ gval($cdata,$count,$dd);
    else $arra[$industry]++;
  }
  foreach($arra as $k=>$v){
    if($k=='')$k='未知';
    $na[] = array('value'=>$v, 'name'=>$k);
  }
  return $na;
}

看到这里,我明白了理解一个系统的框架运行顺序是多么的重要的,可以轻松地排错,理解程序代码。