error message i18n handling
[old-projects.git] / philter / philter / include / emails.inc.php
index 766d91c..0ee1afa 100644 (file)
@@ -54,7 +54,7 @@ class Email {
     function toggle_flag($_flag) {
         $flags = explode(",", $this->flags);
         $key = array_search($_flag,$flags);
-        if($key===false)
+        if($key===NULL || $key===false)
             $flags[]=$_flag;
         else
             unset($flags[$key]);
@@ -97,34 +97,44 @@ class EmailPool {
      * @return  the string containing the form
      */
     function to_form() {
-        $res = "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\" onsubmit=\"return del_onsubmit()\">\n"
-            .  "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\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>"._i18n('email')."</th>\n"
+            .  "    <th>"._i18n('active')."</th>\n"
+            .  "    <th>&nbsp;</th>\n"
+            .  "</tr>\n";
 
         $pair = true;
+
         foreach($this->emails as $id => $email) {
-            $res .= "<tr class=\"".($pair?"pair":"impair")."\"><td>\n"
-                .   "        <input type=\"checkbox\" name=\"emails[$id][active]\""
-                    . ($email->is_active() ? " checked=\"checked\"" : "")." /> ".$email->email."\n"
-                .   "</td><td><input type=\"submit\" name=\"emails[$id][del]\" value=\"Del\" /></td>\n"
+            $res .= "<tr class=\"".($pair?"pair":"impair")."\">\n"
+                .   "<td>".$email->email."</td>\n"
+                .   "<td align=\"center\" width=\"10%\">\n"
+                .   "    <input type=\"checkbox\" name=\"emails[$id][active]\""
+                .   ($email->is_active() ? " checked=\"checked\"" : "")." /></td>\n"
+                .   "<td align=\"center\"><input type=\"button\" "
+                .   "value=\""._i18n('del')."\" onclick=\"del_onclick($id)\" /></td>\n"
                 .   "</tr>\n";
             $pair = !$pair;
         }
 
-        $res .= "<tr><td colspan=\"2\">\n"
-            .   "    <input type=\"submit\" name=\"emails[apply]\" value=\"Apply changes\" /></td>\n"
-            .   "</tr>\n"
-            .   "</table>\n</form>\n";
+        $res .= "</table>\n"
+            .   "<br /><center>\n"
+            .   "    <input id=\"emailsDel\" type=\"hidden\" name=\"emails[del]\" value=\"-1\" />\n"
+            .   "    <input type=\"submit\" name=\"emails[apply]\" value=\""
+            .   _i18n('apply_changes')."\" />\n"
+            .   "</center><br />\n"
+            .   "</form>\n";
 
-        $val = 'your email ...';
+        $val = _i18n('your_email');
         
         $res .= "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
-            .   "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n"
-            .   "<tr><td colspan=\"2\">\n"
-            .   "   <input type=\"text\" name=\"emails[new]\" size=\"60\" value='$val' "
-            .   "onfocus=\"text_onfocus(this,'$val')\" onblur=\"text_onblur(this,'$val')\" />\n"
-            .   "   <input type=\"submit\" name=\"emails[add]\" value=\"Add\" />\n"
-            .   "</td></tr>\n"
-            .   "</table>\n</form>\n";
+            .   _i18n('pool_help2')
+            .   "<input type=\"text\" name=\"emails[new]\" size=\"50\" value='$val' "
+            .   "    onfocus=\"text_onfocus(this,'$val')\" onblur=\"text_onblur(this,'$val')\" />\n"
+            .   "<input type=\"submit\" name=\"emails[add]\" value=\"".
+                _i18n('add')."\" />\n"
+            .   "</form>\n";
         
         return $res;
     }
@@ -163,7 +173,7 @@ class EmailPool {
                 // we check that the email is not already there
                 foreach($this->emails as $id=>$key)
                     if($key->email == $new_mail) {
-                        $philter->set_error("$new_mail is already in the Email Pool");
+                        $philter->set_error("$new_mail "._i18n('pool_err_already'));
                         return false;
                     }
                 
@@ -173,7 +183,35 @@ class EmailPool {
                 $this->emails[$mid]->commit($this->uid, $mid);
                 uasort($this->emails, "email_cmp");
             } else {
-                $philter->set_error($_POST['emails']['new']." is not a valid email");
+                $philter->set_error($_POST['emails']['new'].' '._i18n('pool_err_not_valid'));
+                return false;
+            }
+        } elseif(isset($_POST['emails']['del']) && $_POST['emails']['del'] != -1) {      // 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;
+                    break;
+                }
+            }
+            
+            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(_i18n('pool_err_active'));
+                    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(_i18n('pool_err_need_one'));
                 return false;
             }
         } elseif(isset($_POST['emails']['apply'])) {    // apply actives changes
@@ -185,44 +223,18 @@ class EmailPool {
                 }
             
             if(!$one_active) {
-                $philter->set_error("you must have at least one active email !");
+                $philter->set_error(_i18n('pool_err_need_one'));
                 return false;
             }
 
-            foreach($this->emails as $id=>$key)
+            foreach($this->emails as $id=>$key) {
                 if(isset($_POST['emails'][$id]['active']) xor $this->emails[$id]->is_active()) {
                     $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;
+        }
     }
 }