ProjectTreeController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Project;
  4. use Illuminate\Http\Request;
  5. use App\Services\AuthService;
  6. use Illuminate\Support\Str;
  7. use App\Http\Api\StudioApi;
  8. use Illuminate\Support\Facades\Log;
  9. class ProjectTreeController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index()
  17. {
  18. //
  19. }
  20. /**
  21. * Store a newly created resource in storage.
  22. *
  23. * @param \Illuminate\Http\Request $request
  24. * @return \Illuminate\Http\Response
  25. */
  26. public function store(Request $request)
  27. {
  28. //
  29. $user = AuthService::current($request);
  30. if (!$user) {
  31. return $this->error(__('auth.failed'), 401, 401);
  32. }
  33. $studioId = StudioApi::getIdByName($request->input('studio_name'));
  34. if (!ProjectController::canEdit($user['user_uid'], $studioId)) {
  35. return $this->error(__('auth.failed'), 403, 403);
  36. }
  37. $newData = [];
  38. foreach ($request->input('data') as $key => $value) {
  39. $data = [
  40. 'uid' => Str::uuid(),
  41. 'old_id' => $value['id'],
  42. 'title' => $value['title'],
  43. 'type' => $value['type'],
  44. 'res_id' => $value['res_id'],
  45. 'parent_id' => $value['parent_id'],
  46. 'path' => null,
  47. 'owner_id' => $studioId,
  48. 'editor_id' => $user['user_uid'],
  49. 'created_at' => now(),
  50. 'updated_at' => now(),
  51. ];
  52. if (isset($value['weight'])) {
  53. $data['weight'] = $value['weight'];
  54. }
  55. $newData[] = $data;
  56. }
  57. foreach ($newData as $key => $value) {
  58. if ($value['parent_id']) {
  59. $parent = null;
  60. /*
  61. $parent = \array_find($newData, function ($element) use ($value) {
  62. return $element['old_id'] == $value['parent_id'];
  63. });
  64. */
  65. foreach ($newData as $item) {
  66. if ($item['old_id'] == $value['parent_id']) {
  67. $parent = $item;
  68. break;
  69. }
  70. }
  71. if ($parent) {
  72. $newData[$key]['parent_id'] = $parent['uid'];
  73. $parentPath = $parent['path'] ? json_decode($parent['path']) : [];
  74. $newData[$key]['path'] = json_encode([...$parentPath, $parent['uid']], JSON_UNESCAPED_UNICODE);
  75. } else {
  76. $newData[$key]['parent_id'] = null;
  77. }
  78. } else if (!empty($request->input('parent_id'))) {
  79. $pPath = Project::where('uid', $request->input('parent_id'))->value('path');
  80. $parentPath = json_decode($pPath);
  81. if (!is_array($parentPath)) {
  82. $parentPath = [];
  83. }
  84. $newData[$key]['path'] = json_encode([...$parentPath, $request->input('parent_id')], JSON_UNESCAPED_UNICODE);
  85. $newData[$key]['parent_id'] = $request->input('parent_id');
  86. }
  87. }
  88. $output = [];
  89. foreach ($newData as $key => $value) {
  90. $children = array_filter($newData, function ($element) use ($value) {
  91. return $element['parent_id'] === $value['uid'];
  92. });
  93. $output[] = [
  94. 'id' => $value['uid'],
  95. 'resId' => $value['res_id'],
  96. 'isLeaf' => count($children) === 0,
  97. ];
  98. unset($newData[$key]['old_id']);
  99. unset($newData[$key]['res_id']);
  100. }
  101. $ok = Project::insert($newData);
  102. if ($ok) {
  103. return $this->ok(['rows' => $output, count($output)]);
  104. } else {
  105. return $this->error('error', 200, 200);
  106. }
  107. }
  108. /**
  109. * Display the specified resource.
  110. *
  111. * @param \App\Models\Project $project
  112. * @return \Illuminate\Http\Response
  113. */
  114. public function show(Project $project)
  115. {
  116. //
  117. }
  118. /**
  119. * Update the specified resource in storage.
  120. *
  121. * @param \Illuminate\Http\Request $request
  122. * @param \App\Models\Project $project
  123. * @return \Illuminate\Http\Response
  124. */
  125. public function update(Request $request, Project $project)
  126. {
  127. //
  128. }
  129. /**
  130. * Remove the specified resource from storage.
  131. *
  132. * @param \App\Models\Project $project
  133. * @return \Illuminate\Http\Response
  134. */
  135. public function destroy(Project $project)
  136. {
  137. //
  138. }
  139. }