correcting a bug with the onsubmit handler of the email pool simplify the
authorPierre Habouzit <madcoder@debian.org>
Wed, 10 Sep 2003 09:19:00 +0000 (09:19 +0000)
committerPierre Habouzit <madcoder@debian.org>
Wed, 10 Sep 2003 09:19:00 +0000 (09:19 +0000)
    mail pool handling

philter/philter/include/emails.inc.php
philter/philter/include/philter.inc.php
philter/philter/philter.js

index e83b5b4..15935eb 100644 (file)
@@ -97,7 +97,7 @@ class EmailPool {
      * @return  the string containing the form
      */
     function to_form() {
-        $res = "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\" onsubmit=\"return del_onsubmit()\">\n"
+        $res = "<form id=\"emails\" action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
             .  "<table class=\"bicol\" cellpadding=\"1\" cellspacing=\"1\" width=\"95%\" align=\"center\">\n"
             .  "<tr><th>Email</th>\n"
             .  "    <th>Active</th>\n"
@@ -112,13 +112,15 @@ class EmailPool {
                 .   "<td align=\"center\" width=\"10%\">\n"
                 .   "    <input type=\"checkbox\" name=\"emails[$id][active]\""
                 .   ($email->is_active() ? " checked=\"checked\"" : "")." /></td>\n"
-                .   "<td><input type=\"submit\" name=\"emails[$id][del]\" value=\"Del\" /></td>\n"
+                .   "<td align=\"center\"><input type=\"button\" name=\"d$id\" "
+                .   "value=\"Del\" onclick=\"del_onclick(this)\" /></td>\n"
                 .   "</tr>\n";
             $pair = !$pair;
         }
 
         $res .= "</table>\n"
             .   "<center>\n"
+            .   "    <input id=\"emailsDel\" type=\"hidden\" name=\"emails[del]\" value=\"\" />\n"
             .   "    <input type=\"submit\" name=\"emails[apply]\" value=\"Apply changes\" />\n"
             .   "</center>\n"
             .   "</form>\n";
@@ -181,8 +183,33 @@ class EmailPool {
                 $philter->set_error($_POST['emails']['new']." is not a valid email");
                 return false;
             }
+        } elseif(!empty($_POST['emails']['del'])) {      // delete one email
+            $del_id = $_POST['emails']['del'];
+            // we compute a list of all actives emails
+            $allow = false;
+            foreach($this->emails as $id=>$mail)
+                if($del_id!=$id && $mail->is_active())
+                    $allow = true;
+            
+            if($allow && isset($this->emails[$del_id])) {
+                list($count) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM actions "
+                            ."WHERE uid='{$this->uid}' AND pid='"
+                            .FwdPlugin::rtti()."' AND data='$del_id'"));
+                if($count) {
+                    $philter->set_error("This email is still in use and can't be deleted !");
+                    return false;
+                } else {
+                    mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' "
+                            ."AND email='{$this->emails[$del_id]->email}'");
+                    unset($this->emails[$del_id]);
+                }
+            } else {
+                $philter->set_error("you must have at least one active email !");
+                return false;
+            }
         } elseif(isset($_POST['emails']['apply'])) {    // apply actives changes
             $one_active = false;
+
             foreach($this->emails as $id=>$mail)
                 if(isset($_POST['emails'][$id]['active'])) {
                     $one_active = true;
@@ -199,35 +226,8 @@ class EmailPool {
                     $this->emails[$id]->toggle_flag('active');
                     $this->emails[$id]->commit($this->uid, $id);
                 }
-        } else {                                        // delete one email
-            // we compute a list of all actives emails
-            $actives = array();
-            foreach($this->emails as $id=>$mail)
-                if($mail->is_active())
-                    $actives[$id] = true;
-            
-            foreach($this->emails as $id=>$key)
-                if(isset($_POST['emails'][$id]['del'])) {
-                    if(count($this->emails)>0 && (count($actives)>1 || empty($actives[$id]))) {
-                        list($count) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM actions "
-                                    ."WHERE uid='{$this->uid}' AND pid='"
-                                    .FwdPlugin::rtti()."' AND data='$id'"));
-                        if($count) {
-                            $philter->set_error("This email is still in use and can't be deleted !");
-                            return false;
-                        } else {
-                            mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' "
-                                    ."AND email='{$this->emails[$id]->email}'");
-                            unset($this->emails[$id]);
-                            unset($actives[$id]);
-                        }
-                    } else {
-                        $philter->set_error("you must have at least one active email !");
-                        return false;
-                    }
-                }
-        }
         return true;
+        }
     }
 }
 
index ab5cbc4..c626a0f 100644 (file)
@@ -29,6 +29,7 @@ class Philter {
             'action_plugins' => array(),
             'global_plugin' => array()
         );
+        $this->err = "";
     }
 
     /** returns the error.
index 4205314..806062d 100644 (file)
@@ -29,8 +29,11 @@ function getElement(obj) {
     return false;
 }
 
-function del_onsubmit() {
-    return window.confirm("Voulez vous rĂ©ellement supprimer cette adresse mail ?");
+function del_onclick(button) {
+    if(window.confirm("Voulez vous rĂ©ellement supprimer cette adresse mail ?")) {
+        getElement('emailsDel').value = button.name.slice(1);
+        button.form.submit();
+    }
 }
 
 /********** ORDER FORM FUNCTIONS **********/