Database.php 132 KB

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