project = Maze::input('key');
$this->table = Maze::input('table');
$this->id = Maze::input('update_where_id') ? Maze::input('update_where_id') : Maze::input('where_id');
$this->menu = Maze::input('menu');
$this->menu_id = Maze::input('menu_id');
}
/**
* url
*
* @return array
*/
private function url($key = false, $id = 0, $table = false)
{
$table = $table ? $table : $this->table;
if(strpos($table, 'http://') !== false || strpos($table, 'javascript') !== false)
{
return $table;
}
if($key <= 3)
{
$where = Maze::preInput('option_');
if($where)
{
if(isset($where['where_id']))
{
//unset($where['where_id']);
}
$table .= '&' . http_build_query($where);
}
$oper = Maze::preInput('oper_');
if($oper)
{
$table .= '&' . http_build_query($oper);
}
}
$param = 'key=' . $this->project . '&table=' . $table . '&menu=' . $this->menu . '&menu_id=' . $this->menu_id;
$url = array
(
0 => Maze::url('project/database?key=' . $this->project),
1 => Maze::url('project/database/list?' . $param),
2 => Maze::url('project/database/update?' . $param),
3 => Maze::url('project/database/update?' . $param . '&where_id=' . $id),
4 => Maze::url('database.update_action'),
5 => Maze::url(''),
6 => Maze::url('database.delete_action?' . $param . '&where_id=' . $id),
7 => Maze::url('project/database/stat?' . $param),
8 => Maze::url('database.list_excel?' . $param),
9 => Maze::url('database.stat_excel?' . $param),
);
if($key >= 10 && $key < 20)
{
return $url[1];
}
elseif($key >= 20 && $key < 30)
{
return $url[2];
}
if($key >= 0 && isset($url[$key]))
{
return $url[$key];
}
return $url;
}
/**
* 获得refer 暂时无用了
*
* @return string
*/
private function refer($type = 0)
{
return '';
switch($type)
{
case 0:
$name = '数据库操作';
break;
case 1:
$name = '数据列表';
break;
case 5:
$name = '项目列表';
break;
}
$name = '返回上一级';
$url = $this->url($type);
$refer = '-[' . $name . ']';
return $refer;
}
/**
* info 处理一些基本的信息,供mote模板使用
*
* @return array
*/
public function info()
{
$config = $this->config();
$menu = isset($config['lang']) ? $config['lang'] : $this->project . '-' . $this->table;
$display = '';
# 新增权限
$insert_state = Maze::load('manage/auth.oper', 4);
if((isset($config['manage']['insert']) && $config['manage']['insert'] == false) || !isset($config['manage']['insert']) || $insert_state == false)
{
$display = 'display:none';
}
$info = array
(
'action' => $this->url(4),
'list' => $this->url(1),
'mul' => $this->url(1),
'add' => 'location.href=\'' . $this->url(2) . '\'',
'add_state' => $display,
'project' => $this->project,
'table' => $this->table,
'id' => $this->id,
'main' => '数据管理' . $this->refer(5),
'list_header' => $menu,
'list_desc' => '数据列表',
'project_name' => Maze::load('manage/project.name', $this->project),
'update_header' => $menu,
'update_desc' => '更新数据',
'updateMul' => (!$this->id && isset($config['manage']['updateMul']) && $config['manage']['updateMul']) ? '增加一条' : false,
'updateDesc' => isset($config['manage']['desc']) ? $config['manage']['desc'] : '',
'listDesc' => isset($config['desc']) ? $config['desc'] : '',
'struct' => isset($config['manage']['struct']) ? $config['manage']['struct'] : '',
'stat_desc' => '主动统计',
);
$info['parent'] = Maze::input('oper_parent');
if($info['parent'])
{
$oper = Maze::preInput('oper_');
if($oper)
{
$info['parent'] .= '&' . str_replace('oper_', '', http_build_query($oper));
}
$info['parent'] = 'location.href=\'' . $this->url(11, 0, $info['parent']) . '\'';
$info['parent_state'] = '';
}
else
{
$info['parent_state'] = 'display:none';
}
$info['save'] = Maze::input('oper_save_jump');
if($info['save'])
{
$info['list'] = $this->url(11, 0, $info['save']);
}
$search = Maze::preInput('search_');
if($search)
{
$info['mul'] .= '&' . http_build_query($search);
}
if($this->id > 0)
{
$info['update_header'] = $menu . '-更新数据' . $this->refer(1);
}
return $info;
}
/**
* 获取表名
*
* @return array
*/
public function getTableName($param)
{
if($param[1] == 'other') return '无数据表';
$path = Maze::load('manage/project.path', $param[0]);
$config = $this->config($param[0], $path, $param[1]);
return isset($config['lang']) ? $config['lang'] : $param[0];
}
/**
* config
*
* @return array
*/
public function config($project = false, $path = false, $table = false)
{
if(!$project)
{
$project = $this->project;
$table = $this->table;
}
Maze::$global['manage']['project'] = $project;
$config = Maze::load('manage/project.config', $project);
$project = $config['lib'];
$path = $path ? $path : $config['path'];
if($project && $path && empty($this->config[$path]))
{
$this->config[$path] = array();
$dir = $path . 'database/';
if(is_dir($dir))
{
$database = scandir($dir);
foreach($database as $k => $v)
{
if(strpos($v, '.php') !== false)
{
$k = str_replace('.php', '', $v);
$this->config[$path][$k] = $this->struct($dir . $v, $project);
}
}
}
}
return ($table && isset($this->config[$path][$table])) ? $this->config[$path][$table] : $this->config[$path];
}
/**
* 对database配置文件进行解析
*
* @return array
*/
private function struct($file, $project = false)
{
$config = include($file);
if(isset($config['struct']) && is_array($config['struct']['id']))
{
$config['update'] = false;
$config['manage']['struct'] = '
';
foreach($config['struct'] as $k => $v)
{
if(isset($v['search']))
{
/*
if(strpos($v['search'], 'order') !== false)
{
$config['manage']['search']['order'][$k] = $v['name'];
}
*/
if(strpos($v['search'], 'time') !== false)
{
$config['manage']['search']['time'][$k] = $v['name'];
}
if(strpos($v['search'], 'date') !== false)
{
$config['manage']['search']['date'][$k] = $v['name'];
}
if(strpos($v['search'], 'fulltext') !== false)
{
$config['manage']['search']['fulltext'][$k] = $v['name'];
isset($v['match']) && $config['request']['list']['option'][$k] = array('option', 'like');
}
if(isset($v['option']) && strpos($v['search'], 'select') !== false)
{
$config['manage']['search']['select'][$k]['option'] = $v['option'];
$config['manage']['search']['select'][$k]['lang'] = $v['name'];
if(isset($v['default']))
{
$config['manage']['search']['select'][$k]['default'] = $v['default'];
}
}
}
if(isset($v['table']) && !isset($config['end']))
{
$config['end']['insert'] = $v['value'];
}
if(isset($v['update']))
{
if(!isset($config['manage']['insert']))
{
$config['manage']['insert'] = true;
}
$config['update'] = true;
}
if(isset($v['type']))
{
$config['manage']['struct'] .= ''.$k.' | '.$v['type'].' | '.$v['name'].' |
';
}
}
$config['manage']['struct'] .= '
';
$config['manage']['_list'] = array
(
'col' => 'manage',
'name' => '管理',
'value' => array(),
);
if(!isset($config['manage']['edit']) && $config['update'] == true)
{
# 3和6
$config['manage']['_list']['value'] = array(3 => '编辑');
}
if(isset($config['manage']['list'][0]) && is_array($config['manage']['list'][0]))
{
$config['manage']['_list']['value'] += $config['manage']['list'][0];
unset($config['manage']['list'][0]);
}
elseif(isset($config['manage']['list_button']))
{
$config['manage']['_list']['value'] += $config['manage']['list_button'];
}
if(!$config['manage']['_list']['value'])
{
unset($config['manage']['_list']);
}
}
if(isset($config['auth']))
{
if(is_array($config['auth']))
{
$config['auth']['key'] = $project . '-' . $config['auth']['key'];
}
else
{
$config['auth_key'] = $config['auth'];
$config['auth'] = $project . '-' . $config['auth'];
}
}
return $config;
}
/**
* 列表页搜索
*
* @return array
*/
public function list_search_select(&$result, $search, $name, $lang, $option, $default = false)
{
$value = array();
$value['name'] = $name;
$value['lang'] = $lang;
$value['option'] = $option;
if(isset($search[$name]))
{
$value['value'] = $search[$name];
}
if($default)
{
$value['default'] = $default;
}
$result .= Html::select($value, false) . ' ';
}
/**
* 列表页搜索
*
* @return array
*/
public function list_search_time(&$result, $search, $prefix, $key, $lang, $type = 'time')
{
$value['name'] = $prefix . 'option_start_' . $key;
$value['lang'] = '开始' . $lang;
if(isset($search[$value['name']]))
{
$value['value'] = Maze::maketime($search[$value['name']]);
Maze::setInput($value['name'], $value['value'], 'search_');
}
$result .= Html::$type($value) . ' - ';
$value['name'] = $prefix . 'option_end_' . $key;
$value['lang'] = '结束' . $lang;
if(isset($search[$value['name']]))
{
$value['value'] = Maze::maketime($search[$value['name']]);
Maze::setInput($value['name'], $value['value'], 'search_');
}
$result .= Html::$type($value) . '
';
}
/**
* 列表页搜索
*
* @return array
*/
public function list_button()
{
# 新增权限
if(Maze::load('manage/auth.oper', 2) == false)
{
return '';
}
$config = $this->config();
$result = '';
# 增加主动统计按钮
if(isset($config['manage']['stat']))
{
$config['manage']['button']['数据统计'] = $this->url(7);
}
# 增加下载报表按钮
if(isset($config['manage']['excel']))
{
$config['manage']['button']['导出数据'] = 'onclick="$(\'#form1\').attr(\'action\', \''.$this->url(8).'\');list_search($(this))"';
}
# 列表页按钮
if(isset($config['manage']['button']))
{
$result .= $this->button_list($config['manage']['button']);
}
return $result;
}
private function button_list($button)
{
$result = '';
foreach($button as $k => $v)
{
if(strpos($v, 'modal|') !== false)
{
$t = explode('modal|', $v);
$result .= ' '.$k.'';
}
elseif(strpos($v, '|') !== false)
{
$t = explode('|', $v);
$result .= $t[0] . ' '.$k.'';
}
elseif(strpos($v, 'onclick') !== false)
{
$result .= ' '.$k.'';
}
elseif(strpos($v, 'http://') !== false)
{
$result .= ' '.$k.'';
}
else
{
$result .= ' '.$k.'';
}
}
return $result;
}
/**
* 列表页搜索
*
* @return array
*/
public function list_search()
{
# 新增权限
if(Maze::load('manage/auth.oper', 2) == false)
{
return '';
}
$config = $this->config();
$result = '';
# 搜索
if(isset($config['struct']) && isset($config['manage']['search']))
{
$search = Maze::preInput('search_');
$node = Html::node(array('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
$btn = '';
//$result .= '';
}
//print_r($result);die;
return $result;
}
/**
* 列表页表格头
*
* @return array
*/
public function list_thead()
{
# 新增权限
if(Maze::load('manage/auth.oper', 1) == false)
{
return '';
}
$config = $this->config();
$result = array();
if(isset($config['struct']))
{
if(isset($config['manage']['list']) && $config['manage']['list'] == 'update')
{
# 验证头部权限 这里如果是多个选择权限的话,暂时无法实现。。。
if(isset($config['auth']) && is_string($config['auth']))
{
$top = Maze::load('manage/auth.getTop', array($config['auth']));
if($top)
{
//Maze::setInput('update_' . $config['auth'], $top['value']);
//$common[$config['auth']] = $top['value'];
$url = $this->url(3, $top['value']);
}
}
else
{
$url = $this->url(3, 1);
}
return Maze::location($url);
}
else
{
if(isset($config['manage']['mul']))
{
$result[] = ' | ';
}
foreach($config['struct'] as $k => $v)
{
if(isset($v['list']))
{
if(isset($v['list_name']))
{
$v['name'] = $v['list_name'];
}
else
{
$temp = Html::cue(array('name' => $v['name']));
$v['name'] = $temp['name'];
}
if(isset($v['search']) && strpos($v['search'], 'order') !== false)
{
$link = Maze::url();
$order = 'search_order_'.$k.'';
if(strpos($link, $order) !== false)
{
$link = preg_replace('/[?|&]'.$order.'=(desc|asc)/i', '{order}', $link);
}
else
{
$link .= '{order}';
}
$search = Maze::input($order);
$cancel = '';
if($search)
{
$asc = $search;
$cancel = ' ';
}
else
{
$asc = 'desc';
if(!isset($v['order']) && $k != 'id')
{
$cancel = ' ';
}
}
if($asc == 'asc')
{
$asc = 'desc';
$icon = 'down';
$text = '降序';
}
else
{
$asc = 'asc';
$icon = 'up';
$text = '升序';
}
$link = str_replace('{order}', '&' . $order . '=' . $asc, $link);
$result[] = '' . $v['name'] . ' '.$cancel.' | ';
}
else
{
$result[] = '' . $v['name'] . ' | ';
}
}
}
if(Maze::load('manage/auth.oper', 3) != false && Maze::load('manage/auth.oper', 5) != false && isset($config['manage']['_list']['value']))
{
$result[] = ''.$config['manage']['_list']['name'].' | ';
}
}
}
return $result;
}
/**
* 导出列表
*
* @return array
*/
public function list_excel()
{
Maze::$global['base']['excel'] = 1;
$data = $this->list_tbody(true);
if($data)
{
$config = $this->config();
require MAZE_PROJECT_PATH . 'src/Excel/PHPExcel.php';
$xls = new \PHPExcel();
$xls->getProperties()->setCreator("Maze Excel")
->setLastModifiedBy("Maze Excel")
->setTitle("Office 2007 XLSX Analysis Document")
->setSubject("Office 2007 XLSX Analysis Document")
->setDescription("Mazephp Excel")
->setKeywords("Mazephp Excel")
->setCategory("Mazephp Excel");
$info = $this->info();
$name = $info['list_header'] . '的' . $info['list_desc'] . '-' . $info['project_name'];
# 全局导出
$start = 65;
$act = $xls->setActiveSheetIndex(0);
$act->setTitle($name);
$i = 0;
foreach($config['struct'] as $k => $v)
{
if(isset($v['list']))
{
$num = 1;
$cell = $this->_getChr($start, $i);
$act->setCellValue($cell . '' . $num, $v['name']);
$act->getColumnDimension($cell)->setWidth(20);
$i++;
foreach($data as $ki => $vi)
{
if(isset($vi[$k]))
{
$num += 1;
$value = $vi[$k];
if(is_string($v['list']))
{
$value = $this->value($v['list'], $data[$ki], $config['struct']);
}
elseif(isset($v['option']))
{
$v['option'] = $this->option($v['option']);
if(isset($v['option'][$value]))
{
if(is_array($v['option'][$value]))
{
$value = $v['option'][$value]['name'];
}
else
{
$value = $v['option'][$value];
}
}
}
$act->setCellValue($cell . $num, $value);
}
}
}
}
//$act->getStyle('A1:'.$k.'20')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 垂直居中
//$act->getStyle('A1:'.$k.'20')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 水平居中
$filename = $name . '.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$write = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
$write->save('php://output');
}
//print_r($data);die;
}
private function _getChr($start, $num)
{
$num = $start + $num;
$max = floor($num/91);
if($max > 0)
{
$num = $num - (91*$max) + $start;
$num = chr($start) . chr($num);
}
else
{
$num = chr($num);
}
return $num;
}
/**
* 列表页数据列表
*
* @return array
*/
public function list_tbody($excel = false)
{
# 新增权限
if(Maze::load('manage/auth.oper', 1) == false)
{
return '';
}
$high = array();
$search = Maze::load('manage/auth.oper', 2) ? Maze::preInput('search_') : false;
$param = array();
if($search)
{
$fulltext = Maze::preInput('search_fulltext_');
if($fulltext)
{
//Maze::setInput('option_' . $search['search_fulltext'], '%' . $search['search_fulltext_value'] . '%');
$search['search_fulltext_type'] = 2;
$fix = '';
if($search['search_fulltext_type'] == 2)
{
$fix = '%';
}
$param['search_type'] = $search['search_fulltext_type'];
foreach($fulltext as $k => $v)
{
$k = str_replace('search_fulltext_', '', $k);
$param['option_' . $k] = $fix . $v . $fix;
unset($search[$k]);
# 设定检索高亮
$high[$k] = $v;
}
$this->search = true;
}
$order = Maze::preInput('search_order_');
if($order)
{
foreach($order as $k => $v)
{
$k = str_replace('search_order_', '', $k);
$param['order'][0][$k] = $v;
unset($search[$k]);
}
}
foreach($search as $k => $v)
{
if($v)
{
if(strpos($k, 'option_') !== false)
{
//Maze::setInput(str_replace('search_', '', $k), $v);
$param[str_replace('search_', '', $k)] = $v;
$this->search = true;
}
}
}
//$search = Maze::preInput('option_');
//print_r($search);die;
}
# 批量更新
$mul = Maze::load('manage/auth.oper', 3) ? Maze::preInput('mul_') : false;
if(Maze::input('method') == 'mul' && $mul && $mul['mul_type'] > 0)
{
if($mul['mul_type'] == 1 && empty($mul['mul_where_id']))
{
Maze::abert('您还未选择任何数据条目');
}
Maze::$global['base']['mul_type'] = $mul['mul_type'];
unset($mul['mul_type']);
foreach($mul as $k => $v)
{
if($v)
{
if(is_array($v))
{
$v = implode(',', $v);
}
$param[str_replace('mul_', '', $k)] = $v;
}
}
if(isset($param))
{
$method = $this->project . '/' . $this->table . '-updatemul';
$state = Maze::load($method, $param);
}
//print_r($param);die;
Maze::abert('reload');
}
$config = $this->config();
$result = array();
$method = $this->project . '/' . $this->table . '-list';
# 验证头部权限
if(isset($config['auth']) && is_string($config['auth']))
{
$top = Maze::load('manage/auth.getTop', array($config['auth']));
if($top)
{
Maze::setInput('option_' . $config['auth_key'], $top['value']);
}
}
$data = Maze::load($method, $param);
if(isset($config['manage']['list_type']) && $config['manage']['list_type'] == 'parent')
{
$parent = array();
$child = array();
foreach($data as $k => $v)
{
if($v[$config['name'] . '_id'] <= 0)
{
$parent[$k] = $v;
}
else
{
$v['name'] = '|-----' . $v['name'];
$child[$v[$config['name'] . '_id']][$k] = $v;
}
}
if($parent)
{
$data = array();
foreach($parent as $k => $v)
{
$data[$v['id']] = $v;
if(isset($child[$v['id']]))
{
foreach($child[$v['id']] as $i => $j)
{
$data[$j['id']] = $j;
}
}
}
}
}
if($excel == true)
{
return $data;
}
//print_r($data);die;
//print_r(Helper::page("current"));die;
if($data && isset($config['struct']))
{
foreach($data as $k => $v)
{
$result[$k] = '';
if(isset($config['manage']['mul']))
{
$result[$k] .= ' | ';
}
foreach($config['struct'] as $ki => $vi)
{
if(isset($vi['list']) && isset($v[$ki]))
{
$show = $v[$ki];
if(is_string($vi['list']))
{
$show = $this->value($vi['list'], $data[$k], $config['struct']);
}
if(isset($vi['modal']))
{
$show = Html::modal($this->value($vi['modal'], $data[$k], $config['struct']), $show);
}
elseif(isset($vi['option']))
{
# 验证option是否是匿名函数
$vi['option'] = $this->option($vi['option']);
# 位运算
if(isset($vi['bit']))
{
$ts = array();
foreach($vi['option'] as $tm => $tv)
{
if(isset($vi['bit'][$tm]) && ($show & $tm) > 0)
{
$ts[] = $tv;
}
}
$show = implode(',', $ts);
}
/*
if((isset($vi['child_name']) || (isset($vi['show']) && is_string($vi['show']))))
{
if(isset($vi['option'][$show]) && is_array($vi['option'][$show]))
{
$show = $vi['option'][$show]['name'];
}
else
{
$show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
}
}
*/
elseif((isset($vi['edit']) && $vi['edit']) || $ki == 'state')
{
$vi['name'] = $ki;
$vi['value'] = $v[$ki];
$show = Html::select($vi, 'form-list', '', 'onchange="update($(this), \''.$v['id'].'\', \''.$this->project.'\', \''.$this->table.'\')"');
}
else
{
if(isset($vi['option'][$show]) && is_array($vi['option'][$show]))
{
$show = $vi['option'][$show]['name'];
}
else
{
$show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
}
}
}
$text = $show;
if($high && isset($high[$ki]))
{
$text = str_replace($high[$ki], '' . $high[$ki] . '', $text);
}
if(!isset($vi['option']) && isset($vi['edit']) && $vi['edit'])
{
//$url = Maze::url($vi['edit'] . '?where_id=' . $v['id'] . '&col=' . $vi['col']. '&key=' . $this->project . '&table=' . $this->table);
$url = $this->url(4);
$vi['edit'] = $vi['edit'] == 'textarea' ? $vi['edit'] : 'text';
$result[$k] .= '' . $text . ' | ';
}
else
{
$result[$k] .= '' . $text . ' | ';
}
}
}
if(isset($config['manage']['_list']['value']))
{
$result[$k] .= '';
foreach($config['manage']['_list']['value'] as $kj => $vj)
{
$pj = false;
$h = '';
$oper = true;
if($kj == 3)
{
$oper = Maze::load('manage/auth.oper', 3);
}
elseif($kj == 6)
{
$oper = Maze::load('manage/auth.oper', 5);
$h = '';
}
if(is_array($vj))
{
if(isset($vj[2]))
{
$oper = $this->value($vj[2], $data[$k]);
}
if($oper)
{
$pj = $this->value($vj[1], $data[$k]);
$vj = $vj[0];
}
}
if($oper == true)
{
$result[$k] .= ' ';
}
}
$result[$k] .= ' | ';
}
$result[$k] .= '
';
}
}
return $result;
}
/**
* 列表页批量管理
*
* @return array
*/
public function list_mul()
{
# 新增权限:更新权限
if(Maze::load('manage/auth.oper', 3) == false)
{
return '';
}
$config = $this->config();
$result = '';
if(isset($config['struct']) && isset($config['manage']['mul']))
{
$mul = Maze::preInput('mul_');
$node = Html::node(array('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
$btn = '';
$prefix = 'mul_';
$result .= $node[0];
$default = 0;
$option = array
(
1 => '选中的数据',
);
# 只有当有搜索条件时才会出现
if(Maze::load('manage/auth.oper', 2) && $this->search == true)
{
$option[2] = '所有数据';
$default = 2;
}
$this->list_search_select($result, $mul, $prefix . 'type' . '" id="'.$prefix.'type', '请选择更新方式', $option, $default);
foreach($config['struct'] as $k => $v)
{
if(isset($v['update']) && $v['update'] == 'radio' && (is_array($v['option']) || is_object($v['option'])))
{
$this->list_search_select($result, $mul, $prefix . 'set_' . $k, $v['name'], $this->option($v['option']), $v['default']);
}
}
$result .= $btn . $node[1];
}
//print_r($result);die;
return $result;
}
private function value($value, $data, $struct = array())
{
if(!$data)
{
return $value;
}
if($value == 'table')
{
$result = array();
foreach($data as $k => $v)
{
if(strpos($v, 'http://wx.qlogo.cn/') !== false || strpos($v, '.jpg') !== false || strpos($v, '.gif') !== false || strpos($v, '.png') !== false)
{
$v = '';
}
elseif(strpos($k, 'time') !== false || strpos($k, 'date') !== false)
{
$v = date("Y-m-d H:i:s", $v);
}
elseif(isset($struct[$k]['decode']))
{
$v = json_decode(base64_decode($v), true);
}
if(isset($struct[$k]) && isset($struct[$k]['name']))
{
$k = $struct[$k]['name'];
}
$result[$k] = $v;
}
$value = Maze::table($result);
}
elseif(strpos($value, '{') !== false)
{
# 这里用到了/e,注意这里无论如何不能传入$_GET之类的用户参数 2015/7/21更新为callback方式
//$eval = '$value = ' . preg_replace('/{(.*?)}/e', "\$data['$1']", $value) . ';';
$func = function($r) use($data)
{
return $data[$r[1]];
};
$eval = '$value = ' . preg_replace_callback('/{(.*?)}/', $func, $value) . ';';
eval($eval);
}
return $value;
}
/**
* update
*
* @return array
*/
public function update()
{
$config = $this->config();
$result = array();
$prefix = 'update';
$data = array();
if($this->id > 0)
{
# 新增权限
if(Maze::load('manage/auth.oper', 3) == false)
{
return '';
}
$data = $this->load();
if($data)
{
//$prefix = 'set';
}
}
else
{
# 新增权限
if(Maze::load('manage/auth.oper', 4) == false)
{
return '';
}
}
//print_r($config['manage']['update']);die;
foreach($config['struct'] as $k => $v)
{
if(isset($v['update']))
{
# 处理show模式下的class
$class = $this->update_class($v);
$result[$k] = '';
}
elseif(isset($v['class']))
{
# 分割线
$result[$k] = Html::hr($v);
}
}
if(Maze::input('ajax') == 1)
{
return implode('', $result);
}
//print_r($result);die;
return $result;
}
/**
* update_class
*
* @return string
*/
public function update_class($param)
{
$class = '';
if(isset($param['show']))
{
if(is_array($param['show']))
{
$parent = array();
$child = array();
foreach($param['show'] as $k => $v)
{
if(strpos($v, '_') !== false)
{
$temp = explode('_', $v);
$parent[$temp[0]] = 'show_' . $temp[0];
$child[$v] = 'show_' . $v;
}
elseif($v == 'no')
{
$child[$v] = 'show_no';
}
}
$class = implode(' ', $parent) . ' ' . implode(' ', $child);
}
else
{
$class = 'show_input';
}
}
return $class;
}
/**
* update_html
*
* @return string
*/
public function update_html($param, $key, $index, $data, &$result)
{
if(isset($param['name']) && $param['update'] != 'hidden' && is_string($param['update']))
{
$result[$key] .= Html::pageLeft($param);
}
if($param['update'])
{
# 名称带前缀
$param['name'] = 'update_' . $param['ext'];
$param['index'] = $index;
//$param['value'] = '';
# 得到当前的值
if(isset($data[$index]) && !isset($param['value']))
{
$param['value'] = $data[$index];
}
# 得到下级的值
if(isset($param['child_value']))
{
$param['child_value'] = $this->value($param['child_value'], $data);
}
# 验证option是否是匿名函数
if(isset($param['option']))
{
$param['option'] = $this->option($param['option']);
}
# 处理下级字段
if(isset($param['child']))
{
$param['child'] = $this->option($param['child']);
if(isset($data[$key . '_parent']))
{
$param['parent_value'] = $data[$key . '_parent'];
}
}
# 设置默认值
if(isset($param['default']))
{
$param['default'] = $this->option($param['default']);
}
$method = $param['update'];
if(is_array($method))
{
$result[$key] .= '';
$tab = '';
$this->update_child(array(), $method, $key, $result, -1);
if(isset($param['value']) && $param['value'])
{
$param['value'] = json_decode(base64_decode($param['value']), true);
foreach($param['value'] as $k => $v)
{
$this->update_child($v, $method, $key, $result, $k);
$active = '';
if($k == 0)
{
$active = 'active';
}
$k = $k+1;
$tab .= '第'.$k.'条';
}
}
else
{
$this->update_child(array(), $method, $key, $result, 0);
$tab = '第1条';
}
$result[$key] = str_replace('{tab}', $tab, $result[$key]);
}
else
{
if(isset($param['update_input']) && isset($data[$index . '_input']))
{
$param['update_input_data'] = explode(',', $data[$index . '_input']);
}
$result[$key] .= Html::$method($param);
}
if(isset($param['desc']) && ($param['update'] == 'text' || $param['update'] == 'password'))
{
$result[$key] .= Html::desc($param['desc']);
}
}
}
/**
* update_action
*
* @return array
*/
private function update_child($data, $param, $key, &$result, $index)
{
$style = '';
if($index != 0)
{
$style = 'style="display:none;"';
}
$result[$key] .= '';
}
/**
* update_action
*
* @return array
*/
public function update_action()
{
$method = false;
$config = $this->config();
# 验证头部权限
$common = array();
if(isset($config['auth']) && is_string($config['auth']))
{
$top = Maze::load('manage/auth.getTop', array($config['auth']));
if($top)
{
//Maze::setInput('update_' . $config['auth_key'], $top['value']);
$common[$config['auth_key']] = $top['value'];
}
}
$input = Maze::preInput('update_');
if($this->id > 0)
{
# 新增权限
if(Maze::load('manage/auth.oper', 3) == false)
{
return Maze::out('no');
}
Maze::setInput('where_id', $this->id);
Maze::setInput('update_where_id', $this->id);
$info = $this->load();
if($info)
{
$method = 'update';
$col = Maze::input('col');
$value = Maze::input('value');
if($col && $value)
{
$method = 'update_' . $col;
Maze::setInput('set_' . $col, $value);
Maze::setInput('update_' . $col, $value);
$this->load($method);
Log::add($this->project, $this->table, array('method' => $method, 'input' => array($col => $value)), $this->id);
Maze::out('yes');
return;
}
$prefix = 'set';
}
}
if(!$method)
{
# 新增权限
if(Maze::load('manage/auth.oper', 4) == false)
{
return Maze::out('no');
}
$method = 'insert';
$prefix = 'add';
}
if($method && $input)
{
$update = array();
$i = 0;
$c = array();
foreach($input as $k => $v)
{
if($v && is_string($v) && strpos($v, 'copy.') !== false)
{
$temp = str_replace('copy.', '', $v);
if(isset($input['update_' . $temp]))
{
$v = $input['update_' . $temp];
}
}
if(strpos($k, '_c_') !== false)
{
if($v)
{
$temp = explode('_c_', $k);
$k = str_replace('update_', $prefix . '_', $temp[0]);
if(!isset($c[$k]))
{
$c[$k] = array();
}
if(strpos($temp[1], '_i_') !== false)
{
$temp = explode('_i_', $temp[1]);
if(!isset($c[$k][$temp[1]]))
{
$c[$k][$temp[1]] = 0;
}
else
{
$c[$k][$temp[1]]++;
}
$num = $c[$k][$temp[1]];
$update[$i][$k][$num][$temp[1]] = $v;
}
else
{
//$update[$i][$k][$c][$temp[1]] = $v;
}
}
}
elseif(strpos($k, '__') !== false)
{
$temp1 = explode('__', $k);
$temp2 = explode('_', $temp1[0]);
$i = $temp2[1]-1;
if(strpos($k, 'where_id') !== false)
{
//$update[$i]['where_id'] = $v;
}
else
{
$update[$i]['add_' . $temp1[1]] = $v;
}
}
else
{
$k = str_replace('update_', $prefix . '_', $k);
if(strpos($k, 'where_id') !== false)
{
if($method != 'insert')
{
$update[$i]['where_id'] = $v;
}
}
else
{
$update[$i][$k] = $v;
}
}
if($common)
{
foreach($common as $ki => $vi)
{
$update[$i][$prefix . '_' . $ki] = $vi;
}
}
}
//print_r($input);
//print_r($update);die;
foreach($update as $k => $v)
{
$method = 'insert';
if(isset($v['where_id']) && $v['where_id'] > 0)
{
$method = 'update';
}
$id = $this->load($method, $v);
if($id)
{
Log::add($this->project, $this->table, array('method' => $method, 'input' => $v), $this->id > 0 ? $this->id : $id);
}
}
}
Maze::out('yes');
}
/**
* delete_action
*
* @return array
*/
public function delete_action()
{
# 新增权限
if(Maze::load('manage/auth.oper', 5) == false)
{
return Maze::out('no');
}
$this->load('delete');
//$this->url(1)
Maze::out('yes');
}
/**
* stat 为主动统计提供相关数据
*
* @return array
*/
public function stat()
{
//bundle 包依赖工具
$config = $this->config();
if(empty($config['manage']['stat']))
{
Maze::abert('该项目主动统计功能未开放');
}
$data = $config['manage'];
$data['table'] = '';
$method = $this->project . '/' . $this->table . '-total';
$gp = Maze::input('gp', 'month,day');
$col = is_string($gp) ? explode(',', $gp) : $gp;
$data['search'] = $this->stat_search($config['struct'], $gp, $config['manage']['stat'], $config['manage']['search']);
foreach($col as $k => $v)
{
$this->stat_data($data, $config['struct'], $v, $method);
}
return $data;
}
/**
* stat 为主动统计提供导出数据
*
* @return array
*/
public function stat_excel()
{
Maze::$global['base']['excel'] = 1;
//print_r($data['thead']);die;
$data = $this->stat();
if($data && isset($data['list']))
{
require MAZE_PROJECT_PATH . 'src/Excel/PHPExcel.php';
$xls = new \PHPExcel();
$xls->getProperties()->setCreator("Maze Stat Excel")
->setLastModifiedBy("Maze Stat Excel")
->setTitle("Office 2007 XLSX Maze Document")
->setSubject("Office 2007 XLSX Maze Document")
->setDescription("Mazephp Stat Excel")
->setKeywords("Mazephp Stat Excel")
->setCategory("Mazephp Stat Excel");
$info = $this->info();
$name = $info['list_header'] . '的' . $info['stat_desc'] . '-' . $info['project_name'];
$start = 65;
$s = 0;
foreach($data['thead'] as $k => $v)
{
if($s > 0)
{
$xls->createSheet();
}
$act = $xls->setActiveSheetIndex($s);
$act->setTitle($v[0]);
$s++;
$i = 0;
foreach($v as $kj => $vj)
{
$num = 1;
$cell = $this->_getChr($start, $i);
$act->setCellValue($cell . '' . $num, $vj);
$act->getColumnDimension($cell)->setWidth(20);
$i++;
if(isset($data['list'][$k]))
{
foreach($data['list'][$k] as $ki => $vi)
{
if(isset($vi[$kj]))
{
$num += 1;
$act->setCellValue($cell . $num, $vi[$kj]);
}
}
}
}
}
//$act->getStyle('A1:'.$k.'20')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 垂直居中
//$act->getStyle('A1:'.$k.'20')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 水平居中
$xls->setActiveSheetIndex(0);
$filename = $name . '.xlsx';
header('Pragma: public');
header('Expires: 0');
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Content-Type:application/force-download');
header('Content-Type:application/vnd.ms-execl');
header('Content-Type:application/octet-stream');
header('Content-Type:application/download');
header("Content-Disposition:attachment;filename='".$filename."'");
header('Content-Transfer-Encoding:binary');
$write = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
$write->save('php://output');
}
}
/**
* stat_search
*
* @return mixed
*/
private function stat_search($struct, $col, $stat, $search)
{
unset($struct['id']);
unset($struct['cdate']);
if($stat)
{
$stat = explode(',', $stat);
foreach($stat as $k => $v)
{
if(isset($struct[$v]))
{
unset($struct[$v]);
}
}
}
$struct['day']['name'] = '天';
$struct['month']['name'] = '月';
$struct['year']['name'] = '年';
$param['update'] = 'checkbox';
$param['name'] = 'gp';
$param['value'] = $col;
$param['option'] = $struct;
$html = '';
return $html;
}
/**
* stat_data
*
* @return mixed
*/
private function stat_data(&$data, $struct, $col, $method)
{
Maze::setInput('group', $col);
switch($col)
{
case 'year':
$name = '按年统计';
break;
case 'month':
$name = '按月统计';
break;
case 'day':
$name = '按天统计';
break;
default:
$name = $struct[$col]['name'];
break;
}
$list = Maze::load($method, array('gp' => $col));
$data['table'] .= '';
$data['table'] .= ''.$name.' | 统计数字 |
';
$data['thead'][$col] = array($name, '统计数字');
if($list)
{
$data['table'] .= '';
$total = 0;
foreach($list as $k => $v)
{
if(isset($v[$col]))
{
if(isset($struct[$col]['option']))
{
$option = $this->option($struct[$col]['option']);
if(isset($option[$v[$col]]))
{
$v[$col] = $option[$v[$col]];
if(is_array($v[$col]))
{
$v[$col] = $v[$col]['name'];
}
}
}
$data['table'] .= ''.$v[$col].' | '.$v['total'].' |
';
$total += $v['total'];
$data['list'][$col][] = array($v[$col], $v['total']);
}
}
$data['table'] .= '';
if($total > 0 && $k > 0)
{
$data['table'] .= '汇总 | '.$total.' |
';
$data['list'][$col][] = array('汇总', $total);
}
}
$data['table'] .= '
';
}
/**
* load
*
* @return mixed
*/
private function load($method = 'one', $param = array())
{
return Maze::load($this->project . '/' . $this->table . '-' . $method, $param);
}
/**
* __call
*
* @return object
*/
public function __call($method, $param)
{
if(strpos($method, 'html_') !== false)
{
return Html::text($param);
}
return $this;
}
/**
* option
*
* @return string
*/
private function option($option)
{
if(is_object($option))
{
$function = $option;
$option = $function();
}
return $option;
}
/**
* option
*
* @return string
*/
public function manage()
{
$config = $this->config();
return $config;
}
/**
* valid
*
* @return string
*/
public function valid()
{
//http://placehold.it/350x150
$config = $this->config();
if(isset($config['struct']))
{
$rule = array();
$rule['required']['regex'] = 'none';
$rule['required']['alertText'] = '此项不能为空';
$rule['required']['alertTextCheckboxMultiple'] = '请选择一个选项';
$rule['required']['alertTextCheckboxe'] = '请选择一个选项';
$rule['required']['alertTextDateRange'] = '日期范围不可空白';
foreach($config['struct'] as $k => $v)
{
if(isset($v['match']) && $v['match'] != 'option' && is_string($v['match']) && strpos($v['match'], '/') !== false)
{
$rule[$k]['regex'] = $v['match'];
$rule[$k]['alertText'] = $v['desc'];
}
}
}
$script = '';
//print_r($script);die;
return $script;
}
/**
* save
*
* @return string
*/
public function save()
{
$config = $this->config();
if(isset($config['manage']['save']))
{
return 'yes';
}
return 'no';
}
}