Code

Changed Management Action Post names
[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 $old_cn         = "";
31   var $goFonConferenceOption    = "";
32   var $goFonConferenceOption_P  = "";   // Set PIN 
33   var $goFonConferenceOption_r  = "";   // record Conference
34   var $goFonConferenceOption_M  = "";   // Play Music opn hold
35   var $goFonConferenceOption_s  = "";   // activate menu
36   var $goFonConferenceOption_i  = "";   // announce new and leaving user
37   var $goFonConferenceOption_c  = "";   // Count User
38   var $goFonConferenceOption_D  = "";   // Conference Type, no PIN/PIN
40   var $goFonConferenceOwner    = "";
42   var $error_shown = false;
44   var $goFonConferenceOptionFormat              = "";
45   var $goFonConferenceOptionLifetime    = "";
46   var $telephoneNumber          = "";
48   var $old_tele_number          = false;
49   var $generate_error           = "";
51   var $old_dn;
53   /* Headpage attributes */
54   var $last_dep_sorting= "invalid";
55   var $departments= array();
57   var $dialog ;
59   /* attribute list for save action */
60   var $attributes= array("cn","base", "description", "goFonPIN","goFonConferenceOption_P","goFonConferenceOption_r",
61       "goFonConferenceOption_M","goFonConferenceOption_s","goFonConferenceOption_i","goFonConferenceOption_c",
62       "goFonConferenceOption_D","goFonConferenceOptionFormat","goFonConferenceOptionLifetime","telephoneNumber","goFonConferenceOwner");
64   var $objectclasses= array("top", "goFonConference");
66   function conference ($config, $dn, $plugin= NULL)
67   {
68     plugin::plugin($config, $dn, $plugin);
69     $this->is_account   = TRUE;
70     $this->ui                   = get_userinfo();
71     $this->dn                   = $dn;
72     $this->orig_dn              = $dn;
73     $this->config               = $config;
75     /* Set base */
76     if ($this->dn == "new"){
77       $ui= get_userinfo();
78       if(isset($_SESSION['CurrentMainBase'])){
79         $this->base = $_SESSION['CurrentMainBase'];
80       }else{
81         $this->base= dn2base($ui->dn);
82       }
83     } else {
84       $this->base= preg_replace ("/^[^,]+,ou=conferences,ou=asterisk,ou=configs,ou=systems,/", "", $this->dn);
85     }
87     $this->goFonConferenceOwner=$this->ui->dn;
89     /* Parse Options ... 
90      * Parameter|Lifetime|number
91      */
92     if($this->dn!="new"){
93       $tmp1= split("\|",$this->attrs['goFonConferenceOption'][0]);
95       for($i = 0 ; $i < strlen($tmp1[0]);$i++){
96         $varname = "goFonConferenceOption_".$tmp1[0][$i];
97         if($tmp1[0][$i]=="d"){
98           $this->goFonConferenceOption_D = $tmp1[0][$i];    
99         }else{
100           $this->$varname = $tmp1[0][$i];    
101         }
102       }
104       $this->goFonConferenceOptionLifetime  = $tmp1[1];
105       $this->old_tele_number                = $this->telephoneNumber;
106     }
107     $this->old_dn = $this->dn;
108     $this->old_cn = $this->cn;
109   }
111   function execute()
112   {
113     /* Call parent execute */
114     plugin::execute();
116     $smarty= get_smarty();
119     $tmp = $this->plInfo();
120     foreach($tmp['plProvidedAcls'] as $name => $translation){
121       $smarty->assign($name."ACL",$this->getacl($name));
122     }
124     if($this->acl_is_writeable("base")){
125       $smarty->assign("baseSelect",true);
126     }else{
127       $smarty->assign("baseSelect",false);
128     }
130     $smarty->assign("bases" ,$this->get_allowed_bases());
131     $smarty->assign("base_select"  ,$this->base);
133     $once = true; 
134     foreach($_POST as $name => $value){
135       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
136         $once = false;
137         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
138         $this->dialog->setCurrentBase($this->base);
139       }
140     }
142     /* Dialog handling */
143     if(is_object($this->dialog)){
144       /* Must be called before save_object */
145       $this->dialog->save_object();
147       if($this->dialog->isClosed()){
148         $this->dialog = false;
149       }elseif($this->dialog->isSelected()){
151         /* A new base was selected, check if it is a valid one */
152         $tmp = $this->get_allowed_bases();
153         if(isset($tmp[$this->dialog->isSelected()])){
154           $this->base = $this->dialog->isSelected();
155         }
157         $this->dialog= false;
158       }else{
159         return($this->dialog->execute());
160       }
161     }
163     $smarty->assign("goFonConferenceOptions",               array("D"=>"Conference ","d"=>"Conference without PIN"));
164     $smarty->assign("goFonConferenceOptionFormats",     array("WAV"=>"Wave","GSM"=>"GSM","WAV49"=>"Wave49"));
165     $smarty->assign("goFonConferenceOption",        $this->goFonConferenceOption_D);
167     foreach ($this->attributes as $val){
168       $smarty->assign("$val", $this->$val);
169       if(!$this->$val){
170         $smarty->assign($val."CHK", "");
171       }else{
172         $smarty->assign($val."CHK", " checked ");
173       }
174     }
176     if($_SESSION['js']==1){
177       if($this->goFonConferenceOption_P != "P"){
178         $smarty->assign("goFonPINACL", $this->getacl("goFonPIN",TRUE));
179         $smarty->assign("goFonPIN","");
180       }
181       if($this->goFonConferenceOption_r != "r"){
182         $smarty->assign("goFonConferenceOptionFormatACL", $this->getacl("goFonConferenceOptionr",TRUE));
183       }
184     }
185     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
186   }
188   function remove_from_parent()
189   {
190     $this->SQL_remove_me(true); 
192     $ldap= $this->config->get_ldap_link();
193     $ldap->cd ($this->dn);
194     $ldap->recursive_remove();
196     show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonConference/generic with dn '%s' failed."),$this->dn));
197     /* Optionally execute a command after we're done */
198     $this->handle_post_events('remove');
199   }
202   /* Save data to object */
203   function save_object()
204   {
205     if(isset($_POST['phoneConferenceGeneric'])){
207       /* Create a base backup and reset the
208          base directly after calling plugin::save_object();
209          Base will be set seperatly a few lines below */
210       $base_tmp = $this->base;
211       plugin::save_object();
212       $this->base = $base_tmp;
214       /* Save base, since this is no LDAP attribute */
215       $tmp = $this->get_allowed_bases();
216       if(isset($_POST['base'])){
217         if(isset($tmp[$_POST['base']])){
218           $this->base= $_POST['base'];
219         }
220       }
222       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
223             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
225         /* Acl can't contain _ so we remove it here. */
226         $acl_name = preg_replace("/_/","",$attrs);
228         /* Attribute writeable ? */
229         if($this->acl_is_writeable($acl_name)){
230           if(isset($_POST[$attrs])){
231             $this->$attrs = $_POST[$attrs];
232           }else{
233             $this->$attrs = false;
234           }
235         }
236       }
237     }
238   }
241   /* Check values */
242   function check()
243   {
244     /* Call common method to give check the hook */
245     $message= plugin::check();
247     if($this->is_number_used()){
248       $message[] =  $this->is_number_used();
249     }
251     if((!empty($this->goFonPIN)||($this->goFonConferenceOption_P=="P"))&&($this->goFonConferenceOption_D=="d")){
252       $message[] =_("You have specified a conference 'without PIN' ... please leave the PIN fields empty.");
253     }
255     if((empty($this->goFonPIN))&&($this->goFonConferenceOption_P=="P")&&($this->goFonConferenceOption_D=="D")){
256       $message[]= _("Please enter a PIN.");
257     }
259     if(empty($this->cn)){
260       $message[] =_("Please enter a name for the conference.");
261     }
263     if(!is_numeric($this->telephoneNumber)){
264       $message[] =_("Only numeric chars are allowed in Number field.");
265     }
267     if(!((is_numeric($this->goFonConferenceOptionLifetime))||(empty($this->goFonConferenceOptionLifetime)))){
268       $message[] =_("Only numbers are allowed in Lifetime.");
269     }
271     $this->SQL_remove_me(false);
272     $this->SQL_add_me(false);
274     if(!empty($this->generate_error)){
275       $message[]=$this->generate_error;
276       $this->generate_error="";
277     }
279     return $message;
280   }
283   function SQL_add_me($save){
285     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
286       $this->generate_error = _("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). Your settings can't be saved to asterisk database.");
287       return(false);
288     }
289   
290     // Get Configuration for Mysql database Server
291     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
293     // Connect to DB server
294     $r_con = false;    
296     if(!is_callable("mysql_pconnect")){
297       if(!$this->error_shown){
298         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
299         $this->error_shown = true;
300       }
301       return(true);
302     }
303     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
305     // Check if we are  connected correctly
306     if(!$r_con){
307       $this->generate_error = sprintf(_("The MySQL server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
308           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
309       gosa_log(mysql_error());
310       return false;
311     }
313     // Select database for Extensions
314     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
316     // Test if we have the database selected correctly
317     if(!$db){
318       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
319       gosa_log(mysql_error());
320       return false;
321     }
323     if((!empty($this->telephoneNumber))&&($save==true)){
325       $EXT=array();
327       $parameter  ="";
328       foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
329             "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D") as $attrs){
330         $parameter .= $this->$attrs;
331       }
333       $i=1;
334       $context="GOsa";
335       // Set Language to German
336       $EXT[$i]['exten']   =$this->telephoneNumber;
337       $EXT[$i]['context'] = $context;
338       $EXT[$i]['priority']= $i;
339       $EXT[$i]['app']     ="SetLanguage";
340       $EXT[$i]['appdata'] ="de";
341       $i++;      
343       if($this->goFonConferenceOption_r == "r"){
345         // Recordingformat for conference
346         $EXT[$i]['exten']   =$this->telephoneNumber;
347         $EXT[$i]['context'] =$context;
348         $EXT[$i]['priority']= $i;
349         $EXT[$i]['app']     ="Setvar";
350         $EXT[$i]['appdata'] ="MEETME_RECORDINGFORMAT=".$this->goFonConferenceOptionFormat;
351         $i++;      
353       }
355       // Answer Call
356       $EXT[$i]['exten']   =$this->telephoneNumber;
357       $EXT[$i]['context'] =$context;
358       $EXT[$i]['priority']=$i;
359       $EXT[$i]['app']     ="answer";
360       $EXT[$i]['appdata'] ="";
361       $i++;      
363       // Start Conference 
364       $EXT[$i]['exten']   =$this->telephoneNumber;
365       $EXT[$i]['context'] =$context;
366       $EXT[$i]['priority']=$i;
367       $EXT[$i]['app']     ="MeetMe";
369       if(empty($this->goFonPIN)) {
370         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter;
371       }else{
372         $EXT[$i]['appdata'] =$this->telephoneNumber."|".$parameter."|".$this->goFonPIN;
373       }
374       $i++; 
376       // Start Conference 
377       $EXT[$i]['exten']   =$this->cn;
378       $EXT[$i]['context'] =$context;
379       $EXT[$i]['priority']=1;
380       $EXT[$i]['app']     ="Goto";
381       $EXT[$i]['appdata'] =$this->telephoneNumber."|1";
382       $SQL=array();
384       foreach($EXT as $keytop => $valtop){
385         $s_keys = "";
386         $s_values = ""; 
387         foreach($valtop as $key=>$val){
388           $s_keys   .="`".$key."`,";
389           $s_values .="'".$val."',";
390         }
391         $s_keys   =preg_replace("/\,$/","",$s_keys); 
392         $s_values =preg_replace("/\,$/","",$s_values); 
393         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$s_keys.") VALUES (".$s_values.");"; 
394       }
395       foreach($SQL as $sqlsyn){
396         mysql_query($sqlsyn);
397       }
398     } 
401     @mysql_close($r_con);
402     return(true);
403   }
405   function SQL_remove_me($save){
406     if(!is_callable("mysql_pconnect")){
407       if(!$this->error_shown){
408         print_red(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
409         $this->error_shown = true;
410       }
411       return(true);
412     }
414     if($this->old_tele_number){
415       // Get Configuration for Mysql database Server
416       $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
418       // Connect to DB server
419       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
421       // Check if we are  connected correctly
422       if(!$r_con){
423         $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
424             $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
425         gosa_log(mysql_error());
426         return false;
427       }
429       // Select database for Extensions
430       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
432       // Test if we have the database selected correctly
433       if(!$db){
434         $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
435         gosa_log(mysql_error());
436         return false;
437       }
439       $SQL = "DELETE FROM ".$a_SETUP['EXT_TABLE']." 
440       WHERE   (exten='".$this->old_tele_number."') 
441         OR    (exten='".$this->telephoneNumber."') 
442         OR    (exten='".$this->cn."')  
443         OR    (exten='".$this->old_cn."')";
445       if($save){
446         @mysql_query($SQL);
447       } 
449     }//ENDE  old num availiable ...
450     @mysql_close($r_con);
451     return(true);
452   }
456   /* This function checks if the given phonenumbers are available or already in use*/
457   function is_number_used()
458   {
459     $ldap= $this->config->get_ldap_link();
460     $ldap->cd($this->config->current['BASE']);
461     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
462     while($attrs = $ldap->fetch()) {
463       unset($attrs['telephoneNumber']['count']);
464       foreach($attrs['telephoneNumber'] as $tele){
465         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
466         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
467         $numbers[$tele]=$attrs;
468       }
469     }
471     $num = $this->telephoneNumber;
472     if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!=$this->old_cn))){
473       if(isset($numbers[$num]['uid'][0])){
474         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
475       }else{
476         return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
477       }
478     }
479   }
485   /* Save to LDAP */
486   function save()
487   {
489     if((!isset($this->attrs['goFonPIN']))){
490       $pin_use = false;
491     }else{
492       $pin_use = true;
493     }
495     plugin::save();
497     if(empty($this->old_tele_number)){
498       $this->old_tele_number= $this->telephoneNumber;
499     }
501     $this->SQL_remove_me(true);
502     $this->SQL_add_me(true);
504     if(empty($this->goFonConferenceOption_P)){
505       if($pin_use){
506         $this->attrs['goFonPIN']=array();
507       }else{
508         unset($this->attrs['goFonPIN']);
509       }
510     }
511     $this->attrs['goFonConferenceOption']="";
512     foreach(array("goFonConferenceOption_P","goFonConferenceOption_r","goFonConferenceOption_M","goFonConferenceOption_s",
513           "goFonConferenceOption_i","goFonConferenceOption_c","goFonConferenceOption_D","goFonConferenceOptionFormat") as $attrs){
514       $this->attrs['goFonConferenceOption'] .= $this->$attrs;
515       unset($this->attrs[$attrs]);         
516     }
518     $this->attrs['goFonConferenceOption'].="|".$this->goFonConferenceOptionLifetime;
519     unset($this->attrs['goFonConferenceOptionLifetime']);
521     /* Write back to ldap */
523     unset($this->attrs['base']);
525     $ldap= $this->config->get_ldap_link();
526     $ldap->cd($this->config->current['BASE']);
527     $ldap->cat($this->dn, array('dn'));
529     if ($ldap->count()){
530       $ldap->cd($this->dn);
531       $this->cleanup();
532       $ldap->modify ($this->attrs); 
533       $this->handle_post_events('modify');
534     } else {
535       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
536       $ldap->cd($this->dn);
537       $ldap->add($this->attrs);
538       $this->handle_post_events('add');
539     }
540     show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonConference/generic with dn '%s' failed."),$this->dn));
542     /* Optionally execute a command after we're done */
543     $this->postcreate();
544   }
547   /* Return plugin informations for acl handling */
548   function plInfo()
549   {
550     return (array(
551           "plShortName"   => _("Conference"),
552           "plDescription" => _("Phone conference management"),
553           "plSelfModify"  => TRUE,
554           "plDepends"     => array(),
555           "plPriority"    => 0,
556           "plSection"     => array("addons" => _("Addons")),
557           "plCategory"    => array("gofonconference" => array("description" => _("GOfon conference"),
558                                                   "objectClass" => "gofonConference")),
560           "plProvidedAcls" => array(
561             "cn"                            => _("Name"),
562             "base"                          => _("Base"), 
563             "description"                   => _("Description"), 
564             "goFonPIN"                      => _("Conference PIN"),
566             "goFonConferenceOptionP"       => _("Preset PIN"),
567             "goFonConferenceOptionr"       => _("Record conference"),
568             "goFonConferenceOptionM"       => _("Play music on hold"),
569             "goFonConferenceOptions"       => _("Activate menu"),
570             "goFonConferenceOptioni"       => _("Announce user activity"),
571             "goFonConferenceOptionc"       => _("Count user"),
572             "goFonConferenceOptionD"       => _("Conference type"),
574             "goFonConferenceOptionFormat"   => _("Format"),
575             "goFonConferenceOptionLifetime" => _("Lifetime"),
576             "telephoneNumber"               => _("Telephone number"),
577             "goFonConferenceOwner"          => _("Owner"))
578             ));
579   }
582 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
583 ?>