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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
<& /Elements/Header, Title => 'Schedule', JavaScript => 0 &>
<SCRIPT TYPE="text/javascript">
// sets cell content and bgcolor in a div, for use as a draggable
// (draggable tds have border problems on FF/IE)
function set_cell_div ($cell,content,bgcolor) {
var $div = $cell.data('div');
if (!$div) {
$div = $(document.createElement('div'));
$div.data('cell',$cell);
$cell.data('div',$div);
$cell.append($div);
}
$div.css('width','100%');
$div.css('background-color', bgcolor);
$div.html(content || ' ');
}
// gives cell the appearance dictated by its data
function set_data_cell ($cell) {
$cell.css('border', '1px solid #D7D7D7' );
$cell.css('background-color', $cell.data('bgcolor'));
set_cell_div($cell,$cell.data('content'),$cell.data('bgcolor'));
}
// sets cell data and appearance to schedulable
function set_schedulable_cell ($cell) {
$cell.data('bgcolor', '#FFFFFF' );
$cell.data('ticketid', 0 );
$cell.data('length', 0 );
$cell.data('cells', 0 );
$cell.data('offset', 0 );
$cell.data('label', '' );
$cell.data('content', '' );
set_data_cell($cell);
}
// sets cell data and appearance as an appointment
function set_appointment_cell ($cell,ticketid,bgcolor,label,length,cells,offset) {
$cell.data('bgcolor', bgcolor );
$cell.data('ticketid', ticketid );
$cell.data('length', length );
$cell.data('cells', cells );
$cell.data('offset', offset );
$cell.data('label', label );
$cell.data('content', '');
if ( offset == 0 ) { // first row
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 );
$cell.data('content', title);
}
set_data_cell($cell);
}
% if ( $cells ) {
// hover effects for scheduling new appointment
function boxon(what) {
var $this = $(what);
for ( var c=0; c < <%$cells%>; c++) {
$this.css('background-color', '#ffffdd');
set_cell_div($this,'','#ffffdd');
if ( c == 0 ) {
$this.css('border-top', '1px double black');
}
if ( c == <%$cells-1%> ) {
$this.css('border-bottom', '1px solid black');
}
$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', '#ffffff');
set_cell_div($this,'','#ffffff');
$this.css('border', '1px solid #D7D7D7'); //watch out in IE8 woes, empty string removes 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);
}
}
% } else {
// functions for drag-and-drop rescheduling
// ticket-dependant test if we can drop here
// prevent overlap with other appointments, while allowing appointment to overlap itself
function can_drop ($where, ui) {
var cells = ui.draggable.data('cell').data('cells');
var ticketid = ui.draggable.data('cell').data('ticketid');
for (var c=0; c < cells; c++) {
if (!$where.is('.ui-droppable')) {
return false;
}
if ($where.data('ticketid')) {
if ($where.data('ticketid') != ticketid) {
return false;
}
if ($where.data('offset') == c) { // don't reschedule in the same slot
return false;
}
}
var rownum = $where.parent().prevAll('tr').length;
var colnum = $where.prevAll('td').length;
$where = $where.parent().parent().children('tr').eq(rownum+1).children('td').eq(colnum);
}
return true;
}
// makes cell droppable (can reschedule here, subject to can_drop)
function set_droppable_cell ($cell) {
$cell.droppable({
over: appointment_drag_over,
drop: reschedule_appointment,
tolerance: 'pointer'
});
}
// makes cell draggable (able to be rescheduled)
function set_draggable_cell ($cell) {
var $div = $cell.data('div');
$div.draggable({
containment: '.titlebox-content',
revert: true,
revertDuration: 0,
start: appointment_drag_start,
stop: appointment_drag_stop,
zIndex: 10,
});
}
// gives cell a white (schedulable) appearance, without changing cell data
function set_white_cell ($cell) {
$cell.css('border', '1px solid #D7D7D7' );
$cell.css('background-color', '#FFFFFF');
set_cell_div($cell,'','#FFFFFF');
}
// track drag highlighting
var drag_hi;
// clear drag highlighting
function clear_drag_hi (cells) {
if ( drag_hi ) {
for ( var c=0; c < cells; c++) {
if (drag_hi.data('isdragging')) {
drag_hi.css('border', '1px solid #D7D7D7' );
drag_hi.css('background-color', '#FFFFFF' );
} else {
set_white_cell(drag_hi);
}
var rownum = drag_hi.parent().prevAll('tr').length;
var colnum = drag_hi.prevAll('td').length;
drag_hi = drag_hi.parent().parent().children('tr').eq(rownum+1).children('td').eq(colnum);
}
drag_hi = undefined;
}
}
// drag start event
function appointment_drag_start(event, ui) {
var $this = $(this);
// cell that's dragging
$this = $this.data('cell');
set_cell_div($this,$this.data('label'),$this.data('bgcolor'));
$this.data('isdragging',true);
var offset = $this.data('offset');
var cells = $this.data('cells');
// jump to first cell in appointment
var rownum = $this.parent().prevAll('tr').length;
var colnum = $this.prevAll('td').length;
$this = $this.parent().parent().children('tr').eq(rownum-offset).children('td').eq(colnum);
// loop through all cells in appointment
for ( var c=0; c < cells; c++) {
if ($this.data('isdragging')) {
$this.css('background-color', '#FFFFFF');
} else {
set_white_cell($this);
}
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);
}
}
// drag stop event
function appointment_drag_stop(event, ui) {
var $this = $(this);
// cell that's dragging
$this = $this.data('cell');
var cells = $this.data('cells');
clear_drag_hi(cells);
$this.data('isdragging',false);
var offset = $this.data('offset');
// jump to first cell in appointment
var rownum = $this.parent().prevAll('tr').length;
var colnum = $this.prevAll('td').length;
$this = $this.parent().parent().children('tr').eq(rownum-offset).children('td').eq(colnum);
// loop through all cells in appointment
for ( var c=0; c < cells; c++) {
set_data_cell($this);
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);
}
}
// drag over event
function appointment_drag_over(event, ui) {
// the cell that's dragging
var cells = ui.draggable.data('cell').data('cells');
// the droppable cell that you're over
var $this = $(this);
clear_drag_hi(cells);
if (!can_drop($this, ui)) return;
drag_hi = $this;
// loop through potential appointment cells
for ( var c=0; c < cells; c++) {
$this.css('background-color', '#ffffdd');
if ( !$this.data('isdragging')) {
set_cell_div($this,'','#ffffdd');
} else {
$this.css('background-color','#ffffdd');
}
if ( c == 0 ) {
$this.css('border-top', '1px double black');
}
if ( c == (cells-1) ) {
$this.css('border-bottom', '1px solid black');
}
$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);
}
}
// drop event
function reschedule_appointment( event, ui ) {
// the droppable cell that you're over
var $this = $(this);
if (!can_drop($this, ui)) return;
% #get the ticket number and appointment length (from the draggable object)
var dragcell = ui.draggable.data('cell');
var ticketid = dragcell.data('ticketid');
var length = dragcell.data('length');
var bgcolor = dragcell.data('bgcolor');
var offset = dragcell.data('offset');
% #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 droppable = $this;
ui.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);
} else {
var label = data.sched_label;
// jump to first cell in appointment
var rownum = dragcell.parent().prevAll('tr').length;
var colnum = dragcell.prevAll('td').length;
dragcell = dragcell.parent().parent().children('tr').eq(rownum-offset).children('td').eq(colnum);
// remove old appointment entirely
var epoch = dragcell.data('epoch');
var st_tod_row = dragcell.data('tod_row');
var old_username = dragcell.data('username');
var cells = dragcell.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;
var $cell = $('#'+td_id);
$cell.data('div').draggable('destroy');
set_schedulable_cell($cell);
set_droppable_cell($cell);
}
// set appointment in new position
clear_drag_hi(cells);
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;
var $cell = $('#'+n_td_id);
set_appointment_cell($cell,ticketid,bgcolor,label,length,cells,d);
set_draggable_cell($cell);
set_droppable_cell($cell);
}
}
});
}
% } # end of rescheduling functions
</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>
|