Code

90480d9a5a0a6351aae8862b3696b688785a95e5
[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();
164           if(!$this->is_account) return($message);
166     if($this->is_number_used()){
167       $message[] = $this->is_number_used();
168     }
170     if($this->generate_mysql_entension_entries()){
171       $message[] = $this->generate_mysql_entension_entries();
172     }
174     if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
175       $message[] = _("Timeout must be numeric");
176     }
177     if(!((is_numeric($this->goFonQueueRetry))||(empty($this->goFonQueueRetry)))){
178       $message[] = _("Retry must be numeric");
179     }
180     if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
181       $message[] = _("Max queue length must be numeric");
182     }
183     if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
184       $message[] = _("Announce frequency must be numeric");
185     }
186     if(count($this->telephoneNumber)==0){
187       $message[] = _("There must be least one queue number defined.");
188     }
190     return $message;
191   }
195   function generate_mysql_entension_entries($save = false)
196   {
198     $SQL = array();
199  
200     // Get Configuration for Mysql database Server
201     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
202     $s_parameter  ="";
204     // Connect to DB server
205     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
207     // Check if we are  connected correctly
208     if(!$r_con){
209       gosa_log(mysql_error());
210       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
211           $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
212     }
214     // Select database for Extensions
215     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
217     // Test if we have the database selected correctly
218     if(!$db){
219       gosa_log(mysql_error());
220       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
221     }
223     if($save){
224       $i = 0;
225       $prio = 11; 
227       if(empty($this->cn)){
228         $this->cn = $this->parent->by_object['ogroup']->cn;
229         $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
230       }
232       // Delete old Entries 
233       $delete = array();
234       if(is_array($this->old_phone_numbers)){
235         foreach($this->old_phone_numbers as $phone){
236           $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
237         }
238       }
239       $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
240       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']."  WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
241       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
243       /* Perform queries to delte old entries */
244       foreach($delete as $query){
245         if(!mysql_query($query)){
246           gosa_log(mysql_error());
247           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
248         }
249       }
251       /* Append new Member for this queue */ 
252       $i = 0;
253       $queueuser =array();
254       foreach($this->parent->by_object['ogroup']->memberList as $member){
255         if(in_array("goFonAccount",$member['objectClass'])){
256           $i ++ ;
257           $queueuser[$i]['queue_name']  = $this->attrs['cn'][0]; 
258           $queueuser[$i]['interface']   = "SIP/".$member['uid'][0]; 
259           $queueuser[$i]['penalty']     = 1; 
260         }
261       }
263       /* Parse and Add members to query Array */
264       if(is_array($queueuser)){
265         foreach($queueuser as $user){
266           $entries = "";
267           $values  = "";
268           foreach($user as $attr => $val){
269             $entries.= "`".$attr."`,"; 
270             $values .= "'".$val."',";
271           }
272           $values  = preg_replace("/,$/","",$values);
273           $entries = preg_replace("/,$/","",$entries );
275           $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")"; 
276         }
277       }
278       
280       /* generate Extension entries, with priority  */
281       $i = 0;
282       foreach($this->telephoneNumber as $num){
283         
284         $prio --;
285         $a_ext[$i]['context']  = 'GOsa';
286         $a_ext[$i]['exten']    = $this->attrs['cn'][0];
287         $a_ext[$i]['priority'] = 1;
288         $a_ext[$i]['app']      = "Goto";
289         $a_ext[$i]['appdata']  = $num."|1";
290         $i ++ ; 
291         $a_ext[$i]['context']  = 'GOsa';
292         $a_ext[$i]['exten']    = $num;
293         $a_ext[$i]['priority'] = 1;
294         $a_ext[$i]['app']      = "Wait";
295         $a_ext[$i]['appdata']  = "2";
296         $i ++ ; 
297         $a_ext[$i]['context']  = 'GOsa';
298         $a_ext[$i]['exten']    = $num;
299         $a_ext[$i]['priority'] = 2;
300         $a_ext[$i]['app']      = "Set";
301         $a_ext[$i]['appdata']  = "LANGUAGE|".$this->goFonQueueLanguage;
302         $i ++ ; 
303         $a_ext[$i]['context']  = 'GOsa';
304         $a_ext[$i]['exten']    = $num;
305         $a_ext[$i]['priority'] = 3;
306         $a_ext[$i]['app']      = "Playback";
307         $a_ext[$i]['appdata']  = $this->goFonWelcomeMusic;
308         $i ++ ; 
309         $a_ext[$i]['context']  = 'GOsa';
310         $a_ext[$i]['exten']    = $num;
311         $a_ext[$i]['priority'] = 4;
312         $a_ext[$i]['app']      = "SetCIDName";
313         $a_ext[$i]['appdata']  = $this->parent->by_object['ogroup']->description;
314         $i ++ ; 
315         $a_ext[$i]['context']  = 'GOsa';
316         $a_ext[$i]['exten']    = $num;
317         $a_ext[$i]['priority'] = 5;
318         $a_ext[$i]['app']      = "Queue";
319         $a_ext[$i]['appdata']  =  $this->attrs['cn'][0].
320           "|".
321           $this->goFonDialOption_t.
322           $this->goFonDialOption_T.
323           $this->goFonDialOption_h.
324           $this->goFonDialOption_H.
325           "|".  // Optionalurl egal
326           "|".  // announceoverride
327           "|";  // Timeout
329 //        $i ++ ; 
330 //        $a_ext[$i]['context']  = 'GOsa';
331 //        $a_ext[$i]['exten']    = $num;
332 //        $a_ext[$i]['priority'] = 6;
333 //        $a_ext[$i]['app']      = "SetVar";
334 //        $a_ext[$i]['appdata']  = "Queue_Prio=".$prio;
335         $i ++ ;
337         /* Generate Priority Entry */
338         $queue["announce"]              = "";
339         $queue["monitor_join"]          = "";
340         $queue["monitor_format"]        = "";
341         $queue["queue_holdtime"]        = $this->goFonQueueAnnounce;
342         $queue["queue_lessthan"]        = "";   
343         $queue["announce_round_seconds"]= "";   
344         $queue["retry"]                 = $this->goFonQueueRetry;
345         $queue["wrapuptime"]            = "";
346         $queue["servicelevel"]          = "";
347         $queue["joinempty"]             = "";
348         $queue["leavewhenempty"]        = "";   
349         $queue["eventmemberstatus"]     = "";
350         $queue["eventwhencalled"]       = "";
351         $queue["reportholdtime"]        = "";
352         $queue["memberdelay"]           = "";
353         $queue["weight"]                = "";
354         $queue["timeoutrestart"]        = "";
356         $queue["context"]               = "default";
357         $queue["name"]                  = $this->attrs['cn'][0];  
358         $queue["timeout"]               = $this->goFonTimeOut; 
359         $queue["maxlen"]                = $this->goFonMaxLen;
360         $queue["strategy" ]             = $this->goFonQueueStrategy;
361         $queue["queue_thankyou"]        = $this->goFonQueueThankYou;   
362         $queue["queue_reporthold"]      = $this->goFonQueueReportHold; 
363         $queue["announce_frequency"]    = $this->goFonAnnounceFrequency;
364         $queue["queue_youarenext"]      = $this->goFonQueueYouAreNext;   
365         $queue["queue_thereare"]        = $this->goFonQueueThereAre;   
366         $queue["queue_callswaiting"]    = $this->goFonQueueCallsWaiting;
367         $queue["queue_minutes"]         = $this->goFonQueueMinutes;
368         $queue["queue_seconds"]         = $this->goFonQueueSeconds;   
369         $queue["announce_holdtime"]     = $this->goFonQueueAnnounceHoldtime;   
370         $queue["musiconhold"]           = $this->goFonMusiconHold;
372         $i++;
373       }
375       /* Parse and Add Extension entries */
376       foreach($a_ext as $ext){
377         $entries = "";
378         $values  = "";
379         foreach($ext as $attr => $val){
380           $entries.= "`".$attr."`,";
381           $values .= "'".$val."',";
382         }
383         $values  = preg_replace("/,$/","",$values);
384         $entries = preg_replace("/,$/","",$entries );
385         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
386       }
389       /* Parse and Add Queue */
390       $entries = "";
391       $values  = "";
392       foreach($queue as $attr=>$val){
393         if($val == "") continue;
394         $entries.= "`".$attr."`,";
395         $values .= "'".$val."',";
396       }
397       $values  = preg_replace("/,$/","",$values);
398       $entries = preg_replace("/,$/","",$entries );
399       $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_TABLE']." (".$entries.") VALUES (".$values.")";
401       foreach($SQL as $query){
402          if(!mysql_query($query)){
403           gosa_log(mysql_error());
404           print_red(mysql_error());
405           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
406         }
407       }
409     }
410     return(false);
411   }
415  /* This function checks if the given phonenumbers are available or already in use*/
417   function is_number_used()
418   {
419     $ldap= $this->config->get_ldap_link();
420     $ldap->cd($this->config->current['BASE']);
421     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue))", array("telephoneNumber","cn","uid"));
422     while($attrs = $ldap->fetch()) {
423       unset($attrs['telephoneNumber']['count']);
424       foreach($attrs['telephoneNumber'] as $tele){
425         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
426         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
427         $numbers[$tele]=$attrs;
428       }
429     }
431     foreach($this->telephoneNumber as $num){
432       if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
433         if(isset($numbers[$num]['uid'][0])){
434           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
435         }else{
436           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
437         }
438       }
439     }
440   }
446   function save_object()
447   {
448     plugin::save_object();  
449     if(isset($_POST['phonenumber'])){
450       foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H","goFonMusiconHold") as $val){
451         if(isset($_POST[$val])){
452           $this->$val = $_POST[$val];
453         }else{
454           $this->$val = false;
455         }
456       }
457     }
459   }
461   function save()
462   {
463         if(!$this->is_account) return;
464     $ldap= $this->config->get_ldap_link();
466     plugin::save();
467     $this->attrs['goFonDialOption'] = "";
468     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
469       $this->attrs['goFonDialOption'].=$this->$val; 
470       unset($this->attrs[$val]); 
471     }
472     $this->generate_mysql_entension_entries(true);
473     if($this->attrs['goFonDialOption']=="") $this->attrs['goFonDialOption']=array();
474     
475     /* Save data to LDAP */
476     $ldap->cd($this->dn);
477     $ldap->modify($this->attrs);
479     show_ldap_error($ldap->get_error());
481     /* Optionally execute a command after we're done */
482     if ($this->initially_was_account == $this->is_account){
483       if ($this->is_modified){
484         $this->handle_post_events("mofify");
485       }
486     } else {
487       $this->handle_post_events("add");
488     }
489   }
492   /* remove object from parent */
493   function remove_from_parent()
494   {
495     $SQL = array();
497     // Get Configuration for Mysql database Server
498     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
499     $s_parameter  ="";
501     // Connect to DB server
502     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
504     // Check if we are  connected correctly
505     if(!$r_con){
506       gosa_log(mysql_error());
507       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
508             $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
509     }
511     // Select database for Extensions
512     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
514     // Test if we have the database selected correctly
515     if(!$db){
516       gosa_log(mysql_error());
517       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
518     }
520     $tmp = array_flip($this->attributes);
521     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
522       unset($this->$val);
523       unset($this->attrs[$val]);
524       unset($tmp[$val]);
525     }   
526     foreach(array_flip($tmp) as $key => $val){
527       $tmp2[]=$val;
528     } 
529     $this->attributes = $tmp2;
531     $i = 0;
532     $prio = 11;
534     if(empty($this->cn)){
535       $this->cn = $this->parent->by_object['ogroup']->cn;
536       $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
537     }
539     // Delete old Entries
540     $delete = array();
541     foreach($this->old_phone_numbers as $phone){
542       $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
543     }
544     $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
545     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']." WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
546     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
548     /* Perform queries to delte old entries */
549     foreach($delete as $query){
550       if(!mysql_query($query)){
551         gosa_log(mysql_error());
552         return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
553       }
554     }
558     /* Cancel if there's nothing to do here */
559     if (!$this->initially_was_account){
560       return;
561     }
563     /* include global link_info */
564     $ldap= $this->config->get_ldap_link();
566     /* Remove and write to LDAP */
567     plugin::remove_from_parent();
569     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
570         $this->attributes, "Save");
571     $ldap->cd($this->dn);
572     $ldap->modify($this->attrs);
573     show_ldap_error($ldap->get_error());
574   }
578 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
579 ?>