#57165 add periodic autosave feature to RT-Extension-Drafts
authorgibus <gibus@easter-eggs.com>
Tue, 23 Jan 2018 15:47:22 +0000 (16:47 +0100)
committerEmmanuel Lacour <elacour@easter-eggs.com>
Mon, 26 Mar 2018 14:42:35 +0000 (16:42 +0200)
html/Callbacks/Drafts/Ticket/Update.html/AfterMessageBox
html/Helpers/SaveDraft [new file with mode: 0644]
lib/RT/Extension/Drafts.pm

index 2a92f3c..fb9e389 100644 (file)
@@ -2,6 +2,21 @@
 <input type="submit" class="button" name="LoadDraft" value="<&|/l&>Load draft</&>" />
 % }
 <input type="submit" class="button" name="SaveDraft" value="<% ($Draft && $Draft->id) ? loc('Update draft') : loc('Save draft')%>" />
 <input type="submit" class="button" name="LoadDraft" value="<&|/l&>Load draft</&>" />
 % }
 <input type="submit" class="button" name="SaveDraft" value="<% ($Draft && $Draft->id) ? loc('Update draft') : loc('Save draft')%>" />
+
+<script type="text/javascript">
+jQuery(function() {
+    timeout_save_draft = window.setTimeout(save_draft, <% RT->Config->Get('AutoSaveDraftPeriod', $session{'CurrentUser'}) || 300 %> * 1000);
+});
+
+function save_draft() {
+    jQuery.post("/Helpers/SaveDraft", {UserId: "<% $session{'CurrentUser'}->id %>", TicketId: "<% $Ticket->id %>", Content: jQuery('#UpdateContent').val(), ContentType: jQuery('#UpdateContent').hasClass('richtext') ? 'text/html' : 'text/plain'},
+        function(res) {
+            console.log(res);
+        }
+    );
+    timeout_save_draft = window.setTimeout(save_draft, <% RT->Config->Get('AutoSaveDraftPeriod', $session{'CurrentUser'}) || 300 %> * 1000);
+}
+</script>
 <%init>
 my $Ticket = LoadTicket($ARGS{'id'});
 
 <%init>
 my $Ticket = LoadTicket($ARGS{'id'});
 
diff --git a/html/Helpers/SaveDraft b/html/Helpers/SaveDraft
new file mode 100644 (file)
index 0000000..c7e0ac5
--- /dev/null
@@ -0,0 +1,27 @@
+<%ARGS>
+$UserId => undef;
+$TicketId => undef;
+$Content => undef;
+$ContentType => undef;
+</%ARGS>
+
+<%INIT>
+my $Ticket = LoadTicket($TicketId);
+my $Draft = RT::Attribute->new($session{'CurrentUser'});
+$Draft->LoadByNameAndObject(Object => $session{'CurrentUser'}->UserObj, Name => 'Draft-' . $Ticket->id);
+my ($ok, $msg) = (0, '');
+if ($Content) {
+    if ($Draft && $Draft->Id) {
+        ($ok, $msg) = $Draft->SetContent($Content);
+        $Draft->SetContentType($ContentType);
+    } else {
+        ($ok, $msg) = $Draft->Create(Name => 'Draft-' . $Ticket->id,
+            Object => $session{'CurrentUser'}->UserObj,
+            Content => $Content,
+            ContentType => $ContentType,
+        );
+    }
+}
+$m->out($msg);
+$m->abort;
+</%INIT>
index 90f0581..818a3ca 100644 (file)
@@ -78,4 +78,15 @@ Request Tracker (RT) is Copyright Best Practical Solutions, LLC.
 
 =cut
 
 
 =cut
 
+# User overridable options
+$RT::Config::META{AutoSaveDraftPeriod} = {
+    Section         => 'Ticket composition',
+    Overridable     => 1,
+    SortOrder       => 20,
+    Widget          => '/Widgets/Form/Integer',
+    WidgetArguments => {
+        Description => 'Period (in seconds) to automatically save a response/comment draft'
+    },
+};
+
 1; # End of RT::Extension::Drafts
 1; # End of RT::Extension::Drafts