common.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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'];
  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', ''],
  96. 'add' => ['javascript:;', 'btn btn-success btn-add', 'fa fa-plus', __('Add')],
  97. 'edit' => ['javascript:;', 'btn btn-success btn-edit btn-disabled disabled', 'fa fa-pencil', __('Edit')],
  98. 'del' => ['javascript:;', 'btn btn-danger btn-del btn-disabled disabled', 'fa fa-trash', __('Delete')],
  99. ];
  100. $btnAttr = array_merge($btnAttr, $attr);
  101. $html = [];
  102. foreach ($btns as $k => $v)
  103. {
  104. //如果未定义或没有权限
  105. if (!isset($btnAttr[$v]) || ($v !== 'refresh' && !$auth->check("{$controller}/{$v}")))
  106. {
  107. continue;
  108. }
  109. list($href, $class, $icon, $text) = $btnAttr[$v];
  110. $html[] = '<a href="' . $href . '" class="' . $class . '" ><i class="' . $icon . '"></i> ' . $text . '</a>';
  111. }
  112. return implode(' ', $html);
  113. }
  114. /**
  115. * 生成页面Heading
  116. *
  117. * @param string $title
  118. * @param string $content
  119. * @return string
  120. */
  121. function build_heading($title = NULL, $content = NULL)
  122. {
  123. if (is_null($title) && is_null($content))
  124. {
  125. $path = request()->pathinfo();
  126. $path = $path[0] == '/' ? $path : '/' . $path;
  127. // 根据当前的URI自动匹配父节点的标题和备注
  128. $data = Db::name('auth_rule')->where('name', $path)->field('title,remark')->find();
  129. if ($data)
  130. {
  131. $title = $data['title'];
  132. $content = $data['remark'];
  133. }
  134. }
  135. if (!$content)
  136. return '';
  137. return '<div class="panel-heading"><div class="panel-lead"><em>' . $title . '</em>' . $content . '</div></div>';
  138. }