Database.php 130 KB

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