Database.php 111 KB

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