Database.php 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163
  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) . '&nbsp;&nbsp;&nbsp;';
  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) . '&nbsp;&nbsp;&nbsp;';
  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') . '&nbsp;&nbsp;&nbsp;';
  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) . '&nbsp;-&nbsp;';
  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) . '&nbsp;&nbsp;';
  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"><i class="fa fa-save"></i> 提交保存</button>
  785. <a href="' . $info['list'] . '" class="btn 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="#dever_modal" data-toggle="modal" 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" onclick="location.href=\''.Dever::url($v[1]).'\'">' . $k . '</a>';
  810. } else {
  811. $result .= '&nbsp;&nbsp;<a class="btn btn-default" 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="#dever_modal" data-toggle="modal" class="btn btn-default" onclick="$(\'#dever_modal_body\').html(\'' . $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" ' . $t[1] . '>' . $k . '</a>';
  819. } elseif (strpos($v, 'onclick') !== false) {
  820. $result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" ' . $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" href="' . Dever::url($v) . '">' . $k . '</a>';
  824. } else {
  825. $result .= '&nbsp;&nbsp;<a href="javascript:;" class="btn 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('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
  847. $btn = '<input type="hidden" value="2" name="excel" id="excel"><button class="btn btn-primary" onclick="$(\'#form1\').attr(\'action\', \'' . $this->url('search') . '\');list_search($(this))" type="button" style="height: 38px;margin-left:10px;">搜索</button>';
  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) . '&nbsp;';
  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) . '&nbsp;';
  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) . '&nbsp;';
  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) . '&nbsp;';
  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 "><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. $result .= $num;
  1028. return $result;
  1029. }
  1030. /**
  1031. * 列表页表格头
  1032. *
  1033. * @return array
  1034. */
  1035. public function list_thead()
  1036. {
  1037. # 新增权限
  1038. if (Dever::load('manage/auth.oper', 1) == false) {
  1039. Dever::alert('你没有权限');
  1040. }
  1041. $config = $this->config();
  1042. $result = array();
  1043. if (isset($config['struct'])) {
  1044. if (isset($config['manage']['list']) && $config['manage']['list'] == 'update') {
  1045. # 验证头部权限 这里如果是多个选择权限的话,暂时无法实现。。。
  1046. if (isset($config['top']) && is_string($config['top'])) {
  1047. $top = Dever::load('manage/auth.getTop', array($config['top']));
  1048. if ($top) {
  1049. $url = $this->url('update', $top['value']);
  1050. }
  1051. } else {
  1052. $url = $this->url('update', 1);
  1053. }
  1054. return Dever::location($url);
  1055. } else {
  1056. if (isset($config['manage']['mul'])) {
  1057. $result[] = '<th><input type="checkbox" class="checkbox-checkall" name="checkall" id="checkall" value="list"/></th>';
  1058. }
  1059. $data = array();
  1060. $data = $this->getAdminInfo($data);
  1061. foreach ($config['struct'] as $k => $v) {
  1062. if (isset($v['list']) && $v['list']) {
  1063. # 处理权限
  1064. if (isset($v['auth'])) {
  1065. $auth = $this->value($v['auth'], $data);
  1066. if (!$auth) {
  1067. continue;
  1068. }
  1069. }
  1070. if (isset($v['list_name'])) {
  1071. $v['name'] = $v['list_name'];
  1072. } else {
  1073. $temp = Html::cue(array('name' => $v['name']));
  1074. $v['name'] = $temp['name'];
  1075. }
  1076. if (!isset($v['level'])) {
  1077. $v['level'] = $config['level'];
  1078. }
  1079. if (isset($v['search']) && is_string($v['search']) && strpos($v['search'], 'order') !== false) {
  1080. $link = Dever::url();
  1081. $order = 'search_order_' . $k . '';
  1082. if (strpos($link, $order) !== false) {
  1083. $link = preg_replace('/[?|&]' . $order . '=(desc|asc)/i', '{order}', $link);
  1084. } else {
  1085. $link .= '{order}';
  1086. }
  1087. $search = Dever::input($order);
  1088. $cancel = '';
  1089. if ($search) {
  1090. $asc = $search;
  1091. $cancel = '&nbsp;<a class="glyphicon glyphicon-remove-sign" style="color:black;" href="' . str_replace('{order}', '', $link) . '" title="取消排序"></a>';
  1092. } else {
  1093. $asc = 'desc';
  1094. if (!isset($v['order']) && $k != 'id') {
  1095. $cancel = '&nbsp;<a class="glyphicon glyphicon-circle-arrow-down" style="color:black;" href="' . str_replace('{order}', '&' . $order . '=desc', $link) . '" title="降序"></a>';
  1096. }
  1097. }
  1098. if ($asc == 'asc') {
  1099. $asc = 'desc';
  1100. $icon = 'down';
  1101. $text = '降序';
  1102. } else {
  1103. $asc = 'asc';
  1104. $icon = 'up';
  1105. $text = '升序';
  1106. }
  1107. $link = str_replace('{order}', '&' . $order . '=' . $asc, $link);
  1108. $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>';
  1109. } else {
  1110. $result[] = '<th data-priority="'.$v['level'].'">' . $v['name'] . '</th>';
  1111. }
  1112. }
  1113. }
  1114. if ((Dever::load('manage/auth.oper', 3) != false || Dever::load('manage/auth.oper', 5) != false) && isset($config['manage']['_list']['value'])) {
  1115. $result[] = '<th data-priority="1">' . $config['manage']['_list']['name'] . '</th>';
  1116. }
  1117. }
  1118. }
  1119. return $result;
  1120. }
  1121. /**
  1122. * 导出列表
  1123. *
  1124. * @return array
  1125. */
  1126. public function list_excel_api()
  1127. {
  1128. Dever::$global['base']['excel'] = 1;
  1129. $data = $this->list_tbody(true);
  1130. if ($data) {
  1131. $config = $this->config();
  1132. if (isset($config['manage']['excel']) && $config['manage']['excel']) {
  1133. $method = 'excel';
  1134. $info = $this->info();
  1135. $data = $this->getAdminInfo($data);
  1136. $this->excel($info, $data, $config);
  1137. }
  1138. }
  1139. //print_r($data);die;
  1140. }
  1141. private function excel($info, $data, $config)
  1142. {
  1143. $file = $info['list_header'] . '的' . $info['list_desc'] . '-' . $info['project_name'];
  1144. $header = $body = array();
  1145. $i = 0;
  1146. foreach ($config['struct'] as $k => $v) {
  1147. if (isset($v['list']) && $v['list']) {
  1148. # 处理权限
  1149. if (isset($v['auth'])) {
  1150. $auth = $this->value($v['auth'], $data);
  1151. if (!$auth) {
  1152. continue;
  1153. }
  1154. }
  1155. //$v['name'] = mb_convert_encoding($v['name'],'gb2312','utf-8');
  1156. $header[$i] = $v['name'];
  1157. $i++;
  1158. $j = 0;
  1159. foreach ($data as $ki => $vi) {
  1160. $body[$j][$i] = '';
  1161. if (isset($vi[$k])) {
  1162. $value = $vi[$k];
  1163. if (is_string($v['list'])) {
  1164. $value = $this->value($v['list'], $data[$ki], $config['struct']);
  1165. }
  1166. if (isset($v['option'])) {
  1167. $value = $this->getOptionValue($value, $v);
  1168. }
  1169. //$value = mb_convert_encoding($value,'gb2312','utf-8');
  1170. $body[$j][$i] = $value;
  1171. $j++;
  1172. }
  1173. }
  1174. }
  1175. }
  1176. Dever::excelExport($body, $header, $file);
  1177. }
  1178. private function getOptionValue($value, $v)
  1179. {
  1180. # 验证option是否是匿名函数
  1181. $v['option'] = $this->option($v['option']);
  1182. # 位运算
  1183. if (isset($v['bit'])) {
  1184. $ts = array();
  1185. foreach ($v['option'] as $tm => $tv) {
  1186. if (isset($v['bit'][$tm]) && ($value & $tm) > 0) {
  1187. $ts[] = $tv;
  1188. }
  1189. }
  1190. $value = implode(',', $ts);
  1191. }
  1192. if ((isset($v['child_name']) || (isset($v['show']) && is_string($v['show'])))) {
  1193. if (isset($v['option'][$value]) && is_array($v['option'][$value])) {
  1194. $value = $v['option'][$value]['name'];
  1195. } else {
  1196. $value = isset($v['option'][$value]) ? $v['option'][$value] : $value;
  1197. }
  1198. } else {
  1199. if (strstr($value, ',')) {
  1200. $temp = explode(',', $value);
  1201. $t = array();
  1202. foreach ($temp as $vv) {
  1203. if (isset($v['option'][$vv]) && is_array($v['option'][$vv])) {
  1204. $t[] = $v['option'][$vv]['name'];
  1205. } elseif(isset($v['option'][$vv])) {
  1206. $t[] = $v['option'][$vv];
  1207. } else {
  1208. $t[] = $vv;
  1209. }
  1210. }
  1211. $value = implode("\r\n", $t);
  1212. } elseif (isset($v['option'][$value]) && is_array($v['option'][$value])) {
  1213. $value = $v['option'][$value]['name'];
  1214. } else {
  1215. $value = isset($v['option'][$value]) ? $v['option'][$value] : $value;
  1216. }
  1217. }
  1218. return $value;
  1219. }
  1220. private function _getChr($start, $num)
  1221. {
  1222. $num = $start + $num;
  1223. $max = floor($num / 91);
  1224. if ($max > 0) {
  1225. $num = $num - (91 * $max) + $start;
  1226. $num = chr($start) . chr($num);
  1227. } else {
  1228. $num = chr($num);
  1229. }
  1230. return $num;
  1231. }
  1232. /**
  1233. * 列表页数据列表 检索
  1234. *
  1235. * @return array
  1236. */
  1237. private function list_tbody_search($config, $param, $high, $search)
  1238. {
  1239. # 根据api的数据检索到id
  1240. $api = Dever::preInput('search_api_');
  1241. if ($api) {
  1242. foreach ($api as $k => $v) {
  1243. if ($v) {
  1244. unset($search[$k]);
  1245. $k = str_replace('search_api_', '', $k);
  1246. # 设定检索高亮
  1247. $high[$k] = $v;
  1248. if (isset($config['manage']['search']['api'][$k]['api'])) {
  1249. $col = $config['manage']['search']['api'][$k]['col'];
  1250. $option = array($col => $v);
  1251. $v = Dever::load($config['manage']['search']['api'][$k]['api'], $option);
  1252. $result = $config['manage']['search']['api'][$k]['result'];
  1253. if ($v && is_array($v)) {
  1254. if (isset($v[$result])) {
  1255. $v = $v[$result];
  1256. } else {
  1257. if ($result == 'id') {
  1258. $v = array_keys($v);
  1259. } else {
  1260. $t = array();
  1261. foreach ($v as $k1 => $v1) {
  1262. $t[] = $v1[$result];
  1263. }
  1264. $v = $t;
  1265. }
  1266. $param['option'][$k] = array('yes', 'in');
  1267. }
  1268. } else {
  1269. $v = '-100';
  1270. }
  1271. }
  1272. $param['option_' . $k] = $v;
  1273. }
  1274. }
  1275. }
  1276. # 检索有无值
  1277. $exist = Dever::preInput('search_exist_');
  1278. if ($exist) {
  1279. foreach ($exist as $k => $v) {
  1280. if ($v) {
  1281. unset($search[$k]);
  1282. $k = str_replace('search_exist_', '', $k);
  1283. if ($v == 'yes') {
  1284. # 有值
  1285. $param['option'][$k] = array('yes', '!=');
  1286. $param['option_' . $k] = 'null';
  1287. } elseif ($v == 'no') {
  1288. # 无值
  1289. $param['option'][$k] = array('yes', '=');
  1290. $param['option_' . $k] = 'null';
  1291. } elseif (strpos($v, '|')) {
  1292. # 有无值或者自定义的值
  1293. $temp = explode('|', $v);
  1294. $param['option_' . $k] = $temp[1];
  1295. if ($temp[0] == 'yes') {
  1296. $param['option'][$k] = array('yes', '!=');
  1297. $param['option'][$k . '_t1'] = array('yes-' . $k, '!=');
  1298. } else {
  1299. $param['option'][$k] = array('yes', '=', 'and(');
  1300. $param['option'][$k . '_t1'] = array('yes-' . $k, '=', 'or)');
  1301. }
  1302. $param['option_' . $k . '_t1'] = 'null';
  1303. } else {
  1304. # 自定义的值
  1305. $param['option_' . $k] = $v;
  1306. }
  1307. }
  1308. }
  1309. }
  1310. $fulltext = Dever::preInput('search_fulltext_');
  1311. if ($fulltext) {
  1312. $fulltext_state = false;
  1313. foreach ($fulltext as $k => $v) {
  1314. if ($v) {
  1315. unset($search[$k]);
  1316. $k = str_replace('search_fulltext_', '', $k);
  1317. $param['option_' . $k] = $v;
  1318. # 设定检索高亮
  1319. $high[$k] = $v;
  1320. $fulltext_state = true;
  1321. }
  1322. }
  1323. $this->search = true;
  1324. }
  1325. $linkage = Dever::preInput('search_linkage_');
  1326. if ($linkage) {
  1327. foreach ($linkage as $k => $v) {
  1328. if ($v) {
  1329. unset($search[$k]);
  1330. $k = str_replace('search_linkage_', '', $k);
  1331. if (is_array($v)) {
  1332. $v = implode(',', $v);
  1333. }
  1334. if ($v != -1) {
  1335. $v = Dever::defaultValue($v);
  1336. $param['option_' . $k] = $v;
  1337. # 设定检索高亮
  1338. $high[$k] = $v;
  1339. }
  1340. }
  1341. }
  1342. $this->search = true;
  1343. }
  1344. $exp = Dever::preInput('search_exp_');
  1345. if ($exp) {
  1346. $k = $search['search_exp_col'];
  1347. $v = $search['search_exp_value'];
  1348. $param['option_' . $k] = $v;
  1349. $param['search_type'] = $search['search_exp_type'];
  1350. $this->search = true;
  1351. }
  1352. $order = Dever::preInput('search_order_');
  1353. if ($order) {
  1354. foreach ($order as $k => $v) {
  1355. $k = str_replace('search_order_', '', $k);
  1356. $param['order'][0][$k] = $v;
  1357. unset($search[$k]);
  1358. }
  1359. }
  1360. foreach ($search as $k => $v) {
  1361. if ($v) {
  1362. if (strpos($k, 'option_') !== false) {
  1363. //Dever::setInput(str_replace('search_', '', $k), $v);
  1364. if (is_array($v)) {
  1365. if ($v[0] == 0) {
  1366. continue;
  1367. }
  1368. $v = str_replace(',0', '', implode(',', $v));
  1369. $t = explode(',', $v);
  1370. $parent = end($t);
  1371. $v .= '%';
  1372. }
  1373. $param[str_replace('search_', '', $k)] = $v;
  1374. $this->search = true;
  1375. } elseif ($k == 'search_limit') {
  1376. $num = $this->getNumConfig($config);
  1377. if ($num) {
  1378. $param['page'][0] = $num[$v];
  1379. }
  1380. }
  1381. }
  1382. }
  1383. return array($param, $high);
  1384. }
  1385. private function list_tbody_mul($param)
  1386. {
  1387. $mul = Dever::load('manage/auth.oper', 3) ? Dever::preInput('mul_') : false;
  1388. if (Dever::input('method') == 'mul' && $mul && $mul['mul_type'] > 0) {
  1389. if ($mul['mul_type'] == 1 && empty($mul['mul_where_id'])) {
  1390. Dever::alert('您还未选择任何数据条目');
  1391. }
  1392. Dever::$global['base']['mul_type'] = $mul['mul_type'];
  1393. unset($mul['mul_type']);
  1394. foreach ($mul as $k => $v) {
  1395. if ($v) {
  1396. if (is_array($v)) {
  1397. $v = implode(',', $v);
  1398. }
  1399. $param[str_replace('mul_', '', $k)] = $v;
  1400. }
  1401. }
  1402. if (isset($param)) {
  1403. $method = $this->project . '/' . $this->table . '-updatemul';
  1404. $state = Dever::load($method, $param);
  1405. }
  1406. Dever::alert('reload');
  1407. }
  1408. }
  1409. /**
  1410. * 列表页数据列表:图片形式
  1411. *
  1412. * @return array
  1413. */
  1414. public function list_photo()
  1415. {
  1416. $config = $this->config();
  1417. $result = array();
  1418. if (isset($config['manage']['photo'])) {
  1419. $high = array();
  1420. $search = Dever::load('manage/auth.oper', 2) ? Dever::preInput('search_') : false;
  1421. $param = array();
  1422. $parent = 0;
  1423. if ($search) {
  1424. list($param, $high) = $this->list_tbody_search($config, $param, $high, $search);
  1425. }
  1426. if (empty($param['page'])) {
  1427. $num = $this->getNumConfig($config);
  1428. if ($num) {
  1429. $param['page'][0] = $num[0];
  1430. }
  1431. }
  1432. $method = $this->project . '/' . $this->table . '-list';
  1433. $result = Dever::load($method, $param);
  1434. if ($result) {
  1435. foreach ($result as $k => $v) {
  1436. $result[$k] = Dever::load($config['manage']['photo'], $v);
  1437. $result[$k]['button'] = $this->list_tbody_button($config, $v, 'div');
  1438. }
  1439. }
  1440. }
  1441. return $result;
  1442. }
  1443. /**
  1444. * 列表页数据列表:表格形式
  1445. *
  1446. * @return array
  1447. */
  1448. public function list_tbody($excel = false)
  1449. {
  1450. # 新增权限
  1451. if (Dever::load('manage/auth.oper', 1) == false) {
  1452. Dever::alert('你没有权限');
  1453. }
  1454. if (Dever::input('page_content') == 1) {
  1455. return;
  1456. }
  1457. $config = $this->config();
  1458. $high = array();
  1459. $search = Dever::load('manage/auth.oper', 2) ? Dever::preInput('search_') : false;
  1460. $param = array();
  1461. $parent = 0;
  1462. if ($search) {
  1463. list($param, $high) = $this->list_tbody_search($config, $param, $high, $search);
  1464. }
  1465. if (empty($param['page'])) {
  1466. $num = $this->getNumConfig($config);
  1467. if ($num) {
  1468. $param['page'][0] = $num[0];
  1469. }
  1470. }
  1471. # 批量更新
  1472. $this->list_tbody_mul($param);
  1473. $result = array();
  1474. if ($excel == true) {
  1475. $method = $this->project . '/' . $this->table . '-all';
  1476. } else {
  1477. $method = $this->project . '/' . $this->table . '-list';
  1478. }
  1479. //print_r($param);die;
  1480. $data = Dever::load($method, $param);
  1481. if (isset($config['manage']['list_type']) && $config['manage']['list_type'] == 'parent') {
  1482. $parent = array();
  1483. $child = array();
  1484. foreach ($data as $k => $v) {
  1485. if ($v[$config['name'] . '_id'] <= 0) {
  1486. $parent[$k] = $v;
  1487. } else {
  1488. $v['name'] = '|-----' . $v['name'];
  1489. $child[$v[$config['name'] . '_id']][$k] = $v;
  1490. }
  1491. }
  1492. if ($parent) {
  1493. $data = array();
  1494. foreach ($parent as $k => $v) {
  1495. $data[$v['id']] = $v;
  1496. if (isset($child[$v['id']])) {
  1497. foreach ($child[$v['id']] as $i => $j) {
  1498. $data[$j['id']] = $j;
  1499. }
  1500. }
  1501. }
  1502. }
  1503. } elseif (isset($config['manage']['list_type']) && $config['manage']['list_type'] == 'tree') {
  1504. $data = $this->tree($data, $config, -1);
  1505. }
  1506. if ($excel == true) {
  1507. return $data;
  1508. }
  1509. if (Dever::input('page_type') == 1) {
  1510. Dever::$global['manage']['list_data'] = $data;
  1511. return;
  1512. }
  1513. //print_r($data);die;
  1514. //print_r(Helper::page("current"));die;
  1515. if ($data && isset($config['struct'])) {
  1516. $i = 0;
  1517. $pg = Dever::input('pg', 1);
  1518. $cur = Dever::input('search_limit', 0);
  1519. $num = $this->getNumConfig($config);
  1520. foreach ($data as $k => $v) {
  1521. $data[$k] = $this->getAdminInfo($data[$k]);
  1522. $data[$k]['i'] = $i;
  1523. $ii = intval($num[$cur])*($pg-1);
  1524. $data[$k]['r'] = $i+1+$ii;
  1525. $i++;
  1526. $result[$k] = '<tr>';
  1527. if (isset($config['manage']['mul'])) {
  1528. $result[$k] .= '<td><input type="checkbox" name="mul_where_id[]" class="checkbox-checkall-list" value="' . $v['id'] . '"/></td>';
  1529. }
  1530. foreach ($config['struct'] as $ki => $vi) {
  1531. # 处理同步
  1532. if (isset($vi['sync']) && strpos($ki, '-')) {
  1533. $temp = explode('-', $ki);
  1534. $load = Dever::load($temp[0] . '/' . $temp[1] . '-one', array('option_' . $vi['sync'][1] => $v[$vi['sync'][0]]));
  1535. $v[$ki] = isset($load[$temp[2]]) ? $load[$temp[2]] : $vi['default'];
  1536. }
  1537. if (isset($vi['list']) && $vi['list']) {
  1538. if (empty($v[$ki])) {
  1539. $v[$ki] = $vi['default'];
  1540. }
  1541. # 处理权限
  1542. if (isset($vi['auth'])) {
  1543. $auth = $this->value($vi['auth'], $data[$k]);
  1544. if (!$auth) {
  1545. continue;
  1546. }
  1547. }
  1548. $show = $v[$ki];
  1549. if (is_string($vi['list'])) {
  1550. $show = $this->value($vi['list'], $data[$k], $config['struct']);
  1551. }
  1552. if (isset($vi['key']) && $vi['update'] != 'editor') {
  1553. $show = Dever::upload($show);
  1554. }
  1555. if (isset($vi['modal'])) {
  1556. $show = Html::modal($this->value($vi['modal'], $data[$k], $config['struct']), $show);
  1557. } elseif (isset($vi['option'])) {
  1558. # 验证option是否是匿名函数
  1559. $vi['option'] = $this->option($vi['option']);
  1560. # 位运算
  1561. if (isset($vi['bit'])) {
  1562. $ts = array();
  1563. foreach ($vi['option'] as $tm => $tv) {
  1564. if (isset($vi['bit'][$tm]) && ($show & $tm) > 0) {
  1565. $ts[] = $tv;
  1566. }
  1567. }
  1568. $show = implode(',', $ts);
  1569. }
  1570. if ((isset($vi['child_name']) || (isset($vi['show']) && is_string($vi['show'])))) {
  1571. if (isset($vi['option'][$show]) && is_array($vi['option'][$show])) {
  1572. $show = $vi['option'][$show]['name'];
  1573. } else {
  1574. $show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
  1575. }
  1576. }
  1577. //elseif ($config['state'] == 1 && (isset($vi['edit']) && $vi['edit']) || $ki == 'status')
  1578. elseif ($config['state'] == 1 && (isset($vi['edit']) && $vi['edit']))
  1579. {
  1580. $vi['name'] = 'up_col_' . $ki;
  1581. $vi['value'] = $v[$ki];
  1582. $m = 'select';
  1583. if (isset($vi['update']) && $vi['update'] == 'group') {
  1584. $m = 'group';
  1585. }
  1586. $show = Html::$m($vi, 'form-list', '', 'style="min-width: 100px;" data-old="'.$show.'" onchange="update($(this), \'' . $v['id'] . '\', \'' . $this->project . '\', \'' . $this->table . '\')"');
  1587. } else {
  1588. if (strstr($show, ',')) {
  1589. $temp = explode(',', $show);
  1590. $t = array();
  1591. foreach ($temp as $vv) {
  1592. if (isset($vi['option'][$vv]) && is_array($vi['option'][$vv])) {
  1593. $t[] = $vi['option'][$vv]['name'];
  1594. } elseif(isset($vi['option'][$vv])) {
  1595. $t[] = $vi['option'][$vv];
  1596. } else {
  1597. $t[] = $vv;
  1598. }
  1599. }
  1600. $show = implode('<br />', $t);
  1601. } elseif (isset($vi['option'][$show]) && is_array($vi['option'][$show])) {
  1602. $show = $vi['option'][$show]['name'];
  1603. } else {
  1604. $show = isset($vi['option'][$show]) ? $vi['option'][$show] : $show;
  1605. }
  1606. }
  1607. }
  1608. $text = $show;
  1609. if ($high && isset($high[$ki])) {
  1610. if (isset($v['s_' . $ki])) {
  1611. $text = $v['s_' . $ki];
  1612. $text = str_replace('<em class="dever_highlight">', '<font color="red" class="dever_highlight">', $text);
  1613. $text = str_replace('</em>', '</font>', $text);
  1614. } else {
  1615. $text = preg_replace('/('.$high[$ki].')/i', '<font color="red" class="dever_highlight">${1}</font>', $text);
  1616. }
  1617. }
  1618. if (!isset($vi['option']) && isset($vi['edit']) && $vi['edit']) {
  1619. //$url = Dever::url($vi['edit'] . '?where_id=' . $v['id'] . '&col=' . $vi['col']. '&key=' . $this->project . '&table=' . $this->table);
  1620. $url = $this->url('updateAction');
  1621. $vi['edit'] = $vi['edit'] == 'textarea' ? $vi['edit'] : 'text';
  1622. $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>';
  1623. } else {
  1624. $result[$k] .= '<td>' . $text . '</td>';
  1625. }
  1626. }
  1627. }
  1628. $result[$k] .= $this->list_tbody_button($config, $data[$k]);
  1629. $result[$k] .= '</tr>';
  1630. }
  1631. }
  1632. /*
  1633. if ($result) {
  1634. $thead = $this->list_thead();
  1635. if ($thead) {
  1636. $k = $k + 1;
  1637. $result[$k] = '<tr>';
  1638. foreach ($thead as $k => $v) {
  1639. $result[$k] .= $v;
  1640. }
  1641. $result[$k] .= '</tr>';
  1642. }
  1643. }
  1644. */
  1645. return $result;
  1646. }
  1647. private function list_tbody_button($config, $data, $type = 'td')
  1648. {
  1649. $result = '';
  1650. if (isset($config['manage']['_list']['value'])) {
  1651. $result = '<'.$type.'>';
  1652. if ($config['state'] == 2) {
  1653. if (isset($config['manage']['_list']['value']['update'])) {
  1654. unset($config['manage']['_list']['value']['update']);
  1655. }
  1656. if (isset($config['manage']['_list']['value']['delete'])) {
  1657. $config['manage']['_list']['value']['delete'] = '恢复';
  1658. }
  1659. }
  1660. foreach ($config['manage']['_list']['value'] as $kj => $vj) {
  1661. $pj = false;
  1662. $h = '<span class="am-icon-pencil-square-o"></span>';
  1663. $oper = true;
  1664. if ($kj == 'update') {
  1665. $oper = Dever::load('manage/auth.oper', 3);
  1666. } elseif ($kj == 'delete') {
  1667. $oper = Dever::load('manage/auth.oper', 5);
  1668. $h = '<span class="am-icon-trash-o"></span>';
  1669. }
  1670. if (is_array($vj) && isset($vj[1])) {
  1671. if (isset($vj[2])) {
  1672. $oper = $this->value($vj[2], $data);
  1673. }
  1674. if ($oper && strpos($vj[1], 'modal|') === false) {
  1675. $pj = $this->value($vj[1], $data);
  1676. $vjn = $vj[0];
  1677. }
  1678. } else {
  1679. $vjn = $vj;
  1680. }
  1681. if ($oper == true) {
  1682. if (strpos($kj, 'edit') !== false) {
  1683. $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">' . $vj[0] . '</button></a>&nbsp;&nbsp;';
  1684. } elseif (strpos($kj, 'oper') !== false) {
  1685. $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">' . $vj[0] . '</button></a>&nbsp;&nbsp;';
  1686. } elseif (isset($vj[1]) && strpos($vj[1], 'modal|') !== false) {
  1687. $t = explode('|', $vj[1]);
  1688. $result .= '<a href="#dever_modal" data-toggle="modal" class="btn btn-info btn-rounded waves-effect waves-light m-b-5" onclick=' . $t[1] . ' msg-send="' . $this->value($t[2], $data) . '">' . $vj[0] . '</a>&nbsp;&nbsp;';
  1689. } elseif (isset($vj[1])) {
  1690. $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">' . $h . $vjn . '</button></a>&nbsp;&nbsp;';
  1691. } else {
  1692. $result .= '<span style="margin-left:5px;margin-right:10px;">' . $vj[0] . '</span>';
  1693. }
  1694. }
  1695. }
  1696. $result .= '</'.$type.'>';
  1697. }
  1698. return $result;
  1699. }
  1700. /**
  1701. * 多级分类
  1702. *
  1703. * @return array
  1704. */
  1705. private function tree($data, $config, $parent = 0)
  1706. {
  1707. $key = $config['name'] . '_id';
  1708. $result = array();
  1709. $this->treeData($data, $result, $key, $parent);
  1710. return $result;
  1711. }
  1712. private function treeData($array, &$result, $key, $parent_id = 0, $level = 0)
  1713. {
  1714. if (empty($array)) {
  1715. return;
  1716. }
  1717. $level++;
  1718. foreach ($array as $k => $v) {
  1719. if (isset($v[$key]) && $v[$key] == $parent_id) {
  1720. $v['level'] = $level;
  1721. $v['name'] = $this->treeName($v['name'], $level);
  1722. $result[] = $v;
  1723. unset($array[$k]);
  1724. $this->treeData($array, $result, $key, $v['id'], $level);
  1725. }
  1726. }
  1727. }
  1728. private function treeName($name, $level = 1)
  1729. {
  1730. $string = '';
  1731. if ($level > 1) {
  1732. /*
  1733. $num = 4;
  1734. $string = '-';
  1735. //STR_PAD_LEFT
  1736. $string = str_pad('|', $level * $num, $string);
  1737. */
  1738. $string = str_repeat(' ',$level-1);
  1739. $string .= '|—';
  1740. }
  1741. return $string . $name;
  1742. }
  1743. /**
  1744. * 列表页批量管理
  1745. *
  1746. * @return array
  1747. */
  1748. public function list_mul()
  1749. {
  1750. # 新增权限:更新权限
  1751. if (Dever::load('manage/auth.oper', 3) == false) {
  1752. Dever::alert('你没有权限');
  1753. }
  1754. $config = $this->config();
  1755. $result = '';
  1756. if (isset($config['struct']) && isset($config['manage']['mul'])) {
  1757. $mul = Dever::preInput('mul_');
  1758. $node = Html::node(array('class="am-u-sm-12" style="margin-top:10px;"', 'class="am-form-group am-input-group-sm"'));
  1759. $btn = '<button class="btn btn-danger" onclick="$(\'#form1\').attr(\'action\', \'' . $this->url('list') . '\');list_mul($(this))" type="button" style="height: 38px;margin-left:10px;">更新</button>';
  1760. $prefix = 'mul_';
  1761. $result .= $node[0];
  1762. $default = 0;
  1763. $option = array
  1764. (
  1765. 1 => '选中的数据',
  1766. );
  1767. # 只有当有搜索条件时才会出现
  1768. if (Dever::load('manage/auth.oper', 2) && $this->search == true) {
  1769. $option[2] = '所有数据';
  1770. $default = 2;
  1771. }
  1772. $this->list_search_select($result, $mul, $prefix . 'type' . '" id="' . $prefix . 'type', '请选择更新方式', $option, $default);
  1773. foreach ($config['struct'] as $k => $v) {
  1774. if ($k == 'state') {
  1775. $v['option'] = Dever::config('base')->state;
  1776. if ($this->getState() == 2) {
  1777. unset($v['option'][2]);
  1778. } else {
  1779. unset($v['option'][1]);
  1780. }
  1781. $v['edit'] = true;
  1782. }
  1783. if (isset($v['edit']) && isset($v['option']) && (is_array($v['option']) || is_object($v['option']))) {
  1784. $this->list_search_select($result, $mul, $prefix . 'set_' . $k, $v['name'], $this->option($v['option']), $v['default']);
  1785. }
  1786. }
  1787. $result .= $btn . $node[1];
  1788. }
  1789. //print_r($result);die;
  1790. return $result;
  1791. }
  1792. private function value($value, $data, $struct = array())
  1793. {
  1794. if (!$data) {
  1795. return $value;
  1796. }
  1797. if (is_string($data)) {
  1798. $eval = '$value = ' . $value . ';';
  1799. eval($eval);
  1800. return $value;
  1801. }
  1802. if ($value == 'table' && $struct) {
  1803. $result = $this->struct_data($struct, $data);
  1804. $value = Dever::table($result);
  1805. } elseif (strpos($value, '{') !== false) {
  1806. # 这里用到了/e,注意这里无论如何不能传入$_GET之类的用户参数 2015/7/21更新为callback方式
  1807. //$eval = '$value = ' . preg_replace('/{(.*?)}/e', "\$data['$1']", $value) . ';';
  1808. $func = function ($r) use ($data) {
  1809. return $data[$r[1]];
  1810. };
  1811. $eval = '$value = ' . preg_replace_callback('/{(.*?)}/', $func, $value) . ';';
  1812. eval($eval);
  1813. }
  1814. return $value;
  1815. }
  1816. /**
  1817. * update
  1818. *
  1819. * @return array
  1820. */
  1821. public function update()
  1822. {
  1823. $config = $this->config();
  1824. $result = array();
  1825. $prefix = 'update';
  1826. $data = array();
  1827. if ($this->id) {
  1828. # 新增权限
  1829. if (Dever::load('manage/auth.oper', 3) == false) {
  1830. Dever::alert('你没有权限');
  1831. }
  1832. $data = $this->load('one', $this->id);
  1833. if ($data) {
  1834. //$prefix = 'set';
  1835. }
  1836. } else {
  1837. # 新增权限
  1838. if (Dever::load('manage/auth.oper', 4) == false) {
  1839. Dever::alert('你没有权限');
  1840. }
  1841. }
  1842. //print_r($config['manage']['update']);die;
  1843. $data = $this->getAdminInfo($data);
  1844. $col = Dever::input('col');
  1845. foreach ($config['struct'] as $k => $v) {
  1846. if (isset($v['update'])) {
  1847. if ($col && !strstr(',' . $col . ',', ',' . $k . ',')) {
  1848. continue;
  1849. }
  1850. //处理权限
  1851. if (isset($v['auth'])) {
  1852. $auth = $this->value($v['auth'], $data);
  1853. if (!$auth) {
  1854. continue;
  1855. }
  1856. }
  1857. # 处理show模式下的class
  1858. $class = $this->update_class($k, $v, '', $data);
  1859. $result[$k] = '<div class="form-group ' . $class . '"><div class="margin-top">';
  1860. $result[$k] .= $this->create_update_html($k, $v, $data);
  1861. $result[$k] .= '</div></div>';
  1862. } elseif (!$col && isset($v['class'])) {
  1863. # 分割线
  1864. $v['name'] = $this->value($v['name'], array('test' => 1));
  1865. $result[$k] = Html::hr($v);
  1866. }
  1867. }
  1868. if (Dever::input('ajax') == 1) {
  1869. return implode('', $result);
  1870. }
  1871. //print_r($result);die;
  1872. return $result;
  1873. }
  1874. /**
  1875. * update_class
  1876. *
  1877. * @return string
  1878. */
  1879. public function update_class($name, $param, $prefix = '', $data = array())
  1880. {
  1881. $class = '';
  1882. $key = 'show';
  1883. if (isset($param[$key]) && $param[$key]) {
  1884. $parent = array();
  1885. $child = array();
  1886. if (is_string($param[$key])) {
  1887. parse_str($param[$key], $param[$key]);
  1888. }
  1889. if (is_array($param[$key])) {
  1890. foreach ($param[$key] as $k => $v) {
  1891. if ($k == 'no') {
  1892. $child[$k . '_' . $v] = $key . '_no';
  1893. } elseif (strpos($v, '/')) {
  1894. $parent[$k] = $key . '_' . $k;
  1895. $k = $k . '_ajax';
  1896. $value = '';
  1897. if (isset($data[$name])) {
  1898. $value = $data[$name];
  1899. }
  1900. $v = str_replace('?', '?key='.$name.'&value='.$value.'&source='.$this->name.'&', $v);
  1901. $child[$k] = $key . '_' . $k .'" dever-ajax="'.Dever::url($v).'"';
  1902. } else {
  1903. if (strpos($v, ',')) {
  1904. $temp = explode(',', $v);
  1905. foreach ($temp as $v1) {
  1906. $show = $k . '_' . $v1;
  1907. if ($prefix) {
  1908. $k = str_replace($name, $k, $prefix);
  1909. $show = str_replace($name, $show, $prefix);
  1910. }
  1911. $parent[$k] = $key . '_' . $k;
  1912. $child[$show] = $key . '_' . $show;
  1913. }
  1914. } else {
  1915. $show = $k . '_' . $v;
  1916. if ($prefix) {
  1917. $k = str_replace($name, $k, $prefix);
  1918. $show = str_replace($name, $show, $prefix);
  1919. }
  1920. $parent[$k] = $key . '_' . $k;
  1921. $child[$show] = $key . '_' . $show;
  1922. }
  1923. }
  1924. }
  1925. }
  1926. $class = implode(' ', $parent) . ' ' . implode(' ', $child);
  1927. }
  1928. if (isset($param['control'])) {
  1929. $class .= ' ' . $key . '_input';
  1930. }
  1931. return $class;
  1932. }
  1933. /**
  1934. * update_html
  1935. *
  1936. * @return string
  1937. */
  1938. public function create_update_html($key, $value, $data = array())
  1939. {
  1940. $result = '';
  1941. $value['ext'] = $value['col'] = $key;
  1942. $value['update'] = $value['update'] ? $value['update'] : 'text';
  1943. if (isset($value['match']) && $value['match'] != 'option') {
  1944. if (is_string($value['match']) && strpos($value['match'], '/') !== false) {
  1945. $value['valid'] = 'validate[required,custom[' . $key . ']]';
  1946. } else {
  1947. $value['valid'] = 'validate[required]';
  1948. }
  1949. }
  1950. if (is_string($value['update']) && strpos($value['update'], 'copy.') !== false) {
  1951. $value['value'] = $value['update'];
  1952. $value['update'] = 'hidden';
  1953. }
  1954. $this->update_html($value, $key, $key, $data, $result);
  1955. return $result;
  1956. }
  1957. /**
  1958. * update_html
  1959. *
  1960. * @return string
  1961. */
  1962. public function update_html($param, $key, $index, $data, &$result)
  1963. {
  1964. if (isset($param['name']) && $param['update'] != 'hidden' && isset($param['update'])) {
  1965. $result .= Html::pageLeft($param);
  1966. }
  1967. if ($param['update']) {
  1968. # 名称带前缀
  1969. $param['name'] = 'update_' . $param['ext'];
  1970. $param['index'] = $index;
  1971. //$param['value'] = '';
  1972. # 得到当前的值
  1973. if (isset($data[$index]) && !isset($param['value'])) {
  1974. $param['value'] = $data[$index];
  1975. }
  1976. # 得到下级的值
  1977. if (isset($param['child_value']) && isset($data['id'])) {
  1978. $param['child_value'] = $this->value($param['child_value'], $data);
  1979. }
  1980. # 验证option是否是匿名函数
  1981. if (isset($param['option'])) {
  1982. $param['option'] = $this->option($param['option']);
  1983. }
  1984. if (isset($data[$index]) && isset($param['key']) && $param['update'] != 'editor') {
  1985. $param['value'] = Dever::upload($data[$index]);
  1986. }
  1987. # 处理下级字段
  1988. if (isset($param['child'])) {
  1989. $param['child'] = $this->option($param['child']);
  1990. if (isset($data[$key . '_parent'])) {
  1991. $param['parent_value'] = $data[$key . '_parent'];
  1992. }
  1993. }
  1994. # 设置默认值
  1995. if (isset($param['default'])) {
  1996. $param['default'] = $this->option($param['default']);
  1997. }
  1998. # 处理同步
  1999. if (isset($param['sync']) && isset($data[$param['sync'][0]]) && strpos($key, '-')) {
  2000. $temp = explode('-', $key);
  2001. $load = Dever::load($temp[0] . '/' . $temp[1] . '-one', array('option_' . $param['sync'][1] => $data[$param['sync'][0]]));
  2002. $param['value'] = isset($load[$temp[2]]) ? $load[$temp[2]] : $param['default'];
  2003. }
  2004. $method = $param['update'];
  2005. if (is_array($method)) {
  2006. $result .= '<ul class="nav nav-tabs">{tab}<li><a href="javascript:;" class="dever_form_add" toggle_key="' . $key . '">增加一条</a></li></ul>';
  2007. $tab = '';
  2008. $order = array
  2009. (
  2010. 'col' => 'order',
  2011. 'name' => '排序-按照数字正序排列',
  2012. 'default' => '1',
  2013. 'desc' => '排序',
  2014. 'match' => 'is_string',
  2015. 'update' => 'text',
  2016. );
  2017. array_unshift($method, $order);
  2018. $this->update_child(array(), $method, $key, $result, -1);
  2019. if (isset($param['value']) && $param['value']) {
  2020. $param['value'] = json_decode(base64_decode($param['value']), true);
  2021. foreach ($param['value'] as $k => $v) {
  2022. $this->update_child($v, $method, $key, $result, $k);
  2023. $active = '';
  2024. if ($k == 0) {
  2025. $active = 'active';
  2026. }
  2027. $k = $k + 1;
  2028. $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>';
  2029. }
  2030. } else {
  2031. $this->update_child(array(), $method, $key, $result, 0);
  2032. $tab = '<li class="active"><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>';
  2033. }
  2034. $result = str_replace('{tab}', $tab, $result);
  2035. } else {
  2036. if (isset($param['update_input']) && isset($data[$index . '_input'])) {
  2037. $param['update_input_data'] = explode(',', $data[$index . '_input']);
  2038. }
  2039. $result .= Html::$method($param);
  2040. }
  2041. if (isset($param['desc']) && ($param['update'] == 'text' || $param['update'] == 'password')) {
  2042. $result .= Html::desc($param['desc']);
  2043. }
  2044. }
  2045. }
  2046. /**
  2047. * update_action
  2048. *
  2049. * @return array
  2050. */
  2051. private function update_child($data, $param, $key, &$result, $index)
  2052. {
  2053. $style = '';
  2054. if ($index != 0) {
  2055. $style = 'style="display:none;"';
  2056. }
  2057. $result .= '<div id="' . $key . '-child-' . ($index + 1) . '" class="dever_' . $key . '_child dever_form_add_child" ' . $style . '><div class="dever_form_delete">[删除]</div>';
  2058. foreach ($param as $k => $v) {
  2059. $v['type'] = $v['update'];
  2060. $prefix = '';
  2061. if ($index < 0) {
  2062. $v['ext'] = $key . '_c_' . $v['col'];
  2063. $prefix = $v['ext'];
  2064. } else {
  2065. $v['ext'] = $key . '_c_' . $index . '_i_' . $v['col'];
  2066. $prefix = $v['ext'];
  2067. }
  2068. if ($v['col'] == 'order') {
  2069. $v['default'] = $index+1;
  2070. }
  2071. $class = $this->update_class($v['col'], $v, $prefix);
  2072. $result .= '<div class="form-group ' . $class . '">';
  2073. $this->update_html($v, $key, $v['col'], $data, $result);
  2074. $result .= '</div>';
  2075. }
  2076. $result .= '</div>';
  2077. }
  2078. /**
  2079. * update_action
  2080. *
  2081. * @return array
  2082. */
  2083. public function update_action_api()
  2084. {
  2085. $method = false;
  2086. $config = $this->config();
  2087. $input = Dever::preInput('update_');
  2088. if ($this->id) {
  2089. # 新增权限
  2090. if (Dever::load('manage/auth.oper', 3) == false) {
  2091. Dever::alert('你没有权限');
  2092. }
  2093. Dever::setInput('where_id', $this->id);
  2094. //Dever::setInput('update_where_id', $this->id);
  2095. $info = $this->load('one', $this->id);
  2096. if ($info) {
  2097. $method = 'update';
  2098. $col = Dever::input('col');
  2099. $value = Dever::input('value');
  2100. if ($col && $value) {
  2101. $method = 'update_' . $col;
  2102. //Dever::setInput('set_' . $col, $value);
  2103. //Dever::setInput('update_' . $col, $value);
  2104. $param['where_id'] = $this->id;
  2105. $param['set_' . $col] = $value;
  2106. $this->load($method, $param);
  2107. $struct = $this->struct_data($config['struct'], array($col => $value));
  2108. Log::add($this->project, $this->table, array('method' => $method, 'input' => $struct, 'struct' => $config['struct']), $this->id);
  2109. Dever::out('yes');
  2110. return;
  2111. }
  2112. $prefix = 'set';
  2113. } else {
  2114. $method = 'insert';
  2115. $prefix = 'add';
  2116. }
  2117. }
  2118. if (!$method) {
  2119. # 新增权限
  2120. if (Dever::load('manage/auth.oper', 4) == false) {
  2121. Dever::alert('你没有权限');
  2122. }
  2123. $method = 'insert';
  2124. $prefix = 'add';
  2125. }
  2126. if ($method && $input) {
  2127. $update = $order = array();
  2128. $i = 0;
  2129. $c = array();
  2130. foreach ($input as $k => $v) {
  2131. if ($v === '') {
  2132. $v = 'null';
  2133. }
  2134. if ($v && is_string($v) && strpos($v, 'copy.') !== false) {
  2135. $temp = str_replace('copy.', '', $v);
  2136. if (isset($input['update_' . $temp])) {
  2137. $v = $input['update_' . $temp];
  2138. }
  2139. }
  2140. if (strpos($k, '_c_') !== false) {
  2141. if ($v == 'null') {
  2142. $v = '';
  2143. }
  2144. $temp = explode('_c_', $k);
  2145. $k = str_replace('update_', $prefix . '_', $temp[0]);
  2146. if (!isset($c[$k])) {
  2147. $c[$k] = array();
  2148. }
  2149. if (strpos($temp[1], '_i_') !== false) {
  2150. $temp = explode('_i_', $temp[1]);
  2151. if (!isset($c[$k][$temp[1]])) {
  2152. $c[$k][$temp[1]] = 0;
  2153. } else {
  2154. $c[$k][$temp[1]]++;
  2155. }
  2156. $num = $c[$k][$temp[1]];
  2157. $update[$i][$k][$num][$temp[1]] = $v;
  2158. if ($temp[1] == 'order') {
  2159. $order[$i][$k][$num] = $v;
  2160. }
  2161. } else {
  2162. //$update[$i][$k][$c][$temp[1]] = $v;
  2163. }
  2164. } elseif (strpos($k, '__') !== false) {
  2165. $temp1 = explode('__', $k);
  2166. $temp2 = explode('_', $temp1[0]);
  2167. $i = $temp2[1] - 1;
  2168. if (strpos($k, 'where_id') !== false) {
  2169. //$update[$i]['where_id'] = $v;
  2170. } else {
  2171. $update[$i]['add_' . $temp1[1]] = $v;
  2172. }
  2173. } else {
  2174. $ki = str_replace('update_', $prefix . '_', $k);
  2175. if (strpos($ki, 'where_id') !== false) {
  2176. if ($method != 'insert') {
  2177. $update[$i]['where_id'] = $v;
  2178. }
  2179. } else {
  2180. if (strpos($k, '-')) {
  2181. $k = str_replace('update_', '', $k);
  2182. if (isset($config['struct'][$k]) && isset($config['struct'][$k]['sync'])) {
  2183. $s_col = $config['struct'][$k]['sync'][0];
  2184. $m_col = $config['struct'][$k]['sync'][1];
  2185. if ($s_col == 'id') {
  2186. $s_value = $this->id;
  2187. } else {
  2188. $s_one = $this->load('one', $this->id);
  2189. $s_value = $s_one[$s_col];
  2190. }
  2191. $temp = explode('-', $k);
  2192. $index = $temp[0] . '/' . $temp[1];
  2193. $update_other_set[$index]['set_' . $temp[2]] = $v;
  2194. //$update_other_set[$index]['set_' . $m_col] = $s_value;
  2195. $update_other_add[$index]['add_' . $temp[2]] = $v;
  2196. $update_other_add[$index]['add_' . $m_col] = $s_value;
  2197. $update_other_id[$index]['option_' . $m_col] = $s_value;
  2198. }
  2199. } else {
  2200. if (is_array($v)) {
  2201. $k = str_replace('update_', '', $k);
  2202. $kd = $k . '_id';
  2203. if (isset($config['struct'][$kd])) {
  2204. foreach ($v as $vk => $vt) {
  2205. if ($vt == -1) {
  2206. unset($v[$vk]);
  2207. }
  2208. }
  2209. if ($v) {
  2210. $vd = end($v);
  2211. $kdi = $prefix . '_' . $kd;
  2212. $update[$i][$kdi] = $vd;
  2213. }
  2214. }
  2215. if (isset($config['struct'][$k]) && strpos($config['struct'][$k]['type'], 'int') !== false) {
  2216. $v = end($v);
  2217. }
  2218. }
  2219. $update[$i][$ki] = $v;
  2220. }
  2221. }
  2222. }
  2223. }
  2224. //print_r($update_other_set);die;
  2225. //print_r($input);
  2226. //print_r($update_other);die;
  2227. //print_r($config['struct']);die;
  2228. # 增加复制的数量
  2229. $copy_num = Dever::input('copy_num');
  2230. if ($copy_num > 0) {
  2231. $update[0]['where_id'] = 0;
  2232. for ($i = 1; $i < $copy_num; $i++) {
  2233. $update[$i] = $update[0];
  2234. # 缓存处理
  2235. $update[$i]['rank'] = $i;
  2236. }
  2237. }
  2238. if ($this->id && !$info) {
  2239. $update[0]['add_id'] = $this->id;
  2240. }
  2241. //print_r($update);die;
  2242. foreach ($update as $k => $v) {
  2243. $method = 'insert';
  2244. $old = array();
  2245. if (isset($v['where_id']) && $v['where_id']) {
  2246. $method = 'update';
  2247. $old = $this->load('one', $v['where_id']);
  2248. }
  2249. if (isset($order[$k])) {
  2250. foreach ($order[$k] as $i => $j) {
  2251. array_multisort($j, SORT_ASC, $v[$i]);
  2252. }
  2253. }
  2254. $id = $this->load($method, $v);
  2255. if ($id) {
  2256. $struct = $this->struct_data($config['struct'], $v, $old);
  2257. Log::add($this->project, $this->table, array('method' => $method, 'input' => $struct), $this->id ? $this->id : $id);
  2258. }
  2259. if (isset($update_other_id) && $update_other_id) {
  2260. foreach ($update_other_set as $k => $v) {
  2261. $check = Dever::load($k . '-one', $update_other_id[$k]);
  2262. //print_r($check);die;
  2263. if ($check) {
  2264. $v['where_id'] = $check['id'];
  2265. Dever::load($k . '-update', $v);
  2266. } else {
  2267. Dever::load($k . '-insert', $update_other_add[$k]);
  2268. }
  2269. }
  2270. }
  2271. }
  2272. }
  2273. Dever::out('yes');
  2274. }
  2275. private function struct_data($struct, $data, $old = array())
  2276. {
  2277. $log = $tlog = array();
  2278. foreach ($struct as $i => $j) {
  2279. if (isset($j['list_name'])) {
  2280. $j['name'] = $j['list_name'];
  2281. } elseif (strpos($j['name'], '-')) {
  2282. $temp = explode('-', $j['name']);
  2283. $j['name'] = $temp[0];
  2284. }
  2285. if (isset($j['list_table'])) {
  2286. $j['list'] = $j['list_table'];
  2287. if ($j['list'] == false) {
  2288. $data[$i] = false;
  2289. }
  2290. }
  2291. $list = (isset($j['list']) && is_string($j['list']) && $j['list'] != 'table');
  2292. if (isset($data['where_' . $i]) && $data['where_' . $i]) {
  2293. $log[$j['name']] = $data['where_' . $i];
  2294. } elseif (isset($data['set_' . $i]) && $data['set_' . $i]) {
  2295. $log[$j['name']] = $data['set_' . $i];
  2296. } elseif (isset($data['add_' . $i]) && $data['add_' . $i]) {
  2297. $log[$j['name']] = $data['add_' . $i];
  2298. } elseif (isset($data[$i]) && $data[$i]) {
  2299. $log[$j['name']] = $list ? $this->value($j['list'], $data) : $data[$i];
  2300. }
  2301. if (isset($log[$j['name']])) {
  2302. $tlog[$j['name']] = $log[$j['name']];
  2303. if (is_array($tlog[$j['name']])) {
  2304. if (isset($tlog[$j['name']][0]) && is_array($tlog[$j['name']][0])) {
  2305. $tlog[$j['name']] = json_encode($tlog[$j['name']]);
  2306. if (isset($old[$i]) && is_array($old[$i])) {
  2307. $old[$i] = json_encode($old[$i]);
  2308. }
  2309. } else {
  2310. $tlog[$j['name']] = implode(',', $tlog[$j['name']]);
  2311. if (isset($old[$i]) && is_array($old[$i])) {
  2312. $old[$i] = implode(',', $old[$i]);
  2313. }
  2314. }
  2315. } 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) {
  2316. $log[$j['name']] = '<img layer-src="' . $log[$j['name']] . '" src="' . $log[$j['name']] . '" width="150">';
  2317. if (isset($old[$i]) && $old[$i]) {
  2318. $old[$i] = '<img layer-src="' . $old[$i] . '" src="' . $old[$i] . '" width="150">';
  2319. }
  2320. } elseif (is_numeric($log[$j['name']]) && (strpos($i, 'time') !== false || strpos($i, 'date') !== false)) {
  2321. $log[$j['name']] = date("Y-m-d H:i:s", $log[$j['name']]);
  2322. if (isset($old[$i]) && $old[$i]) {
  2323. $old[$i] = date("Y-m-d H:i:s", $old[$i]);
  2324. }
  2325. } elseif (isset($j['decode'])) {
  2326. $log[$j['name']] = json_decode(base64_decode($log[$j['name']]), true);
  2327. if (isset($old[$i]) && $old[$i]) {
  2328. $old[$i] = json_decode(base64_decode($old[$i]), true);
  2329. }
  2330. }
  2331. if (isset($j['option']) && !$list) {
  2332. $name = array();
  2333. $option = $this->option($j['option']);
  2334. if (is_array($option)) {
  2335. foreach ($option as $a => $b) {
  2336. if (!is_array($log[$j['name']])) {
  2337. if ($a == $log[$j['name']]) {
  2338. $name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
  2339. }
  2340. } elseif (in_array($a, $log[$j['name']])) {
  2341. $name[] = (is_array($b) && isset($b['name'])) ? $b['name'] : $b;
  2342. }
  2343. }
  2344. }
  2345. $log[$j['name']] = implode(',', $name);
  2346. }
  2347. if (isset($old[$i]) && $old[$i] != $tlog[$j['name']]) {
  2348. $log[$j['name']] = array('old' => $old[$i], 'new' => $log[$j['name']]);
  2349. }
  2350. }
  2351. }
  2352. return $log;
  2353. }
  2354. /**
  2355. * delete_action
  2356. *
  2357. * @return array
  2358. */
  2359. public function delete_action_api()
  2360. {
  2361. if (Dever::load('manage/auth.oper', 5) == false) {
  2362. Dever::alert('您没有权限');
  2363. }
  2364. $config = $this->config();
  2365. if (isset($config['manage']['delete']) && $config['manage']['delete'] === 2) {
  2366. $this->load('delete', array('where_id' => $this->id));
  2367. } elseif (isset($config['struct']['state']) && (!isset($config['manage']['delete']) || (isset($config['manage']['delete']) && $config['manage']['delete']))) {
  2368. $this->load('update', array('where_id' => $this->id, 'state' => 2));
  2369. }
  2370. return 'reloaderror';
  2371. }
  2372. /**
  2373. * recovery_action
  2374. *
  2375. * @return array
  2376. */
  2377. public function recovery_action_api()
  2378. {
  2379. if (Dever::load('manage/auth.oper', 5) == false) {
  2380. Dever::alert('您没有权限');
  2381. }
  2382. $this->load('update', array('where_id' => $this->id, 'state' => 1));
  2383. return 'reload';
  2384. }
  2385. /**
  2386. * stat 为主动统计提供相关数据
  2387. *
  2388. * @return array
  2389. */
  2390. public function stat()
  2391. {
  2392. //bundle 包依赖工具
  2393. $config = $this->config();
  2394. if (empty($config['manage']['stat'])) {
  2395. Dever::alert('该项目主动统计功能未开放');
  2396. }
  2397. $data = $config['manage'];
  2398. $data['table'] = '';
  2399. $method = $this->project . '/' . $this->table . '-all';
  2400. $gp = Dever::input('gp', 'month,day');
  2401. $col = is_string($gp) ? explode(',', $gp) : $gp;
  2402. $data['search'] = $this->stat_search($config['struct'], $gp, $config['manage']['stat'], $config['manage']['search']);
  2403. foreach ($col as $k => $v) {
  2404. $this->stat_data($data, $config['struct'], $v, $method);
  2405. }
  2406. return $data;
  2407. }
  2408. /**
  2409. * stat 为主动统计提供导出数据 后续优化
  2410. *
  2411. * @return array
  2412. */
  2413. public function stat_excel()
  2414. {
  2415. Dever::$global['base']['excel'] = 1;
  2416. //print_r($data['thead']);die;
  2417. $data = $this->stat();
  2418. if ($data && isset($data['list'])) {
  2419. $xls = new \PHPExcel();
  2420. $xls->getProperties()->setCreator("Maze Stat Excel")
  2421. ->setLastModifiedBy("Maze Stat Excel")
  2422. ->setTitle("Office 2007 XLSX Maze Document")
  2423. ->setSubject("Office 2007 XLSX Maze Document")
  2424. ->setDescription("Mazephp Stat Excel")
  2425. ->setKeywords("Mazephp Stat Excel")
  2426. ->setCategory("Mazephp Stat Excel");
  2427. $info = $this->info();
  2428. $name = $info['list_header'] . '的' . $info['stat_desc'] . '-' . $info['project_name'];
  2429. $start = 65;
  2430. $s = 0;
  2431. foreach ($data['thead'] as $k => $v) {
  2432. if ($s > 0) {
  2433. $xls->createSheet();
  2434. }
  2435. $act = $xls->setActiveSheetIndex($s);
  2436. $act->setTitle($v[0]);
  2437. $s++;
  2438. $i = 0;
  2439. foreach ($v as $kj => $vj) {
  2440. $num = 1;
  2441. $cell = $this->_getChr($start, $i);
  2442. $act->setCellValue($cell . '' . $num, $vj);
  2443. $act->getColumnDimension($cell)->setWidth(20);
  2444. $i++;
  2445. if (isset($data['list'][$k])) {
  2446. foreach ($data['list'][$k] as $ki => $vi) {
  2447. if (isset($vi[$kj])) {
  2448. $num += 1;
  2449. $act->setCellValue($cell . $num, $vi[$kj]);
  2450. }
  2451. }
  2452. }
  2453. }
  2454. }
  2455. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 垂直居中
  2456. //$act->getStyle('A1:'.$k.'20')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);// 水平居中
  2457. $xls->setActiveSheetIndex(0);
  2458. $filename = $name . '.xlsx';
  2459. header('Pragma: public');
  2460. header('Expires: 0');
  2461. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  2462. header('Content-Type:application/force-download');
  2463. header('Content-Type:application/vnd.ms-execl');
  2464. header('Content-Type:application/octet-stream');
  2465. header('Content-Type:application/download');
  2466. header("Content-Disposition:attachment;filename='" . $filename . "'");
  2467. header('Content-Transfer-Encoding:binary');
  2468. $write = \PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
  2469. $write->save('php://output');
  2470. }
  2471. }
  2472. /**
  2473. * stat_search
  2474. *
  2475. * @return mixed
  2476. */
  2477. private function stat_search($struct, $col, $stat, $search)
  2478. {
  2479. unset($struct['id']);
  2480. unset($struct['cdate']);
  2481. if ($stat) {
  2482. $stat = explode(',', $stat);
  2483. foreach ($stat as $k => $v) {
  2484. if (isset($struct[$v])) {
  2485. unset($struct[$v]);
  2486. }
  2487. }
  2488. }
  2489. $struct['day']['name'] = '天';
  2490. $struct['month']['name'] = '月';
  2491. $struct['year']['name'] = '年';
  2492. $param['update'] = 'checkbox';
  2493. $param['name'] = 'gp';
  2494. $param['value'] = $col;
  2495. $param['option'] = $struct;
  2496. $html = '<form action="' . $this->url('stat') . '" method="post" id="f1">';
  2497. $html .= '<div class="form-group">';
  2498. $html .= Html::pageLeft(array('name' => '统计项', 'col' => 'col'));
  2499. $html .= Html::radio($param);
  2500. $html .= '</div>';
  2501. $input = Dever::preInput('search_');
  2502. if (isset($search['date'])) {
  2503. foreach ($search['date'] as $k => $v) {
  2504. $html .= '<div class="form-group">';
  2505. $this->list_search_time($html, $input, 'search_', $k, $v, 'date');
  2506. $html .= '</div>';
  2507. }
  2508. $input = Dever::preInput('search_');
  2509. if ($input) {
  2510. foreach ($input as $k => $v) {
  2511. if ($v && strpos($k, 'option_') !== false) {
  2512. Dever::setInput(str_replace('search_', '', $k), $v);
  2513. }
  2514. }
  2515. }
  2516. }
  2517. if (isset($search['fulltext'])) {
  2518. $html .= '<div class="form-group">';
  2519. $this->list_search_select($html, $input, 'search_fulltext', '选择检索项', $search['fulltext']);
  2520. $this->list_search_select($html, $input, 'search_fulltext_type', '', array
  2521. (
  2522. 1 => '精确匹配',
  2523. 2 => '模糊匹配',
  2524. 3 => '大于',
  2525. 4 => '大于等于',
  2526. 5 => '小于',
  2527. 6 => '小于等于',
  2528. ), 1);
  2529. $value = array();
  2530. $value['name'] = 'search_fulltext_value';
  2531. $value['class'] = 'form-control';
  2532. if (isset($input[$value['name']])) {
  2533. $value['value'] = $input[$value['name']];
  2534. $fix = '';
  2535. if ($input['search_fulltext_type'] == 2) {
  2536. $fix = '%';
  2537. }
  2538. Dever::setInput('option_' . $input['search_fulltext'], $fix . $value['value'] . $fix);
  2539. //print_r(Dever::input('option_name'));die;
  2540. }
  2541. $html .= Html::text($value, false);
  2542. $html .= '</div>';
  2543. }
  2544. $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>';
  2545. $html .= '&nbsp;&nbsp;<a href="javascript:;" class="btn btn-warning" onclick="$(\'#f1\').attr(\'action\', \'' . $this->url('stat_excel') . '\').submit();">下载报表</a>';
  2546. $html .= '</form>';
  2547. return $html;
  2548. }
  2549. /**
  2550. * stat_data
  2551. *
  2552. * @return mixed
  2553. */
  2554. private function stat_data(&$data, $struct, $col, $method)
  2555. {
  2556. Dever::setInput('group', $col);
  2557. switch ($col) {
  2558. case 'year':
  2559. $name = '按年统计';
  2560. break;
  2561. case 'month':
  2562. $name = '按月统计';
  2563. break;
  2564. case 'day':
  2565. $name = '按天统计';
  2566. break;
  2567. default:
  2568. $name = $struct[$col]['name'];
  2569. break;
  2570. }
  2571. $list = Dever::load($method, array('group' => $col, 'col' => 'count(1) as total,id'));
  2572. $data['table'] .= '<div style="overflow-y:auto;width:100%;max-height:600px;"><table class="table table-bordered table-striped">';
  2573. $data['table'] .= '<thead><tr><th>' . $name . '</th><th>统计数字</th></tr></thead>';
  2574. $data['thead'][$col] = array($name, '统计数字');
  2575. if ($list) {
  2576. $data['table'] .= '<tbody>';
  2577. $total = 0;
  2578. foreach ($list as $k => $v) {
  2579. if (isset($v[$col])) {
  2580. if (isset($struct[$col]['option'])) {
  2581. $option = $this->option($struct[$col]['option']);
  2582. if (isset($option[$v[$col]])) {
  2583. $v[$col] = $option[$v[$col]];
  2584. if (is_array($v[$col])) {
  2585. $v[$col] = $v[$col]['name'];
  2586. }
  2587. }
  2588. }
  2589. $data['table'] .= '<tr><td width="60%" style="word-break:break-all;word-wrap:break-word;">' . $v[$col] . '</td><td>' . $v['total'] . '</td></tr>';
  2590. $total += $v['total'];
  2591. $data['list'][$col][] = array($v[$col], $v['total']);
  2592. }
  2593. }
  2594. $data['table'] .= '</tbody>';
  2595. if ($total > 0 && $k > 0) {
  2596. $data['table'] .= '<tr><td style="font-weight:bold">汇总</td><td style="font-weight:bold">' . $total . '</td></tr>';
  2597. $data['list'][$col][] = array('汇总', $total);
  2598. }
  2599. }
  2600. $data['table'] .= '</table></div>';
  2601. }
  2602. /**
  2603. * load
  2604. *
  2605. * @return mixed
  2606. */
  2607. private function load($method = 'one', $param = array())
  2608. {
  2609. return Dever::load($this->project . '/' . $this->table . '-' . $method, $param);
  2610. }
  2611. /**
  2612. * __call
  2613. *
  2614. * @return object
  2615. */
  2616. public function __call($method, $param)
  2617. {
  2618. if (strpos($method, 'html_') !== false) {
  2619. return Html::text($param);
  2620. }
  2621. return $this;
  2622. }
  2623. /**
  2624. * option
  2625. *
  2626. * @return string
  2627. */
  2628. private function option($option)
  2629. {
  2630. if (is_object($option)) {
  2631. $function = $option;
  2632. $option = $function();
  2633. }
  2634. return $option;
  2635. }
  2636. /**
  2637. * option
  2638. *
  2639. * @return string
  2640. */
  2641. public function manage()
  2642. {
  2643. $config = $this->config();
  2644. return $config;
  2645. }
  2646. /**
  2647. * valid
  2648. *
  2649. * @return string
  2650. */
  2651. public function valid()
  2652. {
  2653. //http://placehold.it/350x150
  2654. $config = $this->config();
  2655. if (isset($config['struct'])) {
  2656. $rule = array();
  2657. $rule['required']['regex'] = 'none';
  2658. $rule['required']['alertText'] = '此项不能为空';
  2659. $rule['required']['alertTextCheckboxMultiple'] = '请选择一个选项';
  2660. $rule['required']['alertTextCheckboxe'] = '请选择一个选项';
  2661. $rule['required']['alertTextDateRange'] = '日期范围不可空白';
  2662. foreach ($config['struct'] as $k => $v) {
  2663. if (isset($v['match']) && $v['match'] != 'option' && is_string($v['match']) && strpos($v['match'], '/') !== false) {
  2664. $rule[$k]['regex'] = $v['match'];
  2665. $rule[$k]['alertText'] = $v['desc'];
  2666. }
  2667. }
  2668. }
  2669. $script = '<script>';
  2670. //参考https://blog.csdn.net/wangzl1163/article/details/51071826
  2671. //http://code.ciaoca.com/jquery/validation-engine/demo/demoOnForm
  2672. $script .= '(function($){
  2673. $.fn.validationEngineLanguage = function(){
  2674. };
  2675. $.validationEngineLanguage = {
  2676. newLang: function(){
  2677. $.validationEngineLanguage.allRules = ';
  2678. //此处进行了转义过滤,其实也可以使用json_encode的第二个参数,不过只支持5.4以上版本,考虑到虚拟主机就放弃了
  2679. $script .= str_replace(array('\\\\', '\\/', '"/', '/"'), array('\\', '/', '/', '/'), json_encode($rule)) . ';';
  2680. $script .= '}
  2681. };
  2682. $.validationEngineLanguage.newLang();
  2683. })(jQuery);';
  2684. $script .= '$(document).ready(function(){
  2685. $(".form1").validationEngine("attach",{onSuccess:function(){submit();},onFailure:function(){submit();}});
  2686. });';
  2687. $script .= '</script>';
  2688. //print_r($script);die;
  2689. return $script;
  2690. }
  2691. /**
  2692. * save
  2693. *
  2694. * @return string
  2695. */
  2696. public function save()
  2697. {
  2698. $config = $this->config();
  2699. if (isset($config['manage']['save'])) {
  2700. return 'yes';
  2701. }
  2702. return 'no';
  2703. }
  2704. }