PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/Admin/NewsController.php

https://gitlab.com/tonycodes/Laravel-5-Bootstrap-3-Starter-Site
PHP | 193 lines | 121 code | 19 blank | 53 comment | 6 complexity | 2d3b64525f2adb7e6fcd315800cc79f6 MD5 | raw file
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Controllers\AdminController;
  3. use App\News;
  4. use App\NewsCategory;
  5. use App\Language;
  6. use Bllim\Datatables\Facade\Datatables;
  7. use Illuminate\Support\Facades\Input;
  8. use App\Http\Requests\Admin\NewsRequest;
  9. use App\Http\Requests\Admin\DeleteRequest;
  10. use App\Http\Requests\Admin\ReorderRequest;
  11. use Illuminate\Support\Facades\Auth;
  12. class NewsController extends AdminController {
  13. /*
  14. * Display a listing of the resource.
  15. *
  16. * @return Response
  17. */
  18. public function index()
  19. {
  20. // Show the page
  21. return view('admin.news.index');
  22. }
  23. /**
  24. * Show the form for creating a new resource.
  25. *
  26. * @return Response
  27. */
  28. public function getCreate()
  29. {
  30. $languages = Language::all();
  31. $language = "";
  32. $newscategories = NewsCategory::all();
  33. $newscategory = "";
  34. // Show the page
  35. return view('admin.news.create_edit', compact('languages', 'language','newscategories','newscategory'));
  36. }
  37. /**
  38. * Store a newly created resource in storage.
  39. *
  40. * @return Response
  41. */
  42. public function postCreate(NewsRequest $request)
  43. {
  44. $news = new News();
  45. $news -> user_id = Auth::id();
  46. $news -> language_id = $request->language_id;
  47. $news -> title = $request->title;
  48. $news -> newscategory_id = $request->newscategory_id;
  49. $news -> introduction = $request->introduction;
  50. $news -> content = $request->content;
  51. $news -> source = $request->source;
  52. $picture = "";
  53. if(Input::hasFile('picture'))
  54. {
  55. $file = Input::file('picture');
  56. $filename = $file->getClientOriginalName();
  57. $extension = $file -> getClientOriginalExtension();
  58. $picture = sha1($filename . time()) . '.' . $extension;
  59. }
  60. $news -> picture = $picture;
  61. $news -> save();
  62. if(Input::hasFile('picture'))
  63. {
  64. $destinationPath = public_path() . '/images/news/'.$news->id.'/';
  65. Input::file('picture')->move($destinationPath, $picture);
  66. }
  67. }
  68. /**
  69. * Show the form for editing the specified resource.
  70. *
  71. * @param int $id
  72. * @return Response
  73. */
  74. public function getEdit($id)
  75. {
  76. $news = News::find($id);
  77. $languages = Language::all();
  78. $language = $news->language_id;
  79. $newscategories = NewsCategory::all();
  80. $newscategory = $news->newscategory_id;
  81. return view('admin.news.create_edit',compact('news','languages','language','newscategories','newscategory'));
  82. }
  83. /**
  84. * Update the specified resource in storage.
  85. *
  86. * @param int $id
  87. * @return Response
  88. */
  89. public function postEdit(NewsRequest $request, $id)
  90. {
  91. $news = News::find($id);
  92. $news -> user_id = Auth::id();
  93. $news -> language_id = $request->language_id;
  94. $news -> title = $request->title;
  95. $news -> newscategory_id = $request->newscategory_id;
  96. $news -> introduction = $request->introduction;
  97. $news -> content = $request->content;
  98. $news -> source = $request->source;
  99. $picture = "";
  100. if(Input::hasFile('picture'))
  101. {
  102. $file = Input::file('picture');
  103. $filename = $file->getClientOriginalName();
  104. $extension = $file -> getClientOriginalExtension();
  105. $picture = sha1($filename . time()) . '.' . $extension;
  106. }
  107. $news -> picture = $picture;
  108. $news -> save();
  109. if(Input::hasFile('picture'))
  110. {
  111. $destinationPath = public_path() . '/images/news/'.$news->id.'/';
  112. Input::file('picture')->move($destinationPath, $picture);
  113. }
  114. }
  115. /**
  116. * Remove the specified resource from storage.
  117. *
  118. * @param $id
  119. * @return Response
  120. */
  121. public function getDelete($id)
  122. {
  123. $news = News::find($id);
  124. // Show the page
  125. return view('admin.news.delete', compact('news'));
  126. }
  127. /**
  128. * Remove the specified resource from storage.
  129. *
  130. * @param $id
  131. * @return Response
  132. */
  133. public function postDelete(DeleteRequest $request,$id)
  134. {
  135. $news = News::find($id);
  136. $news->delete();
  137. }
  138. /**
  139. * Show a list of all the languages posts formatted for Datatables.
  140. *
  141. * @return Datatables JSON
  142. */
  143. public function data()
  144. {
  145. $news = News::join('language', 'language.id', '=', 'news.language_id')
  146. ->join('news_category', 'news_category.id', '=', 'news.newscategory_id')
  147. ->select(array('news.id','news.title','news_category.title as category', 'language.name', 'news.created_at'))
  148. ->orderBy('news.position', 'ASC');
  149. return Datatables::of($news)
  150. ->add_column('actions', '<a href="{{{ URL::to(\'admin/news/\' . $id . \'/edit\' ) }}}" class="btn btn-success btn-sm iframe" ><span class="glyphicon glyphicon-pencil"></span> {{ Lang::get("admin/modal.edit") }}</a>
  151. <a href="{{{ URL::to(\'admin/news/\' . $id . \'/delete\' ) }}}" class="btn btn-sm btn-danger iframe"><span class="glyphicon glyphicon-trash"></span> {{ Lang::get("admin/modal.delete") }}</a>
  152. <input type="hidden" name="row" value="{{$id}}" id="row">')
  153. ->remove_column('id')
  154. ->make();
  155. }
  156. /**
  157. * Reorder items
  158. *
  159. * @param items list
  160. * @return items from @param
  161. */
  162. public function getReorder(ReorderRequest $request) {
  163. $list = $request->list;
  164. $items = explode(",", $list);
  165. $order = 1;
  166. foreach ($items as $value) {
  167. if ($value != '') {
  168. News::where('id', '=', $value) -> update(array('position' => $order));
  169. $order++;
  170. }
  171. }
  172. return $list;
  173. }
  174. }