b4620ee47eb2ccff042e50fa01b5561af69bd17e.svn-base 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | database.php 后台管理 数据库管理
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace MazeApp\Manage;
  8. use Maze;
  9. use MazeApp\Manage\Lib\Input as Html;
  10. use MazeApp\Manage\Lib\Log;
  11. Maze::load('manage/auth.init');
  12. class Database
  13. {
  14. /**
  15. * project
  16. *
  17. * @var string
  18. */
  19. private $project;
  20. /**
  21. * table
  22. *
  23. * @var string
  24. */
  25. private $table;
  26. /**
  27. * menu
  28. *
  29. * @var string
  30. */
  31. private $menu;
  32. /**
  33. * menu_id
  34. *
  35. * @var string
  36. */
  37. private $menu_id;
  38. /**
  39. * id
  40. *
  41. * @var string
  42. */
  43. private $id;
  44. /**
  45. * config
  46. *
  47. * @var array
  48. */
  49. private $config;
  50. /**
  51. * search
  52. *
  53. * @var bool
  54. */
  55. private $search = false;
  56. /**
  57. * __construct
  58. *
  59. * @return mixed
  60. */
  61. public function __construct()
  62. {
  63. $this->project = Maze::input('key');
  64. $this->table = Maze::input('table');
  65. $this->id = Maze::input('update_where_id') ? Maze::input('update_where_id') : Maze::input('where_id');
  66. $this->menu = Maze::input('menu');
  67. $this->menu_id = Maze::input('menu_id');
  68. }
  69. /**
  70. * url
  71. *
  72. * @return array
  73. */
  74. private function url($key = false, $id = 0, $table = false)
  75. {
  76. $table = $table ? $table : $this->table;
  77. if(strpos($table, 'http://') !== false || strpos($table, 'javascript') !== false)
  78. {
  79. return $table;
  80. }
  81. if($key <= 3)
  82. {
  83. $where = Maze::preInput('option_');
  84. if($where)
  85. {
  86. if(isset($where['where_id']))
  87. {
  88. //unset($where['where_id']);
  89. }
  90. $table .= '&' . http_build_query($where);
  91. }
  92. $oper = Maze::preInput('oper_');
  93. if($oper)
  94. {
  95. $table .= '&' . http_build_query($oper);
  96. }
  97. }
  98. $param = 'key=' . $this->project . '&table=' . $table . '&menu=' . $this->menu . '&menu_id=' . $this->menu_id;
  99. $url = array
  100. (
  101. 0 => Maze::url('project/database?key=' . $this->project),
  102. 1 => Maze::url('project/database/list?' . $param),
  103. 2 => Maze::url('project/database/update?' . $param),
  104. 3 => Maze::url('project/database/update?' . $param . '&where_id=' . $id),
  105. 4 => Maze::url('database.update_action'),
  106. 5 => Maze::url(''),
  107. 6 => Maze::url('database.delete_action?' . $param . '&where_id=' . $id),
  108. 7 => Maze::url('project/database/stat?' . $param),
  109. 8 => Maze::url('database.list_excel?' . $param),
  110. 9 => Maze::url('database.stat_excel?' . $param),
  111. );
  112. if($key >= 10 && $key < 20)
  113. {
  114. return $url[1];
  115. }
  116. elseif($key >= 20 && $key < 30)
  117. {
  118. return $url[2];
  119. }
  120. if($key >= 0 && isset($url[$key]))
  121. {
  122. return $url[$key];
  123. }
  124. return $url;
  125. }
  126. /**
  127. * 获得refer 暂时无用了
  128. *
  129. * @return string
  130. */
  131. private function refer($type = 0)
  132. {
  133. return '';
  134. switch($type)
  135. {
  136. case 0:
  137. $name = '数据库操作';
  138. break;
  139. case 1:
  140. $name = '数据列表';
  141. break;
  142. case 5:
  143. $name = '项目列表';
  144. break;
  145. }
  146. $name = '返回上一级';
  147. $url = $this->url($type);
  148. $refer = '-[<a href="'. $url .'">' . $name . '</a>]';
  149. return $refer;
  150. }
  151. /**
  152. * info 处理一些基本的信息,供mote模板使用
  153. *
  154. * @return array
  155. */
  156. public function info()
  157. {
  158. $config = $this->config();
  159. $menu = isset($config['lang']) ? $config['lang'] : $this->project . '-' . $this->table;
  160. $display = '';
  161. # 新增权限
  162. $insert_state = Maze::load('manage/auth.oper', 4);
  163. if((isset($config['manage']['insert']) && $config['manage']['insert'] == false) || !isset($config['manage']['insert']) || $insert_state == false)
  164. {
  165. $display = 'display:none';
  166. }
  167. $info = array
  168. (
  169. 'action' => $this->url(4),
  170. 'list' => $this->url(1),
  171. 'mul' => $this->url(1),
  172. 'add' => 'location.href=\'' . $this->url(2) . '\'',
  173. 'add_state' => $display,
  174. 'project' => $this->project,
  175. 'table' => $this->table,
  176. 'id' => $this->id,
  177. 'main' => '数据管理' . $this->refer(5),
  178. 'list_header' => $menu,
  179. 'list_desc' => '数据列表',
  180. 'project_name' => Maze::load('manage/project.name', $this->project),
  181. 'update_header' => $menu,
  182. 'update_desc' => '更新数据',
  183. 'updateMul' => (!$this->id && isset($config['manage']['updateMul']) && $config['manage']['updateMul']) ? '增加一条' : false,
  184. 'updateDesc' => isset($config['manage']['desc']) ? $config['manage']['desc'] : '',
  185. 'listDesc' => isset($config['desc']) ? $config['desc'] : '',
  186. 'struct' => isset($config['manage']['struct']) ? $config['manage']['struct'] : '',
  187. 'stat_desc' => '主动统计',
  188. );
  189. $info['parent'] = Maze::input('oper_parent');
  190. if($info['parent'])
  191. {
  192. $info['key'] = Maze::input('oper_key');
  193. if($info['key'])
  194. {
  195. $info['parent'] .= '&key=' . $info['key'];
  196. }
  197. $info['parent'] = 'location.href=\'' . $this->url(11, 0, $info['parent']) . '\'';
  198. $info['parent_state'] = '';
  199. }
  200. else
  201. {
  202. $info['parent_state'] = 'display:none';
  203. }
  204. $info['save'] = Maze::input('oper_save_jump');
  205. if($info['save'])
  206. {
  207. $info['list'] = $this->url(11, 0, $info['save']);
  208. }
  209. $search = Maze::preInput('search_');
  210. if($search)
  211. {
  212. $info['mul'] .= '&' . http_build_query($search);
  213. }
  214. if($this->id > 0)
  215. {
  216. $info['update_header'] = $menu . '-更新数据' . $this->refer(1);
  217. }
  218. return $info;
  219. }
  220. /**
  221. * 获取表名
  222. *
  223. * @return array
  224. */
  225. public function getTableName($param)
  226. {
  227. if($param[1] == 'other') return '无数据表';
  228. $path = Maze::load('manage/project.path', $param[0]);
  229. $config = $this->config($param[0], $path, $param[1]);
  230. return isset($config['lang']) ? $config['lang'] : $param[0];
  231. }
  232. /**
  233. * config
  234. *
  235. * @return array
  236. */
  237. public function config($project = false, $path = false, $table = false)
  238. {
  239. if(!$project)
  240. {
  241. $project = $this->project;
  242. $table = $this->table;
  243. }
  244. Maze::$global['manage']['project'] = $project;
  245. $config = Maze::load('manage/project.config', $project);
  246. $project = $config['lib'];
  247. $path = $path ? $path : $config['path'];
  248. if($project && $path && empty($this->config[$path]))
  249. {
  250. $this->config[$path] = array();
  251. $dir = $path . 'database/';
  252. if(is_dir($dir))
  253. {
  254. $database = scandir($dir);
  255. foreach($database as $k => $v)
  256. {
  257. if(strpos($v, '.php') !== false)
  258. {
  259. $k = str_replace('.php', '', $v);
  260. $this->config[$path][$k] = $this->struct($dir . $v, $project);
  261. }
  262. }
  263. }
  264. }
  265. return ($table && isset($this->config[$path][$table])) ? $this->config[$path][$table] : $this->config[$path];
  266. }
  267. /**
  268. * 对database配置文件进行解析
  269. *
  270. * @return array
  271. */
  272. private function struct($file, $project = false)
  273. {
  274. $config = include($file);
  275. if(isset($config['struct']) && is_array($config['struct']['id']))
  276. {
  277. $config['update'] = false;
  278. $config['manage']['struct'] = '<table border=1 width=100% height=100%>';
  279. foreach($config['struct'] as $k => $v)
  280. {
  281. if(isset($v['search']))
  282. {
  283. /*
  284. if(strpos($v['search'], 'order') !== false)
  285. {
  286. $config['manage']['search']['order'][$k] = $v['name'];
  287. }
  288. */
  289. if(strpos($v['search'], 'time') !== false)
  290. {
  291. $config['manage']['search']['time'][$k] = $v['name'];
  292. }
  293. if(strpos($v['search'], 'date') !== false)
  294. {
  295. $config['manage']['search']['date'][$k] = $v['name'];
  296. }
  297. if(strpos($v['search'], 'fulltext') !== false)
  298. {
  299. $config['manage']['search']['fulltext'][$k] = $v['name'];
  300. isset($v['match']) && $config['request']['list']['option'][$k] = array('option', 'like');
  301. }
  302. if(isset($v['option']) && strpos($v['search'], 'select') !== false)
  303. {
  304. $config['manage']['search']['select'][$k]['option'] = $v['option'];
  305. $config['manage']['search']['select'][$k]['lang'] = $v['name'];
  306. if(isset($v['default']))
  307. {
  308. $config['manage']['search']['select'][$k]['default'] = $v['default'];
  309. }
  310. }
  311. }
  312. if(isset($v['table']) && !isset($config['end']))
  313. {
  314. $config['end']['insert'] = $v['value'];
  315. }
  316. if(isset($v['update']))
  317. {
  318. if(!isset($config['manage']['insert']))
  319. {
  320. $config['manage']['insert'] = true;
  321. }
  322. $config['update'] = true;
  323. }
  324. if(isset($v['type']))
  325. {
  326. $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>';
  327. }
  328. }
  329. $config['manage']['struct'] .= '</table>';
  330. $config['manage']['_list'] = array
  331. (
  332. 'col' => 'manage',
  333. 'name' => '管理',
  334. 'value' => array(),
  335. );
  336. if(!isset($config['manage']['edit']) && $config['update'] == true)
  337. {
  338. # 3和6
  339. $config['manage']['_list']['value'] = array(3 => '编辑');
  340. }
  341. if(isset($config['manage']['list'][0]) && is_array($config['manage']['list'][0]))
  342. {
  343. $config['manage']['_list']['value'] += $config['manage']['list'][0];
  344. unset($config['manage']['list'][0]);
  345. }
  346. elseif(isset($config['manage']['list_button']))
  347. {
  348. $config['manage']['_list']['value'] += $config['manage']['list_button'];
  349. }
  350. if(!$config['manage']['_list']['value'])
  351. {
  352. unset($config['manage']['_list']);
  353. }
  354. }
  355. if(isset($config['auth']))
  356. {
  357. if(is_array($config['auth']))
  358. {
  359. $config['auth']['key'] = $project . '-' . $config['auth']['key'];
  360. }
  361. else
  362. {
  363. $config['auth_key'] = $config['auth'];
  364. $config['auth'] = $project . '-' . $config['auth'];
  365. }
  366. }
  367. return $config;
  368. }
  369. /**
  370. * 列表页搜索
  371. *
  372. * @return array
  373. */
  374. public function list_search_select(&$result, $search, $name, $lang, $option, $default = false)
  375. {
  376. $value = array();
  377. $value['name'] = $name;
  378. $value['lang'] = $lang;
  379. $value['option'] = $option;
  380. if(isset($search[$name]))
  381. {
  382. $value['value'] = $search[$name];
  383. }
  384. if($default)
  385. {
  386. $value['default'] = $default;
  387. }
  388. $result .= Html::select($value, false) . '&nbsp;&nbsp;&nbsp;';
  389. }
  390. /**
  391. * 列表页搜索
  392. *
  393. * @return array
  394. */
  395. public function list_search_time(&$result, $search, $prefix, $key, $lang, $type = 'time')
  396. {
  397. $value['name'] = $prefix . 'option_start_' . $key;
  398. $value['lang'] = '开始' . $lang;
  399. if(isset($search[$value['name']]))
  400. {
  401. $value['value'] = Maze::maketime($search[$value['name']]);
  402. Maze::setInput($value['name'], $value['value'], 'search_');
  403. }
  404. $result .= Html::$type($value) . '&nbsp;-&nbsp;';
  405. $value['name'] = $prefix . 'option_end_' . $key;
  406. $value['lang'] = '结束' . $lang;
  407. if(isset($search[$value['name']]))
  408. {
  409. $value['value'] = Maze::maketime($search[$value['name']]);
  410. Maze::setInput($value['name'], $value['value'], 'search_');
  411. }
  412. $result .= Html::$type($value) . '&nbsp;&nbsp;<br />';
  413. }
  414. /**
  415. * 列表页搜索
  416. *
  417. * @return array
  418. */
  419. public function list_button()
  420. {
  421. # 新增权限
  422. if(Maze::load('manage/auth.oper', 2) == false)
  423. {
  424. return '';
  425. }
  426. $config = $this->config();
  427. $result = '';
  428. # 增加主动统计按钮
  429. if(isset($config['manage']['stat']))
  430. {
  431. $config['manage']['button']['数据统计'] = $this->url(7);
  432. }
  433. # 增加下载报表按钮
  434. if(isset($config['manage']['excel']))
  435. {
  436. $config['manage']['button']['导出数据'] = 'onclick="$(\'#form1\').attr(\'action\', \''.$this->url(8).'\');list_search($(this))"';
  437. }
  438. # 列表页按钮
  439. if(isset($config['manage']['button']))
  440. {
  441. $result .= $this->button_list($config['manage']['button']);
  442. }
  443. return $result;
  444. }
  445. private function button_list($button)
  446. {
  447. $result = '';
  448. foreach($button as $k => $v)
  449. {
  450. if(strpos($v, 'modal|') !== false)
  451. {
  452. $t = explode('modal|', $v);
  453. $result .= '&nbsp;&nbsp;<a href="#maze_modal" data-toggle="modal" class="btn btn-default" onclick="$(\'#maze_modal_body\').html(\''.$t[1].'\')">'.$k.'</a>';
  454. }
  455. elseif(strpos($v, '|') !== false)
  456. {
  457. $t = explode('|', $v);
  458. $result .= $t[0] . '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" '.$t[1].'>'.$k.'</a>';
  459. }
  460. elseif(strpos($v, 'onclick') !== false)
  461. {
  462. $result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" '.$v.'>'.$k.'</a>';
  463. }
  464. elseif(strpos($v, 'http://') !== false)
  465. {
  466. $result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-default" onclick="jump(\''.Maze::url($v).'\')">'.$k.'</a>';
  467. }
  468. else
  469. {
  470. $result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-danger" onclick="load(\''.Maze::url($v).'\')">'.$k.'</a>';
  471. }
  472. }
  473. return $result;
  474. }
  475. /**
  476. * 列表页搜索
  477. *
  478. * @return array
  479. */
  480. public function list_search()
  481. {
  482. # 新增权限
  483. if(Maze::load('manage/auth.oper', 2) == false)
  484. {
  485. return '';
  486. }
  487. $config = $this->config();
  488. $result = '';
  489. # 搜索
  490. if(isset($config['struct']) && isset($config['manage']['search']))
  491. {
  492. $search = Maze::preInput('search_');
  493. $node = Html::node(array('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
  494. $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>';
  495. //$result .= '<form id="f1" action="'.$this->url(1).'" method="post">';
  496. $prefix = 'search_';
  497. $result .= $node[0];
  498. if(isset($config['manage']['search']['time']))
  499. {
  500. foreach($config['manage']['search']['time'] as $k => $v)
  501. {
  502. $this->list_search_time($result, $search, $prefix, $k, $v);
  503. }
  504. }
  505. if(isset($config['manage']['search']['date']))
  506. {
  507. foreach($config['manage']['search']['date'] as $k => $v)
  508. {
  509. $this->list_search_time($result, $search, $prefix, $k, $v, 'date');
  510. }
  511. }
  512. if(isset($config['manage']['search']['select']))
  513. {
  514. foreach($config['manage']['search']['select'] as $k => $v)
  515. {
  516. $this->list_search_select($result, $search, $prefix . 'option_' . $k, $v['lang'], $this->option($v['option']), $v['default']);
  517. }
  518. }
  519. /*排序加到列表表头中
  520. if(isset($config['manage']['search']['order']))
  521. {
  522. $this->list_search_select($result, $search, $prefix . 'order', '选择排序', $config['manage']['search']['order']);
  523. $this->list_search_select($result, $search, $prefix . 'desc', false, array('asc' => '正序','desc' => '倒序'));
  524. }
  525. */
  526. if(isset($config['manage']['search']['fulltext']))
  527. {
  528. //$result .= '<br />';
  529. /*
  530. $this->list_search_select($result, $search, $prefix . 'fulltext', '选择检索项', $config['manage']['search']['fulltext']);
  531. $this->list_search_select($result, $search, $prefix . 'fulltext_type', '', array
  532. (
  533. 1 => '精确匹配',
  534. 2 => '模糊匹配',
  535. 3 => '大于',
  536. 4 => '大于等于',
  537. 5 => '小于',
  538. 6 => '小于等于',
  539. ), 1);
  540. */
  541. foreach($config['manage']['search']['fulltext'] as $k => $v)
  542. {
  543. $value = array();
  544. $value['name'] = $prefix.'fulltext_' . $k;
  545. $value['class'] = 'form-control';
  546. if(isset($search[$value['name']]))
  547. {
  548. $value['value'] = $search[$value['name']];
  549. }
  550. $value['placeholder'] = $v;
  551. $result .= Html::text($value, false) . '&nbsp;';
  552. }
  553. }
  554. # 列表页按钮
  555. if(isset($config['manage']['search_button']))
  556. {
  557. $btn .= $this->button_list($config['manage']['search_button']);
  558. }
  559. $result .= $btn . $node[1];
  560. //$result .= '</form>';
  561. }
  562. //print_r($result);die;
  563. return $result;
  564. }
  565. /**
  566. * 列表页表格头
  567. *
  568. * @return array
  569. */
  570. public function list_thead()
  571. {
  572. # 新增权限
  573. if(Maze::load('manage/auth.oper', 1) == false)
  574. {
  575. return '';
  576. }
  577. $config = $this->config();
  578. $result = array();
  579. if(isset($config['struct']))
  580. {
  581. if(isset($config['manage']['list']) && $config['manage']['list'] == 'update')
  582. {
  583. # 验证头部权限 这里如果是多个选择权限的话,暂时无法实现。。。
  584. if(isset($config['auth']) && is_string($config['auth']))
  585. {
  586. $top = Maze::load('manage/auth.getTop', array($config['auth']));
  587. if($top)
  588. {
  589. //Maze::setInput('update_' . $config['auth'], $top['value']);
  590. //$common[$config['auth']] = $top['value'];
  591. $url = $this->url(3, $top['value']);
  592. }
  593. }
  594. else
  595. {
  596. $url = $this->url(3, 1);
  597. }
  598. return Maze::location($url);
  599. }
  600. else
  601. {
  602. if(isset($config['manage']['mul']))
  603. {
  604. $result[] = '<th><input type="checkbox" class="checkbox-checkall" name="checkall" id="checkall" value="list"/></th>';
  605. }
  606. foreach($config['struct'] as $k => $v)
  607. {
  608. if(isset($v['list']))
  609. {
  610. if(isset($v['list_name']))
  611. {
  612. $v['name'] = $v['list_name'];
  613. }
  614. else
  615. {
  616. $temp = Html::cue(array('name' => $v['name']));
  617. $v['name'] = $temp['name'];
  618. }
  619. if(isset($v['search']) && strpos($v['search'], 'order') !== false)
  620. {
  621. $link = Maze::url();
  622. $order = 'search_order_'.$k.'';
  623. if(strpos($link, $order) !== false)
  624. {
  625. $link = preg_replace('/[?|&]'.$order.'=(desc|asc)/i', '{order}', $link);
  626. }
  627. else
  628. {
  629. $link .= '{order}';
  630. }
  631. $search = Maze::input($order);
  632. $cancel = '';
  633. if($search)
  634. {
  635. $asc = $search;
  636. $cancel = '&nbsp;<a class="glyphicon glyphicon-remove-sign" style="color:black;" href="'.str_replace('{order}', '', $link).'" title="取消排序"></a>';
  637. }
  638. else
  639. {
  640. $asc = 'desc';
  641. if(!isset($v['order']) && $k != 'id')
  642. {
  643. $cancel = '&nbsp;<a class="glyphicon glyphicon-circle-arrow-down" style="color:black;" href="'.str_replace('{order}', '&' . $order . '=desc', $link).'" title="降序"></a>';
  644. }
  645. }
  646. if($asc == 'asc')
  647. {
  648. $asc = 'desc';
  649. $icon = 'down';
  650. $text = '降序';
  651. }
  652. else
  653. {
  654. $asc = 'asc';
  655. $icon = 'up';
  656. $text = '升序';
  657. }
  658. $link = str_replace('{order}', '&' . $order . '=' . $asc, $link);
  659. $result[] = '<th><a href="'.$link.'" title="'.$text.'" style="color:black;">' . $v['name'] . '&nbsp;<span class="glyphicon glyphicon-circle-arrow-'.$icon.'"></span></a>'.$cancel.'</th>';
  660. }
  661. else
  662. {
  663. $result[] = '<th>' . $v['name'] . '</th>';
  664. }
  665. }
  666. }
  667. if(Maze::load('manage/auth.oper', 3) != false && Maze::load('manage/auth.oper', 5) != false && isset($config['manage']['_list']['value']))
  668. {
  669. $result[] = '<th>'.$config['manage']['_list']['name'].'</th>';
  670. }
  671. }
  672. }
  673. return $result;
  674. }
  675. /**
  676. * 导出列表
  677. *
  678. * @return array
  679. */
  680. public function list_excel()
  681. {
  682. Maze::$global['base']['excel'] = 1;
  683. $data = $this->list_tbody(true);
  684. if($data)
  685. {
  686. $config = $this->config();
  687. require MAZE_PROJECT_PATH . 'src/Excel/PHPExcel.php';
  688. $xls = new \PHPExcel();
  689. $xls->getProperties()->setCreator("Maze Excel")
  690. ->setLastModifiedBy("Maze Excel")
  691. ->setTitle("Office 2007 XLSX Analysis Document")
  692. ->setSubject("Office 2007 XLSX Analysis Document")
  693. ->setDescription("Mazephp Excel")
  694. ->setKeywords("Mazephp Excel")
  695. ->setCategory("Mazephp Excel");
  696. $info = $this->info();
  697. $name = $info['list_header'] . '的' . $info['list_desc'] . '-' . $info['project_name'];
  698. # 全局导出
  699. $start = 65;
  700. $act = $xls->setActiveSheetIndex(0);
  701. $act->setTitle($name);
  702. $i = 0;
  703. foreach($config['struct'] as $k => $v)
  704. {
  705. if(isset($v['list']))
  706. {
  707. $num = 1;
  708. $cell = $this->_getChr($start, $i);
  709. $act->setCellValue($cell . '' . $num, $v['name']);
  710. $act->getColumnDimension($cell)->setWidth(20);
  711. $i++;
  712. foreach($data as $ki => $vi)
  713. {
  714. if(isset($vi[$k]))
  715. {
  716. $num += 1;
  717. $value = $vi[$k];
  718. if(is_string($v['list']))
  719. {
  720. $value = $this->value($v['list'], $data[$ki], $config['struct']);
  721. }
  722. elseif(isset($v['option']))
  723. {
  724. $v['option'] = $this->option($v['option']);
  725. if(isset($v['option'][$value]))
  726. {
  727. if(is_array($v['option'][$value]))
  728. {
  729. $value = $v['option'][$value]['name'];
  730. }
  731. else
  732. {
  733. $value = $v['option'][$value];
  734. }
  735. }
  736. }
  737. $act->setCellValue($cell . $num, $value);
  738. }
  739. }
  740. }
  741. }
  742. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 垂直居中
  743. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 水平居中
  744. $filename = $name . '.xlsx';
  745. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  746. header('Content-Disposition: attachment;filename="'.$filename.'"');
  747. header('Cache-Control: max-age=0');
  748. $write = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
  749. $write->save('php://output');
  750. }
  751. //print_r($data);die;
  752. }
  753. private function _getChr($start, $num)
  754. {
  755. $num = $start + $num;
  756. $max = floor($num/91);
  757. if($max > 0)
  758. {
  759. $num = $num - (91*$max) + $start;
  760. $num = chr($start) . chr($num);
  761. }
  762. else
  763. {
  764. $num = chr($num);
  765. }
  766. return $num;
  767. }
  768. /**
  769. * 列表页数据列表
  770. *
  771. * @return array
  772. */
  773. public function list_tbody($excel = false)
  774. {
  775. # 新增权限
  776. if(Maze::load('manage/auth.oper', 1) == false)
  777. {
  778. return '';
  779. }
  780. $high = array();
  781. $search = Maze::load('manage/auth.oper', 2) ? Maze::preInput('search_') : false;
  782. $param = array();
  783. if($search)
  784. {
  785. $fulltext = Maze::preInput('search_fulltext_');
  786. if($fulltext)
  787. {
  788. //Maze::setInput('option_' . $search['search_fulltext'], '%' . $search['search_fulltext_value'] . '%');
  789. $search['search_fulltext_type'] = 2;
  790. $fix = '';
  791. if($search['search_fulltext_type'] == 2)
  792. {
  793. $fix = '%';
  794. }
  795. $param['search_type'] = $search['search_fulltext_type'];
  796. foreach($fulltext as $k => $v)
  797. {
  798. $k = str_replace('search_fulltext_', '', $k);
  799. $param['option_' . $k] = $fix . $v . $fix;
  800. unset($search[$k]);
  801. # 设定检索高亮
  802. $high[$k] = $v;
  803. }
  804. $this->search = true;
  805. }
  806. $order = Maze::preInput('search_order_');
  807. if($order)
  808. {
  809. foreach($order as $k => $v)
  810. {
  811. $k = str_replace('search_order_', '', $k);
  812. $param['order'][0][$k] = $v;
  813. unset($search[$k]);
  814. }
  815. }
  816. foreach($search as $k => $v)
  817. {
  818. if($v)
  819. {
  820. if(strpos($k, 'option_') !== false)
  821. {
  822. //Maze::setInput(str_replace('search_', '', $k), $v);
  823. $param[str_replace('search_', '', $k)] = $v;
  824. $this->search = true;
  825. }
  826. }
  827. }
  828. //$search = Maze::preInput('option_');
  829. //print_r($search);die;
  830. }
  831. # 批量更新
  832. $mul = Maze::load('manage/auth.oper', 3) ? Maze::preInput('mul_') : false;
  833. if(Maze::input('method') == 'mul' && $mul && $mul['mul_type'] > 0)
  834. {
  835. if($mul['mul_type'] == 1 && empty($mul['mul_where_id']))
  836. {
  837. Maze::abert('您还未选择任何数据条目');
  838. }
  839. Maze::$global['base']['mul_type'] = $mul['mul_type'];
  840. unset($mul['mul_type']);
  841. foreach($mul as $k => $v)
  842. {
  843. if($v)
  844. {
  845. if(is_array($v))
  846. {
  847. $v = implode(',', $v);
  848. }
  849. $param[str_replace('mul_', '', $k)] = $v;
  850. }
  851. }
  852. if(isset($param))
  853. {
  854. $method = $this->project . '/' . $this->table . '-updatemul';
  855. $state = Maze::load($method, $param);
  856. }
  857. //print_r($param);die;
  858. Maze::abert('reload');
  859. }
  860. $config = $this->config();
  861. $result = array();
  862. $method = $this->project . '/' . $this->table . '-list';
  863. # 验证头部权限
  864. if(isset($config['auth']) && is_string($config['auth']))
  865. {
  866. $top = Maze::load('manage/auth.getTop', array($config['auth']));
  867. if($top)
  868. {
  869. Maze::setInput('option_' . $config['auth_key'], $top['value']);
  870. }
  871. }
  872. $data = Maze::load($method, $param);
  873. if(isset($config['manage']['list_type']) && $config['manage']['list_type'] == 'parent')
  874. {
  875. $parent = array();
  876. $child = array();
  877. foreach($data as $k => $v)
  878. {
  879. if($v[$config['name'] . '_id'] <= 0)
  880. {
  881. $parent[$k] = $v;
  882. }
  883. else
  884. {
  885. $v['name'] = '|-----' . $v['name'];
  886. $child[$v[$config['name'] . '_id']][$k] = $v;
  887. }
  888. }
  889. if($parent)
  890. {
  891. $data = array();
  892. foreach($parent as $k => $v)
  893. {
  894. $data[$v['id']] = $v;
  895. if(isset($child[$v['id']]))
  896. {
  897. foreach($child[$v['id']] as $i => $j)
  898. {
  899. $data[$j['id']] = $j;
  900. }
  901. }
  902. }
  903. }
  904. }
  905. if($excel == true)
  906. {
  907. return $data;
  908. }
  909. //print_r($data);die;
  910. //print_r(Helper::page("current"));die;
  911. if($data && isset($config['struct']))
  912. {
  913. foreach($data as $k => $v)
  914. {
  915. $result[$k] = '<tr>';
  916. if(isset($config['manage']['mul']))
  917. {
  918. $result[$k] .= '<td><input type="checkbox" name="mul_where_id[]" class="checkbox-checkall-list" value="'.$v['id'].'"/></td>';
  919. }
  920. foreach($config['struct'] as $ki => $vi)
  921. {
  922. if(isset($vi['list']) && isset($v[$ki]))
  923. {
  924. $show = $v[$ki];
  925. if(is_string($vi['list']))
  926. {
  927. $show = $this->value($vi['list'], $data[$k], $config['struct']);
  928. }
  929. if(isset($vi['modal']))
  930. {
  931. $show = Html::modal($this->value($vi['modal'], $data[$k], $config['struct']), $show);
  932. }
  933. elseif(isset($vi['option']))
  934. {
  935. # 验证option是否是匿名函数
  936. $vi['option'] = $this->option($vi['option']);
  937. # 位运算
  938. if(isset($vi['bit']))
  939. {
  940. $ts = array();
  941. foreach($vi['option'] as $tm => $tv)
  942. {
  943. if(isset($vi['bit'][$tm]) && ($show & $tm) > 0)
  944. {
  945. $ts[] = $tv;
  946. }
  947. }
  948. $show = implode(',', $ts);
  949. }
  950. /*
  951. if((isset($vi['child_name']) || (isset($vi['show']) && is_string($vi['show']))))
  952. {
  953. if(isset($vi['option'][$show]) && is_array($vi['option'][$show]))
  954. {
  955. $show = $vi['option'][$show]['name'];
  956. }
  957. else
  958. {
  959. $show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
  960. }
  961. }
  962. */
  963. elseif((isset($vi['edit']) && $vi['edit']) || $ki == 'state')
  964. {
  965. $vi['name'] = $ki;
  966. $vi['value'] = $v[$ki];
  967. $show = Html::select($vi, 'form-list', '', 'onchange="update($(this), \''.$v['id'].'\', \''.$this->project.'\', \''.$this->table.'\')"');
  968. }
  969. else
  970. {
  971. if(isset($vi['option'][$show]) && is_array($vi['option'][$show]))
  972. {
  973. $show = $vi['option'][$show]['name'];
  974. }
  975. else
  976. {
  977. $show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
  978. }
  979. }
  980. }
  981. $text = $show;
  982. if($high && isset($high[$ki]))
  983. {
  984. $text = str_replace($high[$ki], '<font color="red">' . $high[$ki] . '</font>', $text);
  985. }
  986. if(!isset($vi['option']) && isset($vi['edit']) && $vi['edit'])
  987. {
  988. //$url = Maze::url($vi['edit'] . '?where_id=' . $v['id'] . '&col=' . $vi['col']. '&key=' . $this->project . '&table=' . $this->table);
  989. $url = $this->url(4);
  990. $vi['edit'] = $vi['edit'] == 'textarea' ? $vi['edit'] : 'text';
  991. $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>';
  992. }
  993. else
  994. {
  995. $result[$k] .= '<td>' . $text . '</td>';
  996. }
  997. }
  998. }
  999. if(isset($config['manage']['_list']['value']))
  1000. {
  1001. $result[$k] .= '<td>';
  1002. foreach($config['manage']['_list']['value'] as $kj => $vj)
  1003. {
  1004. $pj = false;
  1005. $h = '<span class="am-icon-pencil-square-o"></span>';
  1006. $oper = true;
  1007. if($kj == 3)
  1008. {
  1009. $oper = Maze::load('manage/auth.oper', 3);
  1010. }
  1011. elseif($kj == 6)
  1012. {
  1013. $oper = Maze::load('manage/auth.oper', 5);
  1014. $h = '<span class="am-icon-trash-o"></span>';
  1015. }
  1016. if(is_array($vj))
  1017. {
  1018. if(isset($vj[2]))
  1019. {
  1020. $oper = $this->value($vj[2], $data[$k]);
  1021. }
  1022. if($oper)
  1023. {
  1024. $pj = $this->value($vj[1], $data[$k]);
  1025. $vj = $vj[0];
  1026. }
  1027. }
  1028. if($oper == true)
  1029. {
  1030. $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>&nbsp;&nbsp;';
  1031. }
  1032. }
  1033. $result[$k] .= '</td>';
  1034. }
  1035. $result[$k] .= '</tr>';
  1036. }
  1037. }
  1038. return $result;
  1039. }
  1040. /**
  1041. * 列表页批量管理
  1042. *
  1043. * @return array
  1044. */
  1045. public function list_mul()
  1046. {
  1047. # 新增权限:更新权限
  1048. if(Maze::load('manage/auth.oper', 3) == false)
  1049. {
  1050. return '';
  1051. }
  1052. $config = $this->config();
  1053. $result = '';
  1054. if(isset($config['struct']) && isset($config['manage']['mul']))
  1055. {
  1056. $mul = Maze::preInput('mul_');
  1057. $node = Html::node(array('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
  1058. $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>';
  1059. $prefix = 'mul_';
  1060. $result .= $node[0];
  1061. $default = 0;
  1062. $option = array
  1063. (
  1064. 1 => '选中的数据',
  1065. );
  1066. # 只有当有搜索条件时才会出现
  1067. if(Maze::load('manage/auth.oper', 2) && $this->search == true)
  1068. {
  1069. $option[2] = '所有数据';
  1070. $default = 2;
  1071. }
  1072. $this->list_search_select($result, $mul, $prefix . 'type' . '" id="'.$prefix.'type', '请选择更新方式', $option, $default);
  1073. foreach($config['struct'] as $k => $v)
  1074. {
  1075. if(isset($v['update']) && $v['update'] == 'radio' && (is_array($v['option']) || is_object($v['option'])))
  1076. {
  1077. $this->list_search_select($result, $mul, $prefix . 'set_' . $k, $v['name'], $this->option($v['option']), $v['default']);
  1078. }
  1079. }
  1080. $result .= $btn . $node[1];
  1081. }
  1082. //print_r($result);die;
  1083. return $result;
  1084. }
  1085. private function value($value, $data, $struct = array())
  1086. {
  1087. if(!$data)
  1088. {
  1089. return $value;
  1090. }
  1091. if($value == 'table')
  1092. {
  1093. $result = array();
  1094. foreach($data as $k => $v)
  1095. {
  1096. if(strpos($v, 'http://wx.qlogo.cn/') !== false || strpos($v, '.jpg') !== false || strpos($v, '.gif') !== false || strpos($v, '.png') !== false)
  1097. {
  1098. $v = '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="150"></a>';
  1099. }
  1100. elseif(strpos($k, 'time') !== false || strpos($k, 'date') !== false)
  1101. {
  1102. $v = date("Y-m-d H:i:s", $v);
  1103. }
  1104. elseif(isset($struct[$k]['decode']))
  1105. {
  1106. $v = json_decode(base64_decode($v), true);
  1107. }
  1108. if(isset($struct[$k]) && isset($struct[$k]['name']))
  1109. {
  1110. $k = $struct[$k]['name'];
  1111. }
  1112. $result[$k] = $v;
  1113. }
  1114. $value = Maze::table($result);
  1115. }
  1116. elseif(strpos($value, '{') !== false)
  1117. {
  1118. # 这里用到了/e,注意这里无论如何不能传入$_GET之类的用户参数 2015/7/21更新为callback方式
  1119. //$eval = '$value = ' . preg_replace('/{(.*?)}/e', "\$data['$1']", $value) . ';';
  1120. $func = function($r) use($data)
  1121. {
  1122. return $data[$r[1]];
  1123. };
  1124. $eval = '$value = ' . preg_replace_callback('/{(.*?)}/', $func, $value) . ';';
  1125. eval($eval);
  1126. }
  1127. return $value;
  1128. }
  1129. /**
  1130. * update
  1131. *
  1132. * @return array
  1133. */
  1134. public function update()
  1135. {
  1136. $config = $this->config();
  1137. $result = array();
  1138. $prefix = 'update';
  1139. $data = array();
  1140. if($this->id > 0)
  1141. {
  1142. # 新增权限
  1143. if(Maze::load('manage/auth.oper', 3) == false)
  1144. {
  1145. return '';
  1146. }
  1147. $data = $this->load();
  1148. if($data)
  1149. {
  1150. //$prefix = 'set';
  1151. }
  1152. }
  1153. else
  1154. {
  1155. # 新增权限
  1156. if(Maze::load('manage/auth.oper', 4) == false)
  1157. {
  1158. return '';
  1159. }
  1160. }
  1161. //print_r($config['manage']['update']);die;
  1162. foreach($config['struct'] as $k => $v)
  1163. {
  1164. if(isset($v['update']))
  1165. {
  1166. # 处理show模式下的class
  1167. $class = $this->update_class($v);
  1168. $result[$k] = '<div class="form-group '.$class.'"><div class="margin-top">';
  1169. $v['ext'] = $v['col'] = $k;
  1170. $v['update'] = $v['update'] ? $v['update'] : 'text';
  1171. if(isset($v['match']) && $v['match'] != 'option')
  1172. {
  1173. if(is_string($v['match']) && strpos($v['match'], '/') !== false)
  1174. {
  1175. $v['valid'] = 'validate[required,custom['.$k.']]';
  1176. }
  1177. else
  1178. {
  1179. $v['valid'] = 'validate[required]';
  1180. }
  1181. }
  1182. if(is_string($v['update']) && strpos($v['update'], 'copy.') !== false)
  1183. {
  1184. $v['value'] = $v['update'];
  1185. $v['update'] = 'hidden';
  1186. }
  1187. $this->update_html($v, $k, $k, $data, $result);
  1188. $result[$k] .= '</div></div>';
  1189. }
  1190. elseif(isset($v['class']))
  1191. {
  1192. # 分割线
  1193. $result[$k] = Html::hr($v);
  1194. }
  1195. }
  1196. if(Maze::input('ajax') == 1)
  1197. {
  1198. return implode('', $result);
  1199. }
  1200. //print_r($result);die;
  1201. return $result;
  1202. }
  1203. /**
  1204. * update_class
  1205. *
  1206. * @return string
  1207. */
  1208. public function update_class($param)
  1209. {
  1210. $class = '';
  1211. if(isset($param['show']))
  1212. {
  1213. if(is_array($param['show']))
  1214. {
  1215. $parent = array();
  1216. $child = array();
  1217. foreach($param['show'] as $k => $v)
  1218. {
  1219. if(strpos($v, '_') !== false)
  1220. {
  1221. $temp = explode('_', $v);
  1222. $parent[$temp[0]] = 'show_' . $temp[0];
  1223. $child[$v] = 'show_' . $v;
  1224. }
  1225. elseif($v == 'no')
  1226. {
  1227. $child[$v] = 'show_no';
  1228. }
  1229. }
  1230. $class = implode(' ', $parent) . ' ' . implode(' ', $child);
  1231. }
  1232. else
  1233. {
  1234. $class = 'show_input';
  1235. }
  1236. }
  1237. return $class;
  1238. }
  1239. /**
  1240. * update_html
  1241. *
  1242. * @return string
  1243. */
  1244. public function update_html($param, $key, $index, $data, &$result)
  1245. {
  1246. if(isset($param['name']) && $param['update'] != 'hidden' && is_string($param['update']))
  1247. {
  1248. $result[$key] .= Html::pageLeft($param);
  1249. }
  1250. if($param['update'])
  1251. {
  1252. # 名称带前缀
  1253. $param['name'] = 'update_' . $param['ext'];
  1254. $param['index'] = $index;
  1255. //$param['value'] = '';
  1256. # 得到当前的值
  1257. if(isset($data[$index]) && !isset($param['value']))
  1258. {
  1259. $param['value'] = $data[$index];
  1260. }
  1261. # 得到下级的值
  1262. if(isset($param['child_value']))
  1263. {
  1264. $param['child_value'] = $this->value($param['child_value'], $data);
  1265. }
  1266. # 验证option是否是匿名函数
  1267. if(isset($param['option']))
  1268. {
  1269. $param['option'] = $this->option($param['option']);
  1270. }
  1271. # 处理下级字段
  1272. if(isset($param['child']))
  1273. {
  1274. $param['child'] = $this->option($param['child']);
  1275. if(isset($data[$key . '_parent']))
  1276. {
  1277. $param['parent_value'] = $data[$key . '_parent'];
  1278. }
  1279. }
  1280. # 设置默认值
  1281. if(isset($param['default']))
  1282. {
  1283. $param['default'] = $this->option($param['default']);
  1284. }
  1285. $method = $param['update'];
  1286. if(is_array($method))
  1287. {
  1288. $result[$key] .= '<ul class="nav nav-tabs">{tab}<li><a href="javascript:;" class="maze_form_add" toggle_key="'.$key.'">增加一条</a></li></ul>';
  1289. $tab = '';
  1290. $this->update_child(array(), $method, $key, $result, -1);
  1291. if(isset($param['value']) && $param['value'])
  1292. {
  1293. $param['value'] = json_decode(base64_decode($param['value']), true);
  1294. foreach($param['value'] as $k => $v)
  1295. {
  1296. $this->update_child($v, $method, $key, $result, $k);
  1297. $active = '';
  1298. if($k == 0)
  1299. {
  1300. $active = 'active';
  1301. }
  1302. $k = $k+1;
  1303. $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>';
  1304. }
  1305. }
  1306. else
  1307. {
  1308. $this->update_child(array(), $method, $key, $result, 0);
  1309. $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>';
  1310. }
  1311. $result[$key] = str_replace('{tab}', $tab, $result[$key]);
  1312. }
  1313. else
  1314. {
  1315. if(isset($param['update_input']) && isset($data[$index . '_input']))
  1316. {
  1317. $param['update_input_data'] = explode(',', $data[$index . '_input']);
  1318. }
  1319. $result[$key] .= Html::$method($param);
  1320. }
  1321. if(isset($param['desc']) && ($param['update'] == 'text' || $param['update'] == 'password'))
  1322. {
  1323. $result[$key] .= Html::desc($param['desc']);
  1324. }
  1325. }
  1326. }
  1327. /**
  1328. * update_action
  1329. *
  1330. * @return array
  1331. */
  1332. private function update_child($data, $param, $key, &$result, $index)
  1333. {
  1334. $style = '';
  1335. if($index != 0)
  1336. {
  1337. $style = 'style="display:none;"';
  1338. }
  1339. $result[$key] .= '<div id="'.$key.'-child-'.($index+1).'" class="maze_'.$key.'_child maze_form_add_child" '.$style.'><div class="maze_form_delete">[删除]</div>';
  1340. foreach($param as $k => $v)
  1341. {
  1342. $result[$key] .= '<div class="form-group">';
  1343. $v['type'] = $v['update'];
  1344. if($index < 0)
  1345. {
  1346. $v['ext'] = $key . '_c_' . $v['col'];
  1347. }
  1348. else
  1349. {
  1350. $v['ext'] = $key . '_c_' . $index . '_i_' . $v['col'];
  1351. }
  1352. $this->update_html($v, $key, $v['col'], $data, $result);
  1353. $result[$key] .= '</div>';
  1354. }
  1355. $result[$key] .= '</div>';
  1356. }
  1357. /**
  1358. * update_action
  1359. *
  1360. * @return array
  1361. */
  1362. public function update_action()
  1363. {
  1364. $method = false;
  1365. $config = $this->config();
  1366. # 验证头部权限
  1367. $common = array();
  1368. if(isset($config['auth']) && is_string($config['auth']))
  1369. {
  1370. $top = Maze::load('manage/auth.getTop', array($config['auth']));
  1371. if($top)
  1372. {
  1373. //Maze::setInput('update_' . $config['auth_key'], $top['value']);
  1374. $common[$config['auth_key']] = $top['value'];
  1375. }
  1376. }
  1377. $input = Maze::preInput('update_');
  1378. if($this->id > 0)
  1379. {
  1380. # 新增权限
  1381. if(Maze::load('manage/auth.oper', 3) == false)
  1382. {
  1383. return Maze::out('no');
  1384. }
  1385. Maze::setInput('where_id', $this->id);
  1386. Maze::setInput('update_where_id', $this->id);
  1387. $info = $this->load();
  1388. if($info)
  1389. {
  1390. $method = 'update';
  1391. $col = Maze::input('col');
  1392. $value = Maze::input('value');
  1393. if($col && $value)
  1394. {
  1395. $method = 'update_' . $col;
  1396. Maze::setInput('set_' . $col, $value);
  1397. Maze::setInput('update_' . $col, $value);
  1398. $this->load($method);
  1399. Log::add($this->project, $this->table, array('method' => $method, 'input' => array($col => $value)), $this->id);
  1400. Maze::out('yes');
  1401. return;
  1402. }
  1403. $prefix = 'set';
  1404. }
  1405. }
  1406. if(!$method)
  1407. {
  1408. # 新增权限
  1409. if(Maze::load('manage/auth.oper', 4) == false)
  1410. {
  1411. return Maze::out('no');
  1412. }
  1413. $method = 'insert';
  1414. $prefix = 'add';
  1415. }
  1416. if($method && $input)
  1417. {
  1418. $update = array();
  1419. $i = 0;
  1420. $c = array();
  1421. foreach($input as $k => $v)
  1422. {
  1423. if($v && is_string($v) && strpos($v, 'copy.') !== false)
  1424. {
  1425. $temp = str_replace('copy.', '', $v);
  1426. if(isset($input['update_' . $temp]))
  1427. {
  1428. $v = $input['update_' . $temp];
  1429. }
  1430. }
  1431. if(strpos($k, '_c_') !== false)
  1432. {
  1433. if($v)
  1434. {
  1435. $temp = explode('_c_', $k);
  1436. $k = str_replace('update_', $prefix . '_', $temp[0]);
  1437. if(!isset($c[$k]))
  1438. {
  1439. $c[$k] = array();
  1440. }
  1441. if(strpos($temp[1], '_i_') !== false)
  1442. {
  1443. $temp = explode('_i_', $temp[1]);
  1444. if(!isset($c[$k][$temp[1]]))
  1445. {
  1446. $c[$k][$temp[1]] = 0;
  1447. }
  1448. else
  1449. {
  1450. $c[$k][$temp[1]]++;
  1451. }
  1452. $num = $c[$k][$temp[1]];
  1453. $update[$i][$k][$num][$temp[1]] = $v;
  1454. }
  1455. else
  1456. {
  1457. //$update[$i][$k][$c][$temp[1]] = $v;
  1458. }
  1459. }
  1460. }
  1461. elseif(strpos($k, '__') !== false)
  1462. {
  1463. $temp1 = explode('__', $k);
  1464. $temp2 = explode('_', $temp1[0]);
  1465. $i = $temp2[1]-1;
  1466. if(strpos($k, 'where_id') !== false)
  1467. {
  1468. //$update[$i]['where_id'] = $v;
  1469. }
  1470. else
  1471. {
  1472. $update[$i]['add_' . $temp1[1]] = $v;
  1473. }
  1474. }
  1475. else
  1476. {
  1477. $k = str_replace('update_', $prefix . '_', $k);
  1478. if(strpos($k, 'where_id') !== false)
  1479. {
  1480. if($method != 'insert')
  1481. {
  1482. $update[$i]['where_id'] = $v;
  1483. }
  1484. }
  1485. else
  1486. {
  1487. $update[$i][$k] = $v;
  1488. }
  1489. }
  1490. if($common)
  1491. {
  1492. foreach($common as $ki => $vi)
  1493. {
  1494. $update[$i][$prefix . '_' . $ki] = $vi;
  1495. }
  1496. }
  1497. }
  1498. //print_r($input);
  1499. //print_r($update);die;
  1500. foreach($update as $k => $v)
  1501. {
  1502. $method = 'insert';
  1503. if(isset($v['where_id']) && $v['where_id'] > 0)
  1504. {
  1505. $method = 'update';
  1506. }
  1507. $id = $this->load($method, $v);
  1508. if($id)
  1509. {
  1510. Log::add($this->project, $this->table, array('method' => $method, 'input' => $v), $this->id > 0 ? $this->id : $id);
  1511. }
  1512. }
  1513. }
  1514. Maze::out('yes');
  1515. }
  1516. /**
  1517. * delete_action
  1518. *
  1519. * @return array
  1520. */
  1521. public function delete_action()
  1522. {
  1523. # 新增权限
  1524. if(Maze::load('manage/auth.oper', 5) == false)
  1525. {
  1526. return Maze::out('no');
  1527. }
  1528. $this->load('delete');
  1529. //$this->url(1)
  1530. Maze::out('yes');
  1531. }
  1532. /**
  1533. * stat 为主动统计提供相关数据
  1534. *
  1535. * @return array
  1536. */
  1537. public function stat()
  1538. {
  1539. //bundle 包依赖工具
  1540. $config = $this->config();
  1541. if(empty($config['manage']['stat']))
  1542. {
  1543. Maze::abert('该项目主动统计功能未开放');
  1544. }
  1545. $data = $config['manage'];
  1546. $data['table'] = '';
  1547. $method = $this->project . '/' . $this->table . '-total';
  1548. $gp = Maze::input('gp', 'month,day');
  1549. $col = is_string($gp) ? explode(',', $gp) : $gp;
  1550. $data['search'] = $this->stat_search($config['struct'], $gp, $config['manage']['stat'], $config['manage']['search']);
  1551. foreach($col as $k => $v)
  1552. {
  1553. $this->stat_data($data, $config['struct'], $v, $method);
  1554. }
  1555. return $data;
  1556. }
  1557. /**
  1558. * stat 为主动统计提供导出数据
  1559. *
  1560. * @return array
  1561. */
  1562. public function stat_excel()
  1563. {
  1564. Maze::$global['base']['excel'] = 1;
  1565. //print_r($data['thead']);die;
  1566. $data = $this->stat();
  1567. if($data && isset($data['list']))
  1568. {
  1569. require MAZE_PROJECT_PATH . 'src/Excel/PHPExcel.php';
  1570. $xls = new \PHPExcel();
  1571. $xls->getProperties()->setCreator("Maze Stat Excel")
  1572. ->setLastModifiedBy("Maze Stat Excel")
  1573. ->setTitle("Office 2007 XLSX Maze Document")
  1574. ->setSubject("Office 2007 XLSX Maze Document")
  1575. ->setDescription("Mazephp Stat Excel")
  1576. ->setKeywords("Mazephp Stat Excel")
  1577. ->setCategory("Mazephp Stat Excel");
  1578. $info = $this->info();
  1579. $name = $info['list_header'] . '的' . $info['stat_desc'] . '-' . $info['project_name'];
  1580. $start = 65;
  1581. $s = 0;
  1582. foreach($data['thead'] as $k => $v)
  1583. {
  1584. if($s > 0)
  1585. {
  1586. $xls->createSheet();
  1587. }
  1588. $act = $xls->setActiveSheetIndex($s);
  1589. $act->setTitle($v[0]);
  1590. $s++;
  1591. $i = 0;
  1592. foreach($v as $kj => $vj)
  1593. {
  1594. $num = 1;
  1595. $cell = $this->_getChr($start, $i);
  1596. $act->setCellValue($cell . '' . $num, $vj);
  1597. $act->getColumnDimension($cell)->setWidth(20);
  1598. $i++;
  1599. if(isset($data['list'][$k]))
  1600. {
  1601. foreach($data['list'][$k] as $ki => $vi)
  1602. {
  1603. if(isset($vi[$kj]))
  1604. {
  1605. $num += 1;
  1606. $act->setCellValue($cell . $num, $vi[$kj]);
  1607. }
  1608. }
  1609. }
  1610. }
  1611. }
  1612. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 垂直居中
  1613. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 水平居中
  1614. $xls->setActiveSheetIndex(0);
  1615. $filename = $name . '.xlsx';
  1616. header('Pragma: public');
  1617. header('Expires: 0');
  1618. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  1619. header('Content-Type:application/force-download');
  1620. header('Content-Type:application/vnd.ms-execl');
  1621. header('Content-Type:application/octet-stream');
  1622. header('Content-Type:application/download');
  1623. header("Content-Disposition:attachment;filename='".$filename."'");
  1624. header('Content-Transfer-Encoding:binary');
  1625. $write = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
  1626. $write->save('php://output');
  1627. }
  1628. }
  1629. /**
  1630. * stat_search
  1631. *
  1632. * @return mixed
  1633. */
  1634. private function stat_search($struct, $col, $stat, $search)
  1635. {
  1636. unset($struct['id']);
  1637. unset($struct['cdate']);
  1638. if($stat)
  1639. {
  1640. $stat = explode(',', $stat);
  1641. foreach($stat as $k => $v)
  1642. {
  1643. if(isset($struct[$v]))
  1644. {
  1645. unset($struct[$v]);
  1646. }
  1647. }
  1648. }
  1649. $struct['day']['name'] = '天';
  1650. $struct['month']['name'] = '月';
  1651. $struct['year']['name'] = '年';
  1652. $param['update'] = 'checkbox';
  1653. $param['name'] = 'gp';
  1654. $param['value'] = $col;
  1655. $param['option'] = $struct;
  1656. $html = '<form action="'.$this->url(7).'" method="post" id="f1">';
  1657. $html .= '<div class="form-group">';
  1658. $html .= Html::pageLeft(array('name' => '统计项', 'col' => 'col'));
  1659. $html .= Html::radio($param);
  1660. $html .= '</div>';
  1661. $input = Maze::preInput('search_');
  1662. if(isset($search['date']))
  1663. {
  1664. foreach($search['date'] as $k => $v)
  1665. {
  1666. $html .= '<div class="form-group">';
  1667. $this->list_search_time($html, $input, 'search_', $k, $v, 'date');
  1668. $html .= '</div>';
  1669. }
  1670. $input = Maze::preInput('search_');
  1671. if($input)
  1672. {
  1673. foreach($input as $k => $v)
  1674. {
  1675. if($v && strpos($k, 'option_') !== false)
  1676. {
  1677. Maze::setInput(str_replace('search_', '', $k), $v);
  1678. }
  1679. }
  1680. }
  1681. }
  1682. if(isset($search['fulltext']))
  1683. {
  1684. $html .= '<div class="form-group">';
  1685. $this->list_search_select($html, $input, 'search_fulltext', '选择检索项', $search['fulltext']);
  1686. $this->list_search_select($html, $input, 'search_fulltext_type', '', array
  1687. (
  1688. 1 => '精确匹配',
  1689. 2 => '模糊匹配',
  1690. 3 => '大于',
  1691. 4 => '大于等于',
  1692. 5 => '小于',
  1693. 6 => '小于等于',
  1694. ), 1);
  1695. $value = array();
  1696. $value['name'] = 'search_fulltext_value';
  1697. $value['class'] = 'form-control';
  1698. if(isset($input[$value['name']]))
  1699. {
  1700. $value['value'] = $input[$value['name']];
  1701. $fix = '';
  1702. if($input['search_fulltext_type'] == 2)
  1703. {
  1704. $fix = '%';
  1705. }
  1706. Maze::setInput('option_' . $input['search_fulltext'], $fix . $value['value'] . $fix);
  1707. //print_r(Maze::input('option_name'));die;
  1708. }
  1709. $html .= Html::text($value, false);
  1710. $html .= '</div>';
  1711. }
  1712. $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>';
  1713. $html .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" onclick="$(\'#f1\').attr(\'action\', \''.$this->url(9).'\').submit();">下载报表</a>';
  1714. $html .= '</form>';
  1715. return $html;
  1716. }
  1717. /**
  1718. * stat_data
  1719. *
  1720. * @return mixed
  1721. */
  1722. private function stat_data(&$data, $struct, $col, $method)
  1723. {
  1724. Maze::setInput('group', $col);
  1725. switch($col)
  1726. {
  1727. case 'year':
  1728. $name = '按年统计';
  1729. break;
  1730. case 'month':
  1731. $name = '按月统计';
  1732. break;
  1733. case 'day':
  1734. $name = '按天统计';
  1735. break;
  1736. default:
  1737. $name = $struct[$col]['name'];
  1738. break;
  1739. }
  1740. $list = Maze::load($method, array('gp' => $col));
  1741. $data['table'] .= '<div style="overflow-y:auto;width:100%;max-height:600px;"><table class="table table-bordered table-striped">';
  1742. $data['table'] .= '<thead><tr><th>'.$name.'</th><th>统计数字</th></tr></thead>';
  1743. $data['thead'][$col] = array($name, '统计数字');
  1744. if($list)
  1745. {
  1746. $data['table'] .= '<tbody>';
  1747. $total = 0;
  1748. foreach($list as $k => $v)
  1749. {
  1750. if(isset($v[$col]))
  1751. {
  1752. if(isset($struct[$col]['option']))
  1753. {
  1754. $option = $this->option($struct[$col]['option']);
  1755. if(isset($option[$v[$col]]))
  1756. {
  1757. $v[$col] = $option[$v[$col]];
  1758. if(is_array($v[$col]))
  1759. {
  1760. $v[$col] = $v[$col]['name'];
  1761. }
  1762. }
  1763. }
  1764. $data['table'] .= '<tr><td width="60%" style="word-break:break-all;word-wrap:break-word;">'.$v[$col].'</td><td>'.$v['total'].'</td></tr>';
  1765. $total += $v['total'];
  1766. $data['list'][$col][] = array($v[$col], $v['total']);
  1767. }
  1768. }
  1769. $data['table'] .= '</tbody>';
  1770. if($total > 0 && $k > 0)
  1771. {
  1772. $data['table'] .= '<tr><td style="font-weight:bold">汇总</td><td style="font-weight:bold">'.$total.'</td></tr>';
  1773. $data['list'][$col][] = array('汇总', $total);
  1774. }
  1775. }
  1776. $data['table'] .= '</table></div>';
  1777. }
  1778. /**
  1779. * load
  1780. *
  1781. * @return mixed
  1782. */
  1783. private function load($method = 'one', $param = array())
  1784. {
  1785. return Maze::load($this->project . '/' . $this->table . '-' . $method, $param);
  1786. }
  1787. /**
  1788. * __call
  1789. *
  1790. * @return object
  1791. */
  1792. public function __call($method, $param)
  1793. {
  1794. if(strpos($method, 'html_') !== false)
  1795. {
  1796. return Html::text($param);
  1797. }
  1798. return $this;
  1799. }
  1800. /**
  1801. * option
  1802. *
  1803. * @return string
  1804. */
  1805. private function option($option)
  1806. {
  1807. if(is_object($option))
  1808. {
  1809. $function = $option;
  1810. $option = $function();
  1811. }
  1812. return $option;
  1813. }
  1814. /**
  1815. * option
  1816. *
  1817. * @return string
  1818. */
  1819. public function manage()
  1820. {
  1821. $config = $this->config();
  1822. return $config;
  1823. }
  1824. /**
  1825. * valid
  1826. *
  1827. * @return string
  1828. */
  1829. public function valid()
  1830. {
  1831. //http://placehold.it/350x150
  1832. $config = $this->config();
  1833. if(isset($config['struct']))
  1834. {
  1835. $rule = array();
  1836. $rule['required']['regex'] = 'none';
  1837. $rule['required']['alertText'] = '此项不能为空';
  1838. $rule['required']['alertTextCheckboxMultiple'] = '请选择一个选项';
  1839. $rule['required']['alertTextCheckboxe'] = '请选择一个选项';
  1840. $rule['required']['alertTextDateRange'] = '日期范围不可空白';
  1841. foreach($config['struct'] as $k => $v)
  1842. {
  1843. if(isset($v['match']) && $v['match'] != 'option' && is_string($v['match']) && strpos($v['match'], '/') !== false)
  1844. {
  1845. $rule[$k]['regex'] = $v['match'];
  1846. $rule[$k]['alertText'] = $v['desc'];
  1847. }
  1848. }
  1849. }
  1850. $script = '<script>';
  1851. $script .= '(function($){
  1852. $.fn.validationEngineLanguage = function(){
  1853. };
  1854. $.validationEngineLanguage = {
  1855. newLang: function(){
  1856. $.validationEngineLanguage.allRules = ';
  1857. //此处进行了转义过滤,其实也可以使用json_encode的第二个参数,不过只支持5.4以上版本,考虑到虚拟主机就放弃了
  1858. $script .= str_replace(array('\\\\', '\\/', '"/', '/"'), array('\\', '/', '/', '/'), json_encode($rule)) . ';';
  1859. $script .= '}
  1860. };
  1861. $.validationEngineLanguage.newLang();
  1862. })(jQuery);';
  1863. $script .= '$(document).ready(function(){
  1864. $(".form1").validationEngine("attach");
  1865. });';
  1866. $script .= '</script>';
  1867. //print_r($script);die;
  1868. return $script;
  1869. }
  1870. /**
  1871. * save
  1872. *
  1873. * @return string
  1874. */
  1875. public function save()
  1876. {
  1877. $config = $this->config();
  1878. if(isset($config['manage']['save']))
  1879. {
  1880. return 'yes';
  1881. }
  1882. return 'no';
  1883. }
  1884. }