updates to document handling and code editing features
This commit is contained in:
273
app/Livewire/ProjectDocumentList.php
Normal file
273
app/Livewire/ProjectDocumentList.php
Normal file
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\Document;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Exports\DocumentsExport;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Rappasoft\LaravelLivewireTables\DataTableComponent;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Column;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
|
||||
use Rappasoft\LaravelLivewireTables\Views\Filters\TextFilter;
|
||||
|
||||
class ProjectDocumentList extends DataTableComponent
|
||||
{
|
||||
public $projectId;
|
||||
public $folderId = null;
|
||||
|
||||
protected $model = Document::class;
|
||||
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setPrimaryKey('id')
|
||||
->setAdditionalSelects(['documents.id as id'])
|
||||
|
||||
/*->setConfigurableAreas([
|
||||
'toolbar-left-start' => ['includes.areas.toolbar-left-start', ['param1' => 'Default', 'param2' => ['param2' => 2]]],
|
||||
])*/
|
||||
->setPaginationEnabled()
|
||||
->setPaginationMethod('simple')
|
||||
->setPaginationVisibilityEnabled()
|
||||
|
||||
|
||||
//->setReorderEnabled()
|
||||
->setHideReorderColumnUnlessReorderingEnabled()
|
||||
->setSecondaryHeaderTrAttributes(function ($rows) {
|
||||
return ['class' => 'bg-gray-100'];
|
||||
})
|
||||
->setSecondaryHeaderTdAttributes(function (Column $column, $rows) {
|
||||
if ($column->isField('id')) {
|
||||
return ['class' => 'text-red-100'];
|
||||
}
|
||||
return ['default' => true];
|
||||
})
|
||||
->setFooterTrAttributes(function ($rows) {
|
||||
return ['class' => 'bg-gray-100'];
|
||||
})
|
||||
->setFooterTdAttributes(function (Column $column, $rows) {
|
||||
if ($column->isField('name')) {
|
||||
return ['class' => 'text-green-500'];
|
||||
}
|
||||
return ['default' => true];
|
||||
})
|
||||
->setHideBulkActionsWhenEmptyEnabled()
|
||||
->setUseHeaderAsFooterEnabled()
|
||||
|
||||
->setPaginationEnabled()
|
||||
->setPaginationVisibilityEnabled()
|
||||
//->setToolsDisabled()
|
||||
//->setToolBarDisabled()
|
||||
|
||||
// Configuración de paginación
|
||||
->setPerPage(25) // Número de elementos por página
|
||||
->setPerPageAccepted([10, 25, 50, 100]) // Opciones de elementos por página
|
||||
->setPaginationEnabled() // Asegurar que la paginación esté habilitada
|
||||
->setPaginationVisibilityStatus(true); // Hacer visible el paginador
|
||||
;
|
||||
}
|
||||
|
||||
public function mount($projectId = null, $folderId = null)
|
||||
{
|
||||
$this->projectId = $projectId;
|
||||
$this->folderId = $folderId;
|
||||
}
|
||||
|
||||
public function columns(): array
|
||||
{
|
||||
return [
|
||||
Column::make("Código", "code")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->secondaryHeaderFilter('code') // Filtro para esta columna
|
||||
->format(
|
||||
fn($value, $row, Column $column) =>
|
||||
'<a href="'.route('documents.show', $row->id).'" target="_blank" class=" target="_blank" class="flex items-center hover:text-blue-600 transition-colors"">'.$value.'</a>'
|
||||
)->html(),
|
||||
|
||||
Column::make("Nombre", "name")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->secondaryHeaderFilter('name') // Filtro para esta columna
|
||||
->format(
|
||||
fn($value, $row, Column $column) =>
|
||||
'<div class="flex items-center">
|
||||
<span class="mr-2 text-lg">
|
||||
'.\App\Helpers\FileHelper::getFileIconSvg($value).'
|
||||
</span>
|
||||
<a href="'.route('documents.show', $row->id).'"
|
||||
target="_blank"
|
||||
class=" target="_blank" class="flex items-center hover:text-blue-600 transition-colors"">
|
||||
'.$value.'
|
||||
</a>
|
||||
</div>'
|
||||
)->html(),
|
||||
|
||||
Column::make("Estado", "status")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->secondaryHeaderFilter('status') // Filtro para esta columna
|
||||
->format(
|
||||
fn($value, $row, Column $column) =>
|
||||
'<span class="px-2 py-1 text-xs font-semibold rounded-full '.
|
||||
($value === 'active' ? 'bg-green-100 text-green-800' :
|
||||
($value === 'pending' ? 'bg-yellow-100 text-yellow-800' : 'bg-red-100 text-red-800')).'">'.
|
||||
$value.'</span>'
|
||||
)->html(),
|
||||
|
||||
Column::make("Revisión", "revision")
|
||||
->sortable()
|
||||
->searchable(),
|
||||
|
||||
Column::make("Versión", "version")
|
||||
->sortable()
|
||||
->searchable(),
|
||||
|
||||
Column::make("Área", "area")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->secondaryHeaderFilter('area'), // Filtro para esta columna
|
||||
|
||||
Column::make("Disciplina", "discipline")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->secondaryHeaderFilter('discipline'), // Filtro para esta columna
|
||||
|
||||
Column::make("Tipo", "document_type")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->secondaryHeaderFilter('type'), // Filtro para esta columna
|
||||
|
||||
Column::make("Fecha Entrada", "entry_date")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->format(
|
||||
fn($value, $row, Column $column) =>
|
||||
$value ? \Carbon\Carbon::parse($value)->format('d/m/Y') : '-'
|
||||
),
|
||||
|
||||
Column::make("Actualizado", "updated_at")
|
||||
->sortable()
|
||||
->searchable()
|
||||
->format(
|
||||
fn($value, $row, Column $column) =>
|
||||
$value ? \Carbon\Carbon::parse($value)->diffForHumans() : '-'
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): array
|
||||
{
|
||||
return [
|
||||
TextFilter::make('Código', 'code') // Agregar clave 'code'
|
||||
->config([
|
||||
'placeholder' => 'Buscar por código',
|
||||
])
|
||||
->filter(function (Builder $builder, string $value) {
|
||||
$builder->where('documents.code', 'like', '%'.$value.'%');
|
||||
}),
|
||||
|
||||
TextFilter::make('Nombre', 'name') // Agregar clave 'name'
|
||||
->config([
|
||||
'placeholder' => 'Buscar por nombre',
|
||||
])
|
||||
->filter(function (Builder $builder, string $value) {
|
||||
$builder->where('documents.name', 'like', '%'.$value.'%');
|
||||
}),
|
||||
|
||||
SelectFilter::make('Estado', 'status') // Agregar clave 'status'
|
||||
->options([
|
||||
'' => 'Todos',
|
||||
'active' => 'Activo',
|
||||
'pending' => 'Pendiente',
|
||||
'inactive' => 'Inactivo',
|
||||
])
|
||||
->filter(function (Builder $builder, string $value) {
|
||||
if ($value) {
|
||||
$builder->where('documents.status', $value);
|
||||
}
|
||||
}),
|
||||
|
||||
SelectFilter::make('Disciplina', 'discipline') // Agregar clave 'discipline'
|
||||
->options(
|
||||
collect(['Estructural', 'Arquitectura', 'Eléctrica', 'Mecánica', 'Civil', 'Otros'])
|
||||
->prepend('Todas', '')
|
||||
->toArray()
|
||||
)
|
||||
->filter(function (Builder $builder, string $value) {
|
||||
if ($value) {
|
||||
$builder->where('documents.discipline', $value);
|
||||
}
|
||||
}),
|
||||
|
||||
SelectFilter::make('Area', 'area') // Agregar clave 'area'
|
||||
->options(
|
||||
collect(['Estructural', 'Arquitectura', 'Eléctrica', 'Mecánica', 'Civil', 'Otros'])
|
||||
->prepend('Todas', '')
|
||||
->toArray()
|
||||
)
|
||||
->filter(function (Builder $builder, string $value) {
|
||||
if ($value) {
|
||||
$builder->where('documents.area', $value);
|
||||
}
|
||||
}),
|
||||
|
||||
SelectFilter::make('Tipo', 'type') // Agregar clave 'document_type'
|
||||
->options(
|
||||
collect(['Estructural', 'Arquitectura', 'Eléctrica', 'Mecánica', 'Civil', 'Otros'])
|
||||
->prepend('Todas', '')
|
||||
->toArray()
|
||||
)
|
||||
->filter(function (Builder $builder, string $value) {
|
||||
if ($value) {
|
||||
$builder->where('documents.document_type', $value);
|
||||
}
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
public function builder(): Builder
|
||||
{
|
||||
$query = Document::query()->where('project_id', $this->projectId);
|
||||
|
||||
if ($this->folderId) {
|
||||
$query->where('folder_id', $this->folderId);
|
||||
} else {
|
||||
$query->whereNull('folder_id');
|
||||
}
|
||||
|
||||
return $query->with('user');
|
||||
}
|
||||
|
||||
public function bulkActions(): array
|
||||
{
|
||||
return [
|
||||
'activate' => 'Activar',
|
||||
'deactivate' => 'Desactivar',
|
||||
'export' => 'Exportar',
|
||||
];
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
$documents = $this->getSelected();
|
||||
|
||||
$this->clearSelected();
|
||||
|
||||
return Excel::download(new DocumentsExport($documents), 'documentos.xlsx');
|
||||
}
|
||||
|
||||
public function activate()
|
||||
{
|
||||
Document::whereIn('id', $this->getSelected())->update(['status' => 'active']);
|
||||
$this->clearSelected();
|
||||
$this->dispatch('documents-updated');
|
||||
}
|
||||
|
||||
public function deactivate()
|
||||
{
|
||||
Document::whereIn('id', $this->getSelected())->update(['status' => 'inactive']);
|
||||
$this->clearSelected();
|
||||
$this->dispatch('documents-updated');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user