Database.php 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984
  1. <?php
  2. # 此类很大,以后优化,放到Database目录中,拆分为工厂模式
  3. # 必须要优化了...当时为了快速实现直接扔这一个里面了,近期优化
  4. namespace Manage\Src;
  5. use Dever;
  6. use Manage\Src\Lib\Input as Html;
  7. use Manage\Src\Lib\Log;
  8. class Database
  9. {
  10. /**
  11. * project
  12. *
  13. * @var string
  14. */
  15. private $project;
  16. /**
  17. * table
  18. *
  19. * @var string
  20. */
  21. private $table;
  22. /**
  23. * menu
  24. *
  25. * @var string
  26. */
  27. private $menu;
  28. /**
  29. * menu_id
  30. *
  31. * @var string
  32. */
  33. private $menu_id;
  34. /**
  35. * id
  36. *
  37. * @var string
  38. */
  39. private $id;
  40. /**
  41. * config
  42. *
  43. * @var array
  44. */
  45. private $config;
  46. /**
  47. * search
  48. *
  49. * @var bool
  50. */
  51. private $search = false;
  52. /**
  53. * __construct
  54. *
  55. * @return mixed
  56. */
  57. public function __construct()
  58. {
  59. $this->project = Dever::input('project');
  60. $this->table = Dever::input('table');
  61. $this->menu = Dever::input('menu');
  62. $this->menu_id = Dever::input('menu_id');
  63. $this->id = Dever::input('update_where_id', Dever::input('where_id'));
  64. }
  65. /**
  66. * url
  67. *
  68. * @return array
  69. */
  70. private function url($key = false, $id = 0, $table = false, $suffix = '')
  71. {
  72. $table = $table ? $table : $this->table;
  73. if (strpos($table, 'http://') !== false || strpos($table, 'javascript') !== false) {
  74. return $table;
  75. }
  76. $config = array
  77. (
  78. 'project' => 'project/database?project=' . $this->project,
  79. 'list' => 'project/database/list?{param}',
  80. 'search' => 'project/database/list?{param}',
  81. 'add' => 'project/database/update?{param}',
  82. 'update' => 'project/database/update?{param}&where_id=' . $id,
  83. 'updateAction' => 'database.update_action',
  84. 'url' => '',
  85. 'delete' => 'database.delete_action?{param}&where_id=' . $id,
  86. 'recovery' => 'database.recovery_action?{param}&where_id=' . $id,
  87. 'stat' => 'project/database/stat?{param}',
  88. 'excel' => 'database.list_excel?{param}',
  89. 'stat_excel' => 'database.stat_excel?{param}',
  90. 'diy' => 'list?{param}',
  91. );
  92. $state = $this->getState();
  93. if ($key == 'delete' && $state == 2) {
  94. $key = 'recovery';
  95. }
  96. if (strpos($key, 'list') !== false) {
  97. $config[$key] = $config['list'];
  98. } elseif (strpos($key, 'add') !== false) {
  99. $config[$key] = $config['add'];
  100. }
  101. if (isset($config[$key])) {
  102. if (strpos($config[$key], '{param}')) {
  103. $param = $this->getUrlParam($key, $table, $suffix, $state);
  104. $config[$key] = str_replace('{param}', $param, $config[$key]);
  105. }
  106. return Dever::url($config[$key]);
  107. }
  108. return $config;
  109. }
  110. /**
  111. * 获取url需要的参数
  112. *
  113. * @return string
  114. */
  115. private function getUrlParam($key, $table, $suffix, $state)
  116. {
  117. $param = '';
  118. $array = array('project', 'add', 'list', 'update');
  119. if (in_array($key, $array)) {
  120. $where = Dever::preInput('search_');
  121. if ($where) {
  122. if (isset($where['where_id'])) {
  123. //unset($where['where_id']);
  124. }
  125. unset($where['search_option_state']);
  126. $param .= '&' . http_build_query($where);
  127. }
  128. $oper = Dever::preInput('oper_');
  129. if ($oper) {
  130. $param .= '&' . http_build_query($oper);
  131. }
  132. }
  133. if ($key == 'search' && $search_limit = Dever::input('search_limit')) {
  134. $param .= '&search_limit=' . $search_limit;
  135. }
  136. if (strpos($suffix, 'search_option_state') !== false) {
  137. $state = '';
  138. } else {
  139. $state = '&search_option_state=' . $state;
  140. }
  141. $param = 'project=' . $this->project . $param . '&table=' . $table . '&menu=' . $this->menu . '&menu_id=' . $this->menu_id . $state . $suffix;
  142. return $param;
  143. }
  144. /**
  145. * 获得refer
  146. *
  147. * @return string
  148. */
  149. private function refer($type = 'project')
  150. {
  151. return '';
  152. switch ($type) {
  153. case 'project':
  154. $name = '数据库操作';
  155. break;
  156. case 'list':
  157. $name = '数据列表';
  158. break;
  159. case 'url':
  160. $name = '项目列表';
  161. break;
  162. }
  163. $name = '返回上一级';
  164. $url = $this->url($type);
  165. $refer = '-[<a href="' . $url . '">' . $name . '</a>]';
  166. return $refer;
  167. }
  168. /**
  169. * getAdminInfo
  170. *
  171. * @return array
  172. */
  173. private function getAdminInfo($data)
  174. {
  175. $admin = Dever::load('manage/auth.info');
  176. $data['my_admin_id'] = $admin['id'];
  177. $data['my_role_id'] = $admin['role'];
  178. return $data;
  179. }
  180. /**
  181. * cookie 处理cookie
  182. *
  183. * @return array
  184. */
  185. public function cookie()
  186. {
  187. $cookie = $_COOKIE;
  188. $array = array();
  189. # 判断手机版
  190. $mobile = Dever::mobile();
  191. if ((isset($cookie['left_menu']) && $cookie['left_menu'] == 'close') || $mobile) {
  192. $array['wrapper'] = 'enlarged forced';
  193. } else {
  194. $array['wrapper'] = 'forced';
  195. }
  196. return $array;
  197. }
  198. /**
  199. * listStyle
  200. *
  201. * @return array
  202. */
  203. public function listStyle()
  204. {
  205. $mobile = Dever::mobile();
  206. if ($mobile) {
  207. $style = '.container_list .panel-heading .form-control,#mul .form-control,#page .form-control
  208. {
  209. width:70%;
  210. }
  211. .row
  212. {
  213. margin-right: -15px;
  214. margin-left: -15px;
  215. }
  216. #dever_modal_body img
  217. {
  218. max-width:300px;
  219. }
  220. ';
  221. } else {
  222. $style = '#dever_modal_body img
  223. {
  224. max-width:300px;
  225. }';
  226. }
  227. return $style;
  228. }
  229. /**
  230. * info 处理一些基本的信息,供模板使用
  231. *
  232. * @return array
  233. */
  234. public function info()
  235. {
  236. $config = $this->config();
  237. $menu = isset($config['lang']) ? $config['lang'] : $this->project . '-' . $this->table;
  238. if ($config['state'] == 2) {
  239. $menu .= '[回收站]';
  240. }
  241. $top = Dever::load('manage/top.get');
  242. //print_r($top);die;
  243. $display = '';
  244. # 新增权限
  245. $insert_state = Dever::load('manage/auth.oper', 4);
  246. if ((isset($config['manage']['insert']) && $config['manage']['insert'] == false) || !isset($config['manage']['insert']) || $insert_state == false) {
  247. $display = 'display:none';
  248. }
  249. $info = array
  250. (
  251. 'action' => $this->url('updateAction'),
  252. 'list' => $this->url('list'),
  253. 'search' => $this->url('search'),
  254. 'mul' => $this->url('list'),
  255. //'add' => 'location.href=\'' . $this->url('add') . '\'',
  256. 'add' => $this->url('add'),
  257. 'add_state' => $display,
  258. 'project' => $this->project,
  259. 'table' => $this->table,
  260. 'id' => $this->id,
  261. 'main' => '数据管理' . $this->refer('url'),
  262. 'list_header' => $menu,
  263. 'list_desc' => '数据列表',
  264. 'project_name' => Dever::load('manage/project.name', $this->project),
  265. 'update_header' => $menu,
  266. 'update_desc' => '更新数据',
  267. 'updateMul' => (!$this->id && isset($config['manage']['updateMul']) && $config['manage']['updateMul']) ? '增加一条' : false,
  268. 'updateDesc' => isset($config['manage']['desc']) ? $config['manage']['desc'] : '',
  269. 'listDesc' => isset($config['desc']) ? $config['desc'] : '',
  270. 'struct' => isset($config['manage']['struct']) ? $config['manage']['struct'] : '',
  271. 'stat_desc' => '主动统计',
  272. );
  273. $info['parent'] = Dever::input('oper_parent');
  274. if ($info['parent']) {
  275. $oper = Dever::preInput('oper_');
  276. if ($oper) {
  277. $info['parent'] .= '&' . str_replace('oper_', '', http_build_query($oper));
  278. //$info['parent'] .= '&' . http_build_query($oper);
  279. }
  280. //$info['parent'] = 'location.href=\'' . $this->url(Dever::input('oper_index', 11), 0, $info['parent']) . '\'';
  281. $info['parent'] = $this->url(Dever::input('oper_index', 'listData'), 0, $info['parent']);
  282. $info['parent_state'] = '';
  283. } else {
  284. $info['parent_state'] = 'display:none';
  285. }
  286. $info['save'] = Dever::input('oper_save_jump');
  287. if ($info['save']) {
  288. $info['list'] = $this->url(Dever::input('oper_index', 'listData'), 0, $info['save']);
  289. }
  290. $search = Dever::preInput('search_');
  291. if ($search) {
  292. $info['mul'] .= '&' . http_build_query($search);
  293. }
  294. if ($this->id) {
  295. $info['update_header'] = $menu . '-更新数据' . $this->refer('list');
  296. }
  297. return $info;
  298. }
  299. /**
  300. * 获取表名
  301. *
  302. * @return array
  303. */
  304. public function getTableName($project, $table)
  305. {
  306. if ($project == 'other') {
  307. return '无数据表';
  308. }
  309. $path = Dever::load('manage/project.path', $project);
  310. $config = $this->config($project, $path, $table);
  311. $data = isset($config['lang']) ? $config['lang'] : $project;
  312. return $data;
  313. }
  314. private function getState()
  315. {
  316. return Dever::input('search_option_state');
  317. }
  318. /**
  319. * config
  320. *
  321. * @return array
  322. */
  323. public function config($project = false, $path = false, $table = false)
  324. {
  325. if (!$project) {
  326. $project = $this->project;
  327. $table = $this->table;
  328. }
  329. if (!$project) {
  330. return array();
  331. }
  332. Dever::$global['manage']['project'] = $project;
  333. $config = Dever::load('manage/project.config', $project);
  334. if (!is_array($config)) {
  335. Dever::alert($project . ' error');
  336. }
  337. $project = $config['name'];
  338. if (!$path) {
  339. if (isset($config['base']) && isset($config['rel'])) {
  340. $path = DEVER_INCLUDE_PATH . $config['rel'];
  341. } else {
  342. $path = $config['path'];
  343. }
  344. }
  345. if ($project && $path && empty($this->config[$path])) {
  346. $this->config[$path] = array();
  347. $dir = $path . 'database/';
  348. if (is_dir($dir)) {
  349. $database = scandir($dir);
  350. foreach ($database as $k => $v) {
  351. if (strpos($v, '.php') !== false) {
  352. $k = str_replace('.php', '', $v);
  353. $this->config[$path][$k] = $this->struct($dir . $v, $project);
  354. }
  355. }
  356. }
  357. }
  358. return ($table && isset($this->config[$path][$table])) ? $this->config[$path][$table] : $this->config[$path];
  359. }
  360. /**
  361. * 对database配置文件进行解析
  362. *
  363. * @return array
  364. */
  365. private function struct($file, $project = false)
  366. {
  367. $config = include $file;
  368. if (isset($config['struct']) && is_array($config['struct']['id'])) {
  369. $config['update'] = false;
  370. $config['manage']['struct'] = '<table border=1 width=100% height=100%>';
  371. $config['level'] = 1;
  372. foreach ($config['struct'] as $k => $v) {
  373. if (isset($v['level'])) {
  374. $config['level'] = 3;
  375. }
  376. if (isset($v['search_parent'])) {
  377. $config['manage']['search_parent'][$v['search_parent']] = $k;
  378. $config['manage']['search_child'][$k] = $v['search_parent'];
  379. }
  380. if (isset($v['search']) && is_string($v['search'])) {
  381. if (strpos($v['search'], 'time') !== false) {
  382. $config['manage']['search']['time'][$k] = $v['name'];
  383. }
  384. if (strpos($v['search'], 'date') !== false) {
  385. $config['manage']['search']['date'][$k] = $v['name'];
  386. }
  387. if (strpos($v['search'], 'exp') !== false) {
  388. $config['manage']['search']['exp'][$k] = $v['name'];
  389. }
  390. if (strpos($v['search'], 'fulltext') !== false) {
  391. $config['manage']['search']['fulltext'][$k] = $v['name'];
  392. isset($v['match']) && $config['request']['list']['option'][$k] = array('option', 'like');
  393. } elseif (strpos($v['search'], 'text') !== false) {
  394. $config['manage']['search']['text'][$k] = $v['name'];
  395. }
  396. if (isset($v['exist']) && strpos($v['search'], 'exist') !== false) {
  397. $config['manage']['search']['exist'][$k]['option'] = $v['exist'];
  398. $config['manage']['search']['exist'][$k]['lang'] = $v['name'];
  399. }
  400. if (isset($v['option']) && strpos($v['search'], 'select') !== false) {
  401. $config['manage']['search']['select'][$k]['option'] = $v['option'];
  402. $config['manage']['search']['select'][$k]['lang'] = $v['name'];
  403. if (isset($v['default'])) {
  404. $config['manage']['search']['select'][$k]['default'] = $v['default'];
  405. }
  406. }
  407. if (isset($v['option']) && strpos($v['search'], 'group') !== false) {
  408. $config['manage']['search']['group'][$k]['option'] = $v['option'];
  409. $config['manage']['search']['group'][$k]['lang'] = $v['name'];
  410. if (isset($v['default'])) {
  411. $config['manage']['search']['group'][$k]['default'] = $v['default'];
  412. }
  413. }
  414. if (isset($v['option']) && strpos($v['search'], 'linkage') !== false) {
  415. $config['manage']['search']['linkage'][$k]['option'] = $v['option'];
  416. $config['manage']['search']['linkage'][$k]['lang'] = $v['name'];
  417. if (isset($v['default'])) {
  418. $config['manage']['search']['linkage'][$k]['default'] = $v['default'];
  419. }
  420. }
  421. } elseif (isset($v['search']) && is_array($v['search'])) {
  422. if (isset($v['search']['api'])) {
  423. $config['manage']['search']['api'][$k] = $v['search'];
  424. $config['manage']['search']['api'][$k]['lang'] = $v['name'];
  425. }
  426. }
  427. /*
  428. if (isset($v['table']) && !isset($config['end'])) {
  429. $config['end']['insert'] = $v['value'];
  430. }
  431. */
  432. if (isset($v['update'])) {
  433. if (!isset($config['manage']['insert'])) {
  434. $config['manage']['insert'] = true;
  435. }
  436. $config['update'] = true;
  437. }
  438. if (isset($v['type'])) {
  439. $config['manage']['struct'] .= Dever::tbody(array($k, $v['type'], $v['name']));
  440. }
  441. }
  442. $config['manage']['struct'] .= '</table>';
  443. $config['manage']['_list'] = array
  444. (
  445. 'col' => 'manage',
  446. 'name' => '管理',
  447. 'value' => array(),
  448. );
  449. if (!isset($config['manage']['edit']) && $config['update'] == true) {
  450. # 3
  451. $config['manage']['_list']['value']['update'] = '编辑';
  452. }
  453. if (!isset($config['manage']['delete']) || (isset($config['manage']['delete']) && $config['manage']['delete'] == true)) {
  454. # 6
  455. $config['manage']['_list']['value']['delete'] = '删除';
  456. }
  457. if (isset($config['manage']['list'][0]) && is_array($config['manage']['list'][0])) {
  458. $config['manage']['_list']['value'] += $config['manage']['list'][0];
  459. unset($config['manage']['list'][0]);
  460. } elseif (isset($config['manage']['list_button'])) {
  461. $config['manage']['_list']['value'] += $config['manage']['list_button'];
  462. }
  463. if (!$config['manage']['_list']['value']) {
  464. unset($config['manage']['_list']);
  465. }
  466. }
  467. $config['state'] = $this->getState();
  468. $config['project'] = $project;
  469. return $config;
  470. }
  471. /**
  472. * 自定义列表页面
  473. *
  474. * @return array
  475. */
  476. public function page($key = 'list')
  477. {
  478. $key = Dever::input('page_value', $key);
  479. $type = Dever::input('page_type', 1);
  480. $config = $this->config();
  481. $result = array();
  482. $result['s1'] = 'display:none';
  483. $result['s2'] = 'display:';
  484. $result['content'] = '';
  485. $key = 'page_' . $key;
  486. if (isset($config['manage'][$key])) {
  487. $result['content'] = Dever::view($config['manage'][$key], 'manage', $this->project);
  488. //echo $result['content'];die;
  489. if ($result['content']) {
  490. Dever::setInput('page_content', 1);
  491. $result['s1'] = 'display:';
  492. $result['s2'] = 'display:none';
  493. # 添加底部
  494. $result['content'] .= Dever::view('foot', 'inc', 'manage');
  495. }
  496. }
  497. return $result;
  498. }
  499. /**
  500. * 自定义列表页面
  501. *
  502. * @return array
  503. */
  504. public function page_list()
  505. {
  506. return $this->page('list');
  507. }
  508. /**
  509. * 自定义列表页面 只接管table部分,同时必须传值page_type=1
  510. *
  511. * @return array
  512. */
  513. public function page_list_table()
  514. {
  515. $type = Dever::input('page_type', 2);
  516. if ($type == 1) {
  517. return $this->page('list_table');
  518. } else {
  519. return $this->page('list_table_default');
  520. }
  521. }
  522. /**
  523. * 自定义列表页面
  524. *
  525. * @return array
  526. */
  527. public function page_update()
  528. {
  529. return $this->page('update');
  530. }
  531. /**
  532. * 载入自定义资源
  533. *
  534. * @return array
  535. */
  536. public function res()
  537. {
  538. $result = array
  539. (
  540. 'js' => '',
  541. 'css' => '',
  542. );
  543. if (!$this->project) {
  544. return $result;
  545. }
  546. $config = $this->config();
  547. if (isset($config['manage']['res'])) {
  548. foreach ($config['manage']['res'] as $k => $v) {
  549. $result[$k] = $this->getRes($k, $v);
  550. }
  551. }
  552. return $result;
  553. }
  554. /**
  555. * getRes
  556. *
  557. * @return string
  558. */
  559. protected function getRes($path, $value)
  560. {
  561. if (is_array($value)) {
  562. $res = '';
  563. foreach ($value as $k => $v) {
  564. $res .= $this->getRes($path, $v);
  565. }
  566. return $res;
  567. } else {
  568. if ($this->project != 'manage') {
  569. $base = str_replace('assets/', 'assets/manage/', Dever::config('host', $this->project)->$path);
  570. } else {
  571. $base = Dever::config('host', $this->project)->$path;
  572. }
  573. $file = $base . $value . '.' . $path;
  574. $res = '<script type="text/javascript" src="'.$file.'"></script>';
  575. return $res;
  576. }
  577. }
  578. /**
  579. * 头部banner
  580. *
  581. * @return array
  582. */
  583. public function banner()
  584. {
  585. if (!$this->project) {
  586. return '';
  587. }
  588. $config = $this->config();
  589. $result = '';
  590. if (isset($config['manage']['banner'])) {
  591. foreach ($config['manage']['banner'] as $k => $v) {
  592. $config['manage']['banner'][$k] = $this->value($v, 'yes');
  593. }
  594. if (empty($config['manage']['banner']['link'])) {
  595. $config['manage']['banner']['link'] = '#';
  596. $config['manage']['banner']['target'] = '_self';
  597. }
  598. if (empty($config['manage']['banner']['target'])) {
  599. $config['manage']['banner']['target'] = '_blank';
  600. }
  601. $result = '<a target="' . $config['manage']['banner']['target'] . '" href="' . $config['manage']['banner']['link'] . '"><img src="' . $config['manage']['banner']['img'] . '"></a>
  602. <span>' . $config['manage']['banner']['name'] . '</span>';
  603. }
  604. return $result;
  605. }
  606. /**
  607. * 列表页搜索
  608. *
  609. * @return array
  610. */
  611. public function list_search_select(&$result, $search, $name, $lang, $option, $default = false, $bind = array(), $parent = '')
  612. {
  613. $value = array();
  614. if (strpos($lang, '-')) {
  615. $temp = explode('-', $lang);
  616. $lang = $temp[0];
  617. }
  618. $value['name'] = $name;
  619. $value['lang'] = $lang;
  620. $value['option'] = $option;
  621. if (isset($search[$name])) {
  622. $value['value'] = $search[$name];
  623. }
  624. if ($default) {
  625. //$value['default'] = $default;
  626. }
  627. if ($bind) {
  628. $value['bind'] = $bind;
  629. }
  630. $value['parent'] = $parent;
  631. $result .= Html::select($value, false) . '&nbsp;&nbsp;&nbsp;';
  632. }
  633. /**
  634. * 列表页搜索
  635. *
  636. * @return array
  637. */
  638. public function list_search_group(&$result, $search, $name, $lang, $option, $default = false)
  639. {
  640. $value = array();
  641. $value['name'] = $name;
  642. $value['lang'] = $lang;
  643. $value['option'] = $option;
  644. if (isset($search[$name])) {
  645. $value['value'] = $search[$name];
  646. }
  647. if ($default) {
  648. //$value['default'] = $default;
  649. }
  650. $result .= Html::group($value, false) . '&nbsp;&nbsp;&nbsp;';
  651. }
  652. /**
  653. * 列表页搜索
  654. *
  655. * @return array
  656. */
  657. public function list_search_linkage(&$result, $search, $name, $lang, $option, $default = false)
  658. {
  659. $value = array();
  660. $value['name'] = $name;
  661. $value['lang'] = $lang;
  662. $value['option'] = $option;
  663. $value['search_state'] = true;
  664. if (isset($search[$name])) {
  665. $value['value'] = $search[$name];
  666. }
  667. if ($default) {
  668. //$value['default'] = $default;
  669. }
  670. $result .= Html::linkage($value, 'span') . '&nbsp;&nbsp;&nbsp;';
  671. }
  672. /**
  673. * 列表页搜索
  674. *
  675. * @return array
  676. */
  677. public function list_search_time(&$result, $search, $prefix, $key, $lang, $type = 'time')
  678. {
  679. $value['name'] = $prefix . 'option_start_' . $key;
  680. $value['lang'] = '开始' . $lang;
  681. if (isset($search[$value['name']])) {
  682. $value['value'] = Dever::maketime($search[$value['name']]);
  683. Dever::setInput($value['name'], $value['value'], 'search_');
  684. }
  685. $result .= Html::$type($value) . '&nbsp;-&nbsp;';
  686. $value['name'] = $prefix . 'option_end_' . $key;
  687. $value['lang'] = '结束' . $lang;
  688. if (isset($search[$value['name']])) {
  689. $value['value'] = Dever::maketime($search[$value['name']]);
  690. Dever::setInput($value['name'], $value['value'], 'search_');
  691. }
  692. $result .= Html::$type($value) . '&nbsp;&nbsp;';
  693. }
  694. /**
  695. * 列表页头部按钮
  696. *
  697. * @return array
  698. */
  699. public function list_button()
  700. {
  701. if (Dever::load('manage/auth.oper', 2) == false) {
  702. Dever::alert('你没有权限');
  703. }
  704. $config = $this->config();
  705. $result = '';
  706. # 增加主动统计按钮
  707. if (isset($config['manage']['stat'])) {
  708. $name = '数据统计';
  709. if (is_string($config['manage']['stat'])) {
  710. $name = $config['manage']['stat'];
  711. }
  712. $config['manage']['button'][$name] = $this->url('stat');
  713. }
  714. # 增加下载报表按钮
  715. if (isset($config['manage']['excel'])) {
  716. $name = '导出数据';
  717. if (is_string($config['manage']['excel'])) {
  718. $name = $config['manage']['excel'];
  719. }
  720. $config['manage']['button'][$name] = 'onclick="jump(\''.$this->url('excel').'\', \'#form1\')"';
  721. }
  722. # 增加回收站
  723. if (isset($config['struct']['state']) && (!isset($config['manage']['delete']) || (isset($config['manage']['delete']) && $config['manage']['delete'] && isset($config['manage']['_list']['value']['delete'])))) {
  724. if ($config['state'] == 2) {
  725. $config['manage']['button']['返回数据列表'] = $this->url('list', 0, false, '&search_option_state=1');
  726. } else {
  727. $config['manage']['button']['回收站'] = $this->url('list', 0, false, '&search_option_state=2');
  728. }
  729. }
  730. # 列表页按钮
  731. if (isset($config['manage']['button'])) {
  732. $result .= $this->button_list($config['manage']['button']);
  733. }
  734. return $result;
  735. }
  736. /**
  737. * 更新页按钮
  738. *
  739. * @return array
  740. */
  741. public function update_button()
  742. {
  743. # 新增权限
  744. if (Dever::load('manage/auth.oper', 2) == false) {
  745. Dever::alert('你没有权限');
  746. }
  747. $info = $this->info();
  748. $result = '<button type="button" class="btn btn-primary save-data"><i class="fa fa-save"></i> 提交保存</button>
  749. <a href="' . $info['list'] . '" class="btn btn-danger" onclicks="msg({status:1,msg:\'yes\'})">放弃保存</a>';
  750. $config = $this->config();
  751. if (isset($config['manage']['update_button']) && $config['manage']['update_button']) {
  752. $result = '';
  753. foreach ($config['manage']['update_button'] as $k => $v) {
  754. if ($v == 'save-data') {
  755. $result .= '<button type="button" class="btn btn-primary save-data"><i class="fa fa-save"></i> ' . $k . '</button>';
  756. } elseif ($v == 'copy-data') {
  757. $result .= '<a href="javascript:;" class="btn btn-danger" onclick="set_update(\'#copy_num\', 1);set_update(\'#update_where_id\', 0);submit_action();">' . $k . '</a>';
  758. } elseif ($v == 'copy-datas') {
  759. $result .= '<a href="#dever_modal" data-toggle="modal" class="btn btn-danger" onclick="copy_data($(this))">' . $k . '</a>';
  760. } else {
  761. $result .= '<a href="javascript:;" class="btn btn-danger" onclick="' . $v . '">' . $k . '</a>';
  762. }
  763. }
  764. }
  765. return $result;
  766. }
  767. private function button_list($button)
  768. {
  769. $result = '';
  770. foreach ($button as $k => $v) {
  771. if (is_array($v)) {
  772. if ($v[0] == 'location') {
  773. $result .= '&nbsp;&nbsp;<a class="btn btn-default" onclick="location.href=\''.Dever::url($v[1]).'\'">' . $k . '</a>';
  774. } else {
  775. $result .= '&nbsp;&nbsp;<a class="btn btn-default" href="' . $this->url($v[0], false, $v[1]) . '">' . $k . '</a>';
  776. }
  777. } elseif (strpos($v, 'modal|') !== false) {
  778. $t = explode('modal|', $v);
  779. $result .= '&nbsp;&nbsp;<a href="#dever_modal" data-toggle="modal" class="btn btn-default" onclick="$(\'#dever_modal_body\').html(\'' . $t[1] . '\')">' . $k . '</a>';
  780. } elseif (strpos($v, '|') !== false) {
  781. $t = explode('|', $v);
  782. $result .= $t[0] . '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" ' . $t[1] . '>' . $k . '</a>';
  783. } elseif (strpos($v, 'onclick') !== false) {
  784. $result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" ' . $v . '>' . $k . '</a>';
  785. } elseif (strpos($v, 'http://') !== false) {
  786. //$result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-default" onclick="jump(\''.Dever::url($v).'\')">'.$k.'</a>';
  787. $result .= '&nbsp;&nbsp;<a class="btn btn-default" href="' . Dever::url($v) . '">' . $k . '</a>';
  788. } else {
  789. $result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-danger" onclick="load(\'' . Dever::url($v) . '\')">' . $k . '</a>';
  790. }
  791. }
  792. return $result;
  793. }
  794. /**
  795. * 列表页搜索
  796. *
  797. * @return array
  798. */
  799. public function list_search()
  800. {
  801. # 新增权限
  802. if (Dever::load('manage/auth.oper', 2) == false) {
  803. Dever::alert('你没有权限');
  804. }
  805. $config = $this->config();
  806. $result = '';
  807. # 搜索
  808. if (isset($config['struct']) && isset($config['manage']['search'])) {
  809. $search = Dever::preInput('search_');
  810. $node = Html::node(array('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
  811. $btn = '<input type="hidden" value="2" name="excel" id="excel"><button class="btn btn-primary" onclick="$(\'#form1\').attr(\'action\', \'' . $this->url('search') . '\');list_search($(this))" type="button" style="height: 38px;margin-left:10px;">搜索</button>';
  812. //$result .= '<form id="f1" action="'.$this->url('list').'" method="post">';
  813. $prefix = 'search_';
  814. $result .= $node[0];
  815. if (isset($config['manage']['search']['linkage'])) {
  816. foreach ($config['manage']['search']['linkage'] as $k => $v) {
  817. $this->list_search_linkage($result, $search, $prefix . 'linkage_' . $k, $v['lang'], $this->option($v['option']), $v['default']);
  818. $result .= '<br />';
  819. }
  820. }
  821. if (isset($config['manage']['search']['time'])) {
  822. foreach ($config['manage']['search']['time'] as $k => $v) {
  823. $this->list_search_time($result, $search, $prefix, $k, $v);
  824. }
  825. }
  826. if (isset($config['manage']['search']['date'])) {
  827. foreach ($config['manage']['search']['date'] as $k => $v) {
  828. $this->list_search_time($result, $search, $prefix, $k, $v, 'date');
  829. }
  830. }
  831. if (isset($config['manage']['search']['select'])) {
  832. $this->list_search_br($result);
  833. foreach ($config['manage']['search']['select'] as $k => $v) {
  834. $bind = array();
  835. $parent = '';
  836. $option = $this->option($v['option']);
  837. if (isset($config['manage']['search_parent'][$k])) {
  838. $bind = array
  839. (
  840. 'onchange',
  841. 'setOption',
  842. 'search_option_' . $config['manage']['search_parent'][$k]
  843. );
  844. }
  845. elseif (isset($config['manage']['search_child'][$k])) {
  846. $parent = $config['manage']['search_child'][$k];
  847. }
  848. $this->list_search_select($result, $search, $prefix . 'option_' . $k, $v['lang'], $option, $v['default'], $bind, $parent);
  849. }
  850. }
  851. if (isset($config['manage']['search']['exist'])) {
  852. $this->list_search_br($result);
  853. foreach ($config['manage']['search']['exist'] as $k => $v) {
  854. $this->list_search_select($result, $search, $prefix . 'exist_' . $k, $v['lang'], $this->option($v['option']), '');
  855. }
  856. }
  857. if (isset($config['manage']['search']['group'])) {
  858. $this->list_search_br($result);
  859. foreach ($config['manage']['search']['group'] as $k => $v) {
  860. $this->list_search_group($result, $search, $prefix . 'option_' . $k, $v['lang'], $this->option($v['option']), $v['default']);
  861. }
  862. }
  863. if (isset($config['manage']['search']['api'])) {
  864. $this->list_search_br($result);
  865. foreach ($config['manage']['search']['api'] as $k => $v) {
  866. $value = array();
  867. $value['name'] = $prefix . 'api_' . $k;
  868. $value['class'] = 'form-control';
  869. if (isset($search[$value['name']])) {
  870. $value['value'] = $search[$value['name']];
  871. }
  872. if (strpos($v['lang'], '-') !== false) {
  873. $t = explode('-', $v['lang']);
  874. $v['lang'] = $t[0];
  875. }
  876. $value['placeholder'] = $v['lang'];
  877. $result .= Html::text($value, false) . '&nbsp;';
  878. }
  879. }
  880. if (isset($config['manage']['search']['text'])) {
  881. $this->list_search_br($result);
  882. foreach ($config['manage']['search']['text'] as $k => $v) {
  883. $value = array();
  884. $value['name'] = $prefix . 'option_' . $k;
  885. $value['class'] = 'form-control';
  886. if (isset($search[$value['name']])) {
  887. $value['value'] = $search[$value['name']];
  888. }
  889. if (strpos($v, '-') !== false) {
  890. $t = explode('-', $v);
  891. $v = $t[0];
  892. }
  893. $value['placeholder'] = $v;
  894. $result .= Html::text($value, false) . '&nbsp;';
  895. }
  896. }
  897. if (isset($config['manage']['search']['fulltext'])) {
  898. $this->list_search_br($result);
  899. foreach ($config['manage']['search']['fulltext'] as $k => $v) {
  900. $value = array();
  901. $value['name'] = $prefix . 'fulltext_' . $k;
  902. $value['class'] = 'form-control';
  903. if (isset($search[$value['name']])) {
  904. $value['value'] = $search[$value['name']];
  905. }
  906. if (strpos($v, '-') !== false) {
  907. $t = explode('-', $v);
  908. $v = $t[0];
  909. }
  910. $value['placeholder'] = $v;
  911. $result .= Html::text($value, false) . '&nbsp;';
  912. }
  913. }
  914. if (isset($config['manage']['search']['exp'])) {
  915. $result .= '<br />';
  916. $this->list_search_select($result, $search, $prefix . 'exp_col', '选择检索项', $config['manage']['search']['exp']);
  917. $this->list_search_select($result, $search, $prefix . 'exp_type', '', array
  918. (
  919. 3 => '大于',
  920. 4 => '大于等于',
  921. 5 => '小于',
  922. 6 => '小于等于',
  923. ), 1);
  924. $value = array();
  925. $value['name'] = $prefix . 'exp_value';
  926. $value['class'] = 'form-control';
  927. if (isset($search[$value['name']])) {
  928. $value['value'] = $search[$value['name']];
  929. }
  930. $value['placeholder'] = '填写值';
  931. $result .= Html::text($value, false) . '&nbsp;';
  932. }
  933. # 列表页按钮
  934. if (isset($config['manage']['search_button'])) {
  935. $btn .= $this->button_list($config['manage']['search_button']);
  936. }
  937. $result .= $btn . $node[1];
  938. //$result .= '</form>';
  939. # 增加每页显示条数
  940. $config['manage']['num'] = $this->getNumConfig($config);
  941. if ($config['manage']['num']) {
  942. $result = $this->num($result, $config['manage']['num']);
  943. }
  944. }
  945. //print_r($result);die;
  946. return $result;
  947. }
  948. private function list_search_br(&$result)
  949. {
  950. if (strpos($result, '&nbsp;&nbsp;') && !strpos($result, '<br />')) {
  951. $result .= '<br />';
  952. }
  953. }
  954. /**
  955. * 获取条数配置
  956. *
  957. * @return array
  958. */
  959. private function getNumConfig($config)
  960. {
  961. if (isset($config['manage']['list_type'])) {
  962. return false;
  963. }
  964. if (isset($config['manage']['num']) && $config['manage']['num'] == false) {
  965. return false;
  966. }
  967. if (!isset($config['manage']['num'])) {
  968. $config['manage']['num'] = array(20,50,100);
  969. }
  970. return $config['manage']['num'];
  971. }
  972. /**
  973. * 生成条数的html
  974. *
  975. * @return string
  976. */
  977. private function num($result, $config)
  978. {
  979. $limit = '';
  980. $cur = Dever::input('search_limit', 0);
  981. foreach($config as $k => $v) {
  982. if ($cur == $k) {
  983. $btn = 'success';
  984. } else {
  985. $btn = 'default';
  986. }
  987. $url = $this->url('list', 0, false, '&search_limit=' . $k);
  988. $limit .= '<a href="'.$url.'" style="width:60px;font-weight:bold;" class="btn btn-'.$btn.'">'.$v.'</a> ';
  989. }
  990. $num = '<div class="form-group "><div style="padding-top:20px;margin-bottom: -30px;"><label style="color:#bababa;margin-left:8px;margin-right:10px;font-size:14px;font-weight:bold;">每页显示条数:</label>'.$limit.'</div></div>';
  991. $result .= $num;
  992. return $result;
  993. }
  994. /**
  995. * 列表页表格头
  996. *
  997. * @return array
  998. */
  999. public function list_thead()
  1000. {
  1001. # 新增权限
  1002. if (Dever::load('manage/auth.oper', 1) == false) {
  1003. Dever::alert('你没有权限');
  1004. }
  1005. $config = $this->config();
  1006. $result = array();
  1007. if (isset($config['struct'])) {
  1008. if (isset($config['manage']['list']) && $config['manage']['list'] == 'update') {
  1009. # 验证头部权限 这里如果是多个选择权限的话,暂时无法实现。。。
  1010. if (isset($config['top']) && is_string($config['top'])) {
  1011. $top = Dever::load('manage/auth.getTop', array($config['top']));
  1012. if ($top) {
  1013. $url = $this->url('update', $top['value']);
  1014. }
  1015. } else {
  1016. $url = $this->url('update', 1);
  1017. }
  1018. return Dever::location($url);
  1019. } else {
  1020. if (isset($config['manage']['mul'])) {
  1021. $result[] = '<th><input type="checkbox" class="checkbox-checkall" name="checkall" id="checkall" value="list"/></th>';
  1022. }
  1023. $data = array();
  1024. $data = $this->getAdminInfo($data);
  1025. foreach ($config['struct'] as $k => $v) {
  1026. if (isset($v['list']) && $v['list']) {
  1027. # 处理权限
  1028. if (isset($v['auth'])) {
  1029. $auth = $this->value($v['auth'], $data);
  1030. if (!$auth) {
  1031. continue;
  1032. }
  1033. }
  1034. if (isset($v['list_name'])) {
  1035. $v['name'] = $v['list_name'];
  1036. } else {
  1037. $temp = Html::cue(array('name' => $v['name']));
  1038. $v['name'] = $temp['name'];
  1039. }
  1040. if (!isset($v['level'])) {
  1041. $v['level'] = $config['level'];
  1042. }
  1043. if (isset($v['search']) && is_string($v['search']) && strpos($v['search'], 'order') !== false) {
  1044. $link = Dever::url();
  1045. $order = 'search_order_' . $k . '';
  1046. if (strpos($link, $order) !== false) {
  1047. $link = preg_replace('/[?|&]' . $order . '=(desc|asc)/i', '{order}', $link);
  1048. } else {
  1049. $link .= '{order}';
  1050. }
  1051. $search = Dever::input($order);
  1052. $cancel = '';
  1053. if ($search) {
  1054. $asc = $search;
  1055. $cancel = '&nbsp;<a class="glyphicon glyphicon-remove-sign" style="color:black;" href="' . str_replace('{order}', '', $link) . '" title="取消排序"></a>';
  1056. } else {
  1057. $asc = 'desc';
  1058. if (!isset($v['order']) && $k != 'id') {
  1059. $cancel = '&nbsp;<a class="glyphicon glyphicon-circle-arrow-down" style="color:black;" href="' . str_replace('{order}', '&' . $order . '=desc', $link) . '" title="降序"></a>';
  1060. }
  1061. }
  1062. if ($asc == 'asc') {
  1063. $asc = 'desc';
  1064. $icon = 'down';
  1065. $text = '降序';
  1066. } else {
  1067. $asc = 'asc';
  1068. $icon = 'up';
  1069. $text = '升序';
  1070. }
  1071. $link = str_replace('{order}', '&' . $order . '=' . $asc, $link);
  1072. $result[] = '<th data-priority="'.$v['level'].'"><a href="' . $link . '" title="' . $text . '" style="color:black;">' . $v['name'] . '&nbsp;<span class="glyphicon glyphicon-circle-arrow-' . $icon . '"></span></a>' . $cancel . '</th>';
  1073. } else {
  1074. $result[] = '<th data-priority="'.$v['level'].'">' . $v['name'] . '</th>';
  1075. }
  1076. }
  1077. }
  1078. if ((Dever::load('manage/auth.oper', 3) != false || Dever::load('manage/auth.oper', 5) != false) && isset($config['manage']['_list']['value'])) {
  1079. $result[] = '<th data-priority="1">' . $config['manage']['_list']['name'] . '</th>';
  1080. }
  1081. }
  1082. }
  1083. return $result;
  1084. }
  1085. /**
  1086. * 导出列表
  1087. *
  1088. * @return array
  1089. */
  1090. public function list_excel_api()
  1091. {
  1092. Dever::$global['base']['excel'] = 1;
  1093. $data = $this->list_tbody(true);
  1094. if ($data) {
  1095. $config = $this->config();
  1096. if (isset($config['manage']['excel']) && $config['manage']['excel']) {
  1097. $method = 'excel';
  1098. $info = $this->info();
  1099. $data = $this->getAdminInfo($data);
  1100. $this->excel($info, $data, $config);
  1101. }
  1102. }
  1103. //print_r($data);die;
  1104. }
  1105. private function excel($info, $data, $config)
  1106. {
  1107. $file = $info['list_header'] . '的' . $info['list_desc'] . '-' . $info['project_name'];
  1108. $header = $body = array();
  1109. $i = 0;
  1110. foreach ($config['struct'] as $k => $v) {
  1111. if (isset($v['list']) && $v['list']) {
  1112. # 处理权限
  1113. if (isset($v['auth'])) {
  1114. $auth = $this->value($v['auth'], $data);
  1115. if (!$auth) {
  1116. continue;
  1117. }
  1118. }
  1119. //$v['name'] = mb_convert_encoding($v['name'],'gb2312','utf-8');
  1120. $header[$i] = $v['name'];
  1121. $i++;
  1122. $j = 0;
  1123. foreach ($data as $ki => $vi) {
  1124. if (isset($vi[$k])) {
  1125. $value = $vi[$k];
  1126. if (is_string($v['list'])) {
  1127. $value = $this->value($v['list'], $data[$ki], $config['struct']);
  1128. }
  1129. if (isset($v['option'])) {
  1130. $v['option'] = $this->option($v['option']);
  1131. if (strstr($value, ',')) {
  1132. $temp = explode(',', $value);
  1133. $t = array();
  1134. foreach ($temp as $vv) {
  1135. if (isset($v['option'][$vv]) && is_array($v['option'][$vv])) {
  1136. $t[] = $v['option'][$vv]['name'];
  1137. } else {
  1138. $t[] = $vv;
  1139. }
  1140. }
  1141. $value = implode("\r\n", $t);
  1142. } elseif (isset($v['option'][$value])) {
  1143. if (is_array($v['option'][$value])) {
  1144. $value = $v['option'][$value]['name'];
  1145. } else {
  1146. $value = $v['option'][$value];
  1147. }
  1148. }
  1149. }
  1150. //$value = mb_convert_encoding($value,'gb2312','utf-8');
  1151. $body[$j][] = $value;
  1152. $j++;
  1153. }
  1154. }
  1155. }
  1156. }
  1157. Dever::excelExport($body, $header, $file);
  1158. }
  1159. private function _getChr($start, $num)
  1160. {
  1161. $num = $start + $num;
  1162. $max = floor($num / 91);
  1163. if ($max > 0) {
  1164. $num = $num - (91 * $max) + $start;
  1165. $num = chr($start) . chr($num);
  1166. } else {
  1167. $num = chr($num);
  1168. }
  1169. return $num;
  1170. }
  1171. /**
  1172. * 列表页数据列表 检索
  1173. *
  1174. * @return array
  1175. */
  1176. private function list_tbody_search($config, $param, $high, $search)
  1177. {
  1178. # 根据api的数据检索到id
  1179. $api = Dever::preInput('search_api_');
  1180. if ($api) {
  1181. foreach ($api as $k => $v) {
  1182. if ($v) {
  1183. unset($search[$k]);
  1184. $k = str_replace('search_api_', '', $k);
  1185. # 设定检索高亮
  1186. $high[$k] = $v;
  1187. if (isset($config['manage']['search']['api'][$k]['api'])) {
  1188. $col = $config['manage']['search']['api'][$k]['col'];
  1189. $option = array($col => $v);
  1190. $v = Dever::load($config['manage']['search']['api'][$k]['api'], $option);
  1191. $result = $config['manage']['search']['api'][$k]['result'];
  1192. if (is_array($v)) {
  1193. if (isset($v[$result])) {
  1194. $v = $v[$result];
  1195. } else {
  1196. if ($result == 'id') {
  1197. $v = array_keys($v);
  1198. } else {
  1199. $t = array();
  1200. foreach ($v as $k1 => $v1) {
  1201. $t[] = $v1[$result];
  1202. }
  1203. $v = $t;
  1204. }
  1205. $param['option'][$k] = array('yes', 'in');
  1206. }
  1207. }
  1208. }
  1209. $param['option_' . $k] = $v;
  1210. }
  1211. }
  1212. }
  1213. # 检索有无值
  1214. $exist = Dever::preInput('search_exist_');
  1215. if ($exist) {
  1216. foreach ($exist as $k => $v) {
  1217. if ($v) {
  1218. unset($search[$k]);
  1219. $k = str_replace('search_exist_', '', $k);
  1220. if ($v == 'yes') {
  1221. # 有值
  1222. $param['option'][$k] = array('yes', '!=');
  1223. $param['option_' . $k] = 'null';
  1224. } elseif ($v == 'no') {
  1225. # 无值
  1226. $param['option_' . $k] = 'null';
  1227. } elseif (strpos($v, '|')) {
  1228. # 有无值或者自定义的值
  1229. $temp = explode('|', $v);
  1230. $param['option_' . $k] = $temp[1];
  1231. if ($temp[0] == 'yes') {
  1232. $param['option'][$k] = array('yes', '!=');
  1233. $param['option'][$k . '_t1'] = array('yes-' . $k, '!=');
  1234. } else {
  1235. $param['option'][$k] = array('yes', '=', 'and(');
  1236. $param['option'][$k . '_t1'] = array('yes-' . $k, '=', 'or)');
  1237. }
  1238. $param['option_' . $k . '_t1'] = 'null';
  1239. } else {
  1240. # 自定义的值
  1241. $param['option_' . $k] = $v;
  1242. }
  1243. }
  1244. }
  1245. }
  1246. $fulltext = Dever::preInput('search_fulltext_');
  1247. if ($fulltext) {
  1248. $fulltext_state = false;
  1249. foreach ($fulltext as $k => $v) {
  1250. if ($v) {
  1251. unset($search[$k]);
  1252. $k = str_replace('search_fulltext_', '', $k);
  1253. $param['option_' . $k] = $v;
  1254. # 设定检索高亮
  1255. $high[$k] = $v;
  1256. $fulltext_state = true;
  1257. }
  1258. }
  1259. $this->search = true;
  1260. }
  1261. $linkage = Dever::preInput('search_linkage_');
  1262. if ($linkage) {
  1263. foreach ($linkage as $k => $v) {
  1264. if ($v) {
  1265. unset($search[$k]);
  1266. $k = str_replace('search_linkage_', '', $k);
  1267. if (is_array($v)) {
  1268. $v = implode(',', $v);
  1269. }
  1270. $v = str_replace('-1', ',', $v);
  1271. $v = str_replace(',,,', ',', $v);
  1272. $v = str_replace(',,', ',', $v);
  1273. $v = str_replace(',,', ',', $v);
  1274. $param['option_' . $k] = $v;
  1275. # 设定检索高亮
  1276. $high[$k] = $v;
  1277. }
  1278. }
  1279. $this->search = true;
  1280. }
  1281. $exp = Dever::preInput('search_exp_');
  1282. if ($exp) {
  1283. $k = $search['search_exp_col'];
  1284. $v = $search['search_exp_value'];
  1285. $param['option_' . $k] = $v;
  1286. $param['search_type'] = $search['search_exp_type'];
  1287. $this->search = true;
  1288. }
  1289. $order = Dever::preInput('search_order_');
  1290. if ($order) {
  1291. foreach ($order as $k => $v) {
  1292. $k = str_replace('search_order_', '', $k);
  1293. $param['order'][0][$k] = $v;
  1294. unset($search[$k]);
  1295. }
  1296. }
  1297. foreach ($search as $k => $v) {
  1298. if ($v) {
  1299. if (strpos($k, 'option_') !== false) {
  1300. //Dever::setInput(str_replace('search_', '', $k), $v);
  1301. if (is_array($v)) {
  1302. if ($v[0] == 0) {
  1303. continue;
  1304. }
  1305. $v = str_replace(',0', '', implode(',', $v));
  1306. $t = explode(',', $v);
  1307. $parent = end($t);
  1308. $v .= '%';
  1309. }
  1310. $param[str_replace('search_', '', $k)] = $v;
  1311. $this->search = true;
  1312. } elseif ($k == 'search_limit') {
  1313. $num = $this->getNumConfig($config);
  1314. if ($num) {
  1315. $param['page'][0] = $num[$v];
  1316. }
  1317. }
  1318. }
  1319. }
  1320. return array($param, $high);
  1321. }
  1322. private function list_tbody_mul($param)
  1323. {
  1324. $mul = Dever::load('manage/auth.oper', 3) ? Dever::preInput('mul_') : false;
  1325. if (Dever::input('method') == 'mul' && $mul && $mul['mul_type'] > 0) {
  1326. if ($mul['mul_type'] == 1 && empty($mul['mul_where_id'])) {
  1327. Dever::alert('您还未选择任何数据条目');
  1328. }
  1329. Dever::$global['base']['mul_type'] = $mul['mul_type'];
  1330. unset($mul['mul_type']);
  1331. foreach ($mul as $k => $v) {
  1332. if ($v) {
  1333. if (is_array($v)) {
  1334. $v = implode(',', $v);
  1335. }
  1336. $param[str_replace('mul_', '', $k)] = $v;
  1337. }
  1338. }
  1339. if (isset($param)) {
  1340. $method = $this->project . '/' . $this->table . '-updatemul';
  1341. $state = Dever::load($method, $param);
  1342. }
  1343. Dever::alert('reload');
  1344. }
  1345. }
  1346. /**
  1347. * 列表页数据列表
  1348. *
  1349. * @return array
  1350. */
  1351. public function list_tbody($excel = false)
  1352. {
  1353. # 新增权限
  1354. if (Dever::load('manage/auth.oper', 1) == false) {
  1355. Dever::alert('你没有权限');
  1356. }
  1357. if (Dever::input('page_content') == 1) {
  1358. return;
  1359. }
  1360. $config = $this->config();
  1361. $high = array();
  1362. $search = Dever::load('manage/auth.oper', 2) ? Dever::preInput('search_') : false;
  1363. $param = array();
  1364. $parent = 0;
  1365. if ($search) {
  1366. list($param, $high) = $this->list_tbody_search($config, $param, $high, $search);
  1367. }
  1368. if (empty($param['page'])) {
  1369. $num = $this->getNumConfig($config);
  1370. if ($num) {
  1371. $param['page'][0] = $num[0];
  1372. }
  1373. }
  1374. # 批量更新
  1375. $this->list_tbody_mul($param);
  1376. $result = array();
  1377. if ($excel == true) {
  1378. $method = $this->project . '/' . $this->table . '-all';
  1379. } else {
  1380. $method = $this->project . '/' . $this->table . '-list';
  1381. }
  1382. //print_r($param);die;
  1383. $data = Dever::load($method, $param);
  1384. if (isset($config['manage']['list_type']) && $config['manage']['list_type'] == 'parent') {
  1385. $parent = array();
  1386. $child = array();
  1387. foreach ($data as $k => $v) {
  1388. if ($v[$config['name'] . '_id'] <= 0) {
  1389. $parent[$k] = $v;
  1390. } else {
  1391. $v['name'] = '|-----' . $v['name'];
  1392. $child[$v[$config['name'] . '_id']][$k] = $v;
  1393. }
  1394. }
  1395. if ($parent) {
  1396. $data = array();
  1397. foreach ($parent as $k => $v) {
  1398. $data[$v['id']] = $v;
  1399. if (isset($child[$v['id']])) {
  1400. foreach ($child[$v['id']] as $i => $j) {
  1401. $data[$j['id']] = $j;
  1402. }
  1403. }
  1404. }
  1405. }
  1406. } elseif (isset($config['manage']['list_type']) && $config['manage']['list_type'] == 'tree') {
  1407. $data = $this->tree($data, $config, -1);
  1408. }
  1409. if ($excel == true) {
  1410. return $data;
  1411. }
  1412. if (Dever::input('page_type') == 1) {
  1413. Dever::$global['manage']['list_data'] = $data;
  1414. return;
  1415. }
  1416. //print_r($data);die;
  1417. //print_r(Helper::page("current"));die;
  1418. if ($data && isset($config['struct'])) {
  1419. $i = 0;
  1420. $pg = Dever::input('pg', 1);
  1421. $cur = Dever::input('search_limit', 0);
  1422. $num = $this->getNumConfig($config);
  1423. foreach ($data as $k => $v) {
  1424. $data[$k] = $this->getAdminInfo($data[$k]);
  1425. $data[$k]['i'] = $i;
  1426. $ii = intval($num[$cur])*($pg-1);
  1427. $data[$k]['r'] = $i+1+$ii;
  1428. $i++;
  1429. $result[$k] = '<tr>';
  1430. if (isset($config['manage']['mul'])) {
  1431. $result[$k] .= '<td><input type="checkbox" name="mul_where_id[]" class="checkbox-checkall-list" value="' . $v['id'] . '"/></td>';
  1432. }
  1433. foreach ($config['struct'] as $ki => $vi) {
  1434. # 处理同步
  1435. if (isset($vi['sync']) && strpos($ki, '-')) {
  1436. $temp = explode('-', $ki);
  1437. $load = Dever::load($temp[0] . '/' . $temp[1] . '-one', array('option_' . $vi['sync'][1] => $v[$vi['sync'][0]]));
  1438. $v[$ki] = isset($load[$temp[2]]) ? $load[$temp[2]] : $vi['default'];
  1439. }
  1440. if (isset($vi['list']) && $vi['list']) {
  1441. if (empty($v[$ki])) {
  1442. $v[$ki] = $vi['default'];
  1443. }
  1444. # 处理权限
  1445. if (isset($vi['auth'])) {
  1446. $auth = $this->value($vi['auth'], $data[$k]);
  1447. if (!$auth) {
  1448. continue;
  1449. }
  1450. }
  1451. $show = $v[$ki];
  1452. if (is_string($vi['list'])) {
  1453. $show = $this->value($vi['list'], $data[$k], $config['struct']);
  1454. }
  1455. if (isset($vi['key']) && $vi['update'] != 'editor') {
  1456. $show = Dever::upload($show);
  1457. }
  1458. if (isset($vi['modal'])) {
  1459. $show = Html::modal($this->value($vi['modal'], $data[$k], $config['struct']), $show);
  1460. } elseif (isset($vi['option'])) {
  1461. # 验证option是否是匿名函数
  1462. $vi['option'] = $this->option($vi['option']);
  1463. # 位运算
  1464. if (isset($vi['bit'])) {
  1465. $ts = array();
  1466. foreach ($vi['option'] as $tm => $tv) {
  1467. if (isset($vi['bit'][$tm]) && ($show & $tm) > 0) {
  1468. $ts[] = $tv;
  1469. }
  1470. }
  1471. $show = implode(',', $ts);
  1472. }
  1473. if ((isset($vi['child_name']) || (isset($vi['show']) && is_string($vi['show'])))) {
  1474. if (isset($vi['option'][$show]) && is_array($vi['option'][$show])) {
  1475. $show = $vi['option'][$show]['name'];
  1476. } else {
  1477. $show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
  1478. }
  1479. }
  1480. //elseif ($config['state'] == 1 && (isset($vi['edit']) && $vi['edit']) || $ki == 'status')
  1481. elseif ($config['state'] == 1 && (isset($vi['edit']) && $vi['edit']))
  1482. {
  1483. $vi['name'] = 'up_col_' . $ki;
  1484. $vi['value'] = $v[$ki];
  1485. $m = 'select';
  1486. if (isset($vi['update']) && $vi['update'] == 'group') {
  1487. $m = 'group';
  1488. }
  1489. $show = Html::$m($vi, 'form-list', '', 'onchange="update($(this), \'' . $v['id'] . '\', \'' . $this->project . '\', \'' . $this->table . '\')"');
  1490. } else {
  1491. if (strstr($show, ',')) {
  1492. $temp = explode(',', $show);
  1493. $t = array();
  1494. foreach ($temp as $vv) {
  1495. if (isset($vi['option'][$vv]) && is_array($vi['option'][$vv])) {
  1496. $t[] = $vi['option'][$vv]['name'];
  1497. } else {
  1498. $t[] = $vv;
  1499. }
  1500. }
  1501. $show = implode('<br />', $t);
  1502. } elseif (isset($vi['option'][$show]) && is_array($vi['option'][$show])) {
  1503. $show = $vi['option'][$show]['name'];
  1504. } else {
  1505. $show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
  1506. }
  1507. }
  1508. }
  1509. $text = $show;
  1510. if ($high && isset($high[$ki])) {
  1511. if (isset($v['s_' . $ki])) {
  1512. $text = $v['s_' . $ki];
  1513. $text = str_replace('<em class="dever_highlight">', '<font color="red" class="dever_highlight">', $text);
  1514. $text = str_replace('</em>', '</font>', $text);
  1515. } else {
  1516. $text = preg_replace('/('.$high[$ki].')/i', '<font color="red" class="dever_highlight">${1}</font>', $text);
  1517. }
  1518. }
  1519. if (!isset($vi['option']) && isset($vi['edit']) && $vi['edit']) {
  1520. //$url = Dever::url($vi['edit'] . '?where_id=' . $v['id'] . '&col=' . $vi['col']. '&key=' . $this->project . '&table=' . $this->table);
  1521. $url = $this->url('updateAction');
  1522. $vi['edit'] = $vi['edit'] == 'textarea' ? $vi['edit'] : 'text';
  1523. $result[$k] .= '<td class="edit" data-col="' . $ki . '" data-url="' . $url . '" data-id="' . $v['id'] . '" data-project="' . $this->project . '" data-table="' . $this->table . '" data-content="' . $show . '" data-type="' . $vi['edit'] . '">' . $text . '</td>';
  1524. } else {
  1525. $result[$k] .= '<td>' . $text . '</td>';
  1526. }
  1527. }
  1528. }
  1529. if (isset($config['manage']['_list']['value'])) {
  1530. $result[$k] .= '<td>';
  1531. if ($config['state'] == 2) {
  1532. if (isset($config['manage']['_list']['value']['update'])) {
  1533. unset($config['manage']['_list']['value']['update']);
  1534. }
  1535. if (isset($config['manage']['_list']['value']['delete'])) {
  1536. $config['manage']['_list']['value']['delete'] = '恢复';
  1537. }
  1538. }
  1539. foreach ($config['manage']['_list']['value'] as $kj => $vj) {
  1540. $pj = false;
  1541. $h = '<span class="am-icon-pencil-square-o"></span>';
  1542. $oper = true;
  1543. if ($kj == 'update') {
  1544. $oper = Dever::load('manage/auth.oper', 3);
  1545. } elseif ($kj == 'delete') {
  1546. $oper = Dever::load('manage/auth.oper', 5);
  1547. $h = '<span class="am-icon-trash-o"></span>';
  1548. }
  1549. if (is_array($vj) && isset($vj[1])) {
  1550. if (isset($vj[2])) {
  1551. $oper = $this->value($vj[2], $data[$k]);
  1552. }
  1553. if ($oper && strpos($vj[1], 'modal|') === false) {
  1554. $pj = $this->value($vj[1], $data[$k]);
  1555. $vjn = $vj[0];
  1556. }
  1557. } else {
  1558. $vjn = $vj;
  1559. }
  1560. if ($oper == true) {
  1561. if (strpos($kj, 'oper') !== false) {
  1562. $result[$k] .= '<a href="javascript:;" onclick="load(\'' . Dever::url($pj) . '\')"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5">' . $vj[0] . '</button></a>&nbsp;&nbsp;';
  1563. }
  1564. elseif (isset($vj[1]) && strpos($vj[1], 'modal|') !== false) {
  1565. $t = explode('|', $vj[1]);
  1566. $result[$k] .= '<a href="#dever_modal" data-toggle="modal" class="btn btn-info btn-rounded waves-effect waves-light m-b-5" onclick=' . $t[1] . ' msg-send="' . $this->value($t[2], $data[$k]) . '">' . $vj[0] . '</a>&nbsp;&nbsp;';
  1567. } elseif (isset($vj[1])) {
  1568. $result[$k] .= '<a href="' . $this->url($kj, $v['id'], $pj) . '" class="oper_' . $kj . '"><button type="button" class="btn btn-info btn-rounded waves-effect waves-light m-b-5">' . $h . $vjn . '</button></a>&nbsp;&nbsp;';
  1569. } else {
  1570. $result[$k] .= '<span style="margin-left:5px;margin-right:10px;">' . $vj[0] . '</span>';
  1571. }
  1572. }
  1573. }
  1574. $result[$k] .= '</td>';
  1575. }
  1576. $result[$k] .= '</tr>';
  1577. }
  1578. }
  1579. /*
  1580. if ($result) {
  1581. $thead = $this->list_thead();
  1582. if ($thead) {
  1583. $k = $k + 1;
  1584. $result[$k] = '<tr>';
  1585. foreach ($thead as $k => $v) {
  1586. $result[$k] .= $v;
  1587. }
  1588. $result[$k] .= '</tr>';
  1589. }
  1590. }
  1591. */
  1592. return $result;
  1593. }
  1594. /**
  1595. * 多级分类
  1596. *
  1597. * @return array
  1598. */
  1599. private function tree($data, $config, $parent = 0)
  1600. {
  1601. $key = $config['name'] . '_id';
  1602. $result = array();
  1603. $this->treeData($data, $result, $key, $parent);
  1604. return $result;
  1605. }
  1606. private function treeData($array, &$result, $key, $parent_id = 0, $level = 0)
  1607. {
  1608. if (empty($array)) {
  1609. return;
  1610. }
  1611. $level++;
  1612. foreach ($array as $k => $v) {
  1613. if (isset($v[$key]) && $v[$key] == $parent_id) {
  1614. $v['level'] = $level;
  1615. $v['name'] = $this->treeName($v['name'], $level);
  1616. $result[] = $v;
  1617. unset($array[$k]);
  1618. $this->treeData($array, $result, $key, $v['id'], $level);
  1619. }
  1620. }
  1621. }
  1622. private function treeName($name, $level = 1)
  1623. {
  1624. $string = '';
  1625. if ($level > 1) {
  1626. /*
  1627. $num = 4;
  1628. $string = '-';
  1629. //STR_PAD_LEFT
  1630. $string = str_pad('|', $level * $num, $string);
  1631. */
  1632. $string = str_repeat(' ',$level-1);
  1633. $string .= '|—';
  1634. }
  1635. return $string . $name;
  1636. }
  1637. /**
  1638. * 列表页批量管理
  1639. *
  1640. * @return array
  1641. */
  1642. public function list_mul()
  1643. {
  1644. # 新增权限:更新权限
  1645. if (Dever::load('manage/auth.oper', 3) == false) {
  1646. Dever::alert('你没有权限');
  1647. }
  1648. $config = $this->config();
  1649. $result = '';
  1650. if (isset($config['struct']) && isset($config['manage']['mul'])) {
  1651. $mul = Dever::preInput('mul_');
  1652. $node = Html::node(array('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
  1653. $btn = '<button class="btn btn-danger" onclick="$(\'#form1\').attr(\'action\', \'' . $this->url('list') . '\');list_mul($(this))" type="button" style="height: 38px;margin-left:10px;">更新</button>';
  1654. $prefix = 'mul_';
  1655. $result .= $node[0];
  1656. $default = 0;
  1657. $option = array
  1658. (
  1659. 1 => '选中的数据',
  1660. );
  1661. # 只有当有搜索条件时才会出现
  1662. if (Dever::load('manage/auth.oper', 2) && $this->search == true) {
  1663. $option[2] = '所有数据';
  1664. $default = 2;
  1665. }
  1666. $this->list_search_select($result, $mul, $prefix . 'type' . '" id="' . $prefix . 'type', '请选择更新方式', $option, $default);
  1667. foreach ($config['struct'] as $k => $v) {
  1668. if ($k == 'state') {
  1669. $v['option'] = Dever::config('base')->state;
  1670. if ($this->getState() == 2) {
  1671. unset($v['option'][2]);
  1672. } else {
  1673. unset($v['option'][1]);
  1674. }
  1675. $v['edit'] = true;
  1676. }
  1677. if (isset($v['edit']) && isset($v['option']) && (is_array($v['option']) || is_object($v['option']))) {
  1678. $this->list_search_select($result, $mul, $prefix . 'set_' . $k, $v['name'], $this->option($v['option']), $v['default']);
  1679. }
  1680. }
  1681. $result .= $btn . $node[1];
  1682. }
  1683. //print_r($result);die;
  1684. return $result;
  1685. }
  1686. private function value($value, $data, $struct = array())
  1687. {
  1688. if (!$data) {
  1689. return $value;
  1690. }
  1691. if (is_string($data)) {
  1692. $eval = '$value = ' . $value . ';';
  1693. eval($eval);
  1694. return $value;
  1695. }
  1696. if ($value == 'table' && $struct) {
  1697. $result = $this->struct_data($struct, $data);
  1698. $value = Dever::table($result);
  1699. } elseif (strpos($value, '{') !== false) {
  1700. # 这里用到了/e,注意这里无论如何不能传入$_GET之类的用户参数 2015/7/21更新为callback方式
  1701. //$eval = '$value = ' . preg_replace('/{(.*?)}/e', "\$data['$1']", $value) . ';';
  1702. $func = function ($r) use ($data) {
  1703. return $data[$r[1]];
  1704. };
  1705. $eval = '$value = ' . preg_replace_callback('/{(.*?)}/', $func, $value) . ';';
  1706. eval($eval);
  1707. }
  1708. return $value;
  1709. }
  1710. /**
  1711. * update
  1712. *
  1713. * @return array
  1714. */
  1715. public function update()
  1716. {
  1717. $config = $this->config();
  1718. $result = array();
  1719. $prefix = 'update';
  1720. $data = array();
  1721. if ($this->id) {
  1722. # 新增权限
  1723. if (Dever::load('manage/auth.oper', 3) == false) {
  1724. Dever::alert('你没有权限');
  1725. }
  1726. $data = $this->load('one', $this->id);
  1727. if ($data) {
  1728. //$prefix = 'set';
  1729. }
  1730. } else {
  1731. # 新增权限
  1732. if (Dever::load('manage/auth.oper', 4) == false) {
  1733. Dever::alert('你没有权限');
  1734. }
  1735. }
  1736. //print_r($config['manage']['update']);die;
  1737. $data = $this->getAdminInfo($data);
  1738. foreach ($config['struct'] as $k => $v) {
  1739. if (isset($v['update'])) {
  1740. //处理权限
  1741. if (isset($v['auth'])) {
  1742. $auth = $this->value($v['auth'], $data);
  1743. if (!$auth) {
  1744. continue;
  1745. }
  1746. }
  1747. # 处理show模式下的class
  1748. $class = $this->update_class($k, $v);
  1749. $result[$k] = '<div class="form-group ' . $class . '"><div class="margin-top">';
  1750. $v['ext'] = $v['col'] = $k;
  1751. $v['update'] = $v['update'] ? $v['update'] : 'text';
  1752. if (isset($v['match']) && $v['match'] != 'option') {
  1753. if (is_string($v['match']) && strpos($v['match'], '/') !== false) {
  1754. $v['valid'] = 'validate[required,custom[' . $k . ']]';
  1755. } else {
  1756. $v['valid'] = 'validate[required]';
  1757. }
  1758. }
  1759. if (is_string($v['update']) && strpos($v['update'], 'copy.') !== false) {
  1760. $v['value'] = $v['update'];
  1761. $v['update'] = 'hidden';
  1762. }
  1763. $this->update_html($v, $k, $k, $data, $result);
  1764. $result[$k] .= '</div></div>';
  1765. } elseif (isset($v['class'])) {
  1766. # 分割线
  1767. $v['name'] = $this->value($v['name'], array('test' => 1));
  1768. $result[$k] = Html::hr($v);
  1769. }
  1770. }
  1771. if (Dever::input('ajax') == 1) {
  1772. return implode('', $result);
  1773. }
  1774. //print_r($result);die;
  1775. return $result;
  1776. }
  1777. /**
  1778. * update_class
  1779. *
  1780. * @return string
  1781. */
  1782. public function update_class($name, $param, $prefix = '')
  1783. {
  1784. $class = '';
  1785. $key = 'show';
  1786. if (isset($param[$key]) && $param[$key]) {
  1787. $parent = array();
  1788. $child = array();
  1789. if (is_array($param[$key])) {
  1790. foreach ($param[$key] as $k => $v) {
  1791. $this->controlShow($name, $parent, $child, $key, $v, $prefix);
  1792. }
  1793. } else {
  1794. $this->controlShow($name, $parent, $child, $key, $param[$key], $prefix);
  1795. }
  1796. $class = implode(' ', $parent) . ' ' . implode(' ', $child);
  1797. }
  1798. if (isset($param['control'])) {
  1799. $class .= ' ' . $key . '_input';
  1800. }
  1801. return $class;
  1802. }
  1803. /**
  1804. * controlShow
  1805. *
  1806. * @return mixed
  1807. */
  1808. private function controlShow($name, &$parent, &$child, $key, $show, $prefix = '')
  1809. {
  1810. if (strpos($show, '=') !== false) {
  1811. $temp = explode('=', $show);
  1812. $show = str_replace('=', '_', $show);
  1813. if ($prefix) {
  1814. $temp[0] = str_replace($name, $temp[0], $prefix);
  1815. $show = str_replace($name, $show, $prefix);
  1816. }
  1817. $parent[$temp[0]] = $key . '_' . $temp[0];
  1818. $child[$show] = $key . '_' . $show;
  1819. } elseif ($show == 'no') {
  1820. $child[$show] = $key . '_no';
  1821. }
  1822. }
  1823. /**
  1824. * update_html
  1825. *
  1826. * @return string
  1827. */
  1828. public function update_html($param, $key, $index, $data, &$result)
  1829. {
  1830. if (isset($param['name']) && $param['update'] != 'hidden' && isset($param['update'])) {
  1831. $result[$key] .= Html::pageLeft($param);
  1832. }
  1833. if ($param['update']) {
  1834. # 名称带前缀
  1835. $param['name'] = 'update_' . $param['ext'];
  1836. $param['index'] = $index;
  1837. //$param['value'] = '';
  1838. # 得到当前的值
  1839. if (isset($data[$index]) && !isset($param['value'])) {
  1840. $param['value'] = $data[$index];
  1841. }
  1842. # 得到下级的值
  1843. if (isset($param['child_value']) && isset($data['id'])) {
  1844. $param['child_value'] = $this->value($param['child_value'], $data);
  1845. }
  1846. # 验证option是否是匿名函数
  1847. if (isset($param['option'])) {
  1848. $param['option'] = $this->option($param['option']);
  1849. }
  1850. if (isset($data[$index]) && isset($param['key']) && $param['update'] != 'editor') {
  1851. $param['value'] = Dever::upload($data[$index]);
  1852. }
  1853. # 处理下级字段
  1854. if (isset($param['child'])) {
  1855. $param['child'] = $this->option($param['child']);
  1856. if (isset($data[$key . '_parent'])) {
  1857. $param['parent_value'] = $data[$key . '_parent'];
  1858. }
  1859. }
  1860. # 设置默认值
  1861. if (isset($param['default'])) {
  1862. $param['default'] = $this->option($param['default']);
  1863. }
  1864. # 处理同步
  1865. if (isset($param['sync']) && isset($data[$param['sync'][0]]) && strpos($key, '-')) {
  1866. $temp = explode('-', $key);
  1867. $load = Dever::load($temp[0] . '/' . $temp[1] . '-one', array('option_' . $param['sync'][1] => $data[$param['sync'][0]]));
  1868. $param['value'] = isset($load[$temp[2]]) ? $load[$temp[2]] : $param['default'];
  1869. }
  1870. $method = $param['update'];
  1871. if (is_array($method)) {
  1872. $result[$key] .= '<ul class="nav nav-tabs">{tab}<li><a href="javascript:;" class="dever_form_add" toggle_key="' . $key . '">增加一条</a></li></ul>';
  1873. $tab = '';
  1874. $order = array
  1875. (
  1876. 'col' => 'order',
  1877. 'name' => '排序-按照数字正序排列',
  1878. 'default' => '1',
  1879. 'desc' => '排序',
  1880. 'match' => 'is_string',
  1881. 'update' => 'text',
  1882. );
  1883. array_unshift($method, $order);
  1884. $this->update_child(array(), $method, $key, $result, -1);
  1885. if (isset($param['value']) && $param['value']) {
  1886. $param['value'] = json_decode(base64_decode($param['value']), true);
  1887. foreach ($param['value'] as $k => $v) {
  1888. $this->update_child($v, $method, $key, $result, $k);
  1889. $active = '';
  1890. if ($k == 0) {
  1891. $active = 'active';
  1892. }
  1893. $k = $k + 1;
  1894. $tab .= '<li class="' . $active . '"><a href="javascript:;" id="tab-' . $key . '-child-' . $k . '" toggle_key="' . $key . '" toggle_id="' . $k . '" toggle_child=".dever_' . $key . '_child" toggle="#' . $key . '-child-' . $k . '" onclick="showToggle($(this))">第' . $k . '条</a></li>';
  1895. }
  1896. } else {
  1897. $this->update_child(array(), $method, $key, $result, 0);
  1898. $tab = '<li class="active"><a href="javascript:;" id="tab-' . $key . '-child-1" toggle_key="' . $key . '" toggle_id="1" toggle_child=".dever_' . $param['col'] . '_child" toggle="#' . $key . '-child-1" onclick="showToggle($(this))">第1条</a></li>';
  1899. }
  1900. $result[$key] = str_replace('{tab}', $tab, $result[$key]);
  1901. } else {
  1902. if (isset($param['update_input']) && isset($data[$index . '_input'])) {
  1903. $param['update_input_data'] = explode(',', $data[$index . '_input']);
  1904. }
  1905. $result[$key] .= Html::$method($param);
  1906. }
  1907. if (isset($param['desc']) && ($param['update'] == 'text' || $param['update'] == 'password')) {
  1908. $result[$key] .= Html::desc($param['desc']);
  1909. }
  1910. }
  1911. }
  1912. /**
  1913. * update_action
  1914. *
  1915. * @return array
  1916. */
  1917. private function update_child($data, $param, $key, &$result, $index)
  1918. {
  1919. $style = '';
  1920. if ($index != 0) {
  1921. $style = 'style="display:none;"';
  1922. }
  1923. $result[$key] .= '<div id="' . $key . '-child-' . ($index + 1) . '" class="dever_' . $key . '_child dever_form_add_child" ' . $style . '><div class="dever_form_delete">[删除]</div>';
  1924. foreach ($param as $k => $v) {
  1925. $v['type'] = $v['update'];
  1926. $prefix = '';
  1927. if ($index < 0) {
  1928. $v['ext'] = $key . '_c_' . $v['col'];
  1929. } else {
  1930. $v['ext'] = $key . '_c_' . $index . '_i_' . $v['col'];
  1931. $prefix = $v['ext'];
  1932. }
  1933. if ($v['col'] == 'order') {
  1934. $v['default'] = $index+1;
  1935. }
  1936. $class = $this->update_class($v['col'], $v, $prefix);
  1937. $result[$key] .= '<div class="form-group ' . $class . '">';
  1938. $this->update_html($v, $key, $v['col'], $data, $result);
  1939. $result[$key] .= '</div>';
  1940. }
  1941. $result[$key] .= '</div>';
  1942. }
  1943. /**
  1944. * update_action
  1945. *
  1946. * @return array
  1947. */
  1948. public function update_action_api()
  1949. {
  1950. $method = false;
  1951. $config = $this->config();
  1952. $input = Dever::preInput('update_');
  1953. if ($this->id) {
  1954. # 新增权限
  1955. if (Dever::load('manage/auth.oper', 3) == false) {
  1956. Dever::alert('你没有权限');
  1957. }
  1958. Dever::setInput('where_id', $this->id);
  1959. //Dever::setInput('update_where_id', $this->id);
  1960. $info = $this->load('one', $this->id);
  1961. if ($info) {
  1962. $method = 'update';
  1963. $col = Dever::input('col');
  1964. $value = Dever::input('value');
  1965. if ($col && $value) {
  1966. $method = 'update_' . $col;
  1967. //Dever::setInput('set_' . $col, $value);
  1968. //Dever::setInput('update_' . $col, $value);
  1969. $param['where_id'] = $this->id;
  1970. $param['set_' . $col] = $value;
  1971. $this->load($method, $param);
  1972. $struct = $this->struct_data($config['struct'], array($col => $value));
  1973. Log::add($this->project, $this->table, array('method' => $method, 'input' => $struct, 'struct' => $config['struct']), $this->id);
  1974. Dever::out('yes');
  1975. return;
  1976. }
  1977. $prefix = 'set';
  1978. } else {
  1979. $method = 'insert';
  1980. $prefix = 'add';
  1981. }
  1982. }
  1983. if (!$method) {
  1984. # 新增权限
  1985. if (Dever::load('manage/auth.oper', 4) == false) {
  1986. Dever::alert('你没有权限');
  1987. }
  1988. $method = 'insert';
  1989. $prefix = 'add';
  1990. }
  1991. if ($method && $input) {
  1992. $update = $order = array();
  1993. $i = 0;
  1994. $c = array();
  1995. foreach ($input as $k => $v) {
  1996. if ($v === '') {
  1997. $v = 'null';
  1998. }
  1999. if ($v && is_string($v) && strpos($v, 'copy.') !== false) {
  2000. $temp = str_replace('copy.', '', $v);
  2001. if (isset($input['update_' . $temp])) {
  2002. $v = $input['update_' . $temp];
  2003. }
  2004. }
  2005. if (strpos($k, '_c_') !== false) {
  2006. if ($v == 'null') {
  2007. $v = '';
  2008. }
  2009. $temp = explode('_c_', $k);
  2010. $k = str_replace('update_', $prefix . '_', $temp[0]);
  2011. if (!isset($c[$k])) {
  2012. $c[$k] = array();
  2013. }
  2014. if (strpos($temp[1], '_i_') !== false) {
  2015. $temp = explode('_i_', $temp[1]);
  2016. if (!isset($c[$k][$temp[1]])) {
  2017. $c[$k][$temp[1]] = 0;
  2018. } else {
  2019. $c[$k][$temp[1]]++;
  2020. }
  2021. $num = $c[$k][$temp[1]];
  2022. $update[$i][$k][$num][$temp[1]] = $v;
  2023. if ($temp[1] == 'order') {
  2024. $order[$i][$k][$num] = $v;
  2025. }
  2026. } else {
  2027. //$update[$i][$k][$c][$temp[1]] = $v;
  2028. }
  2029. } elseif (strpos($k, '__') !== false) {
  2030. $temp1 = explode('__', $k);
  2031. $temp2 = explode('_', $temp1[0]);
  2032. $i = $temp2[1] - 1;
  2033. if (strpos($k, 'where_id') !== false) {
  2034. //$update[$i]['where_id'] = $v;
  2035. } else {
  2036. $update[$i]['add_' . $temp1[1]] = $v;
  2037. }
  2038. } else {
  2039. $ki = str_replace('update_', $prefix . '_', $k);
  2040. if (strpos($ki, 'where_id') !== false) {
  2041. if ($method != 'insert') {
  2042. $update[$i]['where_id'] = $v;
  2043. }
  2044. } else {
  2045. if (strpos($k, '-')) {
  2046. $k = str_replace('update_', '', $k);
  2047. if (isset($config['struct'][$k]) && isset($config['struct'][$k]['sync'])) {
  2048. $s_col = $config['struct'][$k]['sync'][0];
  2049. $m_col = $config['struct'][$k]['sync'][1];
  2050. if ($s_col == 'id') {
  2051. $s_value = $this->id;
  2052. } else {
  2053. $s_one = $this->load('one', $this->id);
  2054. $s_value = $s_one[$s_col];
  2055. }
  2056. $temp = explode('-', $k);
  2057. $index = $temp[0] . '/' . $temp[1];
  2058. $update_other_set[$index]['set_' . $temp[2]] = $v;
  2059. //$update_other_set[$index]['set_' . $m_col] = $s_value;
  2060. $update_other_add[$index]['add_' . $temp[2]] = $v;
  2061. $update_other_add[$index]['add_' . $m_col] = $s_value;
  2062. $update_other_id[$index]['option_' . $m_col] = $s_value;
  2063. }
  2064. } else {
  2065. if (is_array($v)) {
  2066. $k = str_replace('update_', '', $k);
  2067. $kd = $k . '_id';
  2068. if (isset($config['struct'][$kd])) {
  2069. foreach ($v as $vk => $vt) {
  2070. if ($vt == -1) {
  2071. unset($v[$vk]);
  2072. }
  2073. }
  2074. if ($v) {
  2075. $vd = end($v);
  2076. $kdi = $prefix . '_' . $kd;
  2077. $update[$i][$kdi] = $vd;
  2078. }
  2079. }
  2080. if (isset($config['struct'][$k]) && strpos($config['struct'][$k]['type'], 'int') !== false) {
  2081. $v = end($v);
  2082. }
  2083. }
  2084. $update[$i][$ki] = $v;
  2085. }
  2086. }
  2087. }
  2088. }
  2089. //print_r($update_other_set);die;
  2090. //print_r($input);
  2091. //print_r($update_other);die;
  2092. //print_r($config['struct']);die;
  2093. # 增加复制的数量
  2094. $copy_num = Dever::input('copy_num');
  2095. if ($copy_num > 0) {
  2096. $update[0]['where_id'] = 0;
  2097. for ($i = 1; $i < $copy_num; $i++) {
  2098. $update[$i] = $update[0];
  2099. # 缓存处理
  2100. $update[$i]['rank'] = $i;
  2101. }
  2102. }
  2103. if ($this->id && !$info) {
  2104. $update[0]['add_id'] = $this->id;
  2105. }
  2106. //print_r($update);die;
  2107. foreach ($update as $k => $v) {
  2108. $method = 'insert';
  2109. $old = array();
  2110. if (isset($v['where_id']) && $v['where_id']) {
  2111. $method = 'update';
  2112. $old = $this->load('one', $v['where_id']);
  2113. }
  2114. if (isset($order[$k])) {
  2115. foreach ($order[$k] as $i => $j) {
  2116. array_multisort($j, SORT_ASC, $v[$i]);
  2117. }
  2118. }
  2119. $id = $this->load($method, $v);
  2120. if ($id) {
  2121. $struct = $this->struct_data($config['struct'], $v, $old);
  2122. Log::add($this->project, $this->table, array('method' => $method, 'input' => $struct), $this->id ? $this->id : $id);
  2123. }
  2124. if (isset($update_other_id) && $update_other_id) {
  2125. foreach ($update_other_set as $k => $v) {
  2126. $check = Dever::load($k . '-one', $update_other_id[$k]);
  2127. //print_r($check);die;
  2128. if ($check) {
  2129. $v['where_id'] = $check['id'];
  2130. Dever::load($k . '-update', $v);
  2131. } else {
  2132. Dever::load($k . '-insert', $update_other_add[$k]);
  2133. }
  2134. }
  2135. }
  2136. }
  2137. }
  2138. Dever::out('yes');
  2139. }
  2140. private function struct_data($struct, $data, $old = array())
  2141. {
  2142. $log = $tlog = array();
  2143. foreach ($struct as $i => $j) {
  2144. if (isset($j['list_name'])) {
  2145. $j['name'] = $j['list_name'];
  2146. } elseif (strpos($j['name'], '-')) {
  2147. $temp = explode('-', $j['name']);
  2148. $j['name'] = $temp[0];
  2149. }
  2150. if (isset($j['list_table'])) {
  2151. $j['list'] = $j['list_table'];
  2152. if ($j['list'] == false) {
  2153. $data[$i] = false;
  2154. }
  2155. }
  2156. $list = (isset($j['list']) && is_string($j['list']) && $j['list'] != 'table');
  2157. if (isset($data['where_' . $i]) && $data['where_' . $i]) {
  2158. $log[$j['name']] = $data['where_' . $i];
  2159. } elseif (isset($data['set_' . $i]) && $data['set_' . $i]) {
  2160. $log[$j['name']] = $data['set_' . $i];
  2161. } elseif (isset($data['add_' . $i]) && $data['add_' . $i]) {
  2162. $log[$j['name']] = $data['add_' . $i];
  2163. } elseif (isset($data[$i]) && $data[$i]) {
  2164. $log[$j['name']] = $list ? $this->value($j['list'], $data) : $data[$i];
  2165. }
  2166. if (isset($log[$j['name']])) {
  2167. $tlog[$j['name']] = $log[$j['name']];
  2168. if (is_array($tlog[$j['name']])) {
  2169. if (isset($tlog[$j['name']][0]) && is_array($tlog[$j['name']][0])) {
  2170. $tlog[$j['name']] = json_encode($tlog[$j['name']]);
  2171. } else {
  2172. $tlog[$j['name']] = implode(',', $tlog[$j['name']]);
  2173. }
  2174. } elseif (strpos($log[$j['name']], 'http://wx.qlogo.cn/') !== false || strpos($log[$j['name']], '.jpg') !== false || strpos($log[$j['name']], '.gif') !== false || strpos($log[$j['name']], '.png') !== false) {
  2175. $log[$j['name']] = '<a href="' . $log[$j['name']] . '" target="_blank"><img src="' . $log[$j['name']] . '" width="150"></a>';
  2176. } elseif (is_numeric($log[$j['name']]) && (strpos($i, 'time') !== false || strpos($i, 'date') !== false)) {
  2177. $log[$j['name']] = date("Y-m-d H:i:s", $log[$j['name']]);
  2178. } elseif (isset($j['decode'])) {
  2179. $log[$j['name']] = json_decode(base64_decode($log[$j['name']]), true);
  2180. }
  2181. if (isset($j['option']) && !$list) {
  2182. $name = array();
  2183. $option = $this->option($j['option']);
  2184. if (is_array($option)) {
  2185. foreach ($option as $a => $b) {
  2186. if (!is_array($log[$j['name']])) {
  2187. if ($a == $log[$j['name']]) {
  2188. $name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
  2189. }
  2190. } elseif (in_array($a, $log[$j['name']])) {
  2191. $name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
  2192. }
  2193. }
  2194. }
  2195. $log[$j['name']] = implode(',', $name);
  2196. }
  2197. if (isset($old[$i]) && $old[$i] != $tlog[$j['name']]) {
  2198. $log[$j['name']] = array('old' => $old[$i], 'new' => $log[$j['name']]);
  2199. }
  2200. }
  2201. }
  2202. return $log;
  2203. }
  2204. /**
  2205. * delete_action
  2206. *
  2207. * @return array
  2208. */
  2209. public function delete_action_api()
  2210. {
  2211. if (Dever::load('manage/auth.oper', 5) == false) {
  2212. Dever::alert('您没有权限');
  2213. }
  2214. $config = $this->config();
  2215. if (isset($config['manage']['delete']) && $config['manage']['delete'] === 2) {
  2216. $this->load('delete', array('where_id' => $this->id));
  2217. } elseif (isset($config['struct']['state']) && (!isset($config['manage']['delete']) || (isset($config['manage']['delete']) && $config['manage']['delete']))) {
  2218. $this->load('update', array('where_id' => $this->id, 'state' => 2));
  2219. }
  2220. return 'reloaderror';
  2221. }
  2222. /**
  2223. * recovery_action
  2224. *
  2225. * @return array
  2226. */
  2227. public function recovery_action_api()
  2228. {
  2229. if (Dever::load('manage/auth.oper', 5) == false) {
  2230. Dever::alert('您没有权限');
  2231. }
  2232. $this->load('update', array('where_id' => $this->id, 'state' => 1));
  2233. return 'reload';
  2234. }
  2235. /**
  2236. * stat 为主动统计提供相关数据
  2237. *
  2238. * @return array
  2239. */
  2240. public function stat()
  2241. {
  2242. //bundle 包依赖工具
  2243. $config = $this->config();
  2244. if (empty($config['manage']['stat'])) {
  2245. Dever::alert('该项目主动统计功能未开放');
  2246. }
  2247. $data = $config['manage'];
  2248. $data['table'] = '';
  2249. $method = $this->project . '/' . $this->table . '-all';
  2250. $gp = Dever::input('gp', 'month,day');
  2251. $col = is_string($gp) ? explode(',', $gp) : $gp;
  2252. $data['search'] = $this->stat_search($config['struct'], $gp, $config['manage']['stat'], $config['manage']['search']);
  2253. foreach ($col as $k => $v) {
  2254. $this->stat_data($data, $config['struct'], $v, $method);
  2255. }
  2256. return $data;
  2257. }
  2258. /**
  2259. * stat 为主动统计提供导出数据 后续优化
  2260. *
  2261. * @return array
  2262. */
  2263. public function stat_excel()
  2264. {
  2265. Dever::$global['base']['excel'] = 1;
  2266. //print_r($data['thead']);die;
  2267. $data = $this->stat();
  2268. if ($data && isset($data['list'])) {
  2269. $xls = new \PHPExcel();
  2270. $xls->getProperties()->setCreator("Maze Stat Excel")
  2271. ->setLastModifiedBy("Maze Stat Excel")
  2272. ->setTitle("Office 2007 XLSX Maze Document")
  2273. ->setSubject("Office 2007 XLSX Maze Document")
  2274. ->setDescription("Mazephp Stat Excel")
  2275. ->setKeywords("Mazephp Stat Excel")
  2276. ->setCategory("Mazephp Stat Excel");
  2277. $info = $this->info();
  2278. $name = $info['list_header'] . '的' . $info['stat_desc'] . '-' . $info['project_name'];
  2279. $start = 65;
  2280. $s = 0;
  2281. foreach ($data['thead'] as $k => $v) {
  2282. if ($s > 0) {
  2283. $xls->createSheet();
  2284. }
  2285. $act = $xls->setActiveSheetIndex($s);
  2286. $act->setTitle($v[0]);
  2287. $s++;
  2288. $i = 0;
  2289. foreach ($v as $kj => $vj) {
  2290. $num = 1;
  2291. $cell = $this->_getChr($start, $i);
  2292. $act->setCellValue($cell . '' . $num, $vj);
  2293. $act->getColumnDimension($cell)->setWidth(20);
  2294. $i++;
  2295. if (isset($data['list'][$k])) {
  2296. foreach ($data['list'][$k] as $ki => $vi) {
  2297. if (isset($vi[$kj])) {
  2298. $num += 1;
  2299. $act->setCellValue($cell . $num, $vi[$kj]);
  2300. }
  2301. }
  2302. }
  2303. }
  2304. }
  2305. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 垂直居中
  2306. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 水平居中
  2307. $xls->setActiveSheetIndex(0);
  2308. $filename = $name . '.xlsx';
  2309. header('Pragma: public');
  2310. header('Expires: 0');
  2311. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  2312. header('Content-Type:application/force-download');
  2313. header('Content-Type:application/vnd.ms-execl');
  2314. header('Content-Type:application/octet-stream');
  2315. header('Content-Type:application/download');
  2316. header("Content-Disposition:attachment;filename='" . $filename . "'");
  2317. header('Content-Transfer-Encoding:binary');
  2318. $write = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
  2319. $write->save('php://output');
  2320. }
  2321. }
  2322. /**
  2323. * stat_search
  2324. *
  2325. * @return mixed
  2326. */
  2327. private function stat_search($struct, $col, $stat, $search)
  2328. {
  2329. unset($struct['id']);
  2330. unset($struct['cdate']);
  2331. if ($stat) {
  2332. $stat = explode(',', $stat);
  2333. foreach ($stat as $k => $v) {
  2334. if (isset($struct[$v])) {
  2335. unset($struct[$v]);
  2336. }
  2337. }
  2338. }
  2339. $struct['day']['name'] = '天';
  2340. $struct['month']['name'] = '月';
  2341. $struct['year']['name'] = '年';
  2342. $param['update'] = 'checkbox';
  2343. $param['name'] = 'gp';
  2344. $param['value'] = $col;
  2345. $param['option'] = $struct;
  2346. $html = '<form action="' . $this->url('stat') . '" method="post" id="f1">';
  2347. $html .= '<div class="form-group">';
  2348. $html .= Html::pageLeft(array('name' => '统计项', 'col' => 'col'));
  2349. $html .= Html::radio($param);
  2350. $html .= '</div>';
  2351. $input = Dever::preInput('search_');
  2352. if (isset($search['date'])) {
  2353. foreach ($search['date'] as $k => $v) {
  2354. $html .= '<div class="form-group">';
  2355. $this->list_search_time($html, $input, 'search_', $k, $v, 'date');
  2356. $html .= '</div>';
  2357. }
  2358. $input = Dever::preInput('search_');
  2359. if ($input) {
  2360. foreach ($input as $k => $v) {
  2361. if ($v && strpos($k, 'option_') !== false) {
  2362. Dever::setInput(str_replace('search_', '', $k), $v);
  2363. }
  2364. }
  2365. }
  2366. }
  2367. if (isset($search['fulltext'])) {
  2368. $html .= '<div class="form-group">';
  2369. $this->list_search_select($html, $input, 'search_fulltext', '选择检索项', $search['fulltext']);
  2370. $this->list_search_select($html, $input, 'search_fulltext_type', '', array
  2371. (
  2372. 1 => '精确匹配',
  2373. 2 => '模糊匹配',
  2374. 3 => '大于',
  2375. 4 => '大于等于',
  2376. 5 => '小于',
  2377. 6 => '小于等于',
  2378. ), 1);
  2379. $value = array();
  2380. $value['name'] = 'search_fulltext_value';
  2381. $value['class'] = 'form-control';
  2382. if (isset($input[$value['name']])) {
  2383. $value['value'] = $input[$value['name']];
  2384. $fix = '';
  2385. if ($input['search_fulltext_type'] == 2) {
  2386. $fix = '%';
  2387. }
  2388. Dever::setInput('option_' . $input['search_fulltext'], $fix . $value['value'] . $fix);
  2389. //print_r(Dever::input('option_name'));die;
  2390. }
  2391. $html .= Html::text($value, false);
  2392. $html .= '</div>';
  2393. }
  2394. $html .= '<input type="hidden" name="excel" id="excel" value="2"><a href="javascript:;" class="btn btn-primary" onclick="$(\'#f1\').attr(\'action\', \'' . $this->url('stat') . '\').submit();">确定</a>';
  2395. $html .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" onclick="$(\'#f1\').attr(\'action\', \'' . $this->url('stat_excel') . '\').submit();">下载报表</a>';
  2396. $html .= '</form>';
  2397. return $html;
  2398. }
  2399. /**
  2400. * stat_data
  2401. *
  2402. * @return mixed
  2403. */
  2404. private function stat_data(&$data, $struct, $col, $method)
  2405. {
  2406. Dever::setInput('group', $col);
  2407. switch ($col) {
  2408. case 'year':
  2409. $name = '按年统计';
  2410. break;
  2411. case 'month':
  2412. $name = '按月统计';
  2413. break;
  2414. case 'day':
  2415. $name = '按天统计';
  2416. break;
  2417. default:
  2418. $name = $struct[$col]['name'];
  2419. break;
  2420. }
  2421. $list = Dever::load($method, array('group' => $col, 'col' => 'count(1) as total,id'));
  2422. $data['table'] .= '<div style="overflow-y:auto;width:100%;max-height:600px;"><table class="table table-bordered table-striped">';
  2423. $data['table'] .= '<thead><tr><th>' . $name . '</th><th>统计数字</th></tr></thead>';
  2424. $data['thead'][$col] = array($name, '统计数字');
  2425. if ($list) {
  2426. $data['table'] .= '<tbody>';
  2427. $total = 0;
  2428. foreach ($list as $k => $v) {
  2429. if (isset($v[$col])) {
  2430. if (isset($struct[$col]['option'])) {
  2431. $option = $this->option($struct[$col]['option']);
  2432. if (isset($option[$v[$col]])) {
  2433. $v[$col] = $option[$v[$col]];
  2434. if (is_array($v[$col])) {
  2435. $v[$col] = $v[$col]['name'];
  2436. }
  2437. }
  2438. }
  2439. $data['table'] .= '<tr><td width="60%" style="word-break:break-all;word-wrap:break-word;">' . $v[$col] . '</td><td>' . $v['total'] . '</td></tr>';
  2440. $total += $v['total'];
  2441. $data['list'][$col][] = array($v[$col], $v['total']);
  2442. }
  2443. }
  2444. $data['table'] .= '</tbody>';
  2445. if ($total > 0 && $k > 0) {
  2446. $data['table'] .= '<tr><td style="font-weight:bold">汇总</td><td style="font-weight:bold">' . $total . '</td></tr>';
  2447. $data['list'][$col][] = array('汇总', $total);
  2448. }
  2449. }
  2450. $data['table'] .= '</table></div>';
  2451. }
  2452. /**
  2453. * load
  2454. *
  2455. * @return mixed
  2456. */
  2457. private function load($method = 'one', $param = array())
  2458. {
  2459. return Dever::load($this->project . '/' . $this->table . '-' . $method, $param);
  2460. }
  2461. /**
  2462. * __call
  2463. *
  2464. * @return object
  2465. */
  2466. public function __call($method, $param)
  2467. {
  2468. if (strpos($method, 'html_') !== false) {
  2469. return Html::text($param);
  2470. }
  2471. return $this;
  2472. }
  2473. /**
  2474. * option
  2475. *
  2476. * @return string
  2477. */
  2478. private function option($option)
  2479. {
  2480. if (is_object($option)) {
  2481. $function = $option;
  2482. $option = $function();
  2483. }
  2484. return $option;
  2485. }
  2486. /**
  2487. * option
  2488. *
  2489. * @return string
  2490. */
  2491. public function manage()
  2492. {
  2493. $config = $this->config();
  2494. return $config;
  2495. }
  2496. /**
  2497. * valid
  2498. *
  2499. * @return string
  2500. */
  2501. public function valid()
  2502. {
  2503. //http://placehold.it/350x150
  2504. $config = $this->config();
  2505. if (isset($config['struct'])) {
  2506. $rule = array();
  2507. $rule['required']['regex'] = 'none';
  2508. $rule['required']['alertText'] = '此项不能为空';
  2509. $rule['required']['alertTextCheckboxMultiple'] = '请选择一个选项';
  2510. $rule['required']['alertTextCheckboxe'] = '请选择一个选项';
  2511. $rule['required']['alertTextDateRange'] = '日期范围不可空白';
  2512. foreach ($config['struct'] as $k => $v) {
  2513. if (isset($v['match']) && $v['match'] != 'option' && is_string($v['match']) && strpos($v['match'], '/') !== false) {
  2514. $rule[$k]['regex'] = $v['match'];
  2515. $rule[$k]['alertText'] = $v['desc'];
  2516. }
  2517. }
  2518. }
  2519. $script = '<script>';
  2520. //参考https://blog.csdn.net/wangzl1163/article/details/51071826
  2521. //http://code.ciaoca.com/jquery/validation-engine/demo/demoOnForm
  2522. $script .= '(function($){
  2523. $.fn.validationEngineLanguage = function(){
  2524. };
  2525. $.validationEngineLanguage = {
  2526. newLang: function(){
  2527. $.validationEngineLanguage.allRules = ';
  2528. //此处进行了转义过滤,其实也可以使用json_encode的第二个参数,不过只支持5.4以上版本,考虑到虚拟主机就放弃了
  2529. $script .= str_replace(array('\\\\', '\\/', '"/', '/"'), array('\\', '/', '/', '/'), json_encode($rule)) . ';';
  2530. $script .= '}
  2531. };
  2532. $.validationEngineLanguage.newLang();
  2533. })(jQuery);';
  2534. $script .= '$(document).ready(function(){
  2535. $(".form1").validationEngine("attach",{onSuccess:function(){submit();},onFailure:function(){submit();}});
  2536. });';
  2537. $script .= '</script>';
  2538. //print_r($script);die;
  2539. return $script;
  2540. }
  2541. /**
  2542. * save
  2543. *
  2544. * @return string
  2545. */
  2546. public function save()
  2547. {
  2548. $config = $this->config();
  2549. if (isset($config['manage']['save'])) {
  2550. return 'yes';
  2551. }
  2552. return 'no';
  2553. }
  2554. }