ProjectTreeController.php 4.3 KB

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