Code

Added schema updated - homeServer for conferences
[gosa.git] / plugins / gofon / phoneaccount / class_phoneAccount.inc
index 099a16f5ec8137be403f917f49bd28c2db295699..56347c7032c1533f3b9b7ec08f93b2cda2585ab3 100644 (file)
@@ -82,40 +82,47 @@ class phoneAccount extends plugin
       /* Set available server */
       $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
 
-      /* Set default server */
-      if($this->dn == "new"){
-        $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
-      }
+      /* Servers defined? Watch here... */
+      if (count($this->goFonHomeServers)){
 
-      /* Remember inital home server, to be able to remove old entries */
-      $this->init_HomeServer = $this->goFonHomeServer;
+        /* Set default server */
+        if(empty($this->goFonHomeServer) || $this->goFonHomeServer == "0"){
+          $this->goFonHomeServer= $this->goFonHomeServers[0]['DN'];
+        }
 
-      /* get config */
-      $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer];
+        /* Remember inital home server, to be able to remove old entries */
+        $this->init_HomeServer = $this->goFonHomeServer;
 
-      $r_con =  @mysql_pconnect($cur_cfg['SERVER'],$cur_cfg['LOGIN'],$cur_cfg['PASSWORD']);
-      if(!$r_con){
-        $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
-          $cur_cfg['SERVER'],$cur_cfg['LOGIN']);
-        gosa_log(mysql_error());
-        return false;
-      }
-      $db  =  @mysql_select_db($cur_cfg['DB'],$r_con);
-      if(!$db){
-        $this->generate_error = sprintf(_("Can't select database %s on %s."),$cur_cfg['DB'],$cur_cfg['SERVER']);
-        gosa_log(mysql_error());
-        return false;
-      }
-      $first = false; 
-      foreach($this->phoneNumbers as $key => $val){
-        if(!$first){
-          $first = $key;
+        /* Get config */
+        if(!isset($this->goFonHomeServers[$this->goFonHomeServer])){
+          print_red(sprintf(_("The specified goFonHomeServer '%s' is not available in GOsa server configuration. Saving this account will create a new entry on the server '%s'. Use cancel if you do not want to create a new entry ignoring old accounts."),$this->goFonHomeServer, $this->goFonHomeServers[0]['DN']));
+
+          $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
+          $this->init_HomeServer = $this->goFonHomeServers[0]['DN'];
+        }    
+        $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer];
+
+        $r_con =  @mysql_pconnect($cur_cfg['SERVER'],$cur_cfg['LOGIN'],$cur_cfg['PASSWORD']);
+        if(!$r_con){
+          print_red( sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
+                $cur_cfg['SERVER'],$cur_cfg['LOGIN']));
+          gosa_log(mysql_error());
+        }
+        $db  =  @mysql_select_db($cur_cfg['DB'],$r_con);
+        if(!$db){
+          print_red(sprintf(_("Can't select database %s on %s."),$cur_cfg['DB'],$cur_cfg['SERVER']));
+          gosa_log(mysql_error());
+        }
+
+        $first = false; 
+        foreach($this->phoneNumbers as $key => $val){
+          if(!$first){
+            $first = $key;
+          }
         }
       }
     }
 
-
     /* Get available phone hardware  
      * Search for all available phone hardware  
      */
@@ -137,7 +144,7 @@ class phoneAccount extends plugin
      * Search for all Marcos that are visible and create 
      *  an array with name and parameters 
      */
-    $ldap->search("((objectClass=goFonMacro)(goFonMacroVisible=1))", array("*"));
+    $ldap->search("(&(objectClass=goFonMacro)(goFonMacroVisible=1))", array("*"));
 
     /* Add none for no macro*/
     $this->macros['none']=_("no macro");    
@@ -198,7 +205,7 @@ class phoneAccount extends plugin
       unset($tmp[0]);
 
       /* Check if makro has been removed */
-      if(!isset($this->macroarray[$this->macro])){
+      if(!isset($this->macros[$this->macro])){
         $this->macrostillavailable = false;
       }else{
         $this->macrostillavailable = true;
@@ -416,7 +423,7 @@ class phoneAccount extends plugin
       $this->generate_error = sprintf(
           _("The MySQL Server '%s' isn't reachable as user '%s'. Abort saving entries to keep the database consistent, check GOsa log for mysql error."),
           $a_New['SERVER'],$a_New['LOGIN']);
-      gosa_log(mysql_error($new_connection));
+      gosa_log(@mysql_error($new_connection));
       return false;
     }
     $new_database  =  @mysql_select_db($a_New['DB'],$new_connection);
@@ -424,7 +431,7 @@ class phoneAccount extends plugin
       $this->generate_error = sprintf(
           _("Can't select database %s on %s. Abort saving entries to keep the database consistent, check GOsa log for mysql error."),
           $a_New['DB'],$a_New['SERVER']);
-      gosa_log(mysql_error($new_connection));
+      gosa_log( @mysql_error($new_connection));
       return false;
     }
 
