diff --git a/laravel_app/app/Jobs/ProjectDownloadTiffJob.php b/laravel_app/app/Jobs/ProjectDownloadTiffJob.php index 5d931f8..20ffe36 100644 --- a/laravel_app/app/Jobs/ProjectDownloadTiffJob.php +++ b/laravel_app/app/Jobs/ProjectDownloadTiffJob.php @@ -36,14 +36,12 @@ public function __construct(ProjectDownload $download, Carbon $date,) */ public function handle(): void { - logger(json_encode($this->download->project->getBoundingBoxesAsArray())); $command = [ sprintf('%srunpython.sh', base_path('../')), sprintf('--date=%s', $this->date->format('Y-m-d')), sprintf('--days=%d', $this->days), sprintf('--project_dir=%s', $this->download->project->download_path), - sprintf('--bbox=%s', json_encode($this->download->project->getBoundingBoxesAsArray())), ]; $process = new Process($command); diff --git a/laravel_app/app/Livewire/Projects/ProjectManager.php b/laravel_app/app/Livewire/Projects/ProjectManager.php index 4bad5ed..e5179e8 100644 --- a/laravel_app/app/Livewire/Projects/ProjectManager.php +++ b/laravel_app/app/Livewire/Projects/ProjectManager.php @@ -3,10 +3,8 @@ namespace App\Livewire\Projects; use App\Models\Project; -use App\Models\ProjectBoundingBox; use App\Models\ProjectEmailRecipient; use App\Rules\HarvestFile; -use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; use Livewire\Component; @@ -27,9 +25,9 @@ class ProjectManager extends Component public $showMailSettingsModal = false; public $projectIdBeingDeleted; - public $span_json_path; - public $pivot_json_path; - public $harvest_json_path; +// public $span_json_path; +// public $pivot_json_path; +// public $harvest_json_path; public array $pivotFiles; public array $spanFiles; @@ -58,19 +56,19 @@ public function editMailSettings(Project $project) $this->loadFormData($project); } - private function loadFormData(Project $project) + private function loadFormData(Project $project) { $this->formData = $project->toArray(); - $this->pivot_json_path = $this->formData['pivot_json_path']; - $this->span_json_path = $this->formData['span_json_path']; - $this->harvest_json_path = $this->formData['harvest_json_path']; - $this->formData['pivot_json_path'] = null; - $this->formData['span_json_path'] = null; - $this->formData['harvest_json_path'] = null; - $this->formData['boundingBoxes'] = $project->boundingBoxes->toArray(); +// $this->pivot_json_path = $this->formData['pivot_json_path']; +// $this->span_json_path = $this->formData['span_json_path']; +// $this->harvest_json_path = $this->formData['harvest_json_path']; +// $this->formData['pivot_json_path'] = null; +// $this->formData['span_json_path'] = null; +// $this->formData['harvest_json_path'] = null; +// $this->formData['boundingBoxes'] = $project->boundingBoxes->toArray(); $this->formData['mail_recipients'] = $project->emailRecipients->toArray() ?: [ [ - 'name' => '', + 'name' => '', 'email' => '', ] ]; @@ -82,10 +80,11 @@ public function openCreateProjectModal() $this->showProjectModal = true; } - public function saveProject(Request $request) + public function saveProject() { $this->resetErrorBag(); - dd($this->validateForm()); + $this->mergeFormData(); + $this->validateForm(); Project::saveWithFormData($this->formData); $this->resetFormData(); $this->showProjectModal = false; @@ -102,38 +101,15 @@ public function saveMailSettings() $this->dispatch('saved'); } - public function addBoundingBox() - { - $this->formData['boundingBoxes'][] = - [ - 'name' => '', - 'top_left_latitude' => '', - 'top_left_longitude' => '', - 'bottom_right_latitude' => '', - 'bottom_right_longitude' => '', - ]; - } - public function addEmailRecipient() { $this->formData['mail_recipients'][] = [ - 'name' => '', + 'name' => '', 'email' => '', ]; } - - public function deleteBoundingBox($index) - { - if (array_key_exists('id', $this->formData['boundingBoxes'][$index])) { - ProjectBoundingBox::whereId($this->formData['boundingBoxes'][$index]['id'])->delete(); - } - unset($this->formData['boundingBoxes'][$index]); - $this->formData['boundingBoxes'] = array_values($this->formData['boundingBoxes']); - $this->dispatch('saved'); - } - public function deleteEmailRecipient($index) { if (array_key_exists('id', $this->formData['mail_recipients'][$index])) { @@ -148,7 +124,7 @@ public function deleteEmailRecipient($index) /** * Confirm that the given Project should be deleted. * - * @param int $reportId + * @param int $reportId * @return void */ public function confirmReportDeletion($reportId) @@ -189,21 +165,16 @@ public function render() private function resetFormData() { $this->formData = [ - 'name' => '', - 'pivot_json_path' => '', - 'span_json_path' => '', - 'harvest_json_path' => '', - 'boundingBoxes' => [], - 'mail_subject' => '', - 'mail_template' => '', - 'mail_frequency' => '', - 'mail_day' => '', + 'name' => '', + 'mail_subject' => '', + 'mail_template' => '', + 'mail_frequency' => '', + 'mail_day' => '', 'mail_recipients' => [], ]; $this->pivotFiles = []; $this->spanFiles = []; $this->harvestDataFiles = []; - $this->addBoundingBox(); $this->addEmailRecipient(); } @@ -212,55 +183,58 @@ private function validateForm() $projectIdentifier = $this->formData['id'] ?? null; return Validator::make([ - 'name' => $this->formData['name'], - 'pivot_file' => $this->pivotFiles[0] ?? null, - 'span_file' => $this->spanFiles[0] ?? null, - 'harvest_file' => $this->harvestDataFiles[0] ?? null, - 'boundingBoxes' => $this->formData['boundingBoxes'], + 'name' => $this->formData['name'], + 'pivot_file' => $this->formData['pivot_file'], + 'span_file' => $this->formData['span_file'], + 'harvest_file' => $this->formData['harvest_file'], ], [ - 'name' => [ + 'name' => [ 'required', Rule::unique('projects')->ignore($projectIdentifier), 'string', 'max:255' ], - 'pivot_file.extension' => ['sometimes',function ($attribute, $value, $fail) { - if($value && $value != 'json'){ + 'pivot_file.extension' => ['sometimes', function ($attribute, $value, $fail) { + if ($value && $value != 'json') { $fail('Not a json file'); } }], - 'span_file.extension' => ['sometimes',function ($attribute, $value, $fail) { - if($value && $value != 'json'){ + 'span_file.extension' => ['sometimes', function ($attribute, $value, $fail) { + if ($value && $value != 'json') { $fail('Not a json file'); } }], - 'harvest_file' => ['sometimes',new HarvestFile], - 'boundingBoxes' => ['required', 'array', 'min:1'], - 'boundingBoxes.*.name' => ['required', 'string', 'max:255'], - 'boundingBoxes.*.top_left_latitude' => ['required', 'string'], - 'boundingBoxes.*.top_left_longitude' => ['required', 'string'], - 'boundingBoxes.*.bottom_right_latitude' => ['required', 'string'], - 'boundingBoxes.*.bottom_right_longitude' => ['required', 'string'], + 'harvest_file' => ['sometimes', new HarvestFile], ])->validate(); } private function validateEmailSettingsForm() { Validator::make([ - 'mail_template' => $this->formData['mail_template'], - 'mail_subject' => $this->formData['mail_subject'], - 'mail_frequency' => $this->formData['mail_frequency'], - 'mail_day' => $this->formData['mail_day'], + 'mail_template' => $this->formData['mail_template'], + 'mail_subject' => $this->formData['mail_subject'], + 'mail_frequency' => $this->formData['mail_frequency'], + 'mail_day' => $this->formData['mail_day'], 'mail_recipients' => $this->formData['mail_recipients'], ], [ - 'mail_template' => ['required', 'string',], - 'mail_subject' => ['required', 'string',], - 'mail_frequency' => ['required', 'string',], - 'mail_day' => ['required', 'string',], - 'mail_recipients' => ['required', 'array', 'min:1'], - 'mail_recipients.*.name' => ['required', 'string', 'max:255'], + 'mail_template' => ['required', 'string',], + 'mail_subject' => ['required', 'string',], + 'mail_frequency' => ['required', 'string',], + 'mail_day' => ['required', 'string',], + 'mail_recipients' => ['required', 'array', 'min:1'], + 'mail_recipients.*.name' => ['required', 'string', 'max:255'], 'mail_recipients.*.email' => ['required', 'email'], ])->validateWithBag('saveEmailSettingsForm'); } + + /** + * @return mixed + */ + public function mergeFormData(): void + { + $this->formData['pivot_file'] = $this->pivotFiles[0] ?? null; + $this->formData['span_file'] = $this->spanFiles[0] ?? null; + $this->formData['harvest_file'] = $this->harvestDataFiles[0] ?? null; + } } diff --git a/laravel_app/app/Models/Project.php b/laravel_app/app/Models/Project.php index d544e86..dc29c13 100644 --- a/laravel_app/app/Models/Project.php +++ b/laravel_app/app/Models/Project.php @@ -5,7 +5,6 @@ use App\Jobs\ProjectDownloadTiffJob; use App\Jobs\ProjectMosiacGeneratorJob; use App\Jobs\ProjectReportGeneratorJob; -use App\Livewire\Forms\MailingForm; use Carbon\CarbonPeriod; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -28,52 +27,28 @@ class Project extends Model 'download_path', 'pivot_json_path', 'span_json_path', - 'harvest_json_data' + 'harvest_json_path' ]; public static function saveWithFormData(mixed $formData) { $uniqueIdentifier = ['id' => $formData['id'] ?? null]; $project = Project::updateOrCreate($uniqueIdentifier, $formData); - if ($formData['pivot_json_path']) { - $path = $formData['pivot_json_path']->storeAs($project->download_path .'/Data', 'pivot.geojson'); - $project->update(['pivot_json_path' => $path]); + $baseFrom = 'livewire-tmp/'; + $baseTo = $project->download_path.'/Data/'; + if ($formData['pivot_file']) { + Storage::copy($baseFrom.$formData['pivot_file']['tmpFilename'],$baseTo.'pivot.geojson'); + $project->update(['pivot_json_path' => $baseTo.'pivot.geojson']); } - if ($formData['span_json_path']) { - $path = $formData['span_json_path']->storeAs($project->download_path .'/Data', 'span.geojson'); - $project->update(['span_json_path' => $path]); + if ($formData['span_file']) { + Storage::copy($baseFrom.$formData['span_file']['tmpFilename'],$baseTo.'span.geojson'); + $project->update(['span_json_path' => $baseTo.'span.geojson']); } - if ($formData['harvest_json_data']) { - $path = $formData['harvest_json_path']->storeAs($project->download_path .'/Data', 'harvest.xlsx'); - $project->update(['harvest_json_path' => $path]); + if ($formData['harvest_file']) { + Storage::copy($baseFrom.$formData['harvest_file']['tmpFilename'],$baseTo.'harvest.'.$formData['harvest_file']['extension']); + $project->update(['harvest_json_path' => $baseTo.'harvest.'.$formData['harvest_file']['extension']]); } - $project->upsertBoundingBox($formData); - $project->upsertMailRecipients($formData); } - - private function upsertBoundingBox($formData) - { - $boundingBoxesData = array_map(function ($boundingBox) { - $boundingBox['project_id'] = $this->id; - unset($boundingBox['created_at']); - unset($boundingBox['updated_at']); - $boundingBox['id'] ??= null; - return $boundingBox; - }, $formData['boundingBoxes'] ?? []); - - ProjectBoundingBox::upsert( - $boundingBoxesData, - ['id', 'project_id'], - [ - 'name', - 'top_left_latitude', - 'top_left_longitude', - 'bottom_right_latitude', - 'bottom_right_longitude' - ] - ); - } - private function upsertMailRecipients($formData) { $mailRecipientsData = array_map(function ($mailRecipient) { @@ -95,7 +70,7 @@ public function getMosaicPath() return sprintf('%s/%s', $this->download_path, 'weekly_mosaic'); } - public function getMosaicList(): \Illuminate\Support\Collection + public function getMosaicList(): Collection { return collect(Storage::files($this->getMosaicPath())) ->filter(fn($file) => Str::endsWith($file, '.tif')) @@ -113,7 +88,6 @@ protected static function boot() parent::boot(); // TODO: Change the autogenerated stub static::deleting(function ($project) { - $project->boundingBoxes()->delete(); $project->emailRecipients()->delete(); $project->mailings()->each(function ($mailing) { $mailing->attachments()->delete(); @@ -135,11 +109,6 @@ public function getAttachmentPathAttribute() return '/storage/'.$this->download_path.'/attachments'; } - public function boundingBoxes() - { - return $this->hasMany(ProjectBoundingBox::class); - } - public function emailRecipients() { return $this->hasMany(ProjectEmailRecipient::class); @@ -184,7 +153,7 @@ public function allMosaicsPresent(Carbon $endDate): bool throw new \Exception($message); } - public function getMosaicFilenameListByEndDate(Carbon $endDate): \Illuminate\Support\Collection + public function getMosaicFilenameListByEndDate(Carbon $endDate): Collection { $result = collect([]); for ($i = 0; $i < 4; $i++) { @@ -370,10 +339,4 @@ public function getMosaicsFor($year, $startWeekNumber) return $return; } - - public function getBoundingBoxesAsArray() { - return $this->boundingBoxes->map(function ($boundingBox) { - return $boundingBox->toArrayCustom(); - })->toArray(); - } } diff --git a/laravel_app/app/Models/ProjectBoundingBox.php b/laravel_app/app/Models/ProjectBoundingBox.php deleted file mode 100644 index 0bbc1fd..0000000 --- a/laravel_app/app/Models/ProjectBoundingBox.php +++ /dev/null @@ -1,34 +0,0 @@ -belongsTo(Project::class); - } - - public function toArrayCustom() - { - return [ - (float) $this->top_left_latitude, - (float) $this->top_left_longitude, - (float) $this->bottom_right_latitude, - (float) $this->bottom_right_longitude - ]; - } -} diff --git a/laravel_app/database/migrations/2024_05_22_140729_delete_project_bounding_boxes_table.php b/laravel_app/database/migrations/2024_05_22_140729_delete_project_bounding_boxes_table.php new file mode 100644 index 0000000..5a58d29 --- /dev/null +++ b/laravel_app/database/migrations/2024_05_22_140729_delete_project_bounding_boxes_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignIdFor(Project::class); + $table->string('name'); + $table->string('top_left_latitude'); + $table->string('top_left_longitude'); + $table->string('bottom_right_latitude'); + $table->string('bottom_right_longitude'); + $table->timestamps(); + }); + } +}; diff --git a/laravel_app/resources/views/components/project-manager-properties-modal.blade.php b/laravel_app/resources/views/components/project-manager-properties-modal.blade.php index 84d30fc..cd3ff80 100644 --- a/laravel_app/resources/views/components/project-manager-properties-modal.blade.php +++ b/laravel_app/resources/views/components/project-manager-properties-modal.blade.php @@ -60,76 +60,12 @@ {{ $message }} @enderror - @foreach($projectManager->formData['boundingBoxes'] as $key => $boundingBox) - {{--