Database.php 130 KB

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