Close #720: Save not sent mail when leaving the emails/send page and restore
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 20 Sep 2007 06:27:42 +0000 (08:27 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 20 Sep 2007 06:27:42 +0000 (08:27 +0200)
it the next time the page is loaded. (Also include small layout
improvements);

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
ChangeLog
modules/email.php
templates/emails/send.tpl
upgrade/0.9.15/06_mail_send.sql [new file with mode: 0644]

index 1363407..a540d5b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -38,6 +38,7 @@ Bug/Wish:
         - #687: Don't lose authentication when editing a long mail         -FRU
         - #688: From name in the sending form                              -FRU
         - #703: Fix management of mail adresses containing '+'             -ALK
+        - #720: Save not sent mail at exit                                 -FRU
 
     * Lists:
         - #700: Fix several encoding issues                                -FRU
index 9648534..c280f14 100644 (file)
@@ -313,8 +313,17 @@ class EmailModule extends PLModule
         $page->assign('xorg_title','Polytechnique.org - Envoyer un email');
 
         // action si on recoit un formulaire
-        if (Env::v('submit') == 'Envoyer')
-        {
+        if (Post::has('save')) {
+            unset($_POST['save']);
+            if (trim(preg_replace('/-- .*/', '', Post::v('contenu'))) != "") {
+                $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
+                $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
+                $data = serialize($_POST);
+                XDB::execute("REPLACE INTO  email_send_save
+                                    VALUES  ({?}, {?})", S::i('uid'), $data);
+            }
+            exit;
+        } else if (Env::v('submit') == 'Envoyer') {
             function getEmails($aliases)
             {
                 if (!is_array($aliases)) {
@@ -334,6 +343,9 @@ class EmailModule extends PLModule
                 }
             }
 
+            XDB::execute("DELETE FROM  email_send_save
+                                WHERE  uid = {?}", S::i('uid'));
+
             $to2  = getEmails(Env::v('to_contacts'));
             $cc2  = getEmails(Env::v('cc_contacts'));
             $txt  = str_replace('^M', '', Env::v('contenu'));
@@ -374,8 +386,16 @@ class EmailModule extends PLModule
                 }
             }
         } else {
-            PlUpload::clear(S::v('forlife'), 'emails.send');
-            $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain;
+            $res = XDB::query("SELECT  data
+                                 FROM  email_send_save
+                                WHERE  uid = {?}", S::i('uid'));
+            if ($res->numRows() == 0) {
+                PlUpload::clear(S::v('forlife'), 'emails.send');
+                $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain;
+            } else {
+                $data = unserialize($res->fetchOneCell());
+                $_REQUEST = array_merge($_REQUEST, $data);
+            }
         }
 
         $res = XDB::query(
index 0374746..cca3488 100644 (file)
@@ -22,6 +22,7 @@
 
 <h1>Envoyer un mail</h1>
 
+{javascript name="jquery"}
 <script type="text/javascript">//<![CDATA[
   {literal}
   function _selectAll(id) {
@@ -31,9 +32,9 @@
     }
   }
 
+  var sent = false;
   function check(form)
   {
-    var send = true;
     _selectAll('to_contacts');
     _selectAll('cc_contacts');
     if(form.sujet.value == "" && !confirm("Le sujet du mail est vide, veux-tu continuer ?")) {
@@ -51,6 +52,7 @@
         return false;
       }
     }
+    sent = true;
     return true;
   }
 
     if (box.checked) {
       document.getElementById("preview_bt").style.display = "none";
       document.getElementById("preview").style.display = "none";
+      document.getElementById("preview_pv").style.display = "none";
     } else {
       document.getElementById("preview_bt").style.display = "";
+      document.getElementById("preview").style.display = "";
     }
   }
+
+  $(window).unload(
+    function() {
+      if (sent) {
+        return true;
+      }
+      var form = document.forms.form_mail;
+      var toc = "";
+      var ccc = "";
+      for (var i = 0 ; i < form['to_contacts[]'].length ; ++i) {
+        toc += form['to_contacts[]'].options[i].value + ";";
+      }
+      for (var i = 0 ; i < form['cc_contacts[]'].length ; ++i) {
+        ccc += form['cc_contacts[]'].options[i].value + ";";
+      }
+      $.post(platal_baseurl + "emails/send",
+             { save: true,
+               from: form.from.value,
+               to_contacts: toc,
+               cc_contacts: ccc,
+               contenu: form.contenu.value,
+               to: form.to.value,
+               sujet: form.sujet.value,
+               cc: form.cc.value,
+               bcc: form.bcc.value });
+      return true;
+    });
   {/literal}
 //]]>
 </script>
 
-<form action="emails/send" method="post" enctype="multipart/form-data" onsubmit="return check(this);">
+<form action="emails/send" method="post" enctype="multipart/form-data" id="form_mail" onsubmit="return check(this);">
   <table class="bicol" cellpadding="2" cellspacing="0">
     <tr> 
       <th colspan="2">Destinataires</th>
       <small><input type="checkbox" name="nowiki" value="1" {if $smarty.request.nowiki}checked="checked"{/if} onchange="updateWikiView(this);" />
       coche cette case pour envoyer le mail en texte brut, sans formattage</small>
     </div>
-    <div id="preview" style="display: none">
-      <strong>Aperçu du mail :</strong>
-      <div id="mail_preview">
+    <div id="preview">
+      <div id="preview_pv" style="display: none">
+        <strong>Aperçu du mail :</strong>
+        <div id="mail_preview">
+        </div>
+        <div class="center">
+          <input type="submit" name="submit" value="Envoyer" />
+        </div>
       </div>
       <div class="center">
-        <input type="submit" name="submit" value="Envoyer" />
+        <input type="submit" name="preview" id="preview_bt_top" value="Aperçu"
+               onclick="previewWiki('mail_text', 'mail_preview', true, 'preview_pv'); return false;" />
       </div>
     </div>
     <textarea name='contenu' rows="30" cols="75" id="mail_text" onkeyup="keepAuth()">
       <input type="file" name="uploaded" />
     </div>
     <div class="center">
-      <input type="submit" name="preview" id="preview_bt" value="Aperçu" onclick="previewWiki('mail_text', 'mail_preview', true, 'preview'); return false;" />
+      <input type="submit" name="preview" id="preview_bt" value="Aperçu"
+             onclick="previewWiki('mail_text', 'mail_preview', true, 'preview_pv'); return false;" />
       <input type="submit" name="submit" value="Envoyer" />
     </div>
   </fieldset>
diff --git a/upgrade/0.9.15/06_mail_send.sql b/upgrade/0.9.15/06_mail_send.sql
new file mode 100644 (file)
index 0000000..0474d78
--- /dev/null
@@ -0,0 +1,8 @@
+create table email_send_save (
+  uid smallint(5) unsigned not null,
+  data blob not null,
+
+  primary key uid (uid)
+) CHARSET=utf8;
+
+# vim:set syntax=mysql: