PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Http/Controllers/Admin/VideoController.php

https://gitlab.com/tonycodes/Laravel-5-Bootstrap-3-Starter-Site
PHP | 244 lines | 154 code | 24 blank | 66 comment | 9 complexity | 07370274b55329f7afc531cb42563dda MD5 | raw file
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Controllers\AdminController;
  3. use App\Video;
  4. use App\VideoAlbum;
  5. use App\Language;
  6. use Bllim\Datatables\Facade\Datatables;
  7. use App\Http\Requests\Admin\VideoRequest;
  8. use App\Http\Requests\Admin\ReorderRequest;
  9. use App\Http\Requests\Admin\DeleteRequest;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Input;
  12. use Illuminate\Support\Facades\DB;
  13. class VideoController extends AdminController {
  14. /*
  15. * Display a listing of the resource.
  16. *
  17. * @return Response
  18. */
  19. public function index()
  20. {
  21. // Show the page
  22. return view('admin.video.index');
  23. }
  24. /**
  25. * Show a list of all the photo posts.
  26. *
  27. * @return View
  28. */
  29. public function itemsForAlbum($id) {
  30. $album = VideoAlbum::find($id);
  31. // Show the page
  32. return view('admin.video.index', compact('album'));
  33. }
  34. /**
  35. * Show the form for creating a new resource.
  36. *
  37. * @return Response
  38. */
  39. public function getCreate()
  40. {
  41. $languages = Language::all();
  42. $language = "";
  43. $videoalbums = VideoAlbum::all();
  44. $videoalbum = "";
  45. // Show the page
  46. return view('admin.video.create_edit', compact('languages', 'language','videoalbums','videoalbum'));
  47. }
  48. /**
  49. * Store a newly created resource in storage.
  50. *
  51. * @return Response
  52. */
  53. public function postCreate(VideoRequest $request)
  54. {
  55. $video = new Video();
  56. $video -> user_id = Auth::id();
  57. $video -> language_id = $request->language_id;
  58. $video -> name = $request->name;
  59. $video -> video_album_id = $request->video_album_id;
  60. $video -> description = $request->description;
  61. $video -> album_cover = $request->album_cover;
  62. if($request->youtube!="") {
  63. $youtube = explode('http://www.youtube.com/watch?v=', $request->youtube);
  64. $video->youtube = rtrim($youtube[1]);
  65. }
  66. $video_file = "";
  67. if(Input::hasFile('video'))
  68. {
  69. $file = Input::file('video');
  70. $filename = $file->getClientOriginalName();
  71. $extension = $file -> getClientOriginalExtension();
  72. $video_file = sha1($filename . time()) . '.' . $extension;
  73. $video -> filename = $video_file;
  74. }
  75. $video -> save();
  76. if(Input::hasFile('video'))
  77. {
  78. $videoalbum = VideoAlbum::find($request->video_album_id);
  79. $destinationPath = public_path() . '/images/videoalbum/'.$videoalbum->folderid.'/';
  80. Input::file('video')->move($destinationPath, $video_file);
  81. }
  82. }
  83. /**
  84. * Show the form for editing the specified resource.
  85. *
  86. * @param int $id
  87. * @return Response
  88. */
  89. public function getEdit($id)
  90. {
  91. $video = Video::find($id);
  92. if($video ->youtube!="")
  93. {
  94. $video->youtube = 'http://www.youtube.com/watch?v='.$video->youtube;
  95. }
  96. $languages = Language::all();
  97. $language = $video->language_id;
  98. $videoalbums = VideoAlbum::all();
  99. $videoalbum = $video->photo_album_id;
  100. return view('admin.video.create_edit',compact('video','languages','language','videoalbums','videoalbum'));
  101. }
  102. /**
  103. * Update the specified resource in storage.
  104. *
  105. * @param int $id
  106. * @return Response
  107. */
  108. public function postEdit(VideoRequest $request, $id)
  109. {
  110. $video = Video::find($id);
  111. $video -> user_id = Auth::id();
  112. $video -> language_id = $request->language_id;
  113. $video -> language_id = $request->language_id;
  114. $video -> name = $request->name;
  115. $video -> video_album_id = $request->video_album_id;
  116. $video -> description = $request->description;
  117. $video -> album_cover = $request->album_cover;
  118. if($request->youtube!="") {
  119. $youtube = explode('http://www.youtube.com/watch?v=', $request->youtube);
  120. $video->youtube = rtrim($youtube[1]);
  121. }
  122. $video_file = $video->filename;
  123. if(Input::hasFile('video'))
  124. {
  125. $file = Input::file('video');
  126. $filename = $file->getClientOriginalName();
  127. $extension = $file -> getClientOriginalExtension();
  128. $video_file = sha1($filename . time()) . '.' . $extension;
  129. $video -> filename = $video_file;
  130. }
  131. $video -> save();
  132. if(Input::hasFile('video'))
  133. {
  134. $videoalbum = VideoAlbum::find($request->photo_album_id);
  135. $destinationPath = public_path() . '/images/videoalbum/'.$videoalbum->folderid.'/';
  136. Input::file('video')->move($destinationPath, $video_file);
  137. }
  138. }
  139. /**
  140. * Remove the specified resource from storage.
  141. *
  142. * @param $id
  143. * @return Response
  144. */
  145. public function getDelete($id)
  146. {
  147. $video = Video::find($id);
  148. // Show the page
  149. return view('admin.video.delete', compact('video'));
  150. }
  151. /**
  152. * Remove the specified resource from storage.
  153. *
  154. * @param $id
  155. * @return Response
  156. */
  157. public function postDelete(DeleteRequest $request,$id)
  158. {
  159. $video = Video::find($id);
  160. $video->delete();
  161. }
  162. /**
  163. * Set a Album cover.
  164. *
  165. * @param $id
  166. * @return Response
  167. */
  168. public function getAlbumCover($id,$album=0)
  169. {
  170. $video = Video::find($id);
  171. $videoalbums = Video::where('video_album_id',$video->video_album_id)->get();
  172. foreach($videoalbums as $item)
  173. {
  174. $item -> album_cover = 0;
  175. $item -> save();
  176. }
  177. $video -> album_cover = 1;
  178. $video -> save();
  179. // Show the page
  180. return redirect( (($album==0)?'/admin/video':'/admin/video/'.$album.'/itemsforalbum'));
  181. }
  182. /**
  183. * Show a list of all the languages posts formatted for Datatables.
  184. *
  185. * @return Datatables JSON
  186. */
  187. public function data($albumid=0) {
  188. $condition =(intval($albumid)==0)?">":"=";
  189. $photoalbum = Video::join('language', 'language.id', '=', 'video.language_id')
  190. ->join('video_album', 'video_album.id', '=', 'video.video_album_id')
  191. ->where('video.video_album_id',$condition,$albumid)
  192. ->orderBy('video.position')
  193. ->select(array('video.id',DB::raw($albumid . ' as albumid'), 'video.name','video_album.name as category',
  194. 'video.album_cover','language.name as language', 'video.created_at'));
  195. return Datatables::of($photoalbum)
  196. -> edit_column('album_cover', '<a href="{{{ URL::to(\'admin/video/\' . $id . \'/\' . $albumid . \'/albumcover\' ) }}}" class="btn btn-warning btn-sm" >@if ($album_cover=="1") <span class="glyphicon glyphicon-ok"></span> @else <span class=\'glyphicon glyphicon-remove\'></span> @endif</a>')
  197. -> add_column('actions', '<a href="{{{ URL::to(\'admin/video/\' . $id . \'/edit\' ) }}}" class="btn btn-success btn-sm iframe" ><span class="glyphicon glyphicon-pencil"></span> {{ Lang::get("admin/modal.edit") }}</a>
  198. <a href="{{{ URL::to(\'admin/video/\' . $id . \'/delete\' ) }}}" class="btn btn-sm btn-danger iframe"><span class="glyphicon glyphicon-trash"></span> {{ Lang::get("admin/modal.delete") }}</a>
  199. <input type="hidden" name="row" value="{{$id}}" id="row">') -> remove_column('id')
  200. -> remove_column('albumid')-> make();
  201. }
  202. /**
  203. * Reorder items
  204. *
  205. * @param items list
  206. * @return items from @param
  207. */
  208. public function getReorder(ReorderRequest $request) {
  209. $list = $request->list;
  210. $items = explode(",", $list);
  211. $order = 1;
  212. foreach ($items as $value) {
  213. if ($value != '') {
  214. Video::where('id', '=', $value) -> update(array('position' => $order));
  215. $order++;
  216. }
  217. }
  218. return $list;
  219. }
  220. }