Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Latest Code to add-data-export branch #72

Merged
merged 9 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Exports/XlsformExport/XlsformSettingsExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function collection(): \Illuminate\Support\Collection
'form_id' => $this->xlsform->odk_id ?? Str::slug($this->xlsform->title),
'form_title' => $this->xlsform->title,
'version' => Carbon::now()->toDateTimeString(),
'instance_name' => 'TO BE UPDATED!!', // TODO: fix
'instance_name' => '"instance"', // TODO: fix
],
]);

Expand Down
146 changes: 113 additions & 33 deletions app/Exports/XlsformExport/XlsformSurveyExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Row;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Stats4sd\FilamentOdkLink\Models\OdkLink\XlsformTemplate;

class XlsformSurveyExport implements FromCollection, WithHeadings, WithTitle, WithStyles, ShouldAutoSize, WithColumnWidths
{
public Collection $surveyRows;
public array $dynamicStyles;
public Collection $dynamicStylesRowLists;



public function __construct(public Xlsform $xlsform, public Collection $xlsformTemplateLanguages, public Collection $languageStringTypes)
Expand All @@ -33,7 +35,7 @@ public function __construct(public Xlsform $xlsform, public Collection $xlsformT
->sortBy('id')
->load('languageStrings')
->map(function (SurveyRow $row) {
return [
return collect([
'id' => $row->id,
'type' => $row->type,
'name' => $row->name,
Expand All @@ -49,21 +51,12 @@ public function __construct(public Xlsform $xlsform, public Collection $xlsformT
...$this->getLanguageStrings($row, 'constraint_message'),
'choice_filter' => $row->choice_filter,
'repeat_count' => $row->repeat_count,
...$this->getLanguageStrings($row, 'mediaimage'),
'default' => $row->default,
];
]);
});

$beginGroupRows = $this->surveyRows->filter(fn(SurveyRow $surveyRow) => $surveyRow['type'] === 'begin_group');
$endGroupRows = $this->surveyRows->filter(fn(SurveyRow $surveyRow) => $surveyRow['type'] === 'end_group');
$beginRepeatRows = $this->surveyRows->filter(fn(SurveyRow $surveyRow) => $surveyRow['type'] === 'begin_repeat');
$endRepeatRows = $this->surveyRows->filter(fn(SurveyRow $surveyRow) => $surveyRow['type'] === 'end_repeat');

$this->dynamicStyles = [
...$beginGroupRows->mapWithKeys(fn(SurveyRow $surveyRow) => [$surveyRow['id'] + 1 => [])->toArray(),
'end_group' => $endGroupRows->map(fn(SurveyRow $surveyRow) => $surveyRow['id'])->toArray(),
'begin_repeat' => $beginRepeatRows->map(fn(SurveyRow $surveyRow) => $surveyRow['id'])->toArray(),
'end_repeat' => $endRepeatRows->map(fn(SurveyRow $surveyRow) => $surveyRow['id'])->toArray(),
];
$this->dynamicStyleRowLists = $this->getDynamicStylesRowLists($this->surveyRows);

}

Expand All @@ -88,14 +81,30 @@ public function title(): string
private function getLanguageStrings(mixed $row, string $string): Collection
{
return $this->xlsformTemplateLanguages
->mapWithKeys(function(XlsformTemplateLanguage $xlsformTemplateLanguage) use ($row, $string) {
$key = "$string::{$xlsformTemplateLanguage->language->name} ({$xlsformTemplateLanguage->language->iso_alpha2})";
$value = $row->languageStrings
->where('language_string_type_id', $this->languageStringTypes->where('name', $string)->first()->id)
->where('xlsform_template_language_id', $xlsformTemplateLanguage->id)
->first()?->text ?? '';

return [$key => $value];
->mapWithKeys(function (XlsformTemplateLanguage $xlsformTemplateLanguage) use ($row, $string) {

// fix for mediaimage needing to be media::image, etc.
$outputString = $string;

if($string === 'mediaimage') {
$outputString = 'media::image';
}

if($string === 'mediaaudio') {
$outputString = 'media::audio';
}

if($string === 'mediavideo') {
$outputString = 'media::video';
}

$key = "$outputString::{$xlsformTemplateLanguage->language->name} ({$xlsformTemplateLanguage->language->iso_alpha2})";
$value = $row->languageStrings
->where('language_string_type_id', $this->languageStringTypes->where('name', $string)->first()->id)
->where('xlsform_template_language_id', $xlsformTemplateLanguage->id)
->first()?->text ?? '';

return [$key => $value];
});
}

Expand Down Expand Up @@ -136,21 +145,92 @@ public function columnWidths(): array
];
}

public function styles(Worksheet $sheet): array
public function styles(Worksheet $sheet): void
{
// starting at C, make label + hint columns auto-wrap per Xlsformtemplatelangauge
$wrapLabelArray = $this->xlsformTemplateLanguages->mapWithKeys(fn(XlsformTemplateLanguage $language, $index) => [chr(67 + $index) => ['alignment' => ['wrapText' => true]]]
)->toArray();

$languageCount = $this->xlsformTemplateLanguages->count();

$wrapHintArray = $this->xlsformTemplateLanguages->mapWithKeys(fn(XlsformTemplateLanguage $language, $index) => [chr(67 + $languageCount + $index) => ['alignment' => ['wrapText' => true]]]
)->toArray();
$wrapStyle = ['alignment' => ['wrapText' => true]];

return [
1 => ['font' => ['bold' => true]],
...$wrapLabelArray,
...$wrapHintArray,
$beginGroupStyle = [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => ['rgb' => '8ED873',],
'endColor' => ['rgb' => '8ED873',],
],
];

$endGroupStyle = [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => ['rgb' => 'FA8E78',],
'endColor' => ['rgb' => 'FA8E78',],
],
];

$beginRepeatStyle = [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => ['rgb' => '83CAEB',],
'endColor' => ['rgb' => '83CAEB',],
],
];

$endRepeatStyle = [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => ['rgb' => 'E49EDD',],
'endColor' => ['rgb' => 'E49EDD',],
],
];


// starting at C, make label + hint columns auto-wrap per Xlsformtemplatelangauge
$wrapLabelList = $this->xlsformTemplateLanguages->map(fn(XlsformTemplateLanguage $language, $index) => chr(67 + $index));
$wrapHintList = $this->xlsformTemplateLanguages->map(fn(XlsformTemplateLanguage $language, $index) => chr(67 + $languageCount + $index));


// **** APPLY STYLES ****

$sheet->getStyle('1:1')->getFont()->setBold(true);

foreach($wrapLabelList as $column) {
$sheet->getStyle($column . ':' . $column)->applyFromArray($wrapStyle);
}

foreach($wrapHintList as $column) {
$sheet->getStyle($column . ':' . $column)->applyFromArray($wrapStyle);
}

foreach($this->dynamicStyleRowLists['beginGroupRows'] as $row) {
$sheet->getStyle($row . ':' . $row)->applyFromArray($beginGroupStyle);
}

foreach($this->dynamicStyleRowLists['endGroupRows'] as $row) {
$sheet->getStyle($row . ':' . $row)->applyFromArray($endGroupStyle);
}

foreach($this->dynamicStyleRowLists['beginRepeatRows'] as $row) {
$sheet->getStyle($row . ':' . $row)->applyFromArray($beginRepeatStyle);
}

foreach($this->dynamicStyleRowLists['endRepeatRows'] as $row) {
$sheet->getStyle($row . ':' . $row)->applyFromArray($endRepeatStyle);
}

}

