You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.5 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Business;
  4. use App\Status;
  5. use App\Workflow;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\DB;
  8. class WorkflowController extends Controller
  9. {
  10. public function store($business, Request $request)
  11. {
  12. permit('businessWorkFlows');
  13. Workflow::create($request->merge(['business_id' => $business])->except('_business_info'));
  14. // $statuses = collect($validatedData['statuses'])->map(function($status) use ($workflow, $business) {
  15. // $status['business_id'] = $business;
  16. // $status['workflow_id'] = $workflow->id;
  17. // return $status;
  18. // });
  19. // $statuses = $workflow->statuses()->createMany($statuses->toArray());
  20. return Business::info($business, true);
  21. }
  22. public function update($business, $workflow, Request $request)
  23. {
  24. permit('businessWorkFlows');
  25. $workflowModel = Workflow::findOrFail($workflow);
  26. $workflowModel->update($request->except('_business_info'));
  27. return Business::info($business, true);
  28. }
  29. public function syncStatus($business, $workflowModel)
  30. {
  31. $old_statuses_name = array_keys(collect(\request('_business_info')['workflows'][$workflowModel->id]['statuses'])->keyBy('name')->toArray());
  32. $new_statuses_name = array_keys(collect(\request('statuses'))->keyBy('name')->toArray());
  33. $removed_statuses_name = array_diff(array_merge($old_statuses_name, $new_statuses_name), $new_statuses_name);
  34. foreach ($removed_statuses_name as $status_name) {
  35. //delete all statuses that removed name's from request->statuses
  36. $workflowModel->statuses()->where('name', $status_name)->first()->delete();
  37. }
  38. foreach (request('statuses') as $status) {
  39. //sync another statuses
  40. $workflowModel->statuses()
  41. ->updateOrCreate(
  42. ['name' => $status['name'], 'business_id' => $business, 'workflow_id' => $workflowModel->id],
  43. ['state' => $status['state'], 'order' => $status['order']]
  44. );
  45. }
  46. return $workflowModel;
  47. }
  48. public function delete($business, $workflow)
  49. {
  50. permit('businessWorkFlows');
  51. $workflow = Workflow::findOrFail($workflow);
  52. foreach ($workflow->statuses as $status) {
  53. //delete all statuses related to this workflow
  54. $status->delete();
  55. }
  56. $workflow->delete();
  57. return Business::info($business, true);
  58. }
  59. }