UsefulLinkController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Enums\UsefulLinkLocation;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\UsefulLink;
  6. use App\Settings\LocaleSettings;
  7. use Illuminate\Contracts\Foundation\Application;
  8. use Illuminate\Contracts\View\Factory;
  9. use Illuminate\Contracts\View\View;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\Response;
  13. class UsefulLinkController extends Controller
  14. {
  15. const READ_PERMISSION = "admin.useful_links.read";
  16. const WRITE_PERMISSION = "admin.useful_links.write";
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return Application|Factory|View|Response
  21. */
  22. public function index(LocaleSettings $locale_settings)
  23. {
  24. $this->checkPermission(self::READ_PERMISSION);
  25. return view('admin.usefullinks.index', [
  26. 'locale_datatables' => $locale_settings->datatables
  27. ]);
  28. }
  29. /**
  30. * Show the form for creating a new resource.
  31. *
  32. * @return Application|Factory|View|Response
  33. */
  34. public function create()
  35. {
  36. $this->checkPermission(self::WRITE_PERMISSION);
  37. $positions = UsefulLinkLocation::cases();
  38. return view('admin.usefullinks.create')->with('positions', $positions);
  39. }
  40. /**
  41. * Store a newly created resource in storage.
  42. *
  43. * @param Request $request
  44. * @return RedirectResponse
  45. */
  46. public function store(Request $request)
  47. {
  48. $request->validate([
  49. 'icon' => 'required|string',
  50. 'title' => 'required|string|max:60',
  51. 'link' => 'required|url|string|max:191',
  52. 'description' => 'required|string|max:2000',
  53. ]);
  54. UsefulLink::create([
  55. 'icon' => $request->icon,
  56. 'title' => $request->title,
  57. 'link' => $request->link,
  58. 'description' => $request->description,
  59. 'position' => implode(",",$request->position),
  60. ]);
  61. return redirect()->route('admin.usefullinks.index')->with('success', __('link has been created!'));
  62. }
  63. /**
  64. * Display the specified resource.
  65. *
  66. * @param UsefulLink $usefullink
  67. * @return Response
  68. */
  69. public function show(UsefulLink $usefullink)
  70. {
  71. //
  72. }
  73. /**
  74. * Show the form for editing the specified resource.
  75. *
  76. * @param UsefulLink $usefullink
  77. * @return Application|Factory|View
  78. */
  79. public function edit(UsefulLink $usefullink)
  80. {
  81. $this->checkPermission(self::WRITE_PERMISSION);
  82. $positions = UsefulLinkLocation::cases();
  83. return view('admin.usefullinks.edit', [
  84. 'link' => $usefullink,
  85. 'positions' => $positions,
  86. ]);
  87. }
  88. /**
  89. * Update the specified resource in storage.
  90. *
  91. * @param Request $request
  92. * @param UsefulLink $usefullink
  93. * @return RedirectResponse
  94. */
  95. public function update(Request $request, UsefulLink $usefullink)
  96. {
  97. $request->validate([
  98. 'icon' => 'required|string',
  99. 'title' => 'required|string|max:60',
  100. 'link' => 'required|url|string|max:191',
  101. 'description' => 'required|string|max:2000',
  102. ]);
  103. $usefullink->update([
  104. 'icon' => $request->icon,
  105. 'title' => $request->title,
  106. 'link' => $request->link,
  107. 'description' => $request->description,
  108. 'position' => implode(",",$request->position),
  109. ]);
  110. return redirect()->route('admin.usefullinks.index')->with('success', __('link has been updated!'));
  111. }
  112. /**
  113. * Remove the specified resource from storage.
  114. *
  115. * @param UsefulLink $usefullink
  116. * @return Response
  117. */
  118. public function destroy(UsefulLink $usefullink)
  119. {
  120. $this->checkPermission(self::WRITE_PERMISSION);
  121. $usefullink->delete();
  122. return redirect()->back()->with('success', __('product has been removed!'));
  123. }
  124. public function dataTable()
  125. {
  126. $query = UsefulLink::query();
  127. return datatables($query)
  128. ->addColumn('actions', function (UsefulLink $link) {
  129. return '
  130. <a data-content="'.__('Edit').'" data-toggle="popover" data-trigger="hover" data-placement="top" href="'.route('admin.usefullinks.edit', $link->id).'" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
  131. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.usefullinks.destroy', $link->id).'">
  132. '.csrf_field().'
  133. '.method_field('DELETE').'
  134. <button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  135. </form>
  136. ';
  137. })
  138. ->editColumn('created_at', function (UsefulLink $link) {
  139. return $link->created_at ? $link->created_at->diffForHumans() : '';
  140. })
  141. ->editColumn('icon', function (UsefulLink $link) {
  142. return "<i class='{$link->icon}'></i>";
  143. })
  144. ->rawColumns(['actions', 'icon'])
  145. ->make();
  146. }
  147. }