#57165 add periodic autosave feature to RT-Extension-Drafts
[manu/RT-Extension-Drafts.git] / html / Callbacks / Drafts / Ticket / Update.html / AfterMessageBox
1 % if ( $Draft && $Draft->Content ) {
2 <input type="submit" class="button" name="LoadDraft" value="<&|/l&>Load draft</&>" />
3 % }
4 <input type="submit" class="button" name="SaveDraft" value="<% ($Draft && $Draft->id) ? loc('Update draft') : loc('Save draft')%>" />
5
6 <script type="text/javascript">
7 jQuery(function() {
8     timeout_save_draft = window.setTimeout(save_draft, <% RT->Config->Get('AutoSaveDraftPeriod', $session{'CurrentUser'}) || 300 %> * 1000);
9 });
10
11 function save_draft() {
12     jQuery.post("/Helpers/SaveDraft", {UserId: "<% $session{'CurrentUser'}->id %>", TicketId: "<% $Ticket->id %>", Content: jQuery('#UpdateContent').val(), ContentType: jQuery('#UpdateContent').hasClass('richtext') ? 'text/html' : 'text/plain'},
13         function(res) {
14             console.log(res);
15         }
16     );
17     timeout_save_draft = window.setTimeout(save_draft, <% RT->Config->Get('AutoSaveDraftPeriod', $session{'CurrentUser'}) || 300 %> * 1000);
18 }
19 </script>
20 <%init>
21 my $Ticket = LoadTicket($ARGS{'id'});
22
23 my $Draft = RT::Attribute->new( $session{'CurrentUser'} );
24 $Draft->LoadByNameAndObject( Object => $session{'CurrentUser'}->UserObj, Name => 'Draft-'.$Ticket->id );
25
26 if ( $ARGS{'SaveDraft'} ) {
27     if ( $Draft && $Draft->Id ) {
28         $Draft->SetContent( $ARGS{'UpdateContent'} );
29         $Draft->SetContentType( $ARGS{'UpdateContentType'} || 'text/plain' );
30     } else {
31         $Draft->Create( Name => 'Draft-'.$Ticket->id,
32             Object => $session{'CurrentUser'}->UserObj,
33             Content => $ARGS{'UpdateContent'},
34             ContentType => $ARGS{'UpdateContentType'} || 'text/plain',
35         );
36     }
37 }
38 </%init>