appointment drag and drop, RT#34237
[freeside.git] / rt / share / html / Elements / CalendarSlotSchedule
1 <%ARGS>
2   $Date      => undef
3   @Tickets   => ()
4   $slots     => $default_slots
5   $sday      => undef
6   $tod_row   => undef
7   $timestep  => $default_timestep
8   @username  => ()
9   $LengthMin => $default_timestep
10   $custnum   => undef
11   $pkgnum    => undef
12 </%ARGS>
13 % foreach my $username ( @username ) {
14 %
15 %   my %schedule = UserDaySchedule( username => $username,
16 %                                   'date'   => $Date->strftime('%F'),
17 %                                   Tickets  => \@Tickets,
18 %                                 );
19 %
20 %   my $bgcolor = '666666;border-color:#555555';
21 %   my $content = '';
22 %   my $selectable = 0;
23 %   my $draggable_ticketid = 0;
24 %   my $draggable_length = 0;
25 %   my $droppable = 0;
26 %   my $cells = 0;
27 %
28 %   #white out available times
29 %   foreach my $avail ( @{ $schedule{'avail'} } ) {
30 %     my( $start, $end ) = @$avail;
31 %     next if $start >= ($tod_row+$timestep) || $end <= $tod_row;
32 %     $bgcolor = 'FFFFFF';
33 %     $selectable = 1
34 %       if $LengthMin <= $end - $tod_row  #the slot is long enough
35 %       && ! grep { $_ > $tod_row && $LengthMin > $_ - $tod_row }
36 %              map $_->[0], values %{ $schedule{'scheduled'} };
37 %   }
38 %
39 %   #block out / show / color code existing appointments
40 %   #my %line = ();
41 %   foreach my $id ( keys %{ $schedule{'scheduled'} } ) {
42 %
43 %     my( $starts, $due, $col, $t ) = @{ $schedule{'scheduled'}->{$id} };
44 %
45 %     next if $starts >= ($tod_row+$timestep) || $due <= $tod_row;
46 %
47 %     $bgcolor = $col;
48 %     $selectable = 0;
49 %
50 %     if ( $starts >= $tod_row ) { #first row
51 %       $content .= ($content?', ':''). #$id. ': '.
52 %                   #false laziness w/xmlhttp-ticket-update.html
53 %                   FS::sched_avail::pretty_time($starts). '-'.
54 %                   FS::sched_avail::pretty_time($due);
55 %                   #'install for custname XX miles away'; #XXX placeholder/more
56 %       $draggable_ticketid = $id;
57 %       $draggable_length = $due - $starts;
58 %
59 %       $cells = int( ($due-$starts) / $timestep );
60 %       $cells++ if ($due-$starts) % $timestep;
61 %       
62 %     #} else {
63 %     #  $content .= ($content?', ':''). $id;
64 %     }
65 %   }
66 %
67 %   my $td_id = 'td_'. $Date->epoch. '_'. $tod_row. '_'. $username;
68
69     <td style = "background-color:#<%$bgcolor%>"
70           ID="<% $td_id %>"
71         class = "<% ($selectable && $custnum && $LengthMin) ? 'weeklyselectable' : 'weekly' %>"
72 %#                 <%   $is_today     ? 'today'
73 %#                    : $is_yesterday ? 'yesterday'
74 %#                    : $is_aweekago  ? 'aweekago'
75 %#                    : ''
76 %#                 %>"
77 %     if ( $selectable ) {
78 %
79 %       if ( $custnum && $LengthMin ) {
80 %
81 %         #XXX for now, construct a ticket creation URL
82 %         # eventually, do much the same, but say "appointment made", show time
83 %         # and date, have # options to do things with it? etc.
84 %         # then redir back to customer/appointment view i guess
85 %
86 %         #abstraction is leaking like a sieve... linking back to freeside cust
87 %         # (XXX and eventually, package)
88 %         my $cust_main = qsearchs('cust_main', { custnum=>$custnum } )
89 %           or die "unknown custnum $custnum";
90 %         my $Queue = $cust_main->agent->ticketing_queueid || 1; # || $default_queueid;#XXX really, pick pkg_category queue
91 %         my $member = "freeside://freeside/cust_main/$custnum";
92 %
93 %warn         my $Starts = int($tod_row/60). ':'. sprintf('%02d',$tod_row%60). ':00';
94 %warn         my $Due    = int(($tod_row+$LengthMin)/60). ':'.
95 %                      sprintf('%02d',($tod_row+$LengthMin)%60). ':00';
96 %
97 %         my $url = $RT::WebPath. '/Ticket/Display.html?id=new'.
98 %                   "&Queue=$Queue".
99 %                   "&Owner=$username".
100 %                   '&Starts='. $Date->strftime('%F').'%20'. $Starts.
101 %                   '&Due='.    $Date->strftime('%F').'%20'. $Due.
102 %                   '&new-MemberOf='. $member. #XXX uri_escape?
103 %                   '&Status=new';
104 %                   #'&Requestors='. #XXX Freeside customer requestor(s) (package?
105
106           onmouseover = "boxon(this);"
107           onmouseout  = "boxoff(this);"
108           title       = "<% 'Make appointment for '.
109                               FS::sched_avail::pretty_time($tod_row). '-'.
110                               FS::sched_avail::pretty_time($tod_row+$LengthMin)
111                         %>"
112           onclick     = "window.location.href = '<% $url %>'"
113 %
114 %       } else {
115 %         $droppable = 1;
116 %       }
117 %
118 %     }
119     ><% $content %></td>
120     <SCRIPT TYPE="text/javascript">
121
122       $('#<% $td_id %>').data('username', "<% $username %>");
123       $('#<% $td_id %>').data('starts',   <% $Date->epoch + $tod_row*60 %>);
124       $('#<% $td_id %>').data('epoch',    <% $Date->epoch %>);
125       $('#<% $td_id %>').data('tod_row',  <% $tod_row %>);
126
127 %     if ( $droppable ) {
128         $('#<% $td_id %>').droppable({
129           over: boxon_drop,
130           drop: reschedule_appointment,
131           tolerance: 'pointer'
132         });
133 %     }
134
135 %     if ( $draggable_ticketid ) {
136         $('#<% $td_id %>').draggable({
137           containment: '.titlebox-content',
138 %#          revert:      'invalid',
139           revert: true,
140           revertDuration: 0,
141         });
142         $('#<% $td_id %>').data('ticketid', <% $draggable_ticketid %>);
143         $('#<% $td_id %>').data('length',   <% $draggable_length * 60 %>);
144         $('#<% $td_id %>').data('cells',    <% $cells %>);
145         $('#<% $td_id %>').data('bgcolor',  "#<% $bgcolor %>");
146 %     }
147
148     </SCRIPT>
149 % }
150 <%ONCE>
151 my $default_slots = RT->Config->Get('CalendarWeeklySlots') || 5;
152 my $default_timestep = RT->Config->Get('CalendarWeeklySizeMin') || 30; #1/2h
153 </%ONCE>
154 <%INIT>
155
156 use RTx::Schedule qw( UserDaySchedule );
157 use FS::sched_avail; #just for pretty_time
158
159 #what happened?  alas.  for constructing the ticket creation links
160 use FS::Record qw( qsearchs );
161 use FS::cust_main;
162
163 </%INIT>