Cms.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace addons\cms;
  3. use addons\cms\library\FulltextSearch;
  4. use addons\cms\library\Service;
  5. use addons\cms\model\Archives;
  6. use addons\cms\model\Modelx;
  7. use app\common\library\Menu;
  8. use think\Addons;
  9. use think\Config;
  10. use think\Db;
  11. use think\Request;
  12. /**
  13. * CMS插件
  14. */
  15. class Cms extends Addons
  16. {
  17. /**
  18. * 插件安装方法
  19. * @return bool
  20. */
  21. public function install()
  22. {
  23. $menu = include ADDON_PATH . 'cms' . DS . 'data' . DS . 'menu.php';
  24. Menu::create($menu);
  25. //首次安装创建表并导入测试数据
  26. \think\addons\Service::importsql('cms');
  27. $this->importTestData();
  28. return true;
  29. }
  30. /**
  31. * 导入测试数据
  32. */
  33. protected function importTestData()
  34. {
  35. $sqlFile = ADDON_PATH . 'cms' . DS . 'testdata.sql';
  36. if (is_file($sqlFile)) {
  37. $lines = file($sqlFile);
  38. $templine = '';
  39. foreach ($lines as $line) {
  40. if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 2) == '/*') {
  41. continue;
  42. }
  43. $templine .= $line;
  44. if (substr(trim($line), -1, 1) == ';') {
  45. $templine = str_ireplace('__PREFIX__', config('database.prefix'), $templine);
  46. $templine = str_ireplace('INSERT INTO ', 'INSERT IGNORE INTO ', $templine);
  47. try {
  48. Db::getPdo()->exec($templine);
  49. } catch (\Exception $e) {
  50. //$e->getMessage();
  51. }
  52. $templine = '';
  53. }
  54. }
  55. }
  56. return true;
  57. }
  58. /**
  59. * 插件卸载方法
  60. * @return bool
  61. */
  62. public function uninstall()
  63. {
  64. Menu::delete('cms');
  65. return true;
  66. }
  67. /**
  68. * 插件启用方法
  69. */
  70. public function enable()
  71. {
  72. Menu::enable('cms');
  73. $prefix = Config::get('database.prefix');
  74. // 1.4.0表字段升级
  75. $modelList = Modelx::whereRaw("FIND_IN_SET('price', `fields`)")->select();
  76. foreach ($modelList as $index => $item) {
  77. Db::startTrans();
  78. try {
  79. //更新表数据
  80. Db::execute("UPDATE {$prefix}cms_archives a,{$prefix}{$item['table']} e SET a.price = e.price WHERE a.id = e.id");
  81. //更新表结构
  82. $field = \app\admin\model\cms\Fields::where('source', 'model')->where('name', 'price')->where('source_id', $item['id'])->find();
  83. if ($field) {
  84. $field->delete();
  85. }
  86. Db::commit();
  87. } catch (\Exception $e) {
  88. Db::rollback();
  89. }
  90. }
  91. }
  92. /**
  93. * 插件禁用方法
  94. */
  95. public function disable()
  96. {
  97. Menu::disable('cms');
  98. }
  99. /**
  100. * 插件升级方法
  101. */
  102. public function upgrade()
  103. {
  104. $menu = include ADDON_PATH . 'cms' . DS . 'data' . DS . 'menu.php';
  105. Menu::upgrade('cms', $menu);
  106. }
  107. /**
  108. * 应用初始化
  109. */
  110. public function appInit()
  111. {
  112. // 自定义路由变量规则
  113. \think\Route::pattern([
  114. 'diyname' => "/[a-zA-Z0-9\-_\x{4e00}-\x{9fa5}]+/u",
  115. 'id' => "\d+",
  116. ]);
  117. $config = get_addon_config('cms');
  118. $taglib = Config::get('template.taglib_pre_load');
  119. Config::set('template.taglib_pre_load', ($taglib ? $taglib . ',' : '') . 'addons\\cms\\taglib\\Cms');
  120. Config::set('cms', $config);
  121. }
  122. /**
  123. * 脚本替换
  124. */
  125. public function viewFilter(& $content)
  126. {
  127. $request = \think\Request::instance();
  128. $dispatch = $request->dispatch();
  129. if ($request->module() || !isset($dispatch['method'][0]) || $dispatch['method'][0] != '\think\addons\Route') {
  130. return;
  131. }
  132. $addon = isset($dispatch['var']['addon']) ? $dispatch['var']['addon'] : $request->param('addon');
  133. if ($addon != 'cms') {
  134. return;
  135. }
  136. $style = '';
  137. $script = '';
  138. $result = preg_replace_callback("/<(script|style)\s(data\-render=\"(script|style)\")([\s\S]*?)>([\s\S]*?)<\/(script|style)>/i", function ($match) use (&$style, &$script) {
  139. if (isset($match[1]) && in_array($match[1], ['style', 'script'])) {
  140. ${$match[1]} .= str_replace($match[2], '', $match[0]);
  141. }
  142. return '';
  143. }, $content);
  144. $content = preg_replace_callback('/^\s+(\{__STYLE__\}|\{__SCRIPT__\})\s+$/m', function ($matches) use ($style, $script) {
  145. return $matches[1] == '{__STYLE__}' ? $style : $script;
  146. }, $result ? $result : $content);
  147. }
  148. /**
  149. * 会员中心边栏后
  150. * @return mixed
  151. * @throws \Exception
  152. */
  153. public function userSidenavAfter()
  154. {
  155. $request = Request::instance();
  156. $controllername = strtolower($request->controller());
  157. $actionname = strtolower($request->action());
  158. $config = get_addon_config('cms');
  159. $sidenav = array_filter(explode(',', $config['usersidenav']));
  160. if (!$sidenav) {
  161. return '';
  162. }
  163. $data = [
  164. 'controllername' => $controllername,
  165. 'actionname' => $actionname,
  166. 'sidenav' => $sidenav
  167. ];
  168. return $this->fetch('view/hook/user_sidenav_after', $data);
  169. }
  170. public function xunsearchConfigInit()
  171. {
  172. return FulltextSearch::config();
  173. }
  174. public function xunsearchIndexReset($project)
  175. {
  176. if (!$project['isaddon'] || $project['name'] != 'cms') {
  177. return;
  178. }
  179. return FulltextSearch::reset();
  180. }
  181. }