Code

Cleaned up gotoPrinter save
[gosa.git] / plugins / gofon / conference / class_phoneConferenceGeneric.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class conference extends plugin
22 {
23   /* department attributes */
24   var $cn                                                 = "";
25   var $description              = "";
26   var $base                               = "";
27   var $ou                                               = "";
28   var $goFonPIN                         = "";
29   var $acl = "";
30   var $old_cn         = "";
32   var $goFonConferenceOption    = "";
33   var $goFonConferenceOption_P  = "";   // Set PIN 
34   var $goFonConferenceOption_r  = "";   // record Conference
35   var $goFonConferenceOption_M  = "";   // Play Music opn hold
36   var $goFonConferenceOption_s  = "";   // activate menu
37   var $goFonConferenceOption_i  = "";   // announce new and leaving user
38   var $goFonConferenceOption_c  = "";   // Count User
39   var $goFonConferenceOption_D  = "";   // Conference Type, no PIN/PIN
41   var $goFonConferenceOwner    = "";
43   var $goFonConferenceOptionFormat              = "";
44   var $goFonConferenceOptionLifetime    = "";
45   var $telephoneNumber          = "";
47   var $old_tele_number          = false;
48   var $generate_error           = "";
50   var $old_dn;
52   /* Headpage attributes */
53   var $last_dep_sorting= "invalid";
54   var $departments= array();
56   /* attribute list for save action */
57   var $attributes= array("cn","base", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
58       "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c",
59       "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber","goFonConferenceOwner");
61   var $objectclasses= array("top", "goFonConference");
63   function conference ($config, $dn)
64   {
65     plugin::plugin($config, $dn);
66     $this->is_account   = TRUE;
67     $this->ui                   = get_userinfo();
68     $this->dn                   = $dn;
69     $this->orig_dn              = $dn;
70     $this->config               = $config;
72     /* Set base */
73     if ($this->dn == "new"){
74       $ui= get_userinfo();
75       if(isset($_SESSION['conferencefilter']['depselect'])){
76         $this->base = $_SESSION['conferencefilter']['depselect'];
77       }else{
78         $this->base= dn2base($ui->dn);
79       }
80     } else {
81       $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
82     }
84     $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
85     $this->acl= get_module_permission($acl, "goFonConference", $this->dn);
86     $this->goFonConferenceOwner=$this->ui->dn;
88     /* Parse Options ... 
89      * Parameter|Lifetime|number
90      */
91     if($this->dn!="new"){
92       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
94       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
95         $varname = "goFonConferenceOption_".$tmp1[0][$i];
96         if($tmp1[0][$i]=="d"){
97           $this->goFonConferenceOption_D = $tmp1[0][$i];    
98         }else{
99           $this->$varname = $tmp1[0][$i];    
100         }
101       }
103       $this->goFonConferenceOptionLifetime  = $tmp1[1];
104       $this->old_tele_number                = $this->telephoneNumber;
105     }
106     $this->old_dn = $this->dn;
107     $this->old_cn = $this->cn;
108   }
110   function execute()
111   {
112     //$acl= get_permissions ($this->dn, $this->ui->subtreeACL);
113     //$this->acl= get_module_permission($acl, "goFonConference", $this->dn);
115     $this->config->departments    = get_departments($this->dn);
116     $this->config->make_idepartments();
117     $smarty= get_smarty();
119     $smarty->assign("bases" ,array_flip($this->config->departments));
120     $smarty->assign("base"  ,$this->base);
122     $smarty->assign("goFonConferenceOptions",               array("D"=>"Conference ","d"=>"Conference without PIN"));
123     $smarty->assign("goFonConferenceOptionFormats",     array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
124     $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
126     foreach ($this->attributes as $val){
127       $smarty->assign("$val", $this->$val);
128       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
129       if(!$this->$val){
130         $smarty->assign($val."CHK", "");
131       }else{
132         $smarty->assign($val."CHK", " checked ");
133       }
134     }
136     if($_SESSION['js']==1){
137       if($this->goFonConferenceOption_P != "P"){
138         $smarty->assign("goFonPINACL"," disabled ");
139         $smarty->assign("goFonPIN","");
140       }
141       if($this->goFonConferenceOption_r != "r"){
142         $smarty->assign("goFonConferenceOptionFormatACL"," disabled ");
143       }
144     }
145     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
146   }
148   function remove_from_parent()
149   {
150      $this->SQL_remove_me(true); 
152     $ldap= $this->config->get_ldap_link();
153     $ldap->cd ($this->dn);
154     $ldap->recursive_remove();
156     /* Optionally execute a command after we're done */
157     $this->handle_post_events('remove');
158   }
161   /* Save data to object */
162   function save_object()
163   {
164     plugin::save_object();
165     if(isset($_POST['cn'])){
166       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
167             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonPIN") as $attrs){
168         if(isset($_POST[$attrs])){
169           $this->$attrs = $_POST[$attrs];
170         }else{
171           $this->$attrs = false;
172         }
173       }
174     }
175   }
178   /* Check values */
179   function check()
180   {
181     $message= array();
183     if($this->is_number_used()){
184       $message[] =  $this->is_number_used();
185     }
187     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
188       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
189     }
191     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
192       $message[]= _("Please enter a PIN.");
193     }
195     if(empty($this->cn)){
196       $message[] =_("Please enter a name for the conference.");
197     }
199     if(!is_numeric($this->telephoneNumber)){
200       $message[] =_("Only numeric chars are allowed in Number field.");
201     }
203     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
204       $message[] =_("Only numbers are allowed in Lifetime.");
205     }
207     $this->SQL_remove_me(false);
208     $this->SQL_add_me(false);
210     if(!empty($this->generate_error)){
211       $message[]=$this->generate_error;
212       $this->generate_error="";
213     }
215     return $message;
216   }
219   function SQL_add_me($save){
221     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
222       $this->generate_error = _("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer).\nYour Settings can't be saved to asterisk Database.");
223       return(false);
224     }
225   
226     // Get Configuration for Mysql database Server
227     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
229     // Connect to DB server
230     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
232     // Check if we are  connected correctly
233     if(!$r_con){
234       $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
235           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
236       gosa_log(mysql_error());
237       return false;
238     }
240     // Select database for Extensions
241     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
243     // Test if we have the database selected correctly
244     if(!$db){
245       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
246       gosa_log(mysql_error());
247       return false;
248     }
250     if((!empty($this->telephoneNumber))&&($save==true)){
252       $EXT=array();
254       $parameter  ="";
255       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
256             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
257         $parameter .= $this->$attrs;
258       }
260       $i=1;
261       $context="GOsa";
262       // Set Language to German
263       $EXT[$i]['exten']   =$this->telephoneNumber;
264       $EXT[$i]['context'] = $context;
265       $EXT[$i]['priority']= $i;
266       $EXT[$i]['app']     ="SetLanguage";
267       $EXT[$i]['appdata'] ="de";
268       $i++;      
270       if($this->goFonConferenceOption_r == "r"){
272         // Recordingformat for conference
273         $EXT[$i]['exten']   =$this->telephoneNumber;
274         $EXT[$i]['context'] =$context;
275         $EXT[$i]['priority']= $i;
276         $EXT[$i]['app']     ="Setvar";
277         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
278         $i++;      
280       }
282       // Answer Call
283       $EXT[$i]['exten']   =$this->telephoneNumber;
284       $EXT[$i]['context'] =$context;
285       $EXT[$i]['priority']=$i;
286       $EXT[$i]['app']     ="answer";
287       $EXT[$i]['appdata'] ="";
288       $i++;      
290       // Start Conference 
291       $EXT[$i]['exten']   =$this->telephoneNumber;
292       $EXT[$i]['context'] =$context;
293       $EXT[$i]['priority']=$i;
294       $EXT[$i]['app']     ="MeetMe";
296       if(empty($this->goFonPIN)) {
297         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
298       }else{
299         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
300       }
301       $i++; 
303       // Start Conference 
304       $EXT[$i]['exten']   =$this->cn;
305       $EXT[$i]['context'] =$context;
306       $EXT[$i]['priority']=1;
307       $EXT[$i]['app']     ="Goto";
308       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
309       $SQL=array();
311       foreach($EXT as $keytop => $valtop){
312         $s_keys = "";
313         $s_values = ""; 
314         foreach($valtop as $key=>$val){
315           $s_keys   .="`".$key."`,";
316           $s_values .="'".$val."',";
317         }
318         $s_keys   =preg_replace("/\,$/","",$s_keys); 
319         $s_values =preg_replace("/\,$/","",$s_values); 
320         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
321       }
322       foreach($SQL as $sqlsyn){
323         mysql_query($sqlsyn);
324       }
325     } 
328     @mysql_close($r_con);
329     return(true);
330   }
332   function SQL_remove_me($save){
334     if($this->old_tele_number){
335       // Get Configuration for Mysql database Server
336       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
338       // Connect to DB server
339       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
341       // Check if we are  connected correctly
342       if(!$r_con){
343         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
344             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
345         gosa_log(mysql_error());
346         return false;
347       }
349       // Select database for Extensions
350       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
352       // Test if we have the database selected correctly
353       if(!$db){
354         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
355         gosa_log(mysql_error());
356         return false;
357       }
359       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
360       WHERE   (exten='".$this->old_tele_number."') 
361         OR    (exten='".$this->telephoneNumber."') 
362         OR    (exten='".$this->cn."')  
363         OR    (exten='".$this->old_cn."')";
365       if($save){
366         @mysql_query($SQL);
367       } 
369     }//ENDE  old num availiable ...
370     @mysql_close($r_con);
371     return(true);
372   }
376   /* This function checks if the given phonenumbers are available or already in use*/
377   function is_number_used()
378   {
379     $ldap= $this->config->get_ldap_link();
380     $ldap->cd($this->config->current['BASE']);
381     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
382     while($attrs = $ldap->fetch()) {
383       unset($attrs['telephoneNumber']['count']);
384       foreach($attrs['telephoneNumber'] as $tele){
385         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
386         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
387         $numbers[$tele]=$attrs;
388       }
389     }
391     $num = $this->telephoneNumber;
392     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
393       if(isset($numbers[$num]['uid'][0])){
394         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
395       }else{
396         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
397       }
398     }
399   }
405   /* Save to LDAP */
406   function save()
407   {
409     if((!isset($this->attrs['goFonPIN']))){
410       $pin_use = false;
411     }else{
412       $pin_use = true;
413     }
415     plugin::save();
417     if(empty($this->old_tele_number)){
418       $this->old_tele_number= $this->telephoneNumber;
419     }
421     $this->SQL_remove_me(true);
422     $this->SQL_add_me(true);
424     if(empty($this->goFonConferenceOption_P)){
425       if($pin_use){
426         $this->attrs['goFonPIN']=array();
427       }else{
428         unset($this->attrs['goFonPIN']);
429       }
430     }
431     $this->attrs['goFonConferenceOption']="";
432     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
433           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
434       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
435       unset($this->attrs[$attrs]);         
436     }
438     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
439     unset($this->attrs['goFonConferenceOptionLifetime']);
441     /* Write back to ldap */
443     unset($this->attrs['base']);
445     foreach($this->attributes as $atr){
446       if(chkacl($this->acl, $atr)!=""){
447         unset($this->attrs[$atr]);
448       }
449     }
451     $ldap= $this->config->get_ldap_link();
453     $ldap->cat($this->dn);
454     $a= $ldap->fetch();
455     $ldap->cd($this->dn);
456     if (count($a)){
457       $ldap->modify($this->attrs);
458       $this->handle_post_events('modify');
459     } else {
460       $ldap->add($this->attrs);
461       $this->handle_post_events('add');
462     }
463     show_ldap_error($ldap->get_error());
465     /* Optionally execute a command after we're done */
466     $this->postcreate();
467   }
471 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
472 ?>