Skip to content

Instantly share code, notes, and snippets.

@betinho37
Last active June 1, 2019 13:00
Show Gist options
  • Save betinho37/8f94d3e9c6ecdf68eeec3dfda1bcddf4 to your computer and use it in GitHub Desktop.
Save betinho37/8f94d3e9c6ecdf68eeec3dfda1bcddf4 to your computer and use it in GitHub Desktop.
@extends('adminlte::page')
@section('title', 'Museu Virtual')
@section('content')
<div class="col-md-8 col-md-offset-2">
<h2 class="text-center">Manage Posts</h2>
<br />
<div class="panel panel-default">
<div class="panel-heading">
<ul>
<li><i class="fa fa-file-text-o"></i> All the current Posts</li>
<a href="#" class="add-modal"><li>Add a Post</li></a>
</ul>
</div>
<div class="panel-body">
<table class="table table-striped table-hover ">
<th>Nome</th>
<th>Email</th>
<th>Opcões</th>
@foreach($usuario as $usuario)
<tr>
<td>{{$usuario -> name }}</td>
<td>{{$usuario -> email }}</td>
<td><a href="{{@url('usuario').'/' . $usuario->id .'/'. 'edit' }}" class="btn btn-primary">Editar</a>
<a href="{{@url('/api/usuario').'/destroy/'.$usuario->id.''}}" class="btn btn-danger" onclick="return confirm('Tem certeza de que deseja excluir este item ?');" >Excluir</a></td>
</tr>
@endforeach
</table>
</div><!-- /.panel-body -->
</div><!-- /.panel panel-default -->
</div><!-- /.col-md-8 -->
<!-- Modal form to add a post -->
<div id="addModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title"></h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="row my-9">
<div class="col-md-6">
<label >Nome</label>
<input type="name" class="form-control" id="name" name="name" required>
</div>
<div class="col-md-6">
<label>E-mail</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="col-md-6">
<label>Telefone</label>
<input type="telefone" class="form-control" id="telefone" name="telefone" required>
</div>
<div class="col-md-6">
<label>Estado</label>
{!!Form::select('estadoid', $list_estado, null, ['class'=>'form-control '])!!}
</div>
<div class="col-md-6">
<label>Cidade</label>
<input type="text" class="form-control" id="cidade" name="cidade" required>
</div>
<div class="col-md-6">
<label>Endereço</label>
<input type="text" class="form-control" id="endereco" name="endereco" required>
</div>
<div class="col-md-6">
<label>Senha</label>
<input type="password" name="password" id="password" class="form-control" >
</div>
<div class="col-md-6">
<label>Confirmar Senha</label>
<input type="password" name="password_confirmation" id="password_confirmation" class="form-control"
placeholder="{{ trans('adminlte::adminlte.retype_password') }}" >
</div>
<input type="hidden" id="emailValidateNew" name="emailValidate" value="false">
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-success add" data-dismiss="modal">
<span id="" class='glyphicon glyphicon-check'></span> Add
</button>
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery -->
{{-- <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> --}}
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
<!-- Bootstrap JavaScript -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.1/js/bootstrap.min.js"></script>
<!-- toastr notifications -->
{{-- <script type="text/javascript" src="{{ asset('toastr/toastr.min.js') }}"></script> --}}
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<!-- icheck checkboxes -->
<script type="text/javascript" src="{{ asset('icheck/icheck.min.js') }}"></script>
<!-- Delay table load until everything else is loaded -->
<script>
$(window).load(function(){
$('#usuario').removeAttr('style');
})
</script>
<script>
$('.published').on('ifClicked', function(event){
id = $(this).data('id');
$.ajax({
type: 'POST',
url: "{{ URL::route('usuario') }}",
data: {
'_token': $('input[name=_token]').val(),
'id': id
},
success: function(data) {
// empty
},
});
});
$('.published').on('ifToggled', function(event) {
$(this).closest('tr').toggleClass('warning');
});
</script>
<!-- AJAX CRUD operations -->
<script type="text/javascript">
// add a new post
$(document).on('click', '.add-modal', function() {
// Empty input fields
$('#name').val('');
$('#email').val('');
$('#cep').val('');
$('#endereco').val('');
$('#telefone').val('');
$('#cidade').val('');
$('#estadoid').val('');
$('#password').val('');
$('#password_confirmation').val('');
$('.modal-title').text('Add');
$('#addModal').modal('show');
});
$('.modal-footer').on('click', '.add', function() {
$.ajax({
type: 'POST',
url: 'usuario',
data: {
'_token': $('input[name=_token]').val(),
'name': $('#name').val(),
'email': $('#email').val(),
'cep':$('#cep').val(''),
'endereco':$('#endereco').val(''),
'telefone':$('#telefone').val(''),
'tipousuario':$('#tipousuario').val(''),
'cidade':$('#cidade').val(''),
'estadoid':$('#estadoid').val(''),
'password': $('#password').val(),
'password_confirmation': $('#password_confirmation').val(),
},
success: function(data) {
$('.errorpassword').addClass('hidden');
$('.erroremail').addClass('hidden');
if ((data.errors)) {
setTimeout(function () {
$('#addModal').modal('show');
toastr.error('Validation error!', 'Error Alert', {timeOut: 5000});
}, 500);
if (data.errors.passoword) {
$('.errorpassword').removeClass('hidden');
$('.errorpassword').text(data.errors.password);
}
if (data.errors.email) {
$('.erroremail').removeClass('hidden');
$('.erroremail').text(data.errors.email);
}
} else {
toastr.success('Successfully added Post!', 'Success Alert', {timeOut: 5000});
$('#usuario').prepend("<tr class='item" + data.id + "'><td class='col1'>" + data.id + "</td><td>" + data.title + "</td><td>" + data.content + "</td><td class='text-center'><input type='checkbox' class='new_published' data-id='" + data.id + " '></td><td>Just now!</td><td><button class='show-modal btn btn-success' data-id='" + data.id + "' data-title='" + data.title + "' data-content='" + data.content + "'><span class='glyphicon glyphicon-eye-open'></span> Show</button> <button class='edit-modal btn btn-info' data-id='" + data.id + "' data-title='" + data.title + "' data-content='" + data.content + "'><span class='glyphicon glyphicon-edit'></span> Edit</button> <button class='delete-modal btn btn-danger' data-id='" + data.id + "' data-title='" + data.title + "' data-content='" + data.content + "'><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>");
$('.new_published').iCheck({
checkboxClass: 'icheckbox_square-yellow',
radioClass: 'iradio_square-yellow',
increaseArea: '20%'
});
$('.new_published').on('ifToggled', function(event){
$(this).closest('tr').toggleClass('warning');
});
$('.new_published').on('ifChanged', function(event){
id = $(this).data('id');
$.ajax({
type: 'POST',
url: "{{ URL::route('changeStatus') }}",
data: {
'_token': $('input[name=_token]').val(),
'id': id
},
success: function(data) {
// empty
},
});
});
$('.col1').each(function (index) {
$(this).html(index+1);
});
}
},
});
});
</script>
@endsection
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Estado;
use Illuminate\Support\Facades\Auth;
use DB;
use Response;
use Illuminate\Support\Facades\Input;
class UsuariosController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
private $usuario, $estado;
public function __construct(User $usuario, Estado $estado)
{
$this->usuario = $usuario;
$this->estado = $estado;
}
public function index()
{
$ids = DB::table('users')->select('id')->get();
$total_ids = $ids->sum('id');
$usuario = $this->usuario->all();
$list_estado = $this->estado->listEstado();
return view('admin.index', compact('usuario', 'total_ids','list_estado'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$list_estado = $this->estado->listEstado();
return view('admin.create', compact('list_estado'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request->all());
/* $inputs = $request->all();
$validator = $this->validator($inputs);
if ($validator->fails()) {
return redirect('usuario/create')
->withErrors($validator)
->withInput();
}
$inputs['password'] = bcrypt($inputs['password']);
$this->usuario->create($inputs);
$credentials = $request->only('email', 'password');
if (Auth::check()) {
return redirect()->action('UsuariosController@index');
} else {
if (Auth::attempt($credentials)) {
return redirect()->intended('api/home');
}
}
*/
}
/**
* Display the specified resource.
*
* @param \App\Usuario $usuario
* @return \Illuminate\Http\Response
*/
public function show(User $usuario)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Usuario $usuario
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$list_estado = $this->estado->listEstado();
$usuario = User::find($id);
return view('admin.edit', compact('usuario', 'list_estado'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Usuario $usuario
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$usuario = User::find($id);
$usuario->fill($request->all());
if ($usuario['password'] != null){
$usuario['password'] = bcrypt($usuario['password']);
}
else
unset($usuario['password']);
$usuario->cidade= $request->cidade;
$usuario->save();
return redirect()->action('UsuariosController@index');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Usuario $usuario
* @return \Illuminate\Http\Response
*/
public function destroy(User $usuario, $id)
{
$usuario = User::find($id);
$usuario->delete();
return redirect()->back();
}
protected function validator(array $data)
{
return Validator::make($data, [
'password' => 'required|string|min:6|confirmed',
]);
}
public function perfil($id)
{
$list_estado = $this->estado->listEstado();
$usuario = User::find($id);
return view('admin.create', compact('usuario', 'list_estado'));
}
public function changeStatus()
{
$id = Input::get('id');
$usuario = User::findOrFail($id);
$usuario->is_published = !$usuario->is_published;
$usuario->save();
return response()->json($usuario);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment