NodeController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Location;
  5. use App\Models\Node;
  6. use Exception;
  7. use Illuminate\Contracts\Foundation\Application;
  8. use Illuminate\Contracts\View\Factory;
  9. use Illuminate\Contracts\View\View;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Http\Response;
  14. class NodeController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return Application|Factory|View
  20. */
  21. public function index()
  22. {
  23. return view('admin.nodes.index');
  24. }
  25. /**
  26. * Show the form for creating a new resource.
  27. *
  28. * @return Response
  29. */
  30. public function create()
  31. {
  32. //
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param Request $request
  38. * @return Response
  39. */
  40. public function store(Request $request)
  41. {
  42. //
  43. }
  44. /**
  45. * Display the specified resource.
  46. *
  47. * @param Node $node
  48. * @return Response
  49. */
  50. public function show(Node $node)
  51. {
  52. //
  53. }
  54. /**
  55. * Show the form for editing the specified resource.
  56. *
  57. * @param Node $node
  58. * @return Response
  59. */
  60. public function edit(Node $node)
  61. {
  62. //
  63. }
  64. /**
  65. * Update the specified resource in storage.
  66. *
  67. * @param Request $request
  68. * @param Node $node
  69. * @return RedirectResponse
  70. */
  71. public function update(Request $request, Node $node)
  72. {
  73. $disabled = !!is_null($request->input('disabled'));
  74. $node->update(['disabled' => $disabled]);
  75. return redirect()->back()->with('success', 'Node updated');
  76. }
  77. /**
  78. * Remove the specified resource from storage.
  79. *
  80. * @param Node $node
  81. * @return Response
  82. */
  83. public function destroy(Node $node)
  84. {
  85. //
  86. }
  87. /**
  88. *
  89. * @throws Exception
  90. */
  91. public function sync(){
  92. Node::query()->delete();
  93. Location::query()->delete();
  94. Node::syncNodes();
  95. return redirect()->back()->with('success', 'Locations and Nodes have been synced');
  96. }
  97. /**
  98. * @param Request $request
  99. * @return JsonResponse|mixed
  100. * @throws Exception
  101. */
  102. public function dataTable(Request $request)
  103. {
  104. $query = Node::with(['location']);
  105. $query->select('nodes.*');
  106. return datatables($query)
  107. ->addColumn('location', function (Node $node) {
  108. return $node->location->name;
  109. })
  110. ->addColumn('actions', function (Node $node) {
  111. $checked = $node->disabled == false ? "checked" : "";
  112. return '
  113. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.nodes.update', $node->id) . '">
  114. ' . csrf_field() . '
  115. ' . method_field("PATCH") . '
  116. <div class="custom-control custom-switch">
  117. <input '.$checked.' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch'.$node->id.'">
  118. <label class="custom-control-label" for="switch'.$node->id.'"></label>
  119. </div>
  120. </form>
  121. ';
  122. })
  123. ->editColumn('created_at' , function (Node $node) {
  124. return $node->created_at ? $node->created_at->diffForHumans() : '';
  125. })
  126. ->rawColumns(['actions'])
  127. ->make();
  128. }
  129. }