common.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. use app\common\model\Category;
  3. use fast\Form;
  4. use fast\Tree;
  5. use think\Db;
  6. if (!function_exists('build_select')) {
  7. /**
  8. * 生成下拉列表
  9. * @param string $name
  10. * @param mixed $options
  11. * @param mixed $selected
  12. * @param mixed $attr
  13. * @return string
  14. */
  15. function build_select($name, $options, $selected = [], $attr = [])
  16. {
  17. $options = is_array($options) ? $options : explode(',', $options);
  18. $selected = is_array($selected) ? $selected : explode(',', $selected);
  19. return Form::select($name, $options, $selected, $attr);
  20. }
  21. }
  22. if (!function_exists('build_radios')) {
  23. /**
  24. * 生成单选按钮组
  25. * @param string $name
  26. * @param array $list
  27. * @param mixed $selected
  28. * @return string
  29. */
  30. function build_radios($name, $list = [], $selected = null)
  31. {
  32. $html = [];
  33. $selected = is_null($selected) ? key($list) : $selected;
  34. $selected = is_array($selected) ? $selected : explode(',', $selected);
  35. foreach ($list as $k => $v) {
  36. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::radio($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
  37. }
  38. return '<div class="radio">' . implode(' ', $html) . '</div>';
  39. }
  40. }
  41. if (!function_exists('build_checkboxs')) {
  42. /**
  43. * 生成复选按钮组
  44. * @param string $name
  45. * @param array $list
  46. * @param mixed $selected
  47. * @return string
  48. */
  49. function build_checkboxs($name, $list = [], $selected = null)
  50. {
  51. $html = [];
  52. $selected = is_null($selected) ? [] : $selected;
  53. $selected = is_array($selected) ? $selected : explode(',', $selected);
  54. foreach ($list as $k => $v) {
  55. $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::checkbox($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
  56. }
  57. return '<div class="checkbox">' . implode(' ', $html) . '</div>';
  58. }
  59. }
  60. if (!function_exists('build_category_select')) {
  61. /**
  62. * 生成分类下拉列表框
  63. * @param string $name
  64. * @param string $type
  65. * @param mixed $selected
  66. * @param array $attr
  67. * @return string
  68. */
  69. function build_category_select($name, $type, $selected = null, $attr = [], $header = [])
  70. {
  71. $tree = Tree::instance();
  72. $tree->init(Category::getCategoryArray($type), 'pid');
  73. $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  74. $categorydata = $header ? $header : [];
  75. foreach ($categorylist as $k => $v) {
  76. $categorydata[$v['id']] = $v['name'];
  77. }
  78. $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);
  79. return build_select($name, $categorydata, $selected, $attr);
  80. }
  81. }
  82. if (!function_exists('build_toolbar')) {
  83. /**
  84. * 生成表格操作按钮栏
  85. * @param array $btns 按钮组
  86. * @param array $attr 按钮属性值
  87. * @return string
  88. */
  89. function build_toolbar($btns = NULL, $attr = [])
  90. {
  91. $auth = \app\admin\library\Auth::instance();
  92. $controller = str_replace('.', '/', strtolower(think\Request::instance()->controller()));
  93. $btns = $btns ? $btns : ['refresh', 'add', 'edit', 'del', 'import'];
  94. $btns = is_array($btns) ? $btns : explode(',', $btns);
  95. $index = array_search('delete', $btns);
  96. if ($index !== FALSE) {
  97. $btns[$index] = 'del';
  98. }
  99. $btnAttr = [
  100. 'refresh' => ['javascript:;', 'btn btn-primary btn-refresh', 'fa fa-refresh', '', __('Refresh')],
  101. 'add' => ['javascript:;', 'btn btn-success btn-add', 'fa fa-plus', __('Add'), __('Add')],
  102. 'edit' => ['javascript:;', 'btn btn-success btn-edit btn-disabled disabled', 'fa fa-pencil', __('Edit'), __('Edit')],
  103. 'del' => ['javascript:;', 'btn btn-danger btn-del btn-disabled disabled', 'fa fa-trash', __('Delete'), __('Delete')],
  104. 'import' => ['javascript:;', 'btn btn-danger btn-import', 'fa fa-upload', __('Import'), __('Import')],
  105. ];
  106. $btnAttr = array_merge($btnAttr, $attr);
  107. $html = [];
  108. foreach ($btns as $k => $v) {
  109. //如果未定义或没有权限
  110. if (!isset($btnAttr[$v]) || ($v !== 'refresh' && !$auth->check("{$controller}/{$v}"))) {
  111. continue;
  112. }
  113. list($href, $class, $icon, $text, $title) = $btnAttr[$v];
  114. $extend = $v == 'import' ? 'id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"' : '';
  115. $html[] = '<a href="' . $href . '" class="' . $class . '" title="' . $title . '" ' . $extend . '><i class="' . $icon . '"></i> ' . $text . '</a>';
  116. }
  117. return implode(' ', $html);
  118. }
  119. }
  120. if (!function_exists('build_heading')) {
  121. /**
  122. * 生成页面Heading
  123. *
  124. * @param string $path 指定的path
  125. * @return string
  126. */
  127. function build_heading($path = NULL, $container = TRUE)
  128. {
  129. $title = $content = '';
  130. if (is_null($path)) {
  131. $action = request()->action();
  132. $controller = str_replace('.', '/', request()->controller());
  133. $path = strtolower($controller . ($action && $action != 'index' ? '/' . $action : ''));
  134. }
  135. // 根据当前的URI自动匹配父节点的标题和备注
  136. $data = Db::name('auth_rule')->where('name', $path)->field('title,remark')->find();
  137. if ($data) {
  138. $title = __($data['title']);
  139. $content = __($data['remark']);
  140. }
  141. if (!$content)
  142. return '';
  143. $result = '<div class="panel-lead"><em>' . $title . '</em>' . $content . '</div>';
  144. if ($container) {
  145. $result = '<div class="panel-heading">' . $result . '</div>';
  146. }
  147. return $result;
  148. }
  149. }