@@ -443,7 +450,7 @@ class phoneAccount extends plugin
         $this->generate_error = sprintf(
             _("The old MySQL home server '%s' isn't reachable as user '%s'. Abort saving entries to keep the database consistent, check GOsa log for mysql error."),
             $a_Remove['SERVER'],$a_Remove['LOGIN']);
-        gosa_log(mysql_error($old_connection));
+        gosa_log(@mysql_error($old_connection));
         return false;
       }
       $old_database  =  @mysql_select_db($a_Remove['DB'],$old_connection);
@@ -451,7 +458,7 @@ class phoneAccount extends plugin
         $this->generate_error = sprintf(
             _("Can't select database %s on %s. Abort saving entries to keep the database consistent, check GOsa log for mysql error."),
             $a_Remove['DB'],$a_Remove['SERVER']);
-        gosa_log(mysql_error($old_connection));
+        gosa_log(@mysql_error($old_connection));
         return false;
       }
     }
@@ -551,7 +558,7 @@ class phoneAccount extends plugin
         $sip_data_array['host']         = $s_host;
         $sip_data_array['insecure']     = NULL;
         $sip_data_array['language']     = NULL;
-        $sip_data_array['mailbox']      = "asterisk";
+        $sip_data_array['mailbox']      = $newnums[$i_new_key];
         $sip_data_array['md5secret']    = NULL;
         $sip_data_array['nat']          = "no";
         $sip_data_array['permit']       = NULL;
@@ -698,7 +705,6 @@ class phoneAccount extends plugin
         $EXT[$i]['exten']   = $s_telenums;
         $EXT[$i]['priority']= "Hint";
         $EXT[$i]['app']     = 'SIP/'.$this->uid;
-        $EXT[$i]['appdata'] = '';
         $i ++;  
         /* SetCID */
         //$EXT[$i]['context'] = 'GOsa';
@@ -746,10 +752,12 @@ class phoneAccount extends plugin
       }
 
       // Perform queries ...
-      foreach($SQL_query_array as $query){
-        if(!@mysql_query($query,$new_connection)){
-          print_red(_("Error while performing query:")." ".mysql_error());
-          return false;
+      if($this->goFonHardware != "automatic"){
+        foreach($SQL_query_array as $query){
+          if(!@mysql_query($query,$new_connection)){
+            print_red(_("Error while performing query:")." ".mysql_error());
+            return false;
+          }
         }
       }
     }
@@ -1084,6 +1092,14 @@ class phoneAccount extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    if(!count($this->goFonHomeServers)){
+      $message[] = _("There must be at least one server with an asterisk database to create a phone account.");
+    }
+
+    if(empty($this->goFonHomeServer)){
+      $message[] = _("Please select a valid goFonHomeServer.");
+    }
+
     if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
       $message[]=(_("Voicemail PIN must be between 1-4 characters."));
     }else{
@@ -1126,6 +1142,16 @@ class phoneAccount extends plugin
   {
     plugin::save();
 
+    /* Force saving macro again 
+     * This ensures that 
+     *  - the macro is available on the destiantion server.
+     *  - the macro saved is up to date on the destination server.
+     */
+    if(!empty($this->macro) && $this->macro != "none")  {
+      $macro_tab = new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->macro);
+      $macro_tab -> save();
+    }
+
     /* Save arrays */
     $tmp_numbers = array();
     foreach ($this->phoneNumbers as $number){
@@ -1147,7 +1173,10 @@ class phoneAccount extends plugin
 
     $this->attrs['goFonForwarding']=array();
 
-    $this->generate_mysql_entension_entries(true);
+    $str = $this->generate_mysql_entension_entries(true);
+    if(empty($str)){
+      print_red($str);
+    }
 
     if($this->attrs['goFonMacro']==""){
       $this->attrs['goFonMacro']=array();
@@ -1372,6 +1401,15 @@ class phoneAccount extends plugin
       $this->goFonPIN = $_POST['goFonPIN'];
     }
   }
+
+
+  function allow_remove()
+  {
+    /* Check if previously selected server is still available */
+    if($this->initially_was_account && !isset($this->goFonHomeServers[$this->goFonHomeServer])){
+      return sprintf(_("The previously selected asterisk home server (%s) is no longer available. Remove aborted."),preg_replace("/,/",", ",$this->goFonHomeServer));
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: