Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Admin;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Storage;
- use App\Media;
- use App\PrintMedia;
- use App\MediaFiles;
- class MediaController extends Controller
- {
- public function __construct() {
- $this->middleware('auth');
- }
- public function index() {
- if (!\Auth::user()->pode('Media', 'index')) return redirect('/');
- $media = Media::orderBy('id', 'DESC')
- ->has('media_parent', '=', 'null')->paginate(10);
- return View('Admin.media.index', compact('media'));
- }
- public function create() {
- if (!\Auth::user()->pode('Media', 'create')) return redirect('/');
- $print_media = PrintMedia::orderBy('description', 'asc')->get()->pluck('description', 'id');
- return View("Admin.media.create", compact('print_media'));
- }
- public function show($id) {
- if (!\Auth::user()->pode('Media', 'show')) return redirect('/');
- $media = Media::find($id);
- return View('Admin.media.show', compact('media'));
- }
- public function edit($id) {
- if (!\Auth::user()->pode('Media', 'edit')) return redirect('/');
- $print_media = PrintMedia::orderBy('description', 'asc')->get()->pluck('description', 'id');
- $media = Media::find($id);
- return View('Admin.media.edit', compact('print_media', 'media'));
- }
- public function update(Request $request, $id) {
- if (!\Auth::user()->pode('Media', 'edit')) return redirect('/');
- $this->validate($request, Media::rules('PATCH', $id));
- $data = $request->all();
- $media = Media::find($id);
- $file = $request->file('img_file');
- if ($file) {
- $data['img_file'] = $file->getClientOriginalName();
- Storage::disk('s3')->put('/uploads/media/'.$id.'/'.$data['img_file'], file_get_contents($file), ['visibility' => 'public', 'Expires' => '1 year']);
- Storage::disk('s3')->delete('/uploads/media/'.$id.'/'.$media->img_file);
- } else {
- $data['img_file'] = $media->img_file;
- }
- $media->fill($data)->save();
- $this->save_media($request, $media);
- return redirect(action('Admin\MediaController@show', $id));
- }
- public function destroy($id) {
- if (!\Auth::user()->pode('Media', 'destroy')) return redirect('/');
- $media = Media::find($id);
- if ($media) {
- if (count($media->media_files) > 0) {
- foreach ($media->media_files as $md) {
- $md_id = $md->media_id;
- $md->delete();
- $md = Media::find($md_id);
- if ($md) {
- $md->delete();
- }
- }
- } else {
- $md = MediaFiles::where('media_id', $media->id)->first();
- if ($md) {
- $md->delete();
- }
- }
- $media->delete();
- }
- return redirect(action('Admin\MediaController@index'));
- }
- public function store(Request $request) {
- if (!\Auth::user()->pode('Media', 'create')) return redirect('/');
- $data = $request->all();
- if ($data['media_id'] == 1) {
- $this->validate($request, [
- 'media_id' => 'required',
- 'img_file' => 'max:100000|image'
- ]);
- } else {
- $this->validate($request, Media::rules());
- }
- $file = $request->file('img_file');
- $data['img_file'] = $file->getClientOriginalName();
- $data['client_img_file'] = $file->getClientSize();
- $media = Media::create($data);
- Storage::disk('s3')->put('/uploads/media/'.$media->id.'/'.$media->img_file, file_get_contents($file), ['visibility' => 'public', 'Expires' => '1 year']);
- $this->save_media($request, $media);
- return redirect(action('Admin\MediaController@show', $media->id));
- }
- public function media_files() {
- $md = null;
- echo View('Admin.media_files._form', compact('md'))->render();
- }
- public function media_files_destroy($id) {
- $media_files = MediaFiles::find($id);
- if ($media_files) {
- $media = Media::find($media_files->media->id);
- if ($media) {
- $media->delete();
- }
- $media_files->delete();
- return 'true';
- }
- return 'false';
- }
- /**
- *
- */
- public function filter() {
- if ($_GET['search_media'] != '') {
- $media = Media::whereNotIn('id', MediaFiles::select('media_id')->get())
- ->where('media_id', 1)
- ->where(function ($q) {
- $q->where('id', $_GET['search_media'])
- ->orWhere('title', 'LIKE', "%".$_GET['search_media']."%");
- })
- ->orderBy('id', 'desc')->get();
- } else {
- $media = Media::whereNotIn('id', MediaFiles::select('media_id')->get())
- ->where('media_id', 1)
- ->orderBy('id', 'desc')->get();
- }
- $field_name = isset($_GET['field_name']) ? $_GET['field_name'] : 'media_id';
- echo view('Admin.media._filter_each', compact('media', 'field_name'))->render();
- }
- /**
- * @return string
- */
- private function save_media(Request $request, $media) {
- $data = $request->all();
- $msg = "Saved successfully";
- if (isset($request->media_files)) {
- $i = 0;
- $e = 0;
- foreach ($request->media_files as $md) {
- if ($md->getClientSize() > 100000) {
- $e++;
- $msg = $e." Derived images greater than 100kb";
- } else {
- $media_d = Media::create(array(
- 'img_file' => $md->getClientOriginalName(),
- 'media_id' => $media->media_id,
- 'client_img_file' => $md->getClientSize(),
- 'title' => $media->title,
- 'description' => $media->description,
- 'status' => $media->status,
- ));
- Storage::disk('s3')->put('/uploads/media/'.$media_d->id.'/'.$media_d->img_file, file_get_contents($md), ['visibility' => 'public', 'Expires' => '1 year']);
- $media_files = MediaFiles::create(array(
- 'media_id' => $media_d->id,
- 'media_parent_id' => $media->id,
- 'screen' => $data['screen'][$i],
- ));
- }
- $i++;
- }
- }
- return $msg;
- }
- }
Add Comment
Please, Sign In to add comment