--- /dev/null
+# @tag: clients_webdav
+# @description: WebDAV-Migration für Mandanten
+# @depends: clients
+# @ignore: 0
+package SL::DBUpgrade2::clients_webdav;
+
+use strict;
+use utf8;
+
+use parent qw(SL::DBUpgrade2::Base);
+
+use File::Path qw(make_path);
+use IO::Dir;
+use List::MoreUtils qw(any all);
+use List::Util qw(first);
+
+use SL::DBConnect;
+use SL::DBUtils;
+use SL::Template;
+use SL::Helper::Flash;
+
+use Rose::Object::MakeMethods::Generic (
+ 'scalar --get_set_init' => [ qw(clients old_folders) ],
+);
+
+sub init_clients {
+ my ($self) = @_;
+ return [ selectall_hashref_query($::form, $self->dbh, qq|SELECT * FROM auth.clients ORDER BY lower(name)|) ];
+}
+
+sub init_old_folders {
+ tie my %dir, 'IO::Dir', 'webdav';
+ return [ sort grep { -d } keys %dir ];
+}
+
+sub _unlink_old_folders {
+ my ($self, %params) = @_;
+
+ rmdir $_ for @{ $self->old_folders };
+
+ return 1;
+}
+
+sub _ensure_one_client_exists {
+ my ($self, %params) = @_;
+
+ return if 0 != scalar @{ $self->clients };
+
+ my $sql = <<SQL;
+ INSERT INTO auth.clients (name, dbhost, dbport, dbname, dbuser, dbpasswd, is_default)
+ VALUES (?, ?, 5432, ?, ?, ?, true)
+SQL
+
+ $self->dbh->do($sql, undef, $::locale->text('Default Client (unconfigured)'), ($::locale->text('unconfigured')) x 4);
+
+ undef $self->{clients};
+}
+
+sub _move_files_into {
+ my ($self, $client) = @_;
+
+ tie my %dir, 'IO::Dir', 'webdav';
+ my @entries = grep { !m/^\.\.?$/ } keys %dir;
+
+ make_path('webdav/' . $client->{id});
+ rename "webdav/$_", "webdav/" . $client->{id} . "/$_" for @entries;
+}
+
+sub _create_folders {
+ my ($self, $client) = @_;
+ make_path('webdav/' . $client->{id} . "/$_") for qw(angebote bestellungen anfragen lieferantenbestellungen verkaufslieferscheine einkaufslieferscheine gutschriften rechnungen einkaufsrechnungen);
+}
+
+sub _create_symlink {
+ my ($self, $client) = @_;
+
+ my $name = $client->{name} // '';
+ $name =~ s:/+:_:g;
+
+ make_path('webdav/links');
+ symlink '../' . $client->{id}, "webdav/links/${name}";
+}
+
+sub run {
+ my ($self) = @_;
+
+ # WebDAV not activated? Remove old folders, and we're done.
+ return $self->_unlink_old_folders if !$::lx_office_conf{features}->{webdav};
+
+ # Ensure at least one client exists.
+ $self->_ensure_one_client_exists;
+
+ my $client_to_use;
+ if (1 == scalar @{ $self->clients }) {
+ # Exactly one client? Great, use that one without bothering the
+ # user.
+ $client_to_use = $self->clients->[0];
+
+ } else {
+ # If there's more than one client then let the user select which
+ # client to move the old files into. Maybe she already did?
+ $client_to_use = first { $_->{id} == $::form->{client_id} } @{ $self->clients } if $::form->{client_id};
+
+ if (!$client_to_use) {
+ # Nope, let's select it.
+ print $::form->parse_html_template('dbupgrade/auth/clients_webdav', { SELF => $self, default_client => (first { $_->{is_default} } @{ $self->clients }) });
+ return 2;
+ }
+ }
+
+ # Move files for the selected client.
+ $self->_move_files_into($client_to_use);
+
+ # Create the directory structures for all (even the selected client
+ # -- folders might be missing).
+ for (@{ $self->clients }) {
+ $self->_create_folders($_);
+ $self->_create_symlink($_);
+ }
+
+ return 1;
+}
+
+1;
--- /dev/null
+[%- USE LxERP -%][%- USE L -%]
+
+[%- INCLUDE 'common/flash.html' %]
+
+<h1>[%- LxERP.t8("Introduction of clients") %] -- [% LxERP.t8("Handling of WebDAV") %]</h1>
+
+<p>
+ [% LxERP.t8("The WebDAV feature is activated.") %]
+ [% LxERP.t8("With the introduction of clients each client gets its own WebDAV folder.") %]
+ [% LxERP.t8("In order to migrate the old folder structure into the new structure you have to chose which client the old structure will be assigned to.") %]
+ [% LxERP.t8("All the other clients will start with an empty set of WebDAV folders.") %]
+</p>
+
+<form method="post" action="controller.pl">
+ <p>
+ [% LxERP.t8("Client to assign the existing WebDAV folders to") %]:
+ [% L.select_tag('client_id', SELF.clients, default=default_client.id, title_key='name') %]
+ </p>
+
+ <p>
+ [%- L.hidden_tag('action', 'Admin/apply_dbupgrade_scripts') %]
+ [% L.submit_tag('dummy', LxERP.t8('Continue')) %]
+ </p>
+</form>