2
0

ProjectController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\ShareApi;
  4. use App\Http\Api\StudioApi;
  5. use App\Http\Resources\ProjectResource;
  6. use App\Models\Project;
  7. use App\Services\AuthService;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use Illuminate\Support\Str;
  11. class ProjectController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return Response
  17. */
  18. public function index(Request $request)
  19. {
  20. $user = AuthService::current($request);
  21. if (! $user) {
  22. return $this->error(__('auth.failed'), 401, 401);
  23. }
  24. if ($request->has('studio')) {
  25. $studioId = StudioApi::getIdByName($request->input('studio'));
  26. } else {
  27. $studioId = $user['user_uid'];
  28. }
  29. switch ($request->input('view')) {
  30. case 'studio':
  31. $table = Project::where('owner_id', $studioId)
  32. ->whereNull('parent_id')
  33. ->where('type', $request->input('type', 'instance'));
  34. break;
  35. case 'project-tree':
  36. $table = Project::where('uid', $request->input('project_id'))
  37. ->orWhereJsonContains('path', $request->input('project_id'));
  38. break;
  39. case 'shared':
  40. $type = $request->input('type', 'instance');
  41. $resList = ShareApi::getResList($studioId, $type === 'instance' ? 7 : 6);
  42. $resId = [];
  43. foreach ($resList as $res) {
  44. $resId[] = $res['res_id'];
  45. }
  46. $table = Project::whereIn('uid', $resId);
  47. break;
  48. case 'community':
  49. $table = Project::where('owner_id', '<>', $studioId)
  50. ->whereNull('parent_id')
  51. ->where('privacy', 'public')
  52. ->where('type', $request->input('type', 'instance'));
  53. break;
  54. default:
  55. return $this->error('view', 200, 200);
  56. break;
  57. }
  58. if ($request->has('keyword')) {
  59. $table = $table->where('title', 'like', '%'.$request->input('keyword').'%');
  60. }
  61. if ($request->has('status')) {
  62. $table = $table->whereIn('status', explode(',', $request->input('status')));
  63. }
  64. $count = $table->count();
  65. $table = $table->orderBy($request->input('order', 'id'), $request->input('dir', 'asc'));
  66. $table = $table->skip($request->input('offset', 0))
  67. ->take($request->input('limit', 10000));
  68. $result = $table->get();
  69. return $this->ok(
  70. [
  71. 'rows' => ProjectResource::collection($result),
  72. 'count' => $count,
  73. ]
  74. );
  75. }
  76. public static function canEdit($user_uid, $studio_uid)
  77. {
  78. return $user_uid == $studio_uid;
  79. }
  80. /**
  81. * Store a newly created resource in storage.
  82. *
  83. * @return Response
  84. */
  85. public function store(Request $request)
  86. {
  87. //
  88. $user = AuthService::current($request);
  89. if (! $user) {
  90. return $this->error(__('auth.failed'), 401, 401);
  91. }
  92. $studioId = StudioApi::getIdByName($request->input('studio_name'));
  93. if (! self::canEdit($user['user_uid'], $studioId)) {
  94. return $this->error(__('auth.failed'), 403, 403);
  95. }
  96. $new = Project::firstOrNew(['uid' => $request->input('id')]);
  97. if (Str::isUuid($request->input('id'))) {
  98. $new->uid = $request->input('id');
  99. } else {
  100. $new->uid = Str::uuid();
  101. }
  102. $new->title = $request->input('title');
  103. $new->description = $request->input('description');
  104. $new->parent_id = $request->input('parent_id');
  105. $new->editor_id = $user['user_uid'];
  106. $new->owner_id = $studioId;
  107. $new->type = $request->input('type', 'instance');
  108. if (Str::isUuid($request->input('parent_id'))) {
  109. $parentPath = Project::where('uid', $request->input('parent_id'))->value('path');
  110. $parentPath = json_decode($parentPath);
  111. if (! is_array($parentPath)) {
  112. $parentPath = [];
  113. }
  114. array_push($parentPath, $new->parent_id);
  115. $new->path = json_encode($parentPath, JSON_UNESCAPED_UNICODE);
  116. }
  117. $new->save();
  118. return $this->ok(new ProjectResource($new));
  119. }
  120. /**
  121. * Display the specified resource.
  122. *
  123. * @return Response
  124. */
  125. public function show(Project $project)
  126. {
  127. //
  128. return $this->ok(new ProjectResource($project));
  129. }
  130. /**
  131. * Update the specified resource in storage.
  132. *
  133. * @return Response
  134. */
  135. public function update(Request $request, Project $project)
  136. {
  137. //
  138. $user = AuthService::current($request);
  139. if (! $user) {
  140. return $this->error(__('auth.failed'), 401, 401);
  141. }
  142. if (! self::canEdit($user['user_uid'], $project->owner_id)) {
  143. return $this->error(__('auth.failed'), 403, 403);
  144. }
  145. $project->title = $request->input('title');
  146. $project->description = $request->input('description');
  147. $project->parent_id = $request->input('parent_id');
  148. $project->editor_id = $user['user_uid'];
  149. $project->privacy = $request->input('privacy');
  150. if (Str::isUuid($request->input('parent_id'))) {
  151. $parentPath = Project::where('uid', $request->input('parent_id'))->value('path');
  152. $parentPath = json_decode($parentPath);
  153. if (! is_array($parentPath)) {
  154. $parentPath = [];
  155. }
  156. array_push($parentPath, $project->parent_id);
  157. $project->path = json_encode($parentPath, JSON_UNESCAPED_UNICODE);
  158. }
  159. $project->save();
  160. return $this->ok(new ProjectResource($project));
  161. }
  162. /**
  163. * Remove the specified resource from storage.
  164. *
  165. * @return Response
  166. */
  167. public function destroy(Project $project)
  168. {
  169. //
  170. }
  171. }