common.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 implode(' ', $html);
  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 implode(' ', $html);
  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. $btns = $btns ? $btns : ['refresh', 'add', 'edit', 'delete'];
  86. $btns = is_array($btns) ? $btns : explode(',', $btns);
  87. $btnAttr = [
  88. 'refresh' => ['javascript:;', 'btn btn-primary btn-refresh', 'fa fa-refresh', ''],
  89. 'add' => ['javascript:;', 'btn btn-success btn-add', 'fa fa-plus', __('Add')],
  90. 'edit' => ['javascript:;', 'btn btn-success btn-edit btn-disabled disabled', 'fa fa-pencil', __('Edit')],
  91. 'delete' => ['javascript:;', 'btn btn-danger btn-del btn-disabled disabled', 'fa fa-trash', __('Delete')],
  92. ];
  93. $btnAttr = array_merge($btnAttr, $attr);
  94. $html = [];
  95. foreach ($btns as $k => $v)
  96. {
  97. if (!isset($btnAttr[$v]))
  98. {
  99. continue;
  100. }
  101. list($href, $class, $icon, $text) = $btnAttr[$v];
  102. $html[] = '<a href="' . $href . '" class="' . $class . '" ><i class="' . $icon . '"></i> ' . $text . '</a>';
  103. }
  104. return implode(' ', $html);
  105. }
  106. /**
  107. * 生成页面Heading
  108. *
  109. * @param string $title
  110. * @param string $content
  111. * @return string
  112. */
  113. function build_heading($title = NULL, $content = NULL)
  114. {
  115. if (is_null($title) && is_null($content))
  116. {
  117. $path = request()->pathinfo();
  118. $path = $path[0] == '/' ? $path : '/' . $path;
  119. // 根据当前的URI自动匹配父节点的标题和备注
  120. $data = Db::name('auth_rule')->where('name', $path)->field('title,remark')->find();
  121. if ($data)
  122. {
  123. $title = $data['title'];
  124. $content = $data['remark'];
  125. }
  126. }
  127. if (!$content)
  128. return '';
  129. return '<div class="panel-heading"><div class="panel-lead"><em>' . $title . '</em>' . $content . '</div></div>';
  130. }
  131. /**
  132. * 判断文件或文件夹是否可写
  133. * @param string
  134. * @return bool
  135. */
  136. function is_really_writable($file)
  137. {
  138. if (DIRECTORY_SEPARATOR === '/')
  139. {
  140. return is_writable($file);
  141. }
  142. if (is_dir($file))
  143. {
  144. $file = rtrim($file, '/') . '/' . md5(mt_rand());
  145. if (($fp = @fopen($file, 'ab')) === FALSE)
  146. {
  147. return FALSE;
  148. }
  149. fclose($fp);
  150. @chmod($file, 0777);
  151. @unlink($file);
  152. return TRUE;
  153. }
  154. elseif (!is_file($file) OR ( $fp = @fopen($file, 'ab')) === FALSE)
  155. {
  156. return FALSE;
  157. }
  158. fclose($fp);
  159. return TRUE;
  160. }
  161. /**
  162. * 删除文件夹
  163. * @param string $dirname
  164. * @return boolean
  165. */
  166. function rmdirs($dirname)
  167. {
  168. if (!is_dir($dirname))
  169. return false;
  170. $files = new RecursiveIteratorIterator(
  171. new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
  172. );
  173. foreach ($files as $fileinfo)
  174. {
  175. $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
  176. $todo($fileinfo->getRealPath());
  177. }
  178. @rmdir($dirname);
  179. return true;
  180. }
  181. /**
  182. * 复制文件夹
  183. * @param string $source 源文件夹
  184. * @param string $dest 目标文件夹
  185. */
  186. function copydirs($source, $dest)
  187. {
  188. if (!is_dir($dest))
  189. {
  190. mkdir($dest, 0755);
  191. }
  192. foreach (
  193. $iterator = new RecursiveIteratorIterator(
  194. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item
  195. )
  196. {
  197. if ($item->isDir())
  198. {
  199. $sontDir = $dest . DS . $iterator->getSubPathName();
  200. if (!is_dir($sontDir))
  201. {
  202. mkdir($sontDir);
  203. }
  204. }
  205. else
  206. {
  207. copy($item, $dest . DS . $iterator->getSubPathName());
  208. }
  209. }
  210. }