private function getDynamicStylesRowLists(Collection $surveyRows): Collection
{
$beginGroupRows = $surveyRows->filter(fn(Collection $surveyRow) => $surveyRow['type'] === 'begin_group')->pluck('id');
$endGroupRows = $surveyRows->filter(fn(Collection $surveyRow) => $surveyRow['type'] === 'end_group')->pluck('id');
$beginRepeatRows = $surveyRows->filter(fn(Collection $surveyRow) => $surveyRow['type'] === 'begin_repeat')->pluck('id');
$endRepeatRows = $surveyRows->filter(fn(Collection $surveyRow) => $surveyRow['type'] === 'end_repeat')->pluck('id');

return collect([
'beginGroupRows' => $beginGroupRows->map(fn($id) => $id + 1),
'endGroupRows' => $endGroupRows->map(fn($id) => $id + 1),
'beginRepeatRows' => $beginRepeatRows->map(fn($id) => $id + 1),
'endRepeatRows' => $endRepeatRows->map(fn($id) => $id + 1),
]);
}
}
3 changes: 0 additions & 3 deletions app/Exports/XlsformExport/XlsformWorkbookExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public function __construct(public Xlsform $xlsform)
// get the language string types once to avoid multiple queries
$this->languageStringTypes = LanguageStringType::all();

