ListBuilder.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. namespace Common\Builder;
  3. use Common\Controller\ControllerController;
  4. /**
  5. * 数据列表自动生成器
  6. *
  7. */
  8. class ListBuilder extends ControllerController
  9. {
  10. private $_meta_title; // 页面标题
  11. private $_top_button_list = array(); // 顶部工具栏按钮组
  12. private $_search = array(); // 搜索参数配置
  13. private $_tab_nav = array(); // 页面Tab导航
  14. private $_table_column_list = array(); // 表格标题字段
  15. private $_table_data_list = array(); // 表格数据列表
  16. private $_table_data_list_key = 'id'; // 表格数据列表主键字段名
  17. private $_table_data_page; // 表格数据分页
  18. private $_right_button_list = array(); // 表格右侧操作按钮组
  19. private $_alter_data_list = array(); // 表格数据列表重新修改的项目
  20. private $_extra_html; // 额外功能代码
  21. private $_template; // 模版
  22. /**
  23. * 初始化方法
  24. * @return $this
  25. *
  26. */
  27. protected function _initialize()
  28. {
  29. $this->_template = APP_PATH . 'Common/Builder/Layout/' . MODULE_MARK . '/list.html';
  30. }
  31. /**
  32. * 设置页面标题
  33. * @param $title 标题文本
  34. * @return $this
  35. *
  36. */
  37. public function setMetaTitle($meta_title)
  38. {
  39. $this->_meta_title = $meta_title;
  40. return $this;
  41. }
  42. /**
  43. * 加入一个列表顶部工具栏按钮
  44. * 在使用预置的几种按钮时,比如我想改变新增按钮的名称
  45. * 那么只需要$builder->addTopButton('add', array('title' => '换个马甲'))
  46. * 如果想改变地址甚至新增一个属性用上面类似的定义方法
  47. * @param string $type 按钮类型,主要有add/resume/forbid/recycle/restore/delete/self七几种取值
  48. * @param array $attr 按钮属性,一个定了标题/链接/CSS类名等的属性描述数组
  49. * @return $this
  50. *
  51. */
  52. public function addTopButton($type, $attribute = null)
  53. {
  54. switch ($type) {
  55. case 'addnew': // 添加新增按钮
  56. // 预定义按钮属性以简化使用
  57. $my_attribute['title'] = '新增';
  58. $my_attribute['class'] = 'btn btn-primary-outline btn-pill';
  59. $my_attribute['href'] = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/add');
  60. /**
  61. * 如果定义了属性数组则与默认的进行合并
  62. * 用户定义的同名数组元素会覆盖默认的值
  63. * 比如$builder->addTopButton('add', array('title' => '换个马甲'))
  64. * '换个马甲'这个碧池就会使用山东龙潭寺的十二路谭腿第十一式“风摆荷叶腿”
  65. * 把'新增'踢走自己霸占title这个位置,其它的属性同样道理
  66. */
  67. if ($attribute && is_array($attribute)) {
  68. $my_attribute = array_merge($my_attribute, $attribute);
  69. }
  70. // 这个按钮定义好了把它丢进按钮池里
  71. $this->_top_button_list[] = $my_attribute;
  72. break;
  73. case 'resume': // 添加启用按钮(禁用的反操作)
  74. //预定义按钮属性以简化使用
  75. $my_attribute['title'] = '启用';
  76. $my_attribute['target-form'] = 'ids';
  77. $my_attribute['class'] = 'btn btn-success-outline btn-pill ajax-post confirm';
  78. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME; // 要操作的数据模型
  79. $my_attribute['href'] = U(
  80. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  81. array(
  82. 'status' => 'resume',
  83. 'model' => $my_attribute['model'],
  84. )
  85. );
  86. // 如果定义了属性数组则与默认的进行合并,详细使用方法参考上面的新增按钮
  87. if ($attribute && is_array($attribute)) {
  88. $my_attribute = array_merge($my_attribute, $attribute);
  89. }
  90. // 这个按钮定义好了把它丢进按钮池里
  91. $this->_top_button_list[] = $my_attribute;
  92. break;
  93. case 'forbid': // 添加禁用按钮(启用的反操作)
  94. // 预定义按钮属性以简化使用
  95. $my_attribute['title'] = '禁用';
  96. $my_attribute['target-form'] = 'ids';
  97. $my_attribute['class'] = 'btn btn-warning-outline btn-pill ajax-post confirm';
  98. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  99. $my_attribute['href'] = U(
  100. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  101. array(
  102. 'status' => 'forbid',
  103. 'model' => $my_attribute['model'],
  104. )
  105. );
  106. // 如果定义了属性数组则与默认的进行合并,详细使用方法参考上面的新增按钮
  107. if ($attribute && is_array($attribute)) {
  108. $my_attribute = array_merge($my_attribute, $attribute);
  109. }
  110. //这个按钮定义好了把它丢进按钮池里
  111. $this->_top_button_list[] = $my_attribute;
  112. break;
  113. case 'recycle': // 添加回收按钮(还原的反操作)
  114. // 预定义按钮属性以简化使用
  115. $my_attribute['title'] = '回收';
  116. $my_attribute['target-form'] = 'ids';
  117. $my_attribute['class'] = 'btn btn-danger-outline btn-pill ajax-post confirm';
  118. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  119. $my_attribute['href'] = U(
  120. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  121. array(
  122. 'status' => 'recycle',
  123. 'model' => $my_attribute['model'],
  124. )
  125. );
  126. // 如果定义了属性数组则与默认的进行合并,详细使用方法参考上面的新增按钮
  127. if ($attribute && is_array($attribute)) {
  128. $my_attribute = array_merge($my_attribute, $attribute);
  129. }
  130. // 这个按钮定义好了把它丢进按钮池里
  131. $this->_top_button_list[] = $my_attribute;
  132. break;
  133. case 'restore': // 添加还原按钮(回收的反操作)
  134. // 预定义按钮属性以简化使用
  135. $my_attribute['title'] = '还原';
  136. $my_attribute['target-form'] = 'ids';
  137. $my_attribute['class'] = 'btn btn-success-outline btn-pill ajax-post confirm';
  138. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  139. $my_attribute['href'] = U(
  140. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  141. array(
  142. 'status' => 'restore',
  143. 'model' => $my_attribute['model'],
  144. )
  145. );
  146. // 如果定义了属性数组则与默认的进行合并,详细使用方法参考上面的新增按钮
  147. if ($attribute && is_array($attribute)) {
  148. $my_attribute = array_merge($my_attribute, $attribute);
  149. }
  150. // 这个按钮定义好了把它丢进按钮池里
  151. $this->_top_button_list[] = $my_attribute;
  152. break;
  153. case 'delete': // 添加删除按钮(我没有反操作,删除了就没有了,就真的找不回来了)
  154. // 预定义按钮属性以简化使用
  155. $my_attribute['title'] = '删除';
  156. $my_attribute['target-form'] = 'ids';
  157. $my_attribute['class'] = 'btn btn-danger-outline btn-pill ajax-post confirm';
  158. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  159. $my_attribute['href'] = U(
  160. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  161. array(
  162. 'status' => 'delete',
  163. 'model' => $my_attribute['model'],
  164. )
  165. );
  166. // 如果定义了属性数组则与默认的进行合并,详细使用方法参考上面的新增按钮
  167. if ($attribute && is_array($attribute)) {
  168. $my_attribute = array_merge($my_attribute, $attribute);
  169. }
  170. // 这个按钮定义好了把它丢进按钮池里
  171. $this->_top_button_list[] = $my_attribute;
  172. break;
  173. case 'self': //添加自定义按钮(第一原则使用上面预设的按钮,如果有特殊需求不能满足则使用此自定义按钮方法)
  174. // 预定义按钮属性以简化使用
  175. $my_attribute['target-form'] = 'ids';
  176. $my_attribute['class'] = 'btn btn-danger-outline btn-pill';
  177. // 如果定义了属性数组则与默认的进行合并
  178. if ($attribute && is_array($attribute)) {
  179. $my_attribute = array_merge($my_attribute, $attribute);
  180. } else {
  181. $my_attribute['title'] = '该自定义按钮未配置属性';
  182. }
  183. // 这个按钮定义好了把它丢进按钮池里
  184. $this->_top_button_list[] = $my_attribute;
  185. break;
  186. }
  187. return $this;
  188. }
  189. /**
  190. * 设置搜索参数
  191. * @param $title
  192. * @param $url
  193. * @return $this
  194. *
  195. */
  196. public function setSearch($title, $url)
  197. {
  198. $this->_search = array('title' => $title, 'url' => $url);
  199. return $this;
  200. }
  201. /**
  202. * 设置Tab按钮列表
  203. * @param $tab_list Tab列表 array(
  204. * 'title' => '标题',
  205. * 'href' => 'http://www.corethink.cn'
  206. * )
  207. * @param $current_tab 当前tab
  208. * @return $this
  209. *
  210. */
  211. public function setTabNav($tab_list, $current_tab)
  212. {
  213. $this->_tab_nav = array(
  214. 'tab_list' => $tab_list,
  215. 'current_tab' => $current_tab,
  216. );
  217. return $this;
  218. }
  219. /**
  220. * 加一个表格标题字段
  221. *
  222. */
  223. public function addTableColumn($name, $title, $type = null, $param = null)
  224. {
  225. $column = array(
  226. 'name' => $name,
  227. 'title' => $title,
  228. 'type' => $type,
  229. 'param' => $param,
  230. );
  231. $this->_table_column_list[] = $column;
  232. return $this;
  233. }
  234. /**
  235. * 表格数据列表
  236. *
  237. */
  238. public function setTableDataList($table_data_list)
  239. {
  240. $this->_table_data_list = $table_data_list;
  241. return $this;
  242. }
  243. /**
  244. * 表格数据列表的主键名称
  245. *
  246. */
  247. public function setTableDataListKey($table_data_list_key)
  248. {
  249. $this->_table_data_list_key = $table_data_list_key;
  250. return $this;
  251. }
  252. /**
  253. * 加入一个数据列表右侧按钮
  254. * 在使用预置的几种按钮时,比如我想改变编辑按钮的名称
  255. * 那么只需要$builder->addRightpButton('edit', array('title' => '换个马甲'))
  256. * 如果想改变地址甚至新增一个属性用上面类似的定义方法
  257. * 因为添加右侧按钮的时候你并没有办法知道数据ID,于是我们采用__data_id__作为约定的标记
  258. * __data_id__会在display方法里自动替换成数据的真实ID
  259. * @param string $type 按钮类型,edit/forbid/recycle/restore/delete/self六种取值
  260. * @param array $attr 按钮属性,一个定了标题/链接/CSS类名等的属性描述数组
  261. * @return $this
  262. *
  263. */
  264. public function addRightButton($type, $attribute = null)
  265. {
  266. switch ($type) {
  267. case 'edit': // 编辑按钮
  268. // 预定义按钮属性以简化使用
  269. $my_attribute['name'] = 'edit';
  270. $my_attribute['title'] = '编辑';
  271. $my_attribute['class'] = 'label label-primary-outline label-pill';
  272. $my_attribute['href'] = U(
  273. MODULE_NAME . '/' . CONTROLLER_NAME . '/edit',
  274. array($this->_table_data_list_key => '__data_id__')
  275. );
  276. /**
  277. * 如果定义了属性数组则与默认的进行合并
  278. * 用户定义的同名数组元素会覆盖默认的值
  279. * 比如$builder->addRightButton('edit', array('title' => '换个马甲'))
  280. * '换个马甲'这个碧池就会使用山东龙潭寺的十二路谭腿第十一式“风摆荷叶腿”
  281. * 把'新增'踢走自己霸占title这个位置,其它的属性同样道理
  282. */
  283. if ($attribute && is_array($attribute)) {
  284. $my_attribute = array_merge($my_attribute, $attribute);
  285. }
  286. // 这个按钮定义好了把它丢进按钮池里
  287. $this->_right_button_list[] = $my_attribute;
  288. break;
  289. case 'forbid': // 改变记录状态按钮,会更具数据当前的状态自动选择应该显示启用/禁用
  290. //预定义按钮属
  291. $my_attribute['type'] = 'forbid';
  292. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  293. $my_attribute['forbid0']['name'] = 'forbid';
  294. $my_attribute['forbid0']['title'] = '启用';
  295. $my_attribute['forbid0']['class'] = 'label label-success-outline label-pill ajax-get confirm';
  296. $my_attribute['forbid0']['href'] = U(
  297. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  298. array(
  299. 'status' => 'resume',
  300. 'ids' => '__data_id__',
  301. 'model' => $my_attribute['model'],
  302. )
  303. );
  304. $my_attribute['forbid1']['name'] = 'forbid';
  305. $my_attribute['forbid1']['title'] = '禁用';
  306. $my_attribute['forbid1']['class'] = 'label label-warning-outline label-pill ajax-get confirm';
  307. $my_attribute['forbid1']['href'] = U(
  308. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  309. array(
  310. 'status' => 'forbid',
  311. 'ids' => '__data_id__',
  312. 'model' => $my_attribute['model'],
  313. )
  314. );
  315. /**
  316. * 如果定义了属性数组则与默认的进行合并
  317. * 用户定义的同名数组元素会覆盖默认的值
  318. * 比如$builder->addRightButton('edit', array('title' => '换个马甲'))
  319. * '换个马甲'这个碧池就会使用山东龙潭寺的十二路谭腿第十一式“风摆荷叶腿”
  320. * 把'新增'踢走自己霸占title这个位置,其它的属性同样道理
  321. */
  322. if ($attribute['forbid0'] && is_array($attribute['forbid0'])) {
  323. $my_attribute['forbid0'] = array_merge($my_attribute['forbid0'], $attribute['forbid0']);
  324. }
  325. if ($attribute['forbid1'] && is_array($attribute['forbid1'])) {
  326. $my_attribute['forbid1'] = array_merge($my_attribute['forbid1'], $attribute['forbid1']);
  327. }
  328. // 这个按钮定义好了把它丢进按钮池里
  329. $this->_right_button_list[] = $my_attribute;
  330. break;
  331. case 'recycle':
  332. //预定义按钮属
  333. $my_attribute['type'] = 'recycle';
  334. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  335. $my_attribute['recycle1']['name'] = 'recycle';
  336. $my_attribute['recycle1']['title'] = '回收';
  337. $my_attribute['recycle1']['class'] = 'label label-danger-outline label-pill ajax-get confirm';
  338. $my_attribute['recycle1']['href'] = U(
  339. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  340. array(
  341. 'status' => 'recycle',
  342. 'ids' => '__data_id__',
  343. 'model' => $my_attribute['model'],
  344. )
  345. );
  346. $my_attribute['recycle-1']['name'] = 'restore';
  347. $my_attribute['recycle-1']['title'] = '还原';
  348. $my_attribute['recycle-1']['class'] = 'label label-success-outline label-pill ajax-get confirm ';
  349. $my_attribute['recycle-1']['href'] = U(
  350. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  351. array(
  352. 'status' => 'restore',
  353. 'ids' => '__data_id__',
  354. 'model' => $my_attribute['model'],
  355. )
  356. );
  357. /**
  358. * 如果定义了属性数组则与默认的进行合并
  359. * 用户定义的同名数组元素会覆盖默认的值
  360. * 比如$builder->addRightButton('edit', array('title' => '换个马甲'))
  361. * '换个马甲'这个碧池就会使用山东龙潭寺的十二路谭腿第十一式“风摆荷叶腿”
  362. * 把'新增'踢走自己霸占title这个位置,其它的属性同样道理
  363. */
  364. if ($attribute['recycle-1'] && is_array($attribute['recycle-1'])) {
  365. $my_attribute['recycle-1'] = array_merge($my_attribute['recycle-1'], $attribute['recycle-1']);
  366. }
  367. if ($attribute['recycle1'] && is_array($attribute['recycle1'])) {
  368. $my_attribute['recycle1'] = array_merge($my_attribute['recycle1'], $attribute['recycle1']);
  369. }
  370. // 这个按钮定义好了把它丢进按钮池里
  371. $this->_right_button_list[] = $my_attribute;
  372. break;
  373. case 'restore':
  374. // 预定义按钮属性以简化使用
  375. $my_attribute['name'] = 'restore';
  376. $my_attribute['title'] = '还原';
  377. $my_attribute['class'] = 'label label-success-outline label-pill ajax-get confirm';
  378. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  379. $my_attribute['href'] = U(
  380. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  381. array(
  382. 'status' => 'restore',
  383. 'ids' => '__data_id__',
  384. 'model' => $my_attribute['model'],
  385. )
  386. );
  387. // 如果定义了属性数组则与默认的进行合并,详细使用方法参考上面的顶部按钮
  388. if ($attribute && is_array($attribute)) {
  389. $my_attribute = array_merge($my_attribute, $attribute);
  390. }
  391. // 这个按钮定义好了把它丢进按钮池里
  392. $this->_right_button_list[] = $my_attribute;
  393. break;
  394. case 'delete':
  395. // 预定义按钮属性以简化使用
  396. $my_attribute['name'] = 'delete';
  397. $my_attribute['title'] = '删除';
  398. $my_attribute['class'] = 'label label-danger-outline label-pill ajax-get confirm';
  399. $my_attribute['model'] = $attribute['model'] ?: CONTROLLER_NAME;
  400. $my_attribute['href'] = U(
  401. MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',
  402. array(
  403. 'status' => 'delete',
  404. 'ids' => '__data_id__',
  405. 'model' => $my_attribute['model'],
  406. )
  407. );
  408. // 如果定义了属性数组则与默认的进行合并,详细使用方法参考上面的顶部按钮
  409. if ($attribute && is_array($attribute)) {
  410. $my_attribute = array_merge($my_attribute, $attribute);
  411. }
  412. // 这个按钮定义好了把它丢进按钮池里
  413. $this->_right_button_list[] = $my_attribute;
  414. break;
  415. case 'self':
  416. // 预定义按钮属性以简化使用
  417. $my_attribute['name'] = 'self';
  418. $my_attribute['class'] = 'label label-default';
  419. // 如果定义了属性数组则与默认的进行合并
  420. if ($attribute && is_array($attribute)) {
  421. $my_attribute = array_merge($my_attribute, $attribute);
  422. } else {
  423. $my_attribute['title'] = '该自定义按钮未配置属性';
  424. }
  425. // 这个按钮定义好了把它丢进按钮池里
  426. $this->_right_button_list[] = $my_attribute;
  427. break;
  428. }
  429. return $this;
  430. }
  431. /**
  432. * 设置分页
  433. * @param $page
  434. * @return $this
  435. *
  436. */
  437. public function setTableDataPage($table_data_page)
  438. {
  439. $this->_table_data_page = $table_data_page;
  440. return $this;
  441. }
  442. /**
  443. * 修改列表数据
  444. * 有时候列表数据需要在最终输出前做一次小的修改
  445. * 比如管理员列表ID为1的超级管理员右侧编辑按钮不显示删除
  446. * @param $page
  447. * @return $this
  448. *
  449. */
  450. public function alterTableData($condition, $alter_data)
  451. {
  452. $this->_alter_data_list[] = array(
  453. 'condition' => $condition,
  454. 'alter_data' => $alter_data,
  455. );
  456. return $this;
  457. }
  458. /**
  459. * 设置额外功能代码
  460. * @param $extra_html 额外功能代码
  461. * @return $this
  462. *
  463. */
  464. public function setExtraHtml($extra_html)
  465. {
  466. $this->_extra_html = $extra_html;
  467. return $this;
  468. }
  469. /**
  470. * 设置页面模版
  471. * @param $template 模版
  472. * @return $this
  473. *
  474. */
  475. public function setTemplate($template)
  476. {
  477. $this->_template = $template;
  478. return $this;
  479. }
  480. /**
  481. * 显示页面
  482. *
  483. */
  484. public function display()
  485. {
  486. // 编译data_list中的值
  487. foreach ($this->_table_data_list as &$data) {
  488. // 编译表格右侧按钮
  489. if ($this->_right_button_list) {
  490. foreach ($this->_right_button_list as $right_button) {
  491. // 禁用按钮与隐藏比较特殊,它需要根据数据当前状态判断是显示禁用还是启用
  492. if ($right_button['type'] === 'forbid') {
  493. $right_button = $right_button['forbid' . $data['status']];
  494. }
  495. if ($right_button['type'] === 'recycle') {
  496. if ($data['status'] === '0' || $data['status'] === '1') {
  497. $right_button = $right_button['recycle1'];
  498. } else {
  499. $right_button = $right_button['recycle' . $data['status']];
  500. }
  501. }
  502. // 将约定的标记__data_id__替换成真实的数据ID
  503. $right_button['href'] = preg_replace(
  504. '/__data_id__/i',
  505. $data[$this->_table_data_list_key],
  506. $right_button['href']
  507. );
  508. // 编译按钮属性
  509. $right_button['attribute'] = $this->compileHtmlAttr($right_button);
  510. $data['right_button'][$right_button['name']] = $right_button;
  511. }
  512. }
  513. /**
  514. * 修改列表数据
  515. * 有时候列表数据需要在最终输出前做一次小的修改
  516. * 比如管理员列表ID为1的超级管理员右侧编辑按钮不显示删除
  517. */
  518. if ($this->_alter_data_list) {
  519. foreach ($this->_alter_data_list as $alter) {
  520. if ($data[$alter['condition']['key']] === $alter['condition']['value']) {
  521. if ($alter['alter_data']['right_button']) {
  522. foreach ($alter['alter_data']['right_button'] as &$val) {
  523. if (!$val['attribute']) {
  524. $val['href'] = preg_replace(
  525. '/__data_id__/i',
  526. $data[$this->_table_data_list_key],
  527. $val['href']
  528. );
  529. $val['attribute'] = $this->compileHtmlAttr($val); // 编译按钮属性
  530. }
  531. }
  532. }
  533. $data = array_merge($data, $alter['alter_data']);
  534. }
  535. }
  536. }
  537. // 根据表格标题字段指定类型编译列表数据
  538. foreach ($this->_table_column_list as &$column) {
  539. switch ($column['type']) {
  540. case 'status':
  541. switch ($data[$column['name']]) {
  542. case '-1':
  543. $data[$column['name']] = '<i class="fa fa-trash text-danger"></i>';
  544. break;
  545. case '0':
  546. $data[$column['name']] = '<i class="fa fa-ban text-danger"></i>';
  547. break;
  548. case '1':
  549. $data[$column['name']] = '<i class="fa fa-check text-success"></i>';
  550. break;
  551. }
  552. break;
  553. case 'byte':
  554. $data[$column['name']] = $this->formatBytes($data[$column['name']]);
  555. break;
  556. case 'icon':
  557. $data[$column['name']] = '<i class="fa ' . $data[$column['name']] . '"></i>';
  558. break;
  559. case 'date':
  560. $data[$column['name']] = time_format($data[$column['name']], 'Y-m-d');
  561. break;
  562. case 'datetime':
  563. $data[$column['name']] = time_format($data[$column['name']]);
  564. break;
  565. case 'time':
  566. $data[$column['name']] = time_format($data[$column['name']]);
  567. break;
  568. case 'avatar':
  569. $data[$column['name']] = '<img style="width:40px;height:40px;" src="' . get_cover($data[$column['name']]) . '">';
  570. break;
  571. case 'picture':
  572. $data[$column['name']] = '<img class="picture" src="' . get_cover($data[$column['name']]) . '">';
  573. break;
  574. case 'pictures':
  575. if (!is_array($data[$column['name']])) {
  576. $temp = explode(',', $data[$column['name']]);
  577. }
  578. $data[$column['name']] = '<img class="picture" src="' . get_cover($temp[0]) . '">';
  579. break;
  580. case 'type':
  581. $form_item_type = C('FORM_ITEM_TYPE');
  582. $data[$column['name']] = $form_item_type[$data[$column['name']]][0];
  583. break;
  584. case 'callback': // 调用函数
  585. if (is_array($column['param'])) {
  586. $data[$column['name']] = call_user_func_array($column['param'], array($data[$column['name']]));
  587. } else {
  588. $data[$column['name']] = call_user_func($column['param'], $data[$column['name']]);
  589. }
  590. break;
  591. }
  592. if (is_array($data[$column['name']]) && $column['name'] !== 'right_button') {
  593. $data[$column['name']] = implode(',', $data[$column['name']]);
  594. }
  595. }
  596. }
  597. //编译top_button_list中的HTML属性
  598. if ($this->_top_button_list) {
  599. foreach ($this->_top_button_list as &$button) {
  600. $button['attribute'] = $this->compileHtmlAttr($button);
  601. }
  602. }
  603. $this->assign('meta_title', $this->_meta_title); // 页面标题
  604. $this->assign('top_button_list', $this->_top_button_list); // 顶部工具栏按钮
  605. $this->assign('search', $this->_search); // 搜索配置
  606. $this->assign('tab_nav', $this->_tab_nav); // 页面Tab导航
  607. $this->assign('table_column_list', $this->_table_column_list); // 表格的列
  608. $this->assign('table_data_list', $this->_table_data_list); // 表格数据
  609. $this->assign('table_data_list_key', $this->_table_data_list_key); // 表格数据主键字段名称
  610. $this->assign('table_data_page', $this->_table_data_page); // 表示个数据分页
  611. $this->assign('right_button_list', $this->_right_button_list); // 表格右侧操作按钮
  612. $this->assign('alter_data_list', $this->_alter_data_list); // 表格数据列表重新修改的项目
  613. $this->assign('extra_html', $this->_extra_html); //是否ajax提交
  614. // 显示页面
  615. $template = CONTROLLER_NAME . '/' . ACTION_NAME;
  616. if (is_file($this->view->parseTemplate($template))) {
  617. parent::display();
  618. } else {
  619. $this->assign('is_builder', 'list'); // Builder标记
  620. parent::display($this->_template);
  621. }
  622. }
  623. //编译HTML属性
  624. protected function compileHtmlAttr($attr)
  625. {
  626. $result = array();
  627. foreach ($attr as $key => $value) {
  628. $value = htmlspecialchars($value);
  629. $result[] = "$key=\"$value\"";
  630. }
  631. $result = implode(' ', $result);
  632. return $result;
  633. }
  634. /**
  635. * 格式化字节大小
  636. * @param number $size 字节数
  637. * @param string $delimiter 数字和单位分隔符
  638. * @return string 格式化后的带单位的大小
  639. */
  640. protected function formatBytes($size, $delimiter = '')
  641. {
  642. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  643. for ($i = 0; $size >= 1024 && $i < 5; $i++) {
  644. $size /= 1024;
  645. }
  646. return round($size, 2) . $delimiter . $units[$i];
  647. }
  648. }