Database.php 127 KB

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