Skip to content

Commit

Permalink
Agregar ABM de Areas y Niveles
Browse files Browse the repository at this point in the history
  • Loading branch information
elyambay committed Oct 16, 2018
1 parent ae5b014 commit 4f55953
Show file tree
Hide file tree
Showing 15 changed files with 529 additions and 30 deletions.
4 changes: 4 additions & 0 deletions app/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class Area extends Model
public function detalleEncuestas(){
return $this->hasMany("App\Detalle_encuesta");
}

public function areaEn(){
return $this->hasOne('App\Area_en', 'id' , 'id');
}
}
90 changes: 90 additions & 0 deletions app/Http/Controllers/AreasController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Area;
use App\Area_en;
use flash;
use DB;

class AreasController extends Controller
{
public function index(){
$dbData = Area::get();
// Si terminó de borrar el registro
$toast = session('delete_failed');
if($toast){
session()->forget('delete_failed');
}
return view('areas.list')->with('dbData', $dbData)->with('toast', $toast);
}

public function create(){
return view('areas.create');
}

public function store(Request $request){
DB::transaction(function() use($request){
//Cargamos el área en español
$dbData = new Area($request->all());
//Cargamos el área en inglés
$dbDataEn = new Area_en();
$dbDataEn->descripcion = $request->descripcion_en;
//Guardamos los registros
$dbData->save();
$dbDataEn->save();
});

return redirect()->route('areas.index');
}

public function show($id)
{
//
}

public function edit($id){
$dbData = Area::find($id);
return view('areas.edit')->with('dbData', $dbData);
}

public function update(Request $request, $id){

//Area en español
$dbData = Area::find($id);
//Area en inglés
$dbDataEn = Area_en::find($id);
DB::transaction(function() use($request, $id, $dbData, $dbDataEn){
//Cargamos el área en español
$dbData->fill($request->all());
//Cargamos el área en inglés
$dbDataEn->descripcion = $request->descripcion_en;
//Guardamos los registos
$dbData->save();
$dbDataEn->save();
});



return redirect()->route('areas.index');
}

public function destroy($id){
DB::beginTransaction();
try{
$dbData = Area::find($id);
$dbDataEn = Area_en::find($id);
$dbData->delete();
$dbDataEn->delete();
}catch(\Exception $e){
DB::rollback();
session(['delete_failed'=>"true"]);
return redirect()->back()->withErrors($e->getMessage());
}
DB::commit();

return redirect()->route('areas.index');
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/FileAttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function upload(Request $request){
public function getPeriodosAjax(Request $request){

$periodos = $this->getPeriodos($request->rubro_id);

session(['delete_failed'=>"true"]);
return $periodos;
}

Expand Down
87 changes: 87 additions & 0 deletions app/Http/Controllers/NivelesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Nivel;
use App\Nivel_en;
use flash;
use DB;

class NivelesController extends Controller
{
public function index(){
$dbData = Nivel::get();
// Si terminó de borrar el registro
$toast = session('delete_failed');
if($toast){
session()->forget('delete_failed');
}
return view('niveles.list')->with('dbData', $dbData)->with('toast', $toast);
}

public function create(){
return view('niveles.create');
}

public function store(Request $request){
DB::transaction(function() use($request){
//Cargamos el área en español
$dbData = new Nivel($request->all());
//Cargamos el área en inglés
$dbDataEn = new Nivel_en();
$dbDataEn->descripcion = $request->descripcion_en;
//Guardamos los registros
$dbData->save();
$dbDataEn->save();
});

return redirect()->route('niveles.index');
}

public function show($id)
{
//
}

public function edit($id){
$dbData = nivel::find($id);
return view('niveles.edit')->with('dbData', $dbData);
}

public function update(Request $request, $id){

//nivel en español
$dbData = Nivel::find($id);
//nivel en inglés
$dbDataEn = Nivel_en::find($id);
DB::transaction(function() use($request, $id, $dbData, $dbDataEn){
//Cargamos el área en español
$dbData->fill($request->all());
//Cargamos el área en inglés
$dbDataEn->descripcion = $request->descripcion_en;
//Guardamos los registos
$dbData->save();
$dbDataEn->save();
});
return redirect()->route('niveles.index');
}

public function destroy($id){
DB::beginTransaction();
try{
$dbData = Nivel::find($id);
$dbDataEn = Nivel_en::find($id);
$dbData->delete();
$dbDataEn->delete();

}catch(\Exception $e){
DB::rollback();
session(['delete_failed'=>"true"]);
return redirect()->back()->withErrors($e->getMessage());
}
DB::commit();
return redirect()->route('niveles.index');
}
}
4 changes: 4 additions & 0 deletions app/Nivel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class Nivel extends Model
public function detalleEncuestas(){
return $this->hasMany("App\Detalle_encuesta");
}

public function nivelEn(){
return $this->hasOne('App\Nivel_en', 'id' , 'id');
}
}
30 changes: 30 additions & 0 deletions resources/views/areas/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@extends('layout')
@section('content')
<div class="row">
<div class="browser-window">
<div class="top-bar">
<h4>Crear Nueva Area</h4>
</div>
<div class="content">
<form class="col s12" action="{{route('areas.store')}}" method="POST">
<div class="row">
<div class="input-field col s6">
<input id="descripcion" type="text" class="validate" name="descripcion" >
<label for="descripcion">Descripción (En español)</label>
</div>
<div class="input-field col s6">
<input id="descripcion_en" type="text" class="validate" name="descripcion_en" >
<label for="descripcion_en">Descripción (en inglés)</label>
</div>
</div>
<div class="row">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<button class="btn waves-effect waves-light" type="submit" name="submit">Guardar
<i class="material-icons left">save</i>
</button>
</div>
</form>
</div>
</div>
</div>
@stop
31 changes: 31 additions & 0 deletions resources/views/areas/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@extends('layout')
@section('content')
<div class="row">
<div class="browser-window">
<div class="top-bar">
<h4>Editar Area</h4>
</div>
<div class="content">
<form class="col s12" action="{{route('areas.update', $dbData->id)}}" method="POST">
<div class="row">
<div class="input-field col s6">
<input id="descripcion" type="text" class="validate" name="descripcion" value="{{$dbData->descripcion}}">
<label for="descripcion">Descripción (En español)</label>
</div>
<div class="input-field col s6">
<input id="descripcion_en" type="text" class="validate" name="descripcion_en" value="{{$dbData->areaEn->descripcion}}">
<label for="descripcion_en">Descripción (en inglés)</label>
</div>
</div>
<div class="row">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
{{ method_field('PUT') }}
<button class="btn waves-effect waves-light" type="submit" name="submit">Guardar
<i class="material-icons left">save</i>
</button>
</div>
</form>
</div>
</div>
</div>
@stop
95 changes: 95 additions & 0 deletions resources/views/areas/list.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
@extends('layout')

@section('content')
<div class="row">
<div class="col l4">
<a href="{{ route('areas.create') }}" class="btn waves-effect waves-light lighten-1 white-text"><i class="material-icons left">add</i>Area</a>
</div>
</div>
<div class="row">
<div class="browser-window">
<div class="top-bar">
<h4>Listado de Areas</h4>
</div>
<div class="content">
<table id="Listado" class="highlight">
<thead>
<tr>
<th>Id</th>
<th>Descripción</th>
<th>Descripción (Inglés)</th>
<th>Opciones</th>
</tr>
</thead>
<tbody>
@foreach($dbData as $est)
<tr>
<td>{{ $est->id }}</td>
<td>{{ $est->descripcion }}</td>
<td>{{ $est->areaEn->descripcion }}</td>
<td><a href="{{ route('areas.edit', $est->id) }}" class="btn waves-light waves-effect lighten-1 white-text ">
<i class="material-icons left">edit</i>Editar
</a>

<a href="{{ route('areas.destroy', $est->id) }}" class="btn waves-light waves-effect lighten-1 red white-text" onclick="delete_row({{$est->id}})">
<i class="material-icons left">delete</i>Borrar
</a>
<form id="delete-form{{$est->id}}" action="{{ route('areas.destroy', $est->id) }}" method="POST" style="display: none;">
{{ csrf_field() }}
{{ method_field('DELETE') }}

</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@if($toast)
<div id="toast"></div>
@endif

@endsection
@push('scripts')
<script type="text/javascript">
$(function(){
$('#Listado').DataTable({
"scrollX": false,
"scrollCollapse": false,
"lengthChange": false,
"columnDefs": [
{ "targets": [2],
"orderable": false,
"searchable": false
}
],
"language": {
"decimal": ",",
"thousands": ".",
"zeroRecords": "No hay registros - Lo sentimos",
"info": "Página _PAGE_ de _PAGES_",
"infoEmpty": "No hay registros disponibles",
"infoFiltered": "(Filtrado de un total de _MAX_ registros)"
}
});
});
if($("#toast").length > 0){
M.toast({html: 'Error al borrar el Registro'});
}
function delete_row(row){
event.preventDefault();
if (confirm('Seguro que desea eliminar el registro?')){
document.getElementById('delete-form'+row).submit();
} else {
return false;
}
}
</script>
@endpush
Loading

0 comments on commit 4f55953

Please sign in to comment.