/app/Laravel/Controllers/Backoffice/PageContentController.php

https://bitbucket.org/cityserv/techreportph · PHP · 175 lines · 127 code · 32 blank · 16 comment · 15 complexity · d1dfcd5bb4640a5584a8a2031b14a7fc MD5 · raw file

  1. <?php namespace App\Laravel\Controllers\Backoffice;
  2. /**
  3. *
  4. * Models used for this controller
  5. */
  6. use App\Laravel\Models\PageContent;
  7. /**
  8. *
  9. * Requests used for validating inputs
  10. */
  11. use App\Laravel\Requests\Backoffice\PageContentRequest;
  12. /**
  13. *
  14. * Classes used for this controller
  15. */
  16. use App\Http\Requests\Request;
  17. use Input, Helper, Carbon, Session, Str, File, Image, ImageUploader;
  18. class PageContentController extends Controller{
  19. /**
  20. *
  21. * @var Array $data
  22. */
  23. protected $data;
  24. public function __construct () {
  25. parent::__construct();
  26. $view = Input::get('view','table');
  27. array_merge($this->data, parent::get_data());
  28. $this->data['page_title'] = "Page Content";
  29. $this->data['page_description'] = "This is the general information about ".$this->data['page_title'].".";
  30. $this->data['route_file'] = "page_content";
  31. $this->data['page_locations'] = ['' => "Choose page location", 'asean_bac' => "ASEAN BAC", 'aba' => "ABA", 'about_abac_footer' => "ABOUT ABAC FOOTER", 'members' => "MEMBERS", 'abis' => "ABIS"];
  32. }
  33. public function index () {
  34. $this->data['page_content'] = PageContent::orderBy('created_at',"DESC")->get();
  35. return view('backoffice.'.$this->data['route_file'].'.index',$this->data);
  36. }
  37. public function create () {
  38. return view('backoffice.'.$this->data['route_file'].'.create',$this->data);
  39. }
  40. public function store (PageContentRequest $request) {
  41. try {
  42. $new_page_content = new PageContent;
  43. $new_page_content->fill($request->all());
  44. if($request->hasFile('file')){
  45. $upload = ImageUploader::upload($request->file,'storage/page_content');
  46. $new_page_content->path = $upload["path"];
  47. $new_page_content->directory = $upload["directory"];
  48. $new_page_content->filename = $upload["filename"];
  49. }
  50. if($new_page_content->save()) {
  51. Session::flash('notification-status','success');
  52. Session::flash('notification-msg',"New page content has been added.");
  53. return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
  54. }
  55. Session::flash('notification-status','failed');
  56. Session::flash('notification-msg','Something went wrong.');
  57. return redirect()->back();
  58. } catch (Exception $e) {
  59. Session::flash('notification-status','failed');
  60. Session::flash('notification-msg',$e->getMessage());
  61. return redirect()->back();
  62. }
  63. }
  64. public function edit ($id = NULL) {
  65. $page_content = PageContent::find($id);
  66. if (!$page_content) {
  67. Session::flash('notification-status',"failed");
  68. Session::flash('notification-msg',"Record not found.");
  69. return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
  70. }
  71. $this->data['page_content'] = $page_content;
  72. return view('backoffice.'.$this->data['route_file'].'.edit',$this->data);
  73. }
  74. public function update (PageContentRequest $request, $id = NULL) {
  75. try {
  76. $page_content = PageContent::find($id);
  77. if (!$page_content) {
  78. Session::flash('notification-status',"failed");
  79. Session::flash('notification-msg',"Record not found.");
  80. return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
  81. }
  82. $page_content->fill($request->all());
  83. if($request->hasFile('file')){
  84. $upload = ImageUploader::upload($request->file,'storage/page_content');
  85. if($upload){
  86. if (File::exists("{$page_content->directory}/{$page_content->filename}")){
  87. File::delete("{$page_content->directory}/{$page_content->filename}");
  88. }
  89. if (File::exists("{$page_content->directory}/resized/{$page_content->filename}")){
  90. File::delete("{$page_content->directory}/resized/{$page_content->filename}");
  91. }
  92. if (File::exists("{$page_content->directory}/thumbnails/{$page_content->filename}")){
  93. File::delete("{$page_content->directory}/thumbnails/{$page_content->filename}");
  94. }
  95. }
  96. $page_content->path = $upload["path"];
  97. $page_content->directory = $upload["directory"];
  98. $page_content->filename = $upload["filename"];
  99. }
  100. if($page_content->save()) {
  101. Session::flash('notification-status','success');
  102. Session::flash('notification-msg',"A page content has been updated.");
  103. return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
  104. }
  105. Session::flash('notification-status','failed');
  106. Session::flash('notification-msg','Something went wrong.');
  107. } catch (Exception $e) {
  108. Session::flash('notification-status','failed');
  109. Session::flash('notification-msg',$e->getMessage());
  110. return redirect()->back();
  111. }
  112. }
  113. public function destroy ($id = NULL) {
  114. try {
  115. $page_content = PageContent::find($id);
  116. if (!$page_content) {
  117. Session::flash('notification-status',"failed");
  118. Session::flash('notification-msg',"Record not found.");
  119. return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
  120. }
  121. if (File::exists("{$page_content->directory}/{$page_content->filename}")){
  122. File::delete("{$page_content->directory}/{$page_content->filename}");
  123. }
  124. if (File::exists("{$page_content->directory}/resized/{$page_content->filename}")){
  125. File::delete("{$page_content->directory}/resized/{$page_content->filename}");
  126. }
  127. if (File::exists("{$page_content->directory}/thumbnails/{$page_content->filename}")){
  128. File::delete("{$page_content->directory}/thumbnails/{$page_content->filename}");
  129. }
  130. if($page_content->save() AND $page_content->delete()) {
  131. Session::flash('notification-status','success');
  132. Session::flash('notification-msg',"A page content has been deleted.");
  133. return redirect()->route('backoffice.'.$this->data['route_file'].'.index');
  134. }
  135. Session::flash('notification-status','failed');
  136. Session::flash('notification-msg','Something went wrong.');
  137. } catch (Exception $e) {
  138. Session::flash('notification-status','failed');
  139. Session::flash('notification-msg',$e->getMessage());
  140. return redirect()->back();
  141. }
  142. }
  143. }