AppearanceImport.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. DistAppearance::setStatusToImported($appearanceId);
  49. // 返回响应结果并刷新页面
  50. return $this->response()->success("Successfully imported")->refresh();
  51. }
  52. /**
  53. * 设置要POST到接口的数据
  54. *
  55. * @return array
  56. */
  57. public function parameters()
  58. {
  59. return [
  60. // 发送当前行 username 字段数据到接口
  61. 'title' => $this->row->title,
  62. ];
  63. }
  64. private function readDirectory($dirPath,$parentId = 0) {
  65. // 检查目录是否存在
  66. if (!is_dir($dirPath)) {
  67. return;
  68. }
  69. // 打开目录
  70. $dir = opendir($dirPath);
  71. // 读取目录内容
  72. while (($file = readdir($dir)) !== false) {
  73. // 忽略当前目录和上级目录
  74. if ($file == '.' || $file == '..') {
  75. continue;
  76. }
  77. // 获取文件或文件夹的完整路径
  78. $fullPath = $dirPath . '/' . $file;
  79. // 如果是文件夹,递归读取
  80. if (is_dir($fullPath)) {
  81. //echo "文件夹: $fullPath<br />";
  82. //插数据库
  83. $filePath = str_replace($this->sourcePath, '', $fullPath);
  84. $newParentId = DistAppearanceTemplate::insertTemplateFolder(0, $this->appearanceId, $filePath, $parentId);
  85. $this->readDirectory($fullPath,$newParentId); // 递归调用
  86. } else {
  87. // 如果是文件,读取文件内容
  88. $content = file_get_contents($fullPath);
  89. $filePath = str_replace($this->sourcePath, '', $fullPath);
  90. $filePath = str_replace('/' .$file, '', $filePath);
  91. // 插入数据库
  92. DistAppearanceTemplate::insertTemplateContent(0, $this->appearanceId, $filePath,$file, $parentId, $content);
  93. }
  94. }
  95. // 关闭目录
  96. closedir($dir);
  97. }
  98. }