common.php 5.0 KB

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