Druckerverwaltung auf Admin-Controller umgestellt
authorMoritz Bunkus <m.bunkus@linet-services.de>
Wed, 12 Jun 2013 09:44:33 +0000 (11:44 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 13 Jun 2013 14:31:37 +0000 (16:31 +0200)
12 files changed:
SL/Controller/Admin.pm
SL/DB/Manager/Printer.pm [new file with mode: 0644]
SL/DB/Printer.pm
bin/mozilla/admin.pl
bin/mozilla/admin_printer.pl [deleted file]
templates/webpages/admin/edit_printer.html [new file with mode: 0644]
templates/webpages/admin/list_printers.html [new file with mode: 0644]
templates/webpages/admin/show.html
templates/webpages/admin_printer/_login_form.html [deleted file]
templates/webpages/admin_printer/edit.html [deleted file]
templates/webpages/admin_printer/list.html [deleted file]
templates/webpages/admin_printer/login.html [deleted file]

index 6f9d224..357d7b7 100644 (file)
@@ -5,19 +5,23 @@ use strict;
 use parent qw(SL::Controller::Base);
 
 use IO::File;
+use List::Util qw(first);
 
 use SL::DB::AuthUser;
 use SL::DB::AuthGroup;
+use SL::DB::Printer;
 use SL::Helper::Flash;
 use SL::Locale::String qw(t8);
 use SL::User;
 
 use Rose::Object::MakeMethods::Generic
 (
-  'scalar --get_set_init' => [ qw(client user group nologin_file_name db_cfg all_dateformats all_numberformats all_countrycodes all_stylesheets all_menustyles all_clients all_groups all_users all_rights is_locked) ],
+  'scalar --get_set_init' => [ qw(client user group printer nologin_file_name db_cfg is_locked
+                                  all_dateformats all_numberformats all_countrycodes all_stylesheets all_menustyles all_clients all_groups all_users all_rights all_printers) ],
 );
 
 __PACKAGE__->run_before(\&setup_layout);
+__PACKAGE__->run_before(\&setup_client, only => [ qw(list_printers new_printer edit_printer save_printer delete_printer) ]);
 
 sub get_auth_level { "admin" };
 sub keep_auth_vars {
@@ -290,6 +294,61 @@ sub action_delete_group {
   $self->redirect_to(action => 'show');
 }
 
+#
+# actions: printers
+#
+
+sub action_list_printers {
+  my ($self) = @_;
+  $self->render('admin/list_printers', title => t8('Printer management'));
+}
+
+sub action_new_printer {
+  my ($self) = @_;
+
+  $self->printer(SL::DB::Printer->new);
+  $self->edit_printer_form(title => t8('Create a new printer'));
+}
+
+sub action_edit_printer {
+  my ($self) = @_;
+  $self->edit_printer_form(title => t8('Edit Printer'));
+}
+
+sub action_save_printer {
+  my ($self) = @_;
+  my $params = delete($::form->{printer}) || { };
+  my $is_new = !$params->{id};
+
+  $self->printer($is_new ? SL::DB::Printer->new : SL::DB::Printer->new(id => $params->{id})->load)->assign_attributes(%{ $params });
+
+  my @errors = $self->printer->validate;
+
+  if (@errors) {
+    flash('error', @errors);
+    $self->edit_printer_form(title => $is_new ? t8('Create a new printer') : t8('Edit Printer'));
+    return;
+  }
+
+  $self->printer->save;
+
+  flash_later('info', $is_new ? t8('The printer has been created.') : t8('The printer has been saved.'));
+  $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
+}
+
+sub action_delete_printer {
+  my ($self) = @_;
+
+  if (!$self->printer->delete) {
+    flash('error', t8('The printer could not be deleted.'));
+    $self->edit_printer_form(title => t8('Edit Printer'));
+    return;
+  }
+
+  flash_later('info', t8('The printer has been deleted.'));
+  $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
+}
+
 #
 # actions: locking, unlocking
 #
@@ -319,18 +378,20 @@ sub action_lock_system {
 # initializers
 #
 
-sub init_db_cfg            { $::lx_office_conf{'authentication/database'}                                            }
-sub init_nologin_file_name { $::lx_office_conf{paths}->{userspath} . '/nologin';                                     }
-sub init_is_locked         { -e $_[0]->nologin_file_name                                                             }
-sub init_client            { SL::DB::AuthClient->new(id => ($::form->{id} || ($::form->{client} || {})->{id}))->load }
-sub init_user              { SL::DB::AuthUser  ->new(id => ($::form->{id} || ($::form->{user}   || {})->{id}))->load }
-sub init_group             { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group}  || {})->{id}))->load }
-sub init_all_clients       { SL::DB::Manager::AuthClient->get_all_sorted                                             }
-sub init_all_users         { SL::DB::Manager::AuthUser->get_all_sorted                                               }
-sub init_all_groups        { SL::DB::Manager::AuthGroup->get_all_sorted                                              }
-sub init_all_dateformats   { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd)      ]                                      }
-sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00)          ]                                      }
-sub init_all_stylesheets   { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ]                                      }
+sub init_db_cfg            { $::lx_office_conf{'authentication/database'}                                                    }
+sub init_nologin_file_name { $::lx_office_conf{paths}->{userspath} . '/nologin';                                             }
+sub init_is_locked         { -e $_[0]->nologin_file_name                                                                     }
+sub init_client            { SL::DB::Manager::AuthClient->find_by(id => ($::form->{id} || ($::form->{client}  || {})->{id})) }
+sub init_user              { SL::DB::AuthUser  ->new(id => ($::form->{id} || ($::form->{user}    || {})->{id}))->load        }
+sub init_group             { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group}   || {})->{id}))->load        }
+sub init_printer           { SL::DB::Printer   ->new(id => ($::form->{id} || ($::form->{printer} || {})->{id}))->load        }
+sub init_all_clients       { SL::DB::Manager::AuthClient->get_all_sorted                                                     }
+sub init_all_users         { SL::DB::Manager::AuthUser  ->get_all_sorted                                                     }
+sub init_all_groups        { SL::DB::Manager::AuthGroup ->get_all_sorted                                                     }
+sub init_all_printers      { SL::DB::Manager::Printer   ->get_all_sorted                                                     }
+sub init_all_dateformats   { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd)      ]                                              }
+sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00)          ]                                              }
+sub init_all_stylesheets   { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ]                                              }
 sub init_all_menustyles    {
   return [
     { id => 'old', title => $::locale->text('Old (on the side)') },
@@ -377,6 +438,14 @@ sub setup_layout {
   $::form->{favicon} = "favicon.ico";
 }
 
+sub setup_client {
+  my ($self) = @_;
+
+  $self->client((first { $_->is_default } @{ $self->all_clients }) || $self->all_clients->[0]) if !$self->client;
+  $::auth->set_client($self->client->id);
+}
+
+
 #
 # displaying forms
 #
@@ -409,6 +478,11 @@ sub edit_group_form {
   $self->use_multiselect_js->render('admin/edit_group', %params);
 }
 
+sub edit_printer_form {
+  my ($self, %params) = @_;
+  $self->render('admin/edit_printer', %params);
+}
+
 #
 # helpers
 #
diff --git a/SL/DB/Manager/Printer.pm b/SL/DB/Manager/Printer.pm
new file mode 100644 (file)
index 0000000..ec1e831
--- /dev/null
@@ -0,0 +1,20 @@
+package SL::DB::Manager::Printer;
+
+use strict;
+
+use SL::DB::Helper::Manager;
+use base qw(SL::DB::Helper::Manager);
+
+use SL::DB::Helper::Paginated;
+use SL::DB::Helper::Sorted;
+
+sub object_class { 'SL::DB::Printer' }
+
+__PACKAGE__->make_manager_methods;
+
+sub _sort_spec {
+  return ( default => [ 'printer_description', 1 ],
+           columns => { SIMPLE => 'ALL' } );
+}
+
+1;
index fd58eb5..83f9148 100644 (file)
@@ -3,11 +3,22 @@ package SL::DB::Printer;
 use strict;
 
 use SL::DB::MetaSetup::Printer;
-
-__PACKAGE__->meta->make_manager_class;
+use SL::DB::Manager::Printer;
+use SL::DB::Helper::Util;
 
 sub description {
   goto &printer_description;
 }
 
+sub validate {
+  my ($self) = @_;
+
+  my @errors;
+  push @errors, $::locale->text('The description is missing.')    if !$self->printer_description;
+  push @errors, $::locale->text('The command is missing.')        if !$self->printer_command;
+  push @errors, $::locale->text('The description is not unique.') if !SL::DB::Helper::Util::is_unique($self, 'printer_description');
+
+  return @errors;
+}
+
 1;
index e069476..20f901b 100755 (executable)
@@ -60,7 +60,6 @@ use SL::DBUtils;
 use SL::Template;
 
 require "bin/mozilla/common.pl";
-require "bin/mozilla/admin_printer.pl";
 
 use strict;
 
diff --git a/bin/mozilla/admin_printer.pl b/bin/mozilla/admin_printer.pl
deleted file mode 100644 (file)
index 9d514b3..0000000
+++ /dev/null
@@ -1,117 +0,0 @@
-use strict;
-
-use SL::Printer;
-
-sub get_login {
-  unless ($::form->{login}) {
-    get_login_form();
-    ::end_of_request();
-  }
-  return $::form->{login};
-}
-
-sub get_login_form {
-  my %users = $::auth->read_all_users;
-
-  $::form->header;
-  print $::form->parse_html_template('admin_printer/login', {
-    users => [ values %users ],
-  });
-}
-
-sub printer_dispatcher {
-  for my $action (qw(get_login_form list_printers add_printer edit_printer save_printer delete_printer)) {
-    if ($::form->{$action}) {
-      ::call_sub($::locale->findsub($action));
-      ::end_of_request()
-    }
-  }
-  die "cannot find sub";
-}
-
-sub printer_management {
-  &get_login_form;
-}
-
-sub add_printer {
-  $::lxdebug->enter_sub;
-
-  my $login   = get_login();
-  my %users   = $::auth->read_all_users;
-
-  $::form->header;
-  print $::form->parse_html_template('admin_printer/edit', {
-    title   => $::locale->text("Add Printer"),
-    printer => { },
-    users   => [ values %users ],
-  });
-
-  $::lxdebug->leave_sub
-}
-
-sub edit_printer {
-  $::lxdebug->enter_sub;
-
-  my $login = get_login();
-  my $id    = $::form->{id} or $::form->{printer}{id} or &add_printer;
-  my %users   = $::auth->read_all_users;
-
-  my $printer = SL::Printer->get_printer(id => $id, login => $login);
-
-  $::form->header;
-  print $::form->parse_html_template('admin_printer/edit', {
-    title   => $::locale->text("Edit Printer"),
-    printer => $printer,
-    users   => [ values %users ],
-  });
-
-  $::lxdebug->leave_sub;
-}
-
-sub list_printers {
-  $::lxdebug->enter_sub;
-
-  my $login    = get_login();
-  my $printers = SL::Printer->all_printers(login => $login);
-  my %users   = $::auth->read_all_users;
-
-  $::form->header;
-  print $::form->parse_html_template('admin_printer/list', {
-    title        => $::locale->text('Printer'),
-    all_printers => $printers,
-    edit_link    => build_std_url("login=$login", 'action=edit_printer', 'id='),
-    users        => [ values %users ],
-  });
-
-  $::lxdebug->leave_sub;
-}
-
-
-sub save_printer {
-  $::lxdebug->enter_sub;
-
-  my $login   = get_login();
-  my $printer = $::form->{printer} || die 'no printer to save';
-
-  $::form->error($::locale->text('Description missing!'))     unless $printer->{printer_description};
-  $::form->error($::locale->text('Printer Command missing!')) unless $printer->{printer_command};
-
-  SL::Printer->save_printer(%$::form);
-
-  list_printers();
-  $::lxdebug->leave_sub;
-}
-
-sub delete_printer {
-  $::lxdebug->enter_sub;
-
-  my $login   = get_login();
-  my $printer = $::form->{printer} || die 'no printer to delete';
-
-  SL::Printer->delete_printer(%$::form);
-  list_printers();
-
-  $::lxdebug->leave_sub;
-}
-
-1;
diff --git a/templates/webpages/admin/edit_printer.html b/templates/webpages/admin/edit_printer.html
new file mode 100644 (file)
index 0000000..8e2f5a2
--- /dev/null
@@ -0,0 +1,35 @@
+[%- USE LxERP -%][%- USE HTML -%][%- USE L -%]
+
+[% INCLUDE 'common/flash.html' %]
+
+<h1>[% HTML.escape(title) %]</h1>
+
+<form method="post">
+ [% L.hidden_tag("client.id", SELF.client.id) %]
+ [% L.hidden_tag("action", 'Admin/dispatch') %]
+ [% L.hidden_tag("printer.id", SELF.printer.id) %]
+
+ <table>
+  <tr>
+   <th align="right">[% LxERP.t8('Printer Description') %]</th>
+   <td>[% L.input_tag("printer.printer_description", SELF.printer.printer_description, size=30) %]</td>
+  <tr>
+  <tr>
+   <th align="right">[% LxERP.t8('Printer Command') %]</th>
+   <td>[% L.input_tag("printer.printer_command", SELF.printer.printer_command, size=30) %]</td>
+  </tr>
+  <tr>
+   <th align="right">[% LxERP.t8('Template Code') %]</th>
+   <td>[% L.input_tag("printer.template_code", SELF.printer.template_code, size=8) %]</td>
+  </tr>
+ </table>
+
+ <p>
+  <a href="[% SELF.url_for(action='list_printers', 'client.id'=SELF.client.id) %]">[% LxERP.t8("Back") %]</a>
+  [% L.submit_tag("action_save_printer", LxERP.t8("Save")) %]
+  [%- IF SELF.printer.id %]
+   [% L.submit_tag("action_delete_printer", LxERP.t8("Delete"), confirm=LxERP.t8("Are you sure?")) %]
+ [%- END %]
+ </p>
+
+</form>
diff --git a/templates/webpages/admin/list_printers.html b/templates/webpages/admin/list_printers.html
new file mode 100644 (file)
index 0000000..4da7eff
--- /dev/null
@@ -0,0 +1,70 @@
+[%- USE HTML -%][%- USE LxERP -%][%- USE L -%]
+
+[% INCLUDE 'common/flash.html' %]
+
+<h1>[% HTML.escape(title) %]</h1>
+
+[% IF !SELF.all_clients.size %]
+<div class="error">
+ [% LxERP.t8("Error") %]:
+ [% LxERP.t8("No clients have been created yet.") %]
+</div>
+
+<div>
+ <a href="[% SELF.url_for(action='show') %]">[% LxERP.t8("Back") %]</a>
+</div>
+
+[%- ELSE %]
+
+ <div>
+  [% LxERP.t8("Actions") %]:
+  <span class="link_separator"></span>
+  <a href="[% SELF.url_for(action='show') %]">[% LxERP.t8("Back") %]</a>
+  <span class="link_separator">|</span>
+  <a href="[% SELF.url_for(action='new_printer', 'client.id'=SELF.client.id) %]">[% LxERP.t8("Add printer") %]</a>
+ </div>
+
+ <hr>
+
+ <p>
+  [% LxERP.t8("Client to configure the printers for") %]:
+  [% L.select_tag('client.id', SELF.all_clients, id='client_id', title_key='name', default=SELF.client.id) %]
+ </p>
+
+ [%- IF !SELF.all_printers.size %]
+
+  <p>
+   [% LxERP.t8("No printers have been created yet.") %]
+  </p>
+
+ [%- ELSE %]
+
+  <table width="100%">
+   <tr class="listheading">
+    <th>[% LxERP.t8('Description') %]</th>
+    <th>[% LxERP.t8('Printer Command') %]</th>
+    <th>[% LxERP.t8('Template Code') %]</th>
+   </tr>
+
+   [%- FOREACH printer = SELF.all_printers %]
+    <tr valign="top" class="listrow">
+     <td><a href="[% SELF.url_for(action='edit_printer', 'client.id'=SELF.client.id, id=printer.id) %]">[% HTML.escape(printer.printer_description) %]</a></td>
+     <td>[% HTML.escape(printer.printer_command) %]</td>
+     <td>[% HTML.escape(printer.template_code) %]</td>
+    </tr>
+   [%- END %]
+  </table>
+
+ [%- END %]
+
+ <script type="text/javascript">
+<!--
+  $(function() {
+    $('#client_id').change(function() {
+      window.location.href = "controller.pl?action=Admin/list_printers&client.id=" + $('#client_id').val();
+    });
+  });
+-->
+ </script>
+
+[%- END %]
index 734deae..e046518 100644 (file)
@@ -15,7 +15,7 @@
  <span class="link_separator">|</span>
  [% L.link(SELF.url_for(action="pg_database_administration", controller="admin.pl"), LxERP.t8("Pg Database Administration")) %]
  <span class="link_separator">|</span>
- [% L.link(SELF.url_for(action="printer_management", controller="admin.pl"), LxERP.t8("Printer Management")) %]
+ [% L.link(SELF.url_for(action="list_printers"), LxERP.t8("Printer Management")) %]
  <span class="link_separator">|</span>
  [% IF SELF.is_locked %]
   [% L.link(SELF.url_for(action="unlock_system"), LxERP.t8("Unlock System")) %]
diff --git a/templates/webpages/admin_printer/_login_form.html b/templates/webpages/admin_printer/_login_form.html
deleted file mode 100644 (file)
index 4789d12..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-[%- USE T8 %]
-[%- USE L %]
-<p>[% 'Please select a user' | $T8 %]: [% L.select_tag('login', users, value_key = 'login', title_key = 'login', default = login) %]</p>
-
diff --git a/templates/webpages/admin_printer/edit.html b/templates/webpages/admin_printer/edit.html
deleted file mode 100644 (file)
index 7aef54d..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-[%- USE T8 %]
-
-<form method=post>
-
-<h1 class=listtop colspan=2>[% title %]</h1>
-
-[%- PROCESS 'admin_printer/_login_form.html' %]
-
-<input type=hidden name="printer.id" value="[% printer.id | html %]">
-<table width=100%>
-  <tr height="5"></tr>
-  <tr>
-    <th align=left>[% 'Printer' | $T8 %]</th>
-    <td><input name="printer.printer_description" size=30 value="[% printer.printer_description | html %]"></td>
-  <tr>
-  <tr>
-    <th align=left>[% 'Printer Command' | $T8 %]</th>
-    <td><input name="printer.printer_command" size=30 value="[% printer.printer_command | html %]"></td>
-  </tr>
-  <tr>
-    <th align=left>[% 'Template Code' | $T8 %]</th>
-    <td><input name="printer.template_code" size=5 value="[% printer.template_code | html %]"></td>
-  </tr>
-  <td colspan=2><hr size=3 noshade></td>
-  </tr>
-</table>
-
-<br>
-<input type=hidden name=action value="printer_dispatcher">
-<input type=submit class=submit name=list_printers value="[% 'Back' | $T8 %]">
-<input type=submit class=submit name=save_printer value="[% 'Save' | $T8 %]">
-
-[%- IF id %]
-<input type=submit class=submit name=delete_printer value="[% 'Delete' | $T8 %]">
-[%- END %]
-
-</form>
-
diff --git a/templates/webpages/admin_printer/list.html b/templates/webpages/admin_printer/list.html
deleted file mode 100644 (file)
index 6a413d8..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-[%- USE T8 %]
-
-<form method='post'>
-
-<h1 class=listtop>[% title %]</h1>
-
-[%- PROCESS 'admin_printer/_login_form.html' %]
-
-<table width=100%>
-  <tr>
-    <td>
-      <table width=100%>
-        <tr class=listheading>
-          <th>[% 'Description' | $T8 %]</th>
-          <th>[% 'Printer Command' | $T8 %]</th>
-          <th>[% 'Template Code' | $T8 %]</th>
-        </tr>
-[%- IF all_printers.size %]
-[%- FOREACH row = all_printers %]
-        <tr valign=top class="listrow[% loop.count % 2 %]">
-          <td>&nbsp;<a href="[% edit_link %][% row.id %]">[% row.printer_description %]</a></td>
-          <td align=left>&nbsp;[% row.printer_command | html %]</td>
-          <td align=left>&nbsp;[% row.template_code | html %]</td>
-        </tr>
-[%- END %]
-[%- ELSE %]
-        <tr><td colspan='3'><p class="message_hint">[% 'No data was found.' | $T8 %]</p></td></tr>
-[%- END %]
-      </table>
-    </td>
-  </tr>
-  <tr>
-  <td><hr size=3 noshade></td>
-  </tr>
-</table>
-
-<br>
- <input type="hidden" name="action" value="printer_dispatcher">
- <input type="submit" name='get_login_form' value="[% 'Back' | $T8 %]">
- <input type="submit" name="add_printer" value ="[% 'Add' | $T8 %]">
-</form>
diff --git a/templates/webpages/admin_printer/login.html b/templates/webpages/admin_printer/login.html
deleted file mode 100644 (file)
index 7ddee95..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-[% USE T8 %]
-[% USE L %]
-
-<h1 class=listtop>[% 'Printer Management' | $T8 %]</h1>
-<form method='post'>
-<p>[% 'Printers are created for a user database. Please select a user. The associated database will be edited.' | $T8 %]</p>
-
-[%- PROCESS 'admin_printer/_login_form.html' %]
-
-<input type='hidden' name='action' value='printer_dispatcher'>
-<p>
- <a href="controller.pl?action=Admin/show">[% 'Back' | $T8 %]</a>
-<input type='submit' name='list_printers' value='[% 'Continue' | $T8 %]'>
-</p>
-
-</form>