DistAppearanceTemplateController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\DistAdminDistributor;
  4. use App\Admin\Repositories\DistAppearance;
  5. use App\Admin\Repositories\DistAppearancePublishList;
  6. use App\Admin\Repositories\DistAppearanceTemplate;
  7. use App\Admin\Repositories\DistAppearanceTemplateLog;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Dcat\Admin\Layout\Content;
  13. use Dcat\Admin\Admin;
  14. use App\Admin\Forms\AceLeft;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Str;
  17. use Dcat\Admin\Widgets\Card;
  18. class DistAppearanceTemplateController extends AdminController
  19. {
  20. /*
  21. * monaco editor 编辑代码
  22. */
  23. public function ace(Content $content,Request $request) {
  24. $content->view('admin.pages-custom.ace_content');
  25. if ($request->isMethod('post') || false == empty($request->get('act'))) {
  26. if($request->get('act') == 'tree') {
  27. //获取代码树形结构
  28. $appearanceId = $request->get('appearance_id');
  29. $distId = $request->get('dist_id');
  30. return $this->showTree($appearanceId, $distId);
  31. } elseif ($request->get('act') == 'content') {
  32. //得到文件内容
  33. $appearanceId = $request->get('appearance_id');
  34. $distId = $request->get('dist_id');
  35. $templateCode = $request->get('id');
  36. return DistAppearanceTemplate::getContent($appearanceId,$distId,$templateCode);
  37. } elseif ($request->get('act') == 'content_save') {
  38. //保存文件内容
  39. $appearanceId = $request->get('appearance_id');
  40. $distId = $request->get('dist_id');
  41. $templateCode = $request->get('template_code');
  42. $content = $request->get('content');
  43. return DistAppearanceTemplate::saveContent($appearanceId,$distId,$templateCode, $content);
  44. } else if ($request->get('act') == 'add_del_tree') {
  45. //新建或删除文件
  46. $fileId = $request->get('file_id');
  47. $appearanceId = $request->get('appearance_id');
  48. $distId = $request->get('dist_id');
  49. $fileName = $request->get('file_name_input');
  50. $filePath = $request->get('file_path_name');
  51. $type = $request->get('type');
  52. return DistAppearanceTemplate::addDelTree($appearanceId,$distId,$fileId,$fileName, $filePath,$type);
  53. }elseif ($request->get('act') == 'publish') {
  54. //发报
  55. $appearanceId = $request->get('appearance_id');
  56. $distId = $request->get('dist_id');
  57. DistAppearancePublishList::publish($appearanceId,$distId);
  58. //清缓存
  59. DistAdminDistributor::clearCache($distId);
  60. return ['status'=>1];
  61. } else if ($request->get('act') == 'get_appearance_id') {
  62. //返回appearance_id
  63. $distId = $request->get('dist_id');
  64. $row = DistAdminDistributor::getOneById($distId);
  65. return $row ? $row->appearance_id : '';
  66. } else if ($request->get('act') == 'fetch_template_logs') {
  67. //返回模版修改日志
  68. $fileId = $request->get('file_id');
  69. $appearanceId = $request->get('appearance_id');
  70. $distId = $request->get('dist_id');
  71. $rows = DistAppearanceTemplateLog::fetchTemplateLogs($appearanceId,$distId,$fileId);
  72. return response()->json($rows);
  73. } else if ($request->get('act') == 'fetch_template_log_content') {
  74. //返回模版修改日志内容
  75. $logId = $request->get('log_id');
  76. $rows = DistAppearanceTemplateLog::fetchTemplateLogContent($logId);
  77. $rows->previous_content = htmlspecialchars($rows->previous_content);
  78. return response()->json($rows);
  79. } else if ($request->get('act') == 'restore_template_log') {
  80. //还原模版修改日志
  81. $logId = $request->get('log_id');
  82. DistAppearanceTemplateLog::restoreTemplateLog($logId);
  83. return ['status'=>1];
  84. }
  85. }
  86. $leftForm = new AceLeft();
  87. return $content
  88. ->header('Template Editor')
  89. ->body(admin_view('admin.pages-custom.ace',['leftForm'=>$leftForm]));
  90. }
  91. /*
  92. * 显示代码树
  93. */
  94. private function showTree($appearanceId, $distId) {
  95. $appearanceId = empty($appearanceId) ? 0 : $appearanceId;
  96. $distId = empty($distId) ? 0 : $distId;
  97. $appearanceRow = DistAppearance::getOneById($appearanceId);
  98. $tree = DistAppearanceTemplate::getTemplateTree($appearanceId, $distId);
  99. //print_r($tree);exit;
  100. $html = '<ul class="list-group list-group-flush">';
  101. $i = 0;
  102. foreach ($tree as $key => $value) {
  103. $fa = 'fa-angle-down';
  104. $file_name = $value['file_name'];
  105. $file_name = empty($file_name) ? 'root' : $file_name;
  106. $file_name = '<span class="custom-blue-bold">'. $file_name.'</span>';
  107. if ($i == 0) {
  108. $file_name .= " ({$distId} - {$appearanceId})";
  109. }
  110. $file_name = $file_name . '<span class="float-right"><a href="javascript:void(0)" title="Add File" data-toggle="modal" data-target="#addFileModal" file_path_name="'. $value['file_name']. '"><i class="feather icon-plus" style="font-size: 12px"></i></a></span>';
  111. $html .= '<li class="list-group-item has-submenu">';
  112. $html .= '<div style="padding-top: 5px;padding-bottom: 0px"><i class="fa '.$fa.'"></i> '. $file_name.'</div>';
  113. $html .= $this->treeBuilder($value);
  114. $html .= '</li>';
  115. $i++;
  116. }
  117. $html .= '</ul>';
  118. return $html;
  119. }
  120. private function treeBuilder($value) {
  121. $html = '';
  122. if (!empty($value['children'])) {
  123. $html = '<ul class="submenu">';
  124. foreach ($value['children'] as $k => $v) {
  125. $fa = 'fa-angle-down';
  126. $file_name = $v['file_name'];
  127. if (empty($v['children'])) {
  128. $fa = 'fa-angle-right';
  129. $file_name = '<a href="#" class="file-action" file_id="'. $v['template_code'].'" title="'.$file_name.'">'. $file_name.'</a>'. '<span class="float-right"><a href="javascript:void(0)" title="delete File" class="delFileButton" file_id="'. $v['id'].'" file_name="'. $v['file_name'].'"><i class="feather icon-x"></i></a></span>';
  130. } else {
  131. $file_name = '<span class="custom-blue-bold">'. $file_name.'</span>';
  132. }
  133. $html .= '<li class="list-group-item"><i class="fa '.$fa.'"></i> '. $file_name;
  134. if (!empty($v['children'])) {
  135. $html .= $this->treeBuilder($v);
  136. }
  137. $html .= '</li>';
  138. }
  139. $html .= '</ul>';
  140. }
  141. return $html;
  142. }
  143. }