SectionController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Section;
  4. use App\News;
  5. use App\SectionAdv;
  6. use Illuminate\Http\Request;
  7. class SectionController extends Controller
  8. {
  9. protected $rules = [
  10. 'name' => ['required']
  11. ];
  12. public function index()
  13. {
  14. $sections = Section::orderBy('position')->get(); //->paginate(50);
  15. return view('sections.index',compact('sections'))
  16. ->with('i', (request()->input('page', 1) - 1) * 5);
  17. }
  18. /**
  19. * Show the form for creating a new resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function create()
  24. {
  25. return view('sections.create');
  26. }
  27. /**
  28. * Store a newly created resource in storage.
  29. *
  30. * @param \Illuminate\Http\Request $request
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function store(Request $request)
  34. {
  35. $request->validate($this->rules);
  36. $input = $request->all();
  37. $input["position"] = $input["position"] != '' ? $input["position"] : 0;
  38. $input["before"] = isset($input["before"]) ? ($input["before"] == 'on' ? true : false) : false;
  39. $input["after"] = isset($input["after"]) ? ($input["after"] == 'on' ? true : false) : false;
  40. $input["show_social"] = isset($input["show_social"]) ? ($input["show_social"] == 'on' ? true : false) : false;
  41. $input["show_social_twitter"] = isset($input["show_social_twitter"]) ? ($input["show_social_twitter"] == 'on' ? true : false) : false;
  42. Section::create($input);
  43. return redirect()->route('sections.index')
  44. ->with('success','Section created successfully.');
  45. }
  46. /**
  47. * Display the specified resource.
  48. *
  49. * @param \App\Section $section
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function show(Section $section)
  53. {
  54. return view('sections.show',compact('section'));
  55. }
  56. /**
  57. * Show the form for editing the specified resource.
  58. *
  59. * @param \App\Section $section
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function edit(Section $section)
  63. {
  64. return view('sections.edit',compact('section'));
  65. }
  66. /**
  67. * Update the specified resource in storage.
  68. *
  69. * @param \Illuminate\Http\Request $request
  70. * @param \App\Section $section
  71. * @return \Illuminate\Http\Response
  72. */
  73. public function update(Request $request, Section $section)
  74. {
  75. $request->validate($this->rules);
  76. $input = $request->all();
  77. $input["position"] = $input["position"] != '' ? $input["position"] : 0;
  78. $input["before"] = isset($input["before"]) ? ($input["before"] == 'on' ? true : false) : false;
  79. $input["after"] = isset($input["after"]) ? ($input["after"] == 'on' ? true : false) : false;
  80. $input["show_social"] = isset($input["show_social"]) ? ($input["show_social"] == 'on' ? true : false) : false;
  81. $input["show_social_twitter"] = isset($input["show_social_twitter"]) ? ($input["show_social_twitter"] == 'on' ? true : false) : false;
  82. $section->update($input);
  83. return redirect()->route('sections.index')
  84. ->with('success','Section updated successfully');
  85. }
  86. /**
  87. * Remove the specified resource from storage.
  88. *
  89. * @param \App\Section $section
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function destroy(Section $section)
  93. {
  94. $news = News::where(function ($q) use ($section) {
  95. $q->where('section_id', '=', $section->id)->orWhere('region_1_id', '=', $section->id)->orWhere('region_2_id', '=', $section->id);
  96. })->get();
  97. foreach($news as $n)
  98. {
  99. if ($n->section_id == $section->id)
  100. $n->section_id = null;
  101. if ($n->region_1_id == $section->id)
  102. $n->region_1_id = null;
  103. if ($n->region_2_id == $section->id)
  104. $n->region_2_id = null;
  105. $n->save();
  106. }
  107. SectionAdv::where('section_id', '=', $section->id)->delete();
  108. $section->delete();
  109. return redirect()->route('sections.index')
  110. ->with('success','Section deleted successfully');
  111. }
  112. }