ray($this->xlsformTemplateLanguages);
ray($this->languageStringTypes);

}

public function sheets(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,53 @@
use App\Exports\XlsformExport\XlsformWorkbookExport;
use App\Models\Xlsforms\Xlsform;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Table;
use Maatwebsite\Excel\Facades\Excel;
use Stats4sd\FilamentOdkLink\Jobs\UpdateXlsformTitleInFile;
use Stats4sd\FilamentOdkLink\Services\OdkLinkService;

class XlsformsRelationManager extends \Stats4sd\FilamentOdkLink\Filament\Resources\TeamResource\RelationManagers\XlsformsRelationManager
{



public function table(Table $table): Table
{
return parent::table($table)
->actions([
Action::make('download xls file')
->action(function(Xlsform $record) {

return Excel::download((new XlsformWorkbookExport($record)), 'xlsform.xlsx');

})
->action(function (Xlsform $record) {

return Excel::download((new XlsformWorkbookExport($record)), 'xlsform.xlsx');

}),
// add Publish button
Action::make('publish')
->label('Publish')
->icon('heroicon-m-arrow-up-tray')
->requiresConfirmation()
->action(function (Xlsform $record) {
$odkLinkService = app()->make(OdkLinkService::class);

// create draft if there is no draft yet
if (!$record->has_draft) {
$odkLinkService->createDraftForm($record);
}

// call API to publish form in ODK central
$odkLinkService->publishForm($record);
}),
])
->headerActions([
CreateAction::make()
->label('Add Xlsform to Team')
->after(function (Xlsform $record) {

$odkLinkService = app()->make(OdkLinkService::class);

$record->refresh();
$record->deployDraft($odkLinkService);
}),
]);
}
}
2 changes: 1 addition & 1 deletion app/Filament/Admin/Widgets/DataCollectedWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Filament\Admin\Widgets;

use App\Models\XlsformTemplate;
use App\Models\XlsformTemplates\XlsformTemplate;
use App\Models\SampleFrame\Farm;
use Illuminate\Support\HtmlString;
use Spatie\Permission\Models\Role;
Expand Down
63 changes: 63 additions & 0 deletions app/Filament/App/Clusters/Actions/CreateLookupListEntryAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Filament\App\Clusters\Actions;

use App\Models\Locale;
use App\Models\XlsformTemplates\ChoiceListEntry;
use App\Services\HelperService;
use Filament\Actions\CreateAction;
use Filament\Facades\Filament;
use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class CreateLookupListEntryAction extends CreateAction
{
protected function setUp(): void
{
parent::setUp();

$locales = HelperService::getSelectedTeam()?->locales;

$labelFields = $locales->map(function (Locale $locale) {
return TextInput::make('label_' . $locale->language_id)
->label('label::' . $locale->language_label);
});

$this->form([
TextInput::make('name')->required(),
...$labelFields->toArray(),
]);

// add the current team as owner
if (Filament::hasTenancy()) {
$this->mutateFormDataUsing(
function (array $data): array {

$locales = HelperService::getSelectedTeam()?->locales;
return collect($data)
// don't include the 'label' entries - we will create languageStrings after creating the ChoiceListEntry
->filter(fn($value, $key) => !Str::startsWith($key, 'label_'))
->put('owner_id', Filament::getTenant()->id)
->put('owner_type', get_class(Filament::getTenant()))
->toArray();
})
->after(function (ChoiceListEntry $record, array $data) {
$locales = HelperService::getSelectedTeam()?->locales;

dd($data);

$languageStrings = $locales->map(function (Locale $locale) use ($record, $data) {
$xlsformTemplateLanguage = $record->xlsformTemplate->xlsformTemplateLanguages()->where('language_id', $locale->language_id)->first();

return [
'xlsform_template_language_id' => $xlsformTemplateLanguage->id,
'language_string_type_id' => 1, // hardcoded to labels. TODO: fix;
'text' => $data,
];
});
});
}

}
}
12 changes: 12 additions & 0 deletions app/Filament/App/Clusters/LookupTables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Filament\App\Clusters;

use Filament\Clusters\Cluster;

class LookupTables extends Cluster
{
protected static ?string $navigationIcon = 'heroicon-o-squares-2x2';


}
Loading
Loading