Code

37e71613829ecc2a5878c2613d4a7aa2df3a2499
[gosa.git] / plugins / admin / ogroups / class_phonequeue.inc
1 <?php
3 class phonequeue extends plugin
4 {
5   /* plugin specific values */
6   var $mail= "";
7   var $cn= "";
10   var $goFonTimeOut             ="5"; 
11   var $goFonMaxLen              ="20";
12   var $goFonAnnounceFrequency   ="60";
13   var $goFonDialOption_t        ="t";
14   var $goFonDialOption_T        ="T";
15   var $goFonDialOption_h        ="h";
16   var $goFonQueueAnnounce            ="gonicus-berlin-welcome";
17   var $goFonDialOption_H        ="H";
18   var $goFonMusiconHold         ="default";
19   var $goFonWelcomeMusic        ="gonicus-berlin-welcome";
20   var $goFonQueueReportHold     ="";
21   var $goFonQueueYouAreNext     ="queue-youarenext";
22   var $goFonQueueThereAre       ="queue-therare";
23   var $goFonQueueCallsWaiting   ="queue-callswaiting";
24   var $goFonQueueThankYou       ="queue-thankyou";
25   var $goFonQueueMinutes        ="queue-minutes"; 
26   var $goFonQueueSeconds        ="queue-seconds";
27   var $goFonQueueLanguage            ="queue-holdtime";
28   var $goFonQueueStrategy            ="rrmemory";
29   var $goFonQueueAnnounceHoldtime    ="yes";
30   var $telephoneNumber          =array();
31   var $goFonQueueMember         =array(); 
32   var $goFonDialOption          ="tThH";
33   var $goFonQueueRetry          =5;
35   var $old_phone_numbers        =array();
37   /* attribute list for save action */
38   var $attributes= array( "goFonTimeOut","goFonMaxLen","goFonAnnounceFrequency","goFonDialOption_t","goFonDialOption_T","goFonDialOption_h",
39       "goFonDialOption_H","goFonMusiconHold","goFonWelcomeMusic","goFonQueueReportHold","goFonQueueYouAreNext",
40       "goFonQueueThereAre","goFonQueueCallsWaiting","goFonQueueThankYou","goFonQueueMinutes","goFonQueueSeconds",
41       "telephoneNumber","goFonQueueLanguage","goFonQueueStrategy","goFonQueueAnnounceHoldtime","goFonQueueAnnounce","goFonDialOption","goFonQueueRetry");
42   /* ObjectClass */
43   var $objectclasses= array("goFonQueue");
45   function phonequeue ($config, $dn= NULL)
46   {
47     plugin::plugin($config, $dn);
49     /* Include config object */
50     $this->config= $config;
52     /* Save initial account state */
53     $this->initially_was_account= $this->is_account;
55     if($this->is_account){
56       if(isset($this->attrs['telephoneNumber'])){
57         $this->telephoneNumber=$this->attrs['telephoneNumber'];
58         unset($this->telephoneNumber['count']); 
59       }
61       for($i = 0; $i < strlen($this->goFonDialOption); $i++){
62         $name = "goFonDialOption_".$this->goFonDialOption[$i];
63         $this->$name=$this->goFonDialOption[$i];
64       }
65     }
67     $this->old_phone_numbers = $this->telephoneNumber;
68   }
71   function execute()
72   {
73     /* Do we need to flip is_account state? */
74     if (isset($_POST['modify_state'])){
75       $this->is_account= !$this->is_account;
76     }
78     /* Show tab dialog headers */
79     if ($this->parent != NULL){
80       if ($this->is_account){
81         $display= $this->show_header(_("Remove the phone queue from this Account"),
82             _("Phone queue is enabled for this group. You can disable it by clicking below."));
83       } else {
84         $display= $this->show_header(_("Create phone queue"), _("For this group the phone queues are disabled. You can enable them by clicking below."));
85         return ($display);
86       }
87     }
89     /* Add queue number */ 
90     if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
91       if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
92         $this->telephoneNumber[]=$_POST['phonenumber'];
93       }
94     }
96     /* Delete queue number */ 
97     if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
98       unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
99     }
100   
101     $tmp = array();
102     foreach($this->telephoneNumber as $val){
103       if(!empty($val)){
104         $tmp[]= $val;
105       }
106     }  
107     $this->telephoneNumber=$tmp;
109     /* queue number up */ 
110     if(isset($_POST['up_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
111       if($_POST['goFonQueueNumber_List']>0){
112         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
113         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1];
114         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]    = $down; 
115         $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1]  = $up;
116       }
117     }
119     /* Queuenumber down */ 
120     if(isset($_POST['down_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
121       if(isset($this->telephoneNumber[($_POST['goFonQueueNumber_List']+1)])){
122         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1];
123         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
124         $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1]    = $down; 
125         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]  = $up;
126       }
127     }
130     $smarty= get_smarty();
132     $smarty->assign("goFonQueueLanguageOptions",array('de'=>_('German'),'ur'=>_('Uruguai')));
133     $smarty->assign("goFonQueueAnnounceHoldtimeOptions",array('no'=>_("No"),'yes'=>_('Yes')));
134     $smarty->assign("goFonQueueStrategyOptions",array('ringall'    =>_("ring all available channels until one answers"),
135           'roundrobin' =>_("take turns ringing each available interface"),
136           'leastrecent'=>_("ring interface which was least recently called by this queue"),
137           'fewestcalls'=>_("ring the one with fewest completed calls from this queue"),
138           'random'     =>_("ring random interface"),
139           'rrmemory'   =>_("round robin with memory, remember where we left off last ring pass")));
141     foreach($this->attributes as $key => $val){
142       $smarty->assign($val,$this->$val);  
144       if($this->$val == false){
145         $smarty->assign($val."CHK","");
146       }else{
147         $smarty->assign($val."CHK"," checked ");
148       }
150       if(chkacl($this->acl,$key)==""){
151         $smarty->assign($val."ACL","");
152       }else{
153         $smarty->assign($val."ACL"," disabled ");
154       }
155     }
156     return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE)));
157   }
160   /* Check formular input */
161   function check()
162   {
163     $message= array();
165     if($this->is_number_used()){
166       $message[] = $this->is_number_used();
167     }
169     if($this->generate_mysql_entension_entries()){
170       $message[] = $this->generate_mysql_entension_entries();
171     }
173     if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
174       $message[] = _("Timeout must be numeric");
175     }
176     if(!((is_numeric($this->goFonQueueRetry))||(empty($this->goFonQueueRetry)))){
177       $message[] = _("Retry must be numeric");
178     }
179     if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
180       $message[] = _("Max queue length must be numeric");
181     }
182     if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
183       $message[] = _("Announce frequency must be numeric");
184     }
185     if(count($this->telephoneNumber)==0){
186       $message[] = _("There must be least one queue number defined.");
187     }
189     return $message;
190   }
194   function generate_mysql_entension_entries($save = false)
195   {
197     $SQL = array();
198  
199     // Get Configuration for Mysql database Server
200     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
201     $s_parameter  ="";
203     // Connect to DB server
204     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
206     // Check if we are  connected correctly
207     if(!$r_con){
208       gosa_log(mysql_error());
209       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
210           $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
211     }
213     // Select database for Extensions
214     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
216     // Test if we have the database selected correctly
217     if(!$db){
218       gosa_log(mysql_error());
219       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
220     }
222     if($save){
223       $i = 0;
224       $prio = 11; 
226       if(empty($this->cn)){
227         $this->cn = $this->parent->by_object['ogroup']->cn;
228         $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
229       }
231       // Delete old Entries 
232       $delete = array();
233       if(is_array($this->old_phone_numbers)){
234         foreach($this->old_phone_numbers as $phone){
235           $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
236         }
237       }
238       $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
239       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']."  WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
240       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
242       /* Perform queries to delte old entries */
243       foreach($delete as $query){
244         if(!mysql_query($query)){
245           gosa_log(mysql_error());
246           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
247         }
248       }
250       /* Append new Member for this queue */ 
251       $i = 0;
252       foreach($this->parent->by_object['ogroup']->memberList as $member){
253         if(in_array("goFonAccount",$member['objectClass'])){
254           $i ++ ;
255           $queueuser[$i]['queue_name']  = $this->attrs['cn'][0]; 
256           $queueuser[$i]['interface']   = "SIP/".$member['uid'][0]; 
257           $queueuser[$i]['penalty']     = 1; 
258         }
259       }
261       /* Parse and Add members to query Array */
262       foreach($queueuser as $user){
263         $entries = "";
264         $values  = "";
265         foreach($user as $attr => $val){
266           $entries.= "`".$attr."`,"; 
267           $values .= "'".$val."',";
268         }
269         $values  = preg_replace("/,$/","",$values);
270         $entries = preg_replace("/,$/","",$entries );
271         
272         $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")"; 
273       }
274       
276       /* generate Extension entries, with priority  */
277       $i = 0;
278       foreach($this->telephoneNumber as $num){
279         
280         $prio --;
281         $a_ext[$i]['context']  = 'GOsa';
282         $a_ext[$i]['exten']    = $this->attrs['cn'][0];
283         $a_ext[$i]['priority'] = 1;
284         $a_ext[$i]['app']      = "Goto";
285         $a_ext[$i]['appdata']  = $num."|1";
286         $i ++ ; 
287         $a_ext[$i]['context']  = 'GOsa';
288         $a_ext[$i]['exten']    = $num;
289         $a_ext[$i]['priority'] = 1;
290         $a_ext[$i]['app']      = "Wait";
291         $a_ext[$i]['appdata']  = "2";
292         $i ++ ; 
293         $a_ext[$i]['context']  = 'GOsa';
294         $a_ext[$i]['exten']    = $num;
295         $a_ext[$i]['priority'] = 2;
296         $a_ext[$i]['app']      = "Set";
297         $a_ext[$i]['appdata']  = "LANGUAGE|".$this->goFonQueueLanguage;
298         $i ++ ; 
299         $a_ext[$i]['context']  = 'GOsa';
300         $a_ext[$i]['exten']    = $num;
301         $a_ext[$i]['priority'] = 3;
302         $a_ext[$i]['app']      = "Playback";
303         $a_ext[$i]['appdata']  = $this->goFonWelcomeMusic;
304         $i ++ ; 
305         $a_ext[$i]['context']  = 'GOsa';
306         $a_ext[$i]['exten']    = $num;
307         $a_ext[$i]['priority'] = 4;
308         $a_ext[$i]['app']      = "SetCIDName";
309         $a_ext[$i]['appdata']  = $this->parent->by_object['ogroup']->description;
310         $i ++ ; 
311         $a_ext[$i]['context']  = 'GOsa';
312         $a_ext[$i]['exten']    = $num;
313         $a_ext[$i]['priority'] = 5;
314         $a_ext[$i]['app']      = "Queue";
315         $a_ext[$i]['appdata']  =  $this->attrs['cn'][0].
316           "|".
317           $this->goFonDialOption_t.
318           $this->goFonDialOption_T.
319           $this->goFonDialOption_h.
320           $this->goFonDialOption_H.
321           "|".  // Optionalurl egal
322           "|".  // announceoverride
323           "|";  // Timeout
325 //        $i ++ ; 
326 //        $a_ext[$i]['context']  = 'GOsa';
327 //        $a_ext[$i]['exten']    = $num;
328 //        $a_ext[$i]['priority'] = 6;
329 //        $a_ext[$i]['app']      = "SetVar";
330 //        $a_ext[$i]['appdata']  = "Queue_Prio=".$prio;
331         $i ++ ;
333         /* Generate Priority Entry */
334         $queue["announce"]              = "";
335         $queue["monitor_join"]          = "";
336         $queue["monitor_format"]        = "";
337         $queue["queue_holdtime"]        = $this->goFonQueueAnnounce;
338         $queue["queue_lessthan"]        = "";   
339         $queue["announce_round_seconds"]= "";   
340         $queue["retry"]                 = $this->goFonQueueRetry;
341         $queue["wrapuptime"]            = "";
342         $queue["servicelevel"]          = "";
343         $queue["joinempty"]             = "";
344         $queue["leavewhenempty"]        = "";   
345         $queue["eventmemberstatus"]     = "";
346         $queue["eventwhencalled"]       = "";
347         $queue["reportholdtime"]        = "";
348         $queue["memberdelay"]           = "";
349         $queue["weight"]                = "";
350         $queue["timeoutrestart"]        = "";
352         $queue["context"]               = "default";
353         $queue["name"]                  = $this->attrs['cn'][0];  
354         $queue["timeout"]               = $this->goFonTimeOut; 
355         $queue["maxlen"]                = $this->goFonMaxLen;
356         $queue["strategy" ]             = $this->goFonQueueStrategy;
357         $queue["queue_thankyou"]        = $this->goFonQueueThankYou;   
358         $queue["queue_reporthold"]      = $this->goFonQueueReportHold; 
359         $queue["announce_frequency"]    = $this->goFonAnnounceFrequency;
360         $queue["queue_youarenext"]      = $this->goFonQueueYouAreNext;   
361         $queue["queue_thereare"]        = $this->goFonQueueThereAre;   
362         $queue["queue_callswaiting"]    = $this->goFonQueueCallsWaiting;
363         $queue["queue_minutes"]         = $this->goFonQueueMinutes;
364         $queue["queue_seconds"]         = $this->goFonQueueSeconds;   
365         $queue["announce_holdtime"]     = $this->goFonQueueAnnounceHoldtime;   
366         $queue["musiconhold"]           = $this->goFonMusiconHold;
368         $i++;
369       }
371       /* Parse and Add Extension entries */
372       foreach($a_ext as $ext){
373         $entries = "";
374         $values  = "";
375         foreach($ext as $attr => $val){
376           $entries.= "`".$attr."`,";
377           $values .= "'".$val."',";
378         }
379         $values  = preg_replace("/,$/","",$values);
380         $entries = preg_replace("/,$/","",$entries );
381         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
382       }
385       /* Parse and Add Queue */
386       $entries = "";
387       $values  = "";
388       foreach($queue as $attr=>$val){
389         if($val == "") continue;
390         $entries.= "`".$attr."`,";
391         $values .= "'".$val."',";
392       }
393       $values  = preg_replace("/,$/","",$values);
394       $entries = preg_replace("/,$/","",$entries );
395       $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_TABLE']." (".$entries.") VALUES (".$values.")";
397       foreach($SQL as $query){
398          if(!mysql_query($query)){
399           gosa_log(mysql_error());
400           print_red(mysql_error());
401           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
402         }
403       }
405     }
406     return(false);
407   }
411  /* This function checks if the given phonenumbers are available or already in use*/
413   function is_number_used()
414   {
415     $ldap= $this->config->get_ldap_link();
416     $ldap->cd($this->config->current['BASE']);
417     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue))", array("telephoneNumber","cn","uid"));
418     while($attrs = $ldap->fetch()) {
419       unset($attrs['telephoneNumber']['count']);
420       foreach($attrs['telephoneNumber'] as $tele){
421         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
422         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
423         $numbers[$tele]=$attrs;
424       }
425     }
427     foreach($this->telephoneNumber as $num){
428       if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
429         if(isset($numbers[$num]['uid'][0])){
430           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
431         }else{
432           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
433         }
434       }
435     }
436   }
442   function save_object()
443   {
444     plugin::save_object();  
445     if(isset($_POST['phonenumber'])){
446       foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H","goFonMusiconHold") as $val){
447         if(isset($_POST[$val])){
448           $this->$val = $_POST[$val];
449         }else{
450           $this->$val = false;
451         }
452       }
453     }
455   }
457   function save()
458   {
459     $ldap= $this->config->get_ldap_link();
461     plugin::save();
462     $this->attrs['goFonDialOption'] = "";
463     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
464       $this->attrs['goFonDialOption'].=$this->$val; 
465       unset($this->attrs[$val]); 
466     }
467     $this->generate_mysql_entension_entries(true);
468     if($this->attrs['goFonDialOption']=="") $this->attrs['goFonDialOption']=array();
469     
470     /* Save data to LDAP */
471     $ldap->cd($this->dn);
472     $ldap->modify($this->attrs);
474     show_ldap_error($ldap->get_error());
476     /* Optionally execute a command after we're done */
477     if ($this->initially_was_account == $this->is_account){
478       if ($this->is_modified){
479         $this->handle_post_events("mofify");
480       }
481     } else {
482       $this->handle_post_events("add");
483     }
484   }
487   /* remove object from parent */
488   function remove_from_parent()
489   {
490     $SQL = array();
492     // Get Configuration for Mysql database Server
493     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
494     $s_parameter  ="";
496     // Connect to DB server
497     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
499     // Check if we are  connected correctly
500     if(!$r_con){
501       gosa_log(mysql_error());
502       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
503             $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
504     }
506     // Select database for Extensions
507     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
509     // Test if we have the database selected correctly
510     if(!$db){
511       gosa_log(mysql_error());
512       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
513     }
515     $tmp = array_flip($this->attributes);
516     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
517       unset($this->$val);
518       unset($this->attrs[$val]);
519       unset($tmp[$val]);
520     }   
521     foreach(array_flip($tmp) as $key => $val){
522       $tmp2[]=$val;
523     } 
524     $this->attributes = $tmp2;
526     $i = 0;
527     $prio = 11;
529     if(empty($this->cn)){
530       $this->cn = $this->parent->by_object['ogroup']->cn;
531       $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
532     }
534     // Delete old Entries
535     $delete = array();
536     foreach($this->old_phone_numbers as $phone){
537       $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
538     }
539     $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
540     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']." WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
541     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
543     /* Perform queries to delte old entries */
544     foreach($delete as $query){
545       if(!mysql_query($query)){
546         gosa_log(mysql_error());
547         return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
548       }
549     }
553     /* Cancel if there's nothing to do here */
554     if (!$this->initially_was_account){
555       return;
556     }
558     /* include global link_info */
559     $ldap= $this->config->get_ldap_link();
561     /* Remove and write to LDAP */
562     plugin::remove_from_parent();
564     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
565         $this->attributes, "Save");
566     $ldap->cd($this->dn);
567     $ldap->modify($this->attrs);
568     show_ldap_error($ldap->get_error());
569   }
573 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
574 ?>