ThemeModel.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model;
  12. use think\Model;
  13. class ThemeModel extends Model
  14. {
  15. /**
  16. * 模型名称
  17. * @var string
  18. */
  19. protected $name = 'theme';
  20. /**
  21. * 获取插件列表
  22. */
  23. public function getList()
  24. {
  25. }
  26. public function installTheme($theme)
  27. {
  28. $manifest = WEB_ROOT . "themes/$theme/manifest.json";
  29. if (file_exists_case($manifest)) {
  30. $manifest = file_get_contents($manifest);
  31. $themeData = json_decode($manifest, true);
  32. $themeData['theme'] = $theme;
  33. $this->updateThemeFiles($theme);
  34. $this->save($themeData);
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. }
  40. public function updateTheme($theme)
  41. {
  42. $manifest = WEB_ROOT . "themes/$theme/manifest.json";
  43. if (file_exists_case($manifest)) {
  44. $manifest = file_get_contents($manifest);
  45. $themeData = json_decode($manifest, true);
  46. $this->updateThemeFiles($theme);
  47. $this->where('theme', $theme)->update($themeData);
  48. return true;
  49. } else {
  50. return false;
  51. }
  52. }
  53. /**
  54. * 获取当前前台模板某操作下的模板文件
  55. * @param $action string 控制器操作
  56. * @return array|string|\think\Collection
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @throws \think\exception\DbException
  60. */
  61. public function getActionThemeFiles($action)
  62. {
  63. $theme = config('template.cmf_default_theme');
  64. return ThemeFileModel::where(['theme' => $theme, 'action' => $action])->select();
  65. }
  66. private function updateThemeFiles($theme, $suffix = 'html')
  67. {
  68. $dir = 'themes/' . $theme;
  69. $themeDir = $dir;
  70. $tplFiles = [];
  71. $root_dir_tpl_files = cmf_scan_dir("$dir/*.$suffix");
  72. foreach ($root_dir_tpl_files as $root_tpl_file) {
  73. $root_tpl_file = "$dir/$root_tpl_file";
  74. $configFile = preg_replace("/\.$suffix$/", '.json', $root_tpl_file);
  75. $root_tpl_file_no_suffix = preg_replace("/\.$suffix$/", '', $root_tpl_file);
  76. if (is_file($root_tpl_file) && file_exists_case($configFile)) {
  77. array_push($tplFiles, $root_tpl_file_no_suffix);
  78. }
  79. }
  80. $subDirs = cmf_sub_dirs($dir);
  81. foreach ($subDirs as $dir) {
  82. $subDirTplFiles = cmf_scan_dir("$dir/*.$suffix");
  83. foreach ($subDirTplFiles as $tplFile) {
  84. $tplFile = "$dir/$tplFile";
  85. $configFile = preg_replace("/\.$suffix$/", '.json', $tplFile);
  86. $tplFileNoSuffix = preg_replace("/\.$suffix$/", '', $tplFile);
  87. if (is_file($tplFile) && file_exists_case($configFile)) {
  88. array_push($tplFiles, $tplFileNoSuffix);
  89. }
  90. }
  91. }
  92. foreach ($tplFiles as $tplFile) {
  93. $configFile = $tplFile . ".json";
  94. $file = preg_replace('/^themes\/' . $theme . '\//', '', $tplFile);
  95. $file = strtolower($file);
  96. $config = json_decode(file_get_contents($configFile), true);
  97. $findFile = ThemeFileModel::where(['theme' => $theme, 'file' => $file])->find();
  98. $isPublic = empty($config['is_public']) ? 0 : 1;
  99. $listOrder = empty($config['order']) ? 0 : floatval($config['order']);
  100. $configMore = empty($config['more']) ? [] : $config['more'];
  101. $more = $configMore;
  102. if (empty($findFile)) {
  103. ThemeFileModel::insert([
  104. 'theme' => $theme,
  105. 'action' => $config['action'],
  106. 'file' => $file,
  107. 'name' => $config['name'],
  108. 'more' => json_encode($more),
  109. 'config_more' => json_encode($configMore),
  110. 'description' => $config['description'],
  111. 'is_public' => $isPublic,
  112. 'list_order' => $listOrder
  113. ]);
  114. } else { // 更新文件
  115. $moreInDb = $findFile['more'];
  116. $more = $this->updateThemeConfigMore($configMore, $moreInDb);
  117. ThemeFileModel::where(['theme' => $theme, 'file' => $file])->update([
  118. 'theme' => $theme,
  119. 'action' => $config['action'],
  120. 'file' => $file,
  121. 'name' => $config['name'],
  122. 'more' => json_encode($more),
  123. 'config_more' => json_encode($configMore),
  124. 'description' => $config['description'],
  125. 'is_public' => $isPublic,
  126. 'list_order' => $listOrder
  127. ]);
  128. }
  129. }
  130. // 检查安装过的模板文件是否已经删除
  131. $files = ThemeFileModel::where('theme', $theme)->select();
  132. foreach ($files as $themeFile) {
  133. $tplFile = $themeDir . '/' . $themeFile['file'] . '.' . $suffix;
  134. $tplFileConfigFile = $themeDir . '/' . $themeFile['file'] . '.json';
  135. if (!is_file($tplFile) || !file_exists_case($tplFileConfigFile)) {
  136. ThemeFileModel::where(['theme' => $theme, 'file' => $themeFile['file']])->delete();
  137. }
  138. }
  139. }
  140. private function updateThemeConfigMore($configMore, $moreInDb)
  141. {
  142. if (!empty($configMore['vars'])) {
  143. foreach ($configMore['vars'] as $mVarName => $mVar) {
  144. if (isset($moreInDb['vars'][$mVarName]['value']) && $mVar['type'] == $moreInDb['vars'][$mVarName]['type']) {
  145. $configMore['vars'][$mVarName]['value'] = $moreInDb['vars'][$mVarName]['value'];
  146. if (isset($moreInDb['vars'][$mVarName]['valueText'])) {
  147. $configMore['vars'][$mVarName]['valueText'] = $moreInDb['vars'][$mVarName]['valueText'];
  148. }
  149. }
  150. }
  151. }
  152. if (!empty($configMore['widgets'])) {
  153. foreach ($configMore['widgets'] as $widgetName => $widget) {
  154. if (isset($moreInDb['widgets'][$widgetName]['title'])) {
  155. $configMore['widgets'][$widgetName]['title'] = $moreInDb['widgets'][$widgetName]['title'];
  156. }
  157. if (isset($moreInDb['widgets'][$widgetName]['display'])) {
  158. $configMore['widgets'][$widgetName]['display'] = $moreInDb['widgets'][$widgetName]['display'];
  159. }
  160. if (!empty($widget['vars'])) {
  161. foreach ($widget['vars'] as $widgetVarName => $widgetVar) {
  162. if (isset($moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['value']) && $widgetVar['type'] == $moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['type']) {
  163. $configMore['widgets'][$widgetName]['vars'][$widgetVarName]['value'] = $moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['value'];
  164. if (isset($moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['valueText'])) {
  165. $configMore['widgets'][$widgetName]['vars'][$widgetVarName]['valueText'] = $moreInDb['widgets'][$widgetName]['vars'][$widgetVarName]['valueText'];
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. return $configMore;
  173. }
  174. }