*/ private const DELETE_ORDER = [ 'floor_plans', 'channel_sync_logs', 'channel_tasks', 'portal_emails', 'payments', 'reviews', 'blocked_periods', 'bookings', 'special_offers', 'extras', 'seasons', 'amenities', 'photos', 'appearances', 'pages', 'notices', 'faqs', 'content_blocks', 'guests', 'channels', 'properties', 'site_domains', 'sites', 'tenant_services', 'service_usages', 'tenant_access_logs', 'tenant_user', ]; /** * Writes every row and every uploaded file of this landlord into one zip file. * * @return string the path of the written archive */ public function export(Tenant $tenant, string $directory): string { File::ensureDirectoryExists($directory); $path = rtrim($directory, '/\\').DIRECTORY_SEPARATOR.'mandant-'.$tenant->slug.'-'.now()->format('Ymd-His').'.zip'; $zip = new ZipArchive; if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { throw new RuntimeException('Archiv konnte nicht angelegt werden: '.$path); } $zip->addFromString('mandant.json', (string) json_encode($tenant->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); foreach ($this->tables() as $table) { $rows = $this->rowsOf($table, $tenant); $zip->addFromString( 'daten/'.$table.'.json', (string) json_encode($rows, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), ); } $zip->addFromString( 'daten/benutzer.json', (string) json_encode($this->usersOf($tenant), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), ); $this->addFiles($zip, $tenant); $zip->close(); return $path; } /** * Who worked for this landlord, with their role. Deliberately without password hashes and without the * two-factor secrets: the export is handed out, and those would be worth stealing. * * @return list */ public function usersOf(Tenant $tenant): array { return User::query() ->whereHas('tenants', fn ($query) => $query->where('tenants.id', $tenant->id)) ->withCount('tenants') ->get() ->map(fn (User $user): array => [ 'id' => $user->id, 'name' => $user->name, 'email' => $user->email, 'role' => $user->roleOn($tenant->id)?->value, 'weitere_mandanten' => max(0, $user->tenants_count - 1), ]) ->all(); } /** * Removes the landlord and everything that belongs to them, files included. The landlord row goes last. * * @return array how many rows were removed per table */ public function delete(Tenant $tenant): array { $this->deleteFiles($tenant); $orphans = $this->accountsLosingTheirLastLandlord($tenant); $removed = []; DB::transaction(function () use ($tenant, $orphans, &$removed): void { foreach (self::DELETE_ORDER as $table) { if (! $this->hasTenantColumn($table)) { continue; } $count = DB::table($table)->where('tenant_id', $tenant->id)->delete(); if ($count > 0) { $removed[$table] = $count; } } if ($orphans !== []) { $removed['users'] = User::query()->whereKey($orphans)->delete(); } $tenant->delete(); }); return $removed; } /** * Accounts that belonged to this landlord only. They are removed with them – an account that can reach * nothing is personal data without a purpose. An operator keeps their account, and so does anybody who * still works for another landlord. * * @return list */ private function accountsLosingTheirLastLandlord(Tenant $tenant): array { return User::query() ->where('is_operator', false) ->whereHas('tenants', fn ($query) => $query->where('tenants.id', $tenant->id)) ->withCount('tenants') ->get() ->filter(fn (User $user): bool => $user->tenants_count === 1) ->pluck('id') ->all(); } /** * Tables carrying a tenant_id, in a stable order. * * @return list */ public function tables(): array { return array_values(array_filter(self::DELETE_ORDER, fn (string $table): bool => $this->hasTenantColumn($table))); } /** * @return list> */ private function rowsOf(string $table, Tenant $tenant): array { return array_map( fn (object $row): array => (array) $row, DB::table($table)->where('tenant_id', $tenant->id)->get()->all(), ); } /** * Photos keep their files through Spatie's media library; they hang off the photo, not off the landlord, * so they are collected through the photos of this landlord. */ private function addFiles(ZipArchive $zip, Tenant $tenant): void { foreach ($this->mediaOf($tenant) as $media) { $path = $media->getPath(); if (is_string($path) && File::exists($path)) { $zip->addFile($path, 'dateien/'.$media->id.'-'.$media->file_name); } } } private function deleteFiles(Tenant $tenant): void { foreach ($this->mediaOf($tenant) as $media) { $media->delete(); // removes the stored file as well } } /** * @return Collection */ private function mediaOf(Tenant $tenant): Collection { return app(TenantContext::class)->run( $tenant, fn () => Photo::query()->with('media')->get()->flatMap(fn (Photo $photo) => $photo->media), ); } private function hasTenantColumn(string $table): bool { static $known = []; return $known[$table] ??= Schema::hasTable($table) && Schema::hasColumn($table, 'tenant_id'); } }