PageController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Page;
  5. use App\Models\PageTranslation;
  6. class PageController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index()
  14. {
  15. }
  16. /**
  17. * Show the form for creating a new resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function create()
  22. {
  23. return view('backend.website_settings.pages.create');
  24. }
  25. /**
  26. * Store a newly created resource in storage.
  27. *
  28. * @param \Illuminate\Http\Request $request
  29. * @return \Illuminate\Http\Response
  30. */
  31. public function store(Request $request)
  32. {
  33. $page = new Page;
  34. $page->title = $request->title;
  35. if (Page::where('slug', preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug)))->first() == null) {
  36. $page->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug));
  37. $page->type = "custom_page";
  38. $page->content = $request->content;
  39. $page->meta_title = $request->meta_title;
  40. $page->meta_description = $request->meta_description;
  41. $page->keywords = $request->keywords;
  42. $page->meta_image = $request->meta_image;
  43. $page->save();
  44. $page_translation = PageTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'page_id' => $page->id]);
  45. $page_translation->title = $request->title;
  46. $page_translation->content = $request->content;
  47. $page_translation->save();
  48. flash(translate('New page has been created successfully'))->success();
  49. return redirect()->route('website.pages');
  50. }
  51. flash(translate('Slug has been used already'))->warning();
  52. return back();
  53. }
  54. /**
  55. * Display the specified resource.
  56. *
  57. * @param int $id
  58. * @return \Illuminate\Http\Response
  59. */
  60. public function show($id)
  61. {
  62. //
  63. }
  64. /**
  65. * Show the form for editing the specified resource.
  66. *
  67. * @param int $id
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function edit(Request $request, $id)
  71. {
  72. $lang = $request->lang;
  73. $page_name = $request->page;
  74. $page = Page::where('slug', $id)->first();
  75. if($page != null){
  76. if ($page_name == 'home') {
  77. return view('backend.website_settings.pages.home_page_edit', compact('page','lang'));
  78. }
  79. else{
  80. return view('backend.website_settings.pages.edit', compact('page','lang'));
  81. }
  82. }
  83. abort(404);
  84. }
  85. /**
  86. * Update the specified resource in storage.
  87. *
  88. * @param \Illuminate\Http\Request $request
  89. * @param int $id
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function update(Request $request, $id)
  93. {
  94. $page = Page::findOrFail($id);
  95. if (Page::where('id','!=', $id)->where('slug', preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug)))->first() == null) {
  96. if($page->type == 'custom_page'){
  97. $page->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->slug));
  98. }
  99. if($request->lang == env("DEFAULT_LANGUAGE")){
  100. $page->title = $request->title;
  101. $page->content = $request->content;
  102. }
  103. $page->meta_title = $request->meta_title;
  104. $page->meta_description = $request->meta_description;
  105. $page->keywords = $request->keywords;
  106. $page->meta_image = $request->meta_image;
  107. $page->save();
  108. $page_translation = PageTranslation::firstOrNew(['lang' => $request->lang, 'page_id' => $page->id]);
  109. $page_translation->title = $request->title;
  110. $page_translation->content = $request->content;
  111. $page_translation->save();
  112. flash(translate('Page has been updated successfully'))->success();
  113. return redirect()->route('website.pages');
  114. }
  115. flash(translate('Slug has been used already'))->warning();
  116. return back();
  117. }
  118. /**
  119. * Remove the specified resource from storage.
  120. *
  121. * @param int $id
  122. * @return \Illuminate\Http\Response
  123. */
  124. public function destroy($id)
  125. {
  126. $page = Page::findOrFail($id);
  127. foreach ($page->page_translations as $key => $page_translation) {
  128. $page_translation->delete();
  129. }
  130. if(Page::destroy($id)){
  131. flash(translate('Page has been deleted successfully'))->success();
  132. return redirect()->back();
  133. }
  134. return back();
  135. }
  136. public function show_custom_page($slug){
  137. $page = Page::where('slug', $slug)->first();
  138. if($page != null){
  139. return view('frontend.custom_page', compact('page'));
  140. }
  141. abort(404);
  142. }
  143. public function mobile_custom_page($slug){
  144. $page = Page::where('slug', $slug)->first();
  145. if($page != null){
  146. return view('frontend.m_custom_page', compact('page'));
  147. }
  148. abort(404);
  149. }
  150. }