PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/Admin/PhotoController.php

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