summaryrefslogtreecommitdiff
path: root/rt/share/html/Search/Schedule.html
blob: 96670cdb28cc002e6369b6a6cbaa1c9adb7c43f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<& /Elements/Header, Title => 'Schedule', JavaScript => 0 &>

<SCRIPT TYPE="text/javascript">

% if ( $cells ) {

  function boxon(what) {
    var $this = $(what);
    for ( var c=0; c < <%$cells%>; c++) {

      $this.css('background-color', '#ffffdd');
      if ( c == 0 ) {
        $this.css('border-top', '1px double black');
        $this.css('border-left', '1px double black');
        $this.css('border-right', '1px solid black');
      } else if ( c == <%$cells-1%> ) {
        $this.css('border-left', '1px double black');
        $this.css('border-right', '1px solid black');
        $this.css('border-bottom', '1px solid black');
      } else {
        $this.css('border-left', '1px double black');
        $this.css('border-right', '1px solid black');
      }

      var rownum = $this.parent().prevAll('tr').length;
      var colnum = $this.prevAll('td').length;
      $this = $this.parent().parent().children('tr').eq(rownum+1).children('td').eq(colnum);
    }
  }

  function boxoff(what) {
    var $this = $(what);
    for ( var c=0; c < <%$cells%>; c++) {

      //$this.css('background-color', '');
      //$this.css('border', ''); //IE8 woes, removes cell borders
      $this.removeAttr('style'); //slightly "flashy" on cell changes under IE8
                                 //but at least it doesn't remove cell borders

      var rownum = $this.parent().prevAll('tr').length;
      var colnum = $this.prevAll('td').length;
      $this = $this.parent().parent().children('tr').eq(rownum+1).children('td').eq(colnum);
    }
  }


% }

  var drag_cells = 0;
  var drag_hi;
  function boxon_drop(event, ui) {
    //var $this = $(what);
    var $this = $(this);

    drag_cells = ui.draggable.data('cells');

    if ( drag_hi ) {
      boxoff_do(drag_hi);
    }
    drag_hi = $this;

    for ( var c=0; c < drag_cells; c++) {

      /* well, its not exactly what i want, would prefer if it could properly
         mouse in-out, but this sorta helps for now?
         revisit when everthing else is working */
/*      $this.effect("highlight", {}, 1500); */

      $this.css('background-color', '#ffffdd');
      if ( c == 0 ) {
        $this.css('border-top', '1px double black');
        $this.css('border-left', '1px double black');
        $this.css('border-right', '1px solid black');
      } else if ( c == (drag_cells-1) ) {
        $this.css('border-left', '1px double black');
        $this.css('border-right', '1px solid black');
        $this.css('border-bottom', '1px solid black');
      } else {
        $this.css('border-left', '1px double black');
        $this.css('border-right', '1px solid black');
      }

      var rownum = $this.parent().prevAll('tr').length;
      var colnum = $this.prevAll('td').length;
      $this = $this.parent().parent().children('tr').eq(rownum+1).children('td').eq(colnum);
    }


  }

  function boxoff_do(what) {

    var $this = what;

    for ( var c=0; c < drag_cells; c++) {

      //$this.css('background-color', '');
      //$this.css('border', ''); //IE8 woes, removes cell borders
      $this.removeAttr('style'); //slightly "flashy" on cell changes under IE8
                                 //but at least it doesn't remove cell borders

      var rownum = $this.parent().prevAll('tr').length;
      var colnum = $this.prevAll('td').length;
      $this = $this.parent().parent().children('tr').eq(rownum+1).children('td').eq(colnum);
    }
  }

  function reschedule_appointment( event, ui ) {

%   #get the ticket number and appointment length (from the draggable object)
    var ticketid = ui.draggable.data('ticketid');
    var length   = ui.draggable.data('length');
    var bgcolor  = ui.draggable.data('bgcolor');

%   #and.. the new date and time, and username (from the droppable object)
    var starts   = $(this).data('starts');
    var username = $(this).data('username');

    var due = parseInt(starts) + parseInt(length);

    var n_epoch        = $(this).data('epoch');
    var n_st_tod_row   = $(this).data('tod_row');

    var draggable = ui.draggable;
    var droppable = $(this);
    draggable.effect( "transfer", { to: droppable }, 420 );

%   #tell the backend to reschedule it
    var url = "<% popurl(3) %>misc/xmlhttp-ticket-update.html?" +
              "id=" + ticketid + ";starts=" + starts + ";due=" + due +
              ";username=" + username;

    $.getJSON( url, function( data ) {
      if ( data.error && data.error.length ) {
%       #error?  "that shouldn't happen" but should display 
        alert(data.error);
%       #XX and should revert the dragable...
      } else {

        //draggable.effect( "transfer", { to: droppable }, 1000 );

        var label = data.sched_label;

%       #remove the old appointment entirely
        var epoch        = ui.draggable.data('epoch');
        var st_tod_row   = ui.draggable.data('tod_row');
        var old_username = ui.draggable.data('username');
        var cells        = ui.draggable.data('cells');
        for ( var c=0; c < cells; c++) {
          var tod_row = parseInt(st_tod_row) + (c * <%$timestep%>);
          var td_id = 'td_' + epoch +
                      '_' + String( tod_row ) +
                      '_' + old_username;
          $('#'+td_id).css('background-color', '#FFFFFF');
          $('#'+td_id).text('');
%         #(and make those boxes droppable)
          $('#'+td_id).droppable({
            over: boxon_drop,
            drop: reschedule_appointment,
            tolerance: 'pointer'
          });
        }

%       #maybe use that animation which shows the box from point A to B

        if ( drag_hi ) {
          boxoff_do(drag_hi);
        }
        for ( var d=0; d < cells; d++) {
          var n_tod_row = parseInt(n_st_tod_row) + (d * <%$timestep%>);
          var n_td_id = 'td_' + n_epoch +
                        '_' + String( n_tod_row ) +
                        '_' + username;
          $('#'+n_td_id).css('background-color', bgcolor);
%         #remove their droppable
          $('#'+n_td_id).droppable('destroy');
          if ( d == 0 ) {
            var title = 
              label +
              ' <A HREF="<%$RT::WebPath%>/Ticket/Display.html?id=' + ticketid + '" target="_blank">view</A> ' +
              <% include('/elements/popup_link.html',
                   action=>$RT::WebPath.'/Ticket/ModifyCustomFieldsPopup.html?id=__MAGIC_TICKET_ID__',
                   label =>'edit',
                   actionlabel => 'Edit appointment',
                   height      => 436, # better: A + B * (num_custom_fields)
                 ) |n,js_string
              %>;
            title = title.replace( /__MAGIC_TICKET_ID__/, ticketid );
            $('#'+n_td_id).html( title );
%           #(and make the top draggable, so we could do it all over again)
            $('#'+n_td_id).draggable({
              containment: '.titlebox-content',
%#              revert:      'invalid',
              revert: true,
              revertDuration: 0,
            });
            $('#'+n_td_id).data('ticketid', ticketid );
            $('#'+n_td_id).data('length',   length );
            $('#'+n_td_id).data('cells',    cells );
            $('#'+n_td_id).data('bgcolor',  bgcolor );
          }
        }

      }

    });

  }

