Database.php 116 KB

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