12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | database.php 后台管理 数据库管理
- |--------------------------------------------------------------------------
- */
- namespace MazeApp\Manage;
- use Maze;
- use MazeApp\Manage\Lib\Input as Html;
- use MazeApp\Manage\Lib\Log;
- Maze::load('manage/auth.init');
- class Database
- {
- /**
- * project
- *
- * @var string
- */
- private $project;
- /**
- * table
- *
- * @var string
- */
- private $table;
-
- /**
- * menu
- *
- * @var string
- */
- private $menu;
- /**
- * menu_id
- *
- * @var string
- */
- private $menu_id;
- /**
- * id
- *
- * @var string
- */
- private $id;
- /**
- * config
- *
- * @var array
- */
- private $config;
- /**
- * search
- *
- * @var bool
- */
- private $search = false;
- /**
- * __construct
- *
- * @return mixed
- */
- public function __construct()
- {
- $this->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 = '-[<a href="'. $url .'">' . $name . '</a>]';
- 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'])
- {
- $info['key'] = Maze::input('oper_key');
- if($info['key'])
- {
- $info['parent'] .= '&key=' . $info['key'];
- }
- $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'] = '<table border=1 width=100% height=100%>';
-
- 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'] .= '<tr><td style=width:20%;line-height:1.75;font-weight:bold>'.$k.'</td><td style=width:20%;word-break:break-all;word-wrap:break-word;>'.$v['type'].'</td><td style=word-break:break-all;word-wrap:break-word;>'.$v['name'].'</td></tr>';
- }
- }
-
- $config['manage']['struct'] .= '</table>';
-
- $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) . ' <br />';
- }
-
- /**
- * 列表页搜索
- *
- * @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 .= ' <a href="#maze_modal" data-toggle="modal" class="btn btn-default" onclick="$(\'#maze_modal_body\').html(\''.$t[1].'\')">'.$k.'</a>';
- }
- elseif(strpos($v, '|') !== false)
- {
- $t = explode('|', $v);
- $result .= $t[0] . ' <a href="javascript:;" class="btn btn-warning" '.$t[1].'>'.$k.'</a>';
- }
- elseif(strpos($v, 'onclick') !== false)
- {
- $result .= ' <a href="javascript:;" class="btn btn-warning" '.$v.'>'.$k.'</a>';
- }
- elseif(strpos($v, 'http://') !== false)
- {
- $result .= ' <a href="javascript:;" class="btn btn-default" onclick="jump(\''.Maze::url($v).'\')">'.$k.'</a>';
- }
- else
- {
- $result .= ' <a href="javascript:;" class="btn btn-danger" onclick="load(\''.Maze::url($v).'\')">'.$k.'</a>';
- }
- }
-
- 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 = '<input type="hidden" value="2" name="excel" id="excel"><button class="btn btn-primary" onclick="$(\'#form1\').attr(\'action\', \''.$this->url(1).'\');list_search($(this))" type="button" style="height: 38px;margin-left:10px;">搜索</button>';
- //$result .= '<form id="f1" action="'.$this->url(1).'" method="post">';
- $prefix = 'search_';
- $result .= $node[0];
- if(isset($config['manage']['search']['time']))
- {
- foreach($config['manage']['search']['time'] as $k => $v)
- {
- $this->list_search_time($result, $search, $prefix, $k, $v);
- }
- }
-
- if(isset($config['manage']['search']['date']))
- {
- foreach($config['manage']['search']['date'] as $k => $v)
- {
- $this->list_search_time($result, $search, $prefix, $k, $v, 'date');
- }
- }
- if(isset($config['manage']['search']['select']))
- {
- foreach($config['manage']['search']['select'] as $k => $v)
- {
- $this->list_search_select($result, $search, $prefix . 'option_' . $k, $v['lang'], $this->option($v['option']), $v['default']);
- }
- }
- /*排序加到列表表头中
- if(isset($config['manage']['search']['order']))
- {
- $this->list_search_select($result, $search, $prefix . 'order', '选择排序', $config['manage']['search']['order']);
- $this->list_search_select($result, $search, $prefix . 'desc', false, array('asc' => '正序','desc' => '倒序'));
- }
- */
- if(isset($config['manage']['search']['fulltext']))
- {
- //$result .= '<br />';
-
- /*
- $this->list_search_select($result, $search, $prefix . 'fulltext', '选择检索项', $config['manage']['search']['fulltext']);
-
- $this->list_search_select($result, $search, $prefix . 'fulltext_type', '', array
- (
- 1 => '精确匹配',
- 2 => '模糊匹配',
- 3 => '大于',
- 4 => '大于等于',
- 5 => '小于',
- 6 => '小于等于',
- ), 1);
- */
- foreach($config['manage']['search']['fulltext'] as $k => $v)
- {
- $value = array();
- $value['name'] = $prefix.'fulltext_' . $k;
- $value['class'] = 'form-control';
- if(isset($search[$value['name']]))
- {
- $value['value'] = $search[$value['name']];
- }
- $value['placeholder'] = $v;
- $result .= Html::text($value, false) . ' ';
- }
- }
- # 列表页按钮
- if(isset($config['manage']['search_button']))
- {
- $btn .= $this->button_list($config['manage']['search_button']);
- }
-
- $result .= $btn . $node[1];
- //$result .= '</form>';
- }
- //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[] = '<th><input type="checkbox" class="checkbox-checkall" name="checkall" id="checkall" value="list"/></th>';
- }
- 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 = ' <a class="glyphicon glyphicon-remove-sign" style="color:black;" href="'.str_replace('{order}', '', $link).'" title="取消排序"></a>';
- }
- else
- {
- $asc = 'desc';
- if(!isset($v['order']) && $k != 'id')
- {
- $cancel = ' <a class="glyphicon glyphicon-circle-arrow-down" style="color:black;" href="'.str_replace('{order}', '&' . $order . '=desc', $link).'" title="降序"></a>';
- }
- }
-
- if($asc == 'asc')
- {
- $asc = 'desc';
- $icon = 'down';
- $text = '降序';
- }
- else
- {
- $asc = 'asc';
- $icon = 'up';
- $text = '升序';
- }
-
- $link = str_replace('{order}', '&' . $order . '=' . $asc, $link);
-
- $result[] = '<th><a href="'.$link.'" title="'.$text.'" style="color:black;">' . $v['name'] . ' <span class="glyphicon glyphicon-circle-arrow-'.$icon.'"></span></a>'.$cancel.'</th>';
- }
- else
- {
- $result[] = '<th>' . $v['name'] . '</th>';
- }
- }
- }
- if(Maze::load('manage/auth.oper', 3) != false && Maze::load('manage/auth.oper', 5) != false && isset($config['manage']['_list']['value']))
- {
- $result[] = '<th>'.$config['manage']['_list']['name'].'</th>';
- }
- }
- }
- 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] = '<tr>';
-
- if(isset($config['manage']['mul']))
- {
- $result[$k] .= '<td><input type="checkbox" name="mul_where_id[]" class="checkbox-checkall-list" value="'.$v['id'].'"/></td>';
- }
- 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], '<font color="red">' . $high[$ki] . '</font>', $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] .= '<td class="edit" data-col="'.$ki.'" data-url="' . $url . '" data-id="'.$v['id'].'" data-key="'.$this->project.'" data-table="'.$this->table.'" data-content="' . $show . '" data-type="'.$vi['edit'].'">' . $text . '</td>';
- }
- else
- {
- $result[$k] .= '<td>' . $text . '</td>';
- }
- }
- }
- if(isset($config['manage']['_list']['value']))
- {
- $result[$k] .= '<td>';
- foreach($config['manage']['_list']['value'] as $kj => $vj)
- {
- $pj = false;
-
- $h = '<span class="am-icon-pencil-square-o"></span>';
- $oper = true;
- if($kj == 3)
- {
- $oper = Maze::load('manage/auth.oper', 3);
- }
- elseif($kj == 6)
- {
- $oper = Maze::load('manage/auth.oper', 5);
-
- $h = '<span class="am-icon-trash-o"></span>';
- }
-
- 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] .= '<a href="' . $this->url($kj, $v['id'], $pj) . '" class="oper_' . $kj . '"><button type="button" class="am-btn am-btn-default am-btn-xs am-text-secondary">' . $h . $vj . '</button></a> ';
- }
- }
- $result[$k] .= '</td>';
- }
- $result[$k] .= '</tr>';
- }
- }
- 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 = '<button class="btn btn-danger" onclick="$(\'#form1\').attr(\'action\', \''.$this->url(1).'\');list_mul($(this))" type="button" style="height: 38px;margin-left:10px;">批量更新</button>';
- $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 = '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="150"></a>';
- }
- 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] = '<div class="form-group '.$class.'"><div class="margin-top">';
-
- $v['ext'] = $v['col'] = $k;
-
- $v['update'] = $v['update'] ? $v['update'] : 'text';
-
- if(isset($v['match']) && $v['match'] != 'option')
- {
- if(is_string($v['match']) && strpos($v['match'], '/') !== false)
- {
- $v['valid'] = 'validate[required,custom['.$k.']]';
- }
- else
- {
- $v['valid'] = 'validate[required]';
- }
- }
-
- if(is_string($v['update']) && strpos($v['update'], 'copy.') !== false)
- {
- $v['value'] = $v['update'];
- $v['update'] = 'hidden';
- }
-
- $this->update_html($v, $k, $k, $data, $result);
- $result[$k] .= '</div></div>';
- }
- 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] .= '<ul class="nav nav-tabs">{tab}<li><a href="javascript:;" class="maze_form_add" toggle_key="'.$key.'">增加一条</a></li></ul>';
- $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 .= '<li class="'.$active.'"><a href="javascript:;" id="tab-'.$key.'-child-'.$k.'" toggle_key="'.$key.'" toggle_id="'.$k.'" toggle_child=".maze_'.$key.'_child" toggle="#'.$key.'-child-'.$k.'" onclick="showToggle($(this))">第'.$k.'条</a></li>';
- }
- }
- else
- {
- $this->update_child(array(), $method, $key, $result, 0);
- $tab = '<li class="active"><a href="javascript:;" id="tab-'.$key.'-child-1" toggle_key="'.$key.'" toggle_id="1" toggle_child=".maze_'.$param['col'].'_child" toggle="#'.$key.'-child-1" onclick="showToggle($(this))">第1条</a></li>';
- }
- $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] .= '<div id="'.$key.'-child-'.($index+1).'" class="maze_'.$key.'_child maze_form_add_child" '.$style.'><div class="maze_form_delete">[删除]</div>';
- foreach($param as $k => $v)
- {
- $result[$key] .= '<div class="form-group">';
-
- $v['type'] = $v['update'];
- if($index < 0)
- {
- $v['ext'] = $key . '_c_' . $v['col'];
- }
- else
- {
- $v['ext'] = $key . '_c_' . $index . '_i_' . $v['col'];
- }
- $this->update_html($v, $key, $v['col'], $data, $result);
-
- $result[$key] .= '</div>';
- }
-
- $result[$key] .= '</div>';
- }
- /**
- * 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 = '<form action="'.$this->url(7).'" method="post" id="f1">';
- $html .= '<div class="form-group">';
- $html .= Html::pageLeft(array('name' => '统计项', 'col' => 'col'));
- $html .= Html::radio($param);
- $html .= '</div>';
- $input = Maze::preInput('search_');
- if(isset($search['date']))
- {
- foreach($search['date'] as $k => $v)
- {
- $html .= '<div class="form-group">';
- $this->list_search_time($html, $input, 'search_', $k, $v, 'date');
- $html .= '</div>';
- }
-
- $input = Maze::preInput('search_');
-
- if($input)
- {
- foreach($input as $k => $v)
- {
- if($v && strpos($k, 'option_') !== false)
- {
- Maze::setInput(str_replace('search_', '', $k), $v);
- }
- }
- }
- }
-
- if(isset($search['fulltext']))
- {
- $html .= '<div class="form-group">';
- $this->list_search_select($html, $input, 'search_fulltext', '选择检索项', $search['fulltext']);
-
- $this->list_search_select($html, $input, 'search_fulltext_type', '', array
- (
- 1 => '精确匹配',
- 2 => '模糊匹配',
- 3 => '大于',
- 4 => '大于等于',
- 5 => '小于',
- 6 => '小于等于',
- ), 1);
- $value = array();
- $value['name'] = 'search_fulltext_value';
- $value['class'] = 'form-control';
- if(isset($input[$value['name']]))
- {
- $value['value'] = $input[$value['name']];
-
- $fix = '';
- if($input['search_fulltext_type'] == 2)
- {
- $fix = '%';
- }
- Maze::setInput('option_' . $input['search_fulltext'], $fix . $value['value'] . $fix);
- //print_r(Maze::input('option_name'));die;
- }
- $html .= Html::text($value, false);
- $html .= '</div>';
- }
- $html .= '<input type="hidden" name="excel" id="excel" value="2"><a href="javascript:;" class="btn btn-primary" onclick="$(\'#f1\').attr(\'action\', \''.$this->url(7).'\').submit();">确定</a>';
-
- $html .= ' <a href="javascript:;" class="btn btn-warning" onclick="$(\'#f1\').attr(\'action\', \''.$this->url(9).'\').submit();">下载报表</a>';
- $html .= '</form>';
-
- 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'] .= '<div style="overflow-y:auto;width:100%;max-height:600px;"><table class="table table-bordered table-striped">';
-
- $data['table'] .= '<thead><tr><th>'.$name.'</th><th>统计数字</th></tr></thead>';
-
- $data['thead'][$col] = array($name, '统计数字');
- if($list)
- {
- $data['table'] .= '<tbody>';
- $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'] .= '<tr><td width="60%" style="word-break:break-all;word-wrap:break-word;">'.$v[$col].'</td><td>'.$v['total'].'</td></tr>';
- $total += $v['total'];
-
- $data['list'][$col][] = array($v[$col], $v['total']);
- }
-
- }
- $data['table'] .= '</tbody>';
- if($total > 0 && $k > 0)
- {
- $data['table'] .= '<tr><td style="font-weight:bold">汇总</td><td style="font-weight:bold">'.$total.'</td></tr>';
- $data['list'][$col][] = array('汇总', $total);
- }
- }
-
- $data['table'] .= '</table></div>';
- }
- /**
- * 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 = '<script>';
-
-
- $script .= '(function($){
- $.fn.validationEngineLanguage = function(){
- };
- $.validationEngineLanguage = {
- newLang: function(){
- $.validationEngineLanguage.allRules = ';
- //此处进行了转义过滤,其实也可以使用json_encode的第二个参数,不过只支持5.4以上版本,考虑到虚拟主机就放弃了
- $script .= str_replace(array('\\\\', '\\/', '"/', '/"'), array('\\', '/', '/', '/'), json_encode($rule)) . ';';
- $script .= '}
- };
- $.validationEngineLanguage.newLang();
- })(jQuery);';
- $script .= '$(document).ready(function(){
- $(".form1").validationEngine("attach");
- });';
- $script .= '</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';
- }
- }
|