From 08669c947afaeb4ac355e9cbd68ee82567d32f42 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Sat, 5 Apr 2014 16:46:31 -0700 Subject: [PATCH] installers, RT#16584 --- FS/FS/sched_avail.pm | 34 ++++++++++ httemplate/browse/sched_item.html | 72 ++++++++++++++++++++-- httemplate/edit/process/sched_item.html | 32 ++++++++++ httemplate/edit/sched_item.html | 49 +++++++++++++++ httemplate/elements/select-time-from_to.html | 17 +++++ httemplate/elements/select-time.html | 49 +++++++++++++++ .../elements/tr-select-avail-time-from_to.html | 58 +++++++++++++++++ 7 files changed, 307 insertions(+), 4 deletions(-) create mode 100644 httemplate/edit/process/sched_item.html create mode 100644 httemplate/edit/sched_item.html create mode 100644 httemplate/elements/select-time-from_to.html create mode 100644 httemplate/elements/select-time.html create mode 100644 httemplate/elements/tr-select-avail-time-from_to.html diff --git a/FS/FS/sched_avail.pm b/FS/FS/sched_avail.pm index 5d9f8dfba..912ad543d 100644 --- a/FS/FS/sched_avail.pm +++ b/FS/FS/sched_avail.pm @@ -112,6 +112,40 @@ sub check { $self->SUPER::check; } +=item stime_pretty + +=item etime_pretty + +=cut + +sub stime_pretty { shift->_time_pretty('stime', @_); } +sub etime_pretty { shift->_time_pretty('etime', @_); } + +sub _time_pretty { + my( $self, $field ) = @_; + + pretty_time( $self->$field() ); +} + +#helper sub +sub pretty_time { + my $t = shift; + + return 'Midnight' if $t == 0 || $t == 1440; + return 'Noon' if $t == 720; + + my $h = int( $t / 60 ); + my $m = $t % 60; + + my $ap = 'AM'; + if ( $h == 0 || $h == 24 ) { $h = 12; } + elsif ( $h == 12 ) { $ap = 'PM'; } + elsif ( $h > 12 ) { $ap = 'PM'; $h -= 12; } + + sprintf('%02d:%02d'." $ap", $h, $m); + +} + =back =head1 BUGS diff --git a/httemplate/browse/sched_item.html b/httemplate/browse/sched_item.html index 09435805a..372d4fd7e 100644 --- a/httemplate/browse/sched_item.html +++ b/httemplate/browse/sched_item.html @@ -1,11 +1,17 @@ <& elements/browse.html, - 'title' => 'Installers', #PL($sched_item_class->classname), + 'title' => 'Installer schedules', #$sched_item_class->classname. ' schedules', 'name_singular' => 'installer', #$sched_item_class->classname 'query' => { 'table' => 'sched_item' }, 'count_query' => 'SELECT COUNT(*) FROM sched_item', - 'header' => [ 'Installer', ], #$sched_item_class->classname - 'fields' => [ 'name' ], - #'links' => [ $link, ], + 'header' => [ 'Installer', #$sched_item_class->classname + @dow, + 'Overrides', + ], + 'fields' => [ 'name', + ( map dow_factory($_), (0..6) ), + $override_sub, + ], + 'links' => [ $link, ], 'disableable' => 1, 'disabled_statuspos' => 1, &> @@ -15,4 +21,62 @@ die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); +my $link = [ $p.'edit/sched_item.html?', 'itemnum' ]; + +my @dow = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); + +sub dow_factory { + my $wday = shift; + + return sub { + my $sched_item = shift; + + my @sched_avail = qsearch({ + 'table' => 'sched_avail', + 'hashref' => { + 'itemnum' => $sched_item->itemnum, + 'wday' => $wday, #thanks, closure + 'override_date' => '', + }, + 'order_by' => 'ORDER BY stime', + }); + + return 'Not available' unless @sched_avail; + + #data + # align bgcolor colspan rowspan style valign width + # link onclick size data_style + + [ + map { + [ { data=> $_->stime_pretty }, + { data=> '-', }, + { data=> $_->etime_pretty }, + ]; + } + @sched_avail + ]; + + }; + +} + +my $override_sub = sub { + my $sched_item = shift; + + my @sched_avail = qsearch({ + 'table' => 'sched_avail', + 'hashref' => { + 'itemnum' => $sched_item->itemnum, + 'override_date' => { op=>'!=', value=>'' }, + }, + 'order_by' => 'ORDER BY override_date, stime', + }); + + return 'None' unless @sched_avail; + + #XXX print each override date (and day of week) + +}; + diff --git a/httemplate/edit/process/sched_item.html b/httemplate/edit/process/sched_item.html new file mode 100644 index 000000000..7731a35b9 --- /dev/null +++ b/httemplate/edit/process/sched_item.html @@ -0,0 +1,32 @@ +%if ( $error ) { +% $cgi->param('error', $error ); +<% $cgi->redirect(popurl(2). "sched_item.html?". $cgi->query_string ) %> +%} else { +<% $cgi->redirect(popurl(3). "browse/sched_item.html") %> +%} +<%init> + +#XXX more specific AccessRight for scheduling +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); + +my $itemnum = $cgi->param('itemnum'); + +my $sched_item = qsearchs('sched_item', { 'itemnum' => $itemnum }) + or die "unknown itemnum $itemnum"; + +my @sched_avail = (); +foreach my $wday (0..6) { + if ( $cgi->param("sched_avail$wday") eq 'Y' ) { + push @sched_avail, new FS::sched_avail { + 'itemnum' => $itemnum, + 'wday' => $wday, + 'stime' => $cgi->param("sched_avail$wday".'_stime'), + 'etime' => $cgi->param("sched_avail$wday".'_etime'), + }; + } +} + +my $error = $sched_item->replace_sched_avail(@sched_avail); + + diff --git a/httemplate/edit/sched_item.html b/httemplate/edit/sched_item.html new file mode 100644 index 000000000..7dfe26888 --- /dev/null +++ b/httemplate/edit/sched_item.html @@ -0,0 +1,49 @@ +<& elements/edit.html, + 'name_singular' => 'Installer schedule', #$sched_item_class->classname. ' schedule', + 'table' => 'sched_item', + 'viewall_dir' => 'browse', + 'no_pkey_display' => 1, + 'labels' => { 'name' => 'Name', + map { ( "sched_avail$_" => $dow[$_] ) } (0..6), + }, + 'fields' => [ + { 'field' => 'name', + 'type' => 'fixed', + }, + ( map dow_factory($_), (0..6) ), + ], + 'edit_callback' => $edit_callback, +&> +<%init> + +#XXX more specific AccessRight for scheduling +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); + +my @dow = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); + +sub dow_factory { + my $wday = shift; + + { 'field' => 'sched_avail'. $wday, + 'type' => 'select-avail-time-from_to', + 'prefix' => 'sched_avail'. $wday. '_', + }; + +} + +my $edit_callback = sub { + my( $cgi, $sched_item, $fields_listref, $opt_hashref ) = @_; + foreach my $sched_avail ( $sched_item->sched_avail ) { + if ( $sched_avail->override_date ) { + #XXX handle + } else { + my $param = 'sched_avail'. $sched_avail->wday; + $cgi->param($param, 'Y'); + $cgi->param($param.'_stime', $sched_avail->stime); + $cgi->param($param.'_etime', $sched_avail->etime); + } + } +}; + + diff --git a/httemplate/elements/select-time-from_to.html b/httemplate/elements/select-time-from_to.html new file mode 100644 index 000000000..9d49b7ab8 --- /dev/null +++ b/httemplate/elements/select-time-from_to.html @@ -0,0 +1,17 @@ +<& select-time.html, + %opt, + 'field' => $opt{'prefix'}. 'stime', + 'selected_default' => 480, #8am... configure? + 'curr_value' => $cgi->param($opt{'prefix'}.'stime'), +&> + - +<& select-time.html, + %opt, + 'field' => $opt{'prefix'}. 'etime', + 'selected_default' => 1080, #6pm.. configure? + 'curr_value' => $cgi->param($opt{'prefix'}.'etime'), +&> +<%init> + my %opt = @_; + + diff --git a/httemplate/elements/select-time.html b/httemplate/elements/select-time.html new file mode 100644 index 000000000..6e22e1e3d --- /dev/null +++ b/httemplate/elements/select-time.html @@ -0,0 +1,49 @@ +% unless ( $opt{'js_only'} ) { + +% } +<%init> + +my %opt = @_; +my $increment = $opt{increment} || 30; +my $selected_default = $opt{selected_default}; +my $disabled = $opt{'disabled'} || ''; + +my $curr_value = $opt{curr_value}; + +my $onchange = ''; +if ( $opt{'onchange'} ) { + $onchange = $opt{'onchange'}; + $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/; + $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack. all onchange + #callbacks should act the same + $onchange = 'onChange="'. $onchange. '"' unless $onchange =~ /^onChange=/i; +} + +sub pretty_time { + my $t = shift; + + return 'Midnight' if $t == 0 || $t == 1440; + return 'Noon' if $t == 720; + + my $h = int( $t / 60 ); + my $m = $t % 60; + + my $ap = 'AM'; + if ( $h == 0 || $h == 24 ) { $h = 12; } + elsif ( $h == 12 ) { $ap = 'PM'; } + elsif ( $h > 12 ) { $ap = 'PM'; $h -= 12; } + + sprintf('%02d:%02d'." $ap", $h, $m); + +} + + diff --git a/httemplate/elements/tr-select-avail-time-from_to.html b/httemplate/elements/tr-select-avail-time-from_to.html new file mode 100644 index 000000000..b867388f8 --- /dev/null +++ b/httemplate/elements/tr-select-avail-time-from_to.html @@ -0,0 +1,58 @@ +% unless ( $opt{'js_only'} ) { + + <& tr-td-label.html, %opt &> + + <% $style %>> +% } + <& checkbox.html, + %opt, + prefix => '', + value => 'Y', + curr_value => $curr_value, + onchange => $opt{field}. '_changed', + &> + + <& select-time-from_to.html, + %opt, + disabled => ($curr_value eq 'Y' ? '' : 'DISABLED' ) + &> + +% unless ( $opt{'js_only'} ) { + + +% } +% unless ( $opt{'html_only'} || $opt{'js_only'} ) { + +% } +<%init> + +my %opt = @_; + +my $style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : ''; + +my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : ''; + +my $curr_value = $cgi->param($opt{'field'}); #wtf? + + -- 2.11.0