diff --git a/app/Exports/DocentesExport.php b/app/Exports/DocentesExport.php new file mode 100644 index 0000000..6c43a91 --- /dev/null +++ b/app/Exports/DocentesExport.php @@ -0,0 +1,33 @@ +docentes = $docentes; + } + + public function collection() + { + return $this->docentes; + } + + public function headings(): array + { + return [ + 'ID', + 'Nombre', + 'Teléfono', + 'Correo', + 'Tipo de Licencia', + 'Materia' + ]; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/DocentesController.php b/app/Http/Controllers/DocentesController.php index 2f4ad9e..26dc233 100644 --- a/app/Http/Controllers/DocentesController.php +++ b/app/Http/Controllers/DocentesController.php @@ -4,6 +4,9 @@ namespace App\Http\Controllers; use App\Models\Docentes; use Illuminate\Http\Request; +use Maatwebsite\Excel\Facades\Excel; +use Barryvdh\DomPDF\Facade\Pdf; +use App\Exports\DocentesExport; class DocentesController extends Controller { @@ -12,7 +15,25 @@ class DocentesController extends Controller */ public function index(Request $request) { - $docentes = Docentes::all(); + $busqueda = $request->busqueda; + + if ($busqueda) { + // Busca docentes que coincidan con el término de búsqueda + $docentes = Docentes::where('nombre', 'LIKE', "%{$busqueda}%") + ->orWhere('correo', 'LIKE', "%{$busqueda}%") + ->orWhere('tipo_licencia', 'LIKE', "%{$busqueda}%") + ->orWhere('materia', 'LIKE', "%{$busqueda}%") + ->get(); + + if ($docentes->isEmpty()) { + return redirect()->route('docentes.index') + ->with('error', 'No existe ningún docente con el término "' . $busqueda . '". Por favor, inténtalo de nuevo.'); + } + } else { + // Si no hay búsqueda, mostrar todos los docentes + $docentes = Docentes::all(); + } + return view('docentes', ['docentes' => $docentes]); } @@ -71,4 +92,21 @@ class DocentesController extends Controller $docente->delete(); return redirect()->route('docentes.index')->with('success', 'Docente eliminado correctamente'); } + + public function export($format) + { + $docentes = Docentes::all(); + + switch($format) { + case 'csv': + return Excel::download(new DocentesExport($docentes), 'docentes.csv', \Maatwebsite\Excel\Excel::CSV); + case 'excel': + return Excel::download(new DocentesExport($docentes), 'docentes.xlsx'); + case 'pdf': + return PDF::loadView('exports.docentes', ['docentes' => $docentes]) + ->download('docentes.pdf'); + default: + return redirect()->back()->with('error', 'Formato no soportado'); + } + } } diff --git a/composer.json b/composer.json index 7cf3259..81d0a18 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,9 @@ "laravel/sanctum": "^3.2", "laravel/tinker": "^2.8", "laravel/ui": "^4.6", - "maatwebsite/excel": "^3.1", - "phpoffice/phpspreadsheet": "^4.1" + "maatwebsite/excel": "^1.1", + "phpoffice/phpspreadsheet": "^4.1", + "psr/simple-cache": "2.0" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/composer.lock b/composer.lock index f8586b8..2089c77 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "acb583cdead6464e77a88f1ff4c131d6", + "content-hash": "c0f0a026d64d98ad86849a80b4a5135b", "packages": [ { "name": "barryvdh/laravel-dompdf", @@ -3680,16 +3680,16 @@ }, { "name": "psr/simple-cache", - "version": "3.0.0", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/php-fig/simple-cache.git", - "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865" + "reference": "8707bf3cea6f710bf6ef05491234e3ab06f6432a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/764e0b3939f5ca87cb904f570ef9be2d78a07865", - "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/8707bf3cea6f710bf6ef05491234e3ab06f6432a", + "reference": "8707bf3cea6f710bf6ef05491234e3ab06f6432a", "shasum": "" }, "require": { @@ -3698,7 +3698,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -3725,9 +3725,9 @@ "simple-cache" ], "support": { - "source": "https://github.com/php-fig/simple-cache/tree/3.0.0" + "source": "https://github.com/php-fig/simple-cache/tree/2.0.0" }, - "time": "2021-10-29T13:26:27+00:00" + "time": "2021-10-29T13:22:09+00:00" }, { "name": "psy/psysh", diff --git a/resources/views/docentes.blade.php b/resources/views/docentes.blade.php index 58a05a1..470628c 100644 --- a/resources/views/docentes.blade.php +++ b/resources/views/docentes.blade.php @@ -30,7 +30,25 @@ - +
+ + + + + + + + + + + +
+ +
@@ -44,7 +62,7 @@
@if(request('busqueda')) @@ -172,10 +190,29 @@ document.addEventListener('DOMContentLoaded', function() { new DataTable('#docentes-table', { layout: { topStart: { - buttons: ['copy', 'csv', 'excel', 'pdf', 'print'] + buttons: ['copy', 'csv', 'excel', 'pdf'] } } }); + +function copyToClipboard() { + const table = document.querySelector('table'); + const range = document.createRange(); + range.selectNode(table); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + document.execCommand('copy'); + window.getSelection().removeAllRanges(); + + // Mostrar mensaje de éxito + Swal.fire({ + icon: 'success', + title: 'Copiado', + text: 'La tabla ha sido copiada al portapapeles', + timer: 1500, + showConfirmButton: false + }); +} @endsection diff --git a/resources/views/docentesCrearEditar.blade.php b/resources/views/docentesCrearEditar.blade.php index f5d33f3..34b26f3 100644 --- a/resources/views/docentesCrearEditar.blade.php +++ b/resources/views/docentesCrearEditar.blade.php @@ -25,9 +25,27 @@ -
- - +
+ +
+ +
+ +
+
+ @error('tipo_licencia') +

{{ $message }}

+ @enderror
diff --git a/resources/views/exports/docentes.blade.php b/resources/views/exports/docentes.blade.php new file mode 100644 index 0000000..09121a1 --- /dev/null +++ b/resources/views/exports/docentes.blade.php @@ -0,0 +1,47 @@ + + + + Docentes + + + +

Lista de Docentes

+ + + + + + + + + + + + + @foreach($docentes as $docente) + + + + + + + + + @endforeach + +
IDNombreTeléfonoCorreoTipo de LicenciaMateria
{{ $docente->id }}{{ $docente->nombre }}{{ $docente->telefono }}{{ $docente->correo }}{{ $docente->tipo_licencia }}{{ $docente->materia }}
+ + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 862bab8..91c4d56 100644 --- a/routes/web.php +++ b/routes/web.php @@ -41,7 +41,7 @@ Route::resource('capacidades', CapacidadController::class); Route::get('marcas/excel', [MarcaController::class, 'exportExcel'])->name('marcas.excel'); Route::get('marcas/pdf', [MarcaController::class, 'exportPDF'])->name('marcas.pdf'); - +Route::get('/docentes/export/{format}', [DocentesController::class, 'export'])->name('docentes.export'); // Rutas protegidas que requieren autenticación Route::middleware(['auth'])->group(function () {