Database.php 111 KB

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