</SCRIPT>

<& /Search/Calendar.html,
     @_,
     Query       => "( Status = 'new' OR Status = 'open' OR Status = 'stalled')
                     AND ( Type = 'reminder' OR 'Type' = 'ticket' )
                     AND Queue = $queueid ",
     slots       => scalar(@usernames),
     Embed       => 'Schedule.html',
     DimPast     => 1,
     Display     => 'Schedule',
     DisplayArgs => [ username  => \@usernames,
                      LengthMin => $LengthMin,
                      #oops, more freeside abstraction-leaking
                      custnum   => $ARGS{custnum},
                      pkgnum    => $ARGS{pkgnum},
                      RedirectToBasics => $ARGS{RedirectToBasics},
                    ],
&>

<%ONCE>

my $timestep =  RT->Config->Get('CalendarWeeklySizeMin') || 30; #1/2h

</%ONCE>
<%init>

#abstraction-leaking
my $conf = new FS::Conf;
my $queueid = $conf->config('ticket_system-appointment-queueid')
  or die "ticket_system-appointment-queueid configuration not set";

my @files = ();
#if ( ! $initialized ) {
  push @files, map "overlibmws$_", ( '', qw( _iframe _draggable _crossframe ) );
  push @files, map { "${_}contentmws" } qw( iframe ajax );
#%}

my @usernames = ();
if ( ref($ARGS{username}) ) {
  @usernames = @{ $ARGS{username} };
} elsif ( $ARGS{username} ) {
  @usernames = ( $ARGS{username} );
} else {
  #look them up ourslves... again, more FS abstraction-leaking, but 
  # we want to link to the schedule view, and better than doing this every
  # menu render
  use FS::Record qw( qsearch );
  use FS::sched_item;
  my @sched_item = qsearch('sched_item', { 'disabled' => '', });
  @usernames = map $_->access_user->username, @sched_item;
}

( my $LengthMin = $ARGS{LengthMin} ) =~ /^\d+$/ or die 'non-numeric LengthMin';

my $cells = int($LengthMin / $timestep);
$cells++ if $LengthMin % $timestep;

</%init>