Skip to content

Instantly share code, notes, and snippets.

@betinho37
Created September 5, 2018 18:01
Show Gist options
  • Save betinho37/65e833e9fd1acae10f0839ef8496619d to your computer and use it in GitHub Desktop.
Save betinho37/65e833e9fd1acae10f0839ef8496619d to your computer and use it in GitHub Desktop.
Git
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Publicacao;
use App\User;
use App\Categoria;
use Illuminate\Support\Facades\DB;
use Storage;
use File;
class PublicacaoController extends Controller
{
private $publicacao, $user, $categoria;
public function __construct(Publicacao $publicacao, User $user, Categoria $categoria)
{
$this->publicacao = $publicacao;
$this->user = $user;
$this->categoria = $categoria;
}
public function index()
{
/*$publicacao = $this->publicacao->all();
return view('publicacoes.index', compact('publicacao'));*/
$publicacao = Publicacao::where('situacao', '=', 1)->get();
return view('publicacoes.index', ['publicacao' => $publicacao]);
}
public function home()
{
$tipousuario = auth()->user()->tipousuario;
$publicacao = $this->publicacao;
if($tipousuario != 0) {
$publicacao = $publicacao->where('publicacoes.userid', '=', auth()->user()->id)
->orderBy('situacao', 'asc')
->simplePaginate(100);
return view('publicacoes.controle', compact('publicacao'));
}
$publicacao = $publicacao->orderBy('situacao', 'asc')->get();
return view('admin.home', compact('publicacao'));
}
public function show()
{
$publicacao = Publicacao::find($id);
return view('publicacoes.carrousel', compact('publicacao'));
}
public function create()
{
$publicacao = Publicacao::get();
$list_user = $this->user->listUser();
$list_categoria = $this->categoria->listCategoria();
return view('publicacoes.create', compact('publicacao','list_user', 'list_categoria'));
}
public function store(Request $request)
{
$files =[];
if ($request->file('arquivo')) $files[] = $request->file('arquivo');
foreach ($files as $file)
{
if(!empty($file)){
$destinationPath = public_path().DIRECTORY_SEPARATOR.'uploads';
$extension=$file->getClientOriginalExtension();
$filename= str_random (10).'.'.$extension;
$file->move($destinationPath, $filename);
}
}
if ($request->file('pdf')) $files[] = $request->file('pdf');
foreach ($files as $file)
{
if(!empty($file)){
$destinationPath = public_path().DIRECTORY_SEPARATOR.'uploads';
$extension=$file->getClientOriginalExtension();
$filename= str_random (10).'.'.$extension;
$file->move($destinationPath, $filename);
}
}
}
public function edit($id)
{
$list_user = $this->user->listUser();
$list_categoria = $this->categoria->listCategoria();
$publicacao = Publicacao::find($id);
return view('publicacoes.edit', compact('publicacao','list_user','list_categoria'));
}
public function update(Request $request, $id)
{
$publicacao = Publicacao::find($id);
//var_dump(\Input::file('arquivo')); die();
if ($file = \Input::file('arquivo'))
{
$destinationPath = public_path().DIRECTORY_SEPARATOR.'uploads';
$extension = $file->getClientOriginalExtension();
$arquivo['arquivo'] = str_random (30).'.'.$extension;
$name['arquivo'] = $file->getClientOriginalName();
$file->move( $destinationPath, $name['arquivo'] );
$publicacao->arquivo = $name['arquivo'];
$publicacao->save();
}
$publicacao->situacao = $request->situacao;
$publicacao->categoriaid= $request->categoriaid;
$publicacao->posicaoimagem= $request->posicaoimagem;
$publicacao->descricao= $request->descricao;
$publicacao->titulo= $request->titulo;
$publicacao->nome= $request->nome;
$publicacao->save();
return redirect()->action('PublicacaoController@controle');
}
public function controle()
{
$tipousuario = auth()->user()->tipousuario;
$publicacao = $this->publicacao;
if($tipousuario != 0) {
$publicacao = $publicacao->where('publicacoes.userid', '=', auth()->user()->id)
->orderBy('situacao', 'asc')
->orderBy('created_at', 'desc');
}
$publicacao = $publicacao->orderBy('situacao', 'asc')
->orderBy('created_at', 'desc')
->simplePaginate(10);
return view('publicacoes.controle', compact('publicacao'));
}
public function categorias()
{
$publicacao = $this->categoria->all();
return view('publicacoes.categorias.index', compact('publicacao'));
}
public function pinturas()
{
//$publicacao = DB::table('publicacoes')->simplePaginate(6);
$publicacao = Publicacao::where('categoriaid', '=', 2)->Simplepaginate(6);
return view('publicacoes.categorias.pinturas.index', ['publicacao' => $publicacao]);
}
public function livros()
{
$publicacao = Publicacao::where('categoriaid', '=', 1)->simplePaginate(6);
return view('publicacoes.categorias.livros.index', ['publicacao' => $publicacao]);
}
public function gravuras()
{
$publicacao = Publicacao::where('categoriaid', '=', 3)->simplePaginate(6);
return view('publicacoes.categorias.gravuras.index', ['publicacao' => $publicacao]);
}
public function deletfile($name)
{
$publicacao = Publicacao::where('arquivo', $name)->first();
$publicacao->arquivo = null;
unlink(public_path('uploads/'. $name));
$publicacao->save();
return redirect()->back();
}
public function download($id)
{
$publicacao = Publicacao::find($id);
$filename = $publicacao->arquivo;
return response()->download(public_path("uploads/{$filename}"));
}
public function destroy($id)
{
$publicacao = Publicacao::find($id);
if ($publicacao->arquivo) {
unlink(public_path('uploads/' . $publicacao->arquivo));
$publicacao->delete();
}
$publicacao->delete();
return redirect()->back();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment