AppearanceImport.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Admin\Actions\Grid;
  3. use App\Admin\Forms\InquiryAssignment as InquiryAssignmentForm;
  4. use Dcat\Admin\Grid\BatchAction;
  5. use Dcat\Admin\Grid\RowAction;
  6. use Dcat\Admin\Widgets\Modal;
  7. use Illuminate\Http\Request;
  8. use App\Admin\Repositories\DistAppearance;
  9. use App\Admin\Repositories\DistAppearanceTemplate;
  10. class AppearanceImport extends RowAction
  11. {
  12. public $sourcePath;
  13. public $appearanceId;
  14. /**
  15. * 返回字段标题
  16. *
  17. * @return string
  18. */
  19. public function title()
  20. {
  21. return 'Import Tmpl';
  22. }
  23. public function confirm()
  24. {
  25. return [
  26. "Are you sure you want to import?",
  27. $this->row->title,
  28. ];
  29. }
  30. /*
  31. * 处理请求
  32. */
  33. public function handle(Request $request)
  34. {
  35. $appearanceId = $this->getKey();
  36. $folder = $request->get('folder');
  37. //导入模板
  38. $path = resource_path().'/appearance/'.$folder;
  39. if (!is_dir($path)) {
  40. return $this->response()->error("The directory does not exist")->refresh();
  41. }
  42. $this->sourcePath = $path;
  43. $this->appearanceId = $appearanceId;
  44. // 清空旧的
  45. DistAppearanceTemplate::deleteTemplates(0, $appearanceId);
  46. // 导入模板
  47. $this->readDirectory($path);
  48. // 更新状态
  49. DistAppearance::setStatusToImported($appearanceId);
  50. // 返回响应结果并刷新页面
  51. return $this->response()->success("Successfully imported")->refresh();
  52. }
  53. /**
  54. * 设置要POST到接口的数据
  55. *
  56. * @return array
  57. */
  58. public function parameters()
  59. {
  60. return [
  61. // 发送当前行 username 字段数据到接口
  62. 'title' => $this->row->title,
  63. ];
  64. }
  65. private function readDirectory($dirPath,$parentId = 0) {
  66. // 检查目录是否存在
  67. if (!is_dir($dirPath)) {
  68. return;
  69. }
  70. // 打开目录
  71. $dir = opendir($dirPath);
  72. // 读取目录内容
  73. while (($file = readdir($dir)) !== false) {
  74. // 忽略当前目录和上级目录
  75. if ($file == '.' || $file == '..') {
  76. continue;
  77. }
  78. // 获取文件或文件夹的完整路径
  79. $fullPath = $dirPath . '/' . $file;
  80. // 如果是文件夹,递归读取
  81. if (is_dir($fullPath)) {
  82. //echo "文件夹: $fullPath<br />";
  83. //插数据库
  84. $filePath = str_replace($this->sourcePath, '', $fullPath);
  85. $fileName = basename($fullPath);
  86. $newParentId = DistAppearanceTemplate::insertTemplateFolder(0, $this->appearanceId, $filePath,$fileName, $parentId);
  87. $this->readDirectory($fullPath,$newParentId); // 递归调用
  88. } else {
  89. // 如果是文件,读取文件内容
  90. $content = file_get_contents($fullPath);
  91. $filePath = str_replace($this->sourcePath, '', $fullPath);
  92. $fileName = basename($fullPath);
  93. // 插入数据库
  94. DistAppearanceTemplate::insertTemplateContent(0, $this->appearanceId, $filePath,$fileName, $parentId, $content);
  95. }
  96. }
  97. // 关闭目录
  98. closedir($dir);
  99. }
  100. }