NodeController.php 3.7 KB

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