Betterform.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace addons\betterform;
  3. use app\common\library\Menu;
  4. use think\Addons;
  5. use think\Loader;
  6. /**
  7. * 插件
  8. */
  9. class Betterform extends Addons
  10. {
  11. /**
  12. * 插件安装方法
  13. * @return bool
  14. */
  15. public function install()
  16. {
  17. return true;
  18. }
  19. /**
  20. * 插件卸载方法
  21. * @return bool
  22. */
  23. public function uninstall()
  24. {
  25. return true;
  26. }
  27. /**
  28. * 插件启用方法
  29. * @return bool
  30. */
  31. public function enable()
  32. {
  33. return true;
  34. }
  35. /**
  36. * 插件禁用方法
  37. * @return bool
  38. */
  39. public function disable()
  40. {
  41. return true;
  42. }
  43. public function viewFilter(&$content)
  44. {
  45. $request = \think\Request::instance();
  46. $dispatch = $request->dispatch();
  47. if (!$dispatch) {
  48. return;
  49. }
  50. if (!$request->module() || $request->module() !== 'admin') {
  51. return;
  52. }
  53. $config = get_addon_config('betterform');
  54. //在head前引入CSS
  55. $content = preg_replace("/<\/head>/i", "<link href='/assets/addons/betterform/css/common.css' rel='stylesheet' />" . "\n\$0", $content);
  56. //如果不存在表单
  57. if (!preg_match('/<form (.*?)data-toggle="validator"/i', $content)) {
  58. return;
  59. }
  60. // 避免栈空间不足
  61. ini_set('pcre.jit', false);
  62. // 匹配<div class="form-group">标签
  63. $regex = '/<div[^>]*class\s*=\s*"[^"]*\bform-group\b[^"]*"[^>]*>(?:(?!<div[^>]*class\s*=\s*"[^"]*\bform-group\b[^"]*").)*?data-rule="[^"]*?(required|checked)[^"]*?"[^>]*>/si';
  64. $result = preg_replace_callback($regex, function ($matches) use ($config) {
  65. return str_replace("form-group", "form-group required-{$config['asteriskposition']}", $matches[0]);
  66. }, $content);
  67. $content = is_null($result) ? $content : $result;
  68. // 匹配<tr>
  69. $pattern = '/(<tr[^>]*>)\s*<td[^>]*>(.*?)<\/td>\s*<td[^>]*>.*?<input[^>]*data-rule="[^"]*required[^"]*"[^>]*>.*?<\/td>\s*<\/tr>/si';
  70. $result = preg_replace_callback($pattern, function ($matches) use ($config) {
  71. if (preg_match('/(<tr[^>]*)class\s*=\s*"[^"]*"/i', $matches[1])) {
  72. return preg_replace('/(<tr[^>]*)class\s*=\s*"([^"]*)"/i', '$1class="$2 required-' . $config['asteriskposition'] . '"', $matches[0]);
  73. } else {
  74. return str_replace("<tr", "<tr class=\"required-{$config['asteriskposition']}\"", $matches[0]);
  75. }
  76. }, $content);
  77. $content = is_null($result) ? $content : $result;
  78. }
  79. /**
  80. * @param $params
  81. */
  82. public function configInit(&$params)
  83. {
  84. $config = $this->getConfig();
  85. $config['area'] = preg_match("/\[(.*?)\]/i", $config['area']) ? array_slice(array_values((array)json_decode($config['area'], true)), 0, 2) : $config['area'];
  86. $config['shade'] = floatval($config['shade']);
  87. $config['shadeClose'] = boolval($config['shadeClose']);
  88. $params['betterform'] = $config;
  89. }
  90. }