Code

8e7f227234d01cfedd8a9f00b43482669717052a
[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(isset($this->attrs['telephoneNumber'])){
56       $this->telephoneNumber=$this->attrs['telephoneNumber'];
57       unset($this->telephoneNumber['count']); 
58     }
61     for($i = 0; $i < strlen($this->goFonDialOption); $i++){
62       $name = "goFonDialOption_".$this->goFonDialOption[$i];
63       $this->$name=$this->goFonDialOption[$i];
64     }
65     
66     $this->old_phone_numbers = $this->telephoneNumber;
67   }
70   function execute()
71   {
72     /* Do we need to flip is_account state? */
73     if (isset($_POST['modify_state'])){
74       $this->is_account= !$this->is_account;
75     }
77     /* Show tab dialog headers */
78     if ($this->parent != NULL){
79       if ($this->is_account){
80         $display= $this->show_header(_("Remove the phone queue from this Account"),
81             _("Phone queue is enabled for this group. You can disable it by clicking below."));
82       } else {
83         $display= $this->show_header(_("Create phone queue"), _("For this group the phone queues are disabled. You can enable them by clicking below."));
84         return ($display);
85       }
86     }
88     /* Add queue number */ 
89     if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
90       if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
91         $this->telephoneNumber[]=$_POST['phonenumber'];
92       }
93     }
95     /* Delete queue number */ 
96     if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
97       unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
98     }
99   
100     $tmp = array();
101     foreach($this->telephoneNumber as $val){
102       if(!empty($val)){
103         $tmp[]= $val;
104       }
105     }  
106     $this->telephoneNumber=$tmp;
108     /* queue number up */ 
109     if(isset($_POST['up_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
110       if($_POST['goFonQueueNumber_List']>0){
111         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
112         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1];
113         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]    = $down; 
114         $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1]  = $up;
115       }
116     }
118     /* Queuenumber down */ 
119     if(isset($_POST['down_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
120       if(isset($this->telephoneNumber[($_POST['goFonQueueNumber_List']+1)])){
121         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1];
122         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
123         $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1]    = $down; 
124         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]  = $up;
125       }
126     }
129     $smarty= get_smarty();
131     $smarty->assign("goFonQueueLanguageOptions",array('de'=>_('German'),'ur'=>_('Uruguai')));
132     $smarty->assign("goFonQueueAnnounceHoldtimeOptions",array('no'=>_("No"),'yes'=>_('Yes')));
133     $smarty->assign("goFonQueueStrategyOptions",array('ringall'    =>_("ring all available channels until one answers"),
134           'roundrobin' =>_("take turns ringing each available interface"),
135           'leastrecent'=>_("ring interface which was least recently called by this queue"),
136           'fewestcalls'=>_("ring the one with fewest completed calls from this queue"),
137           'random'     =>_("ring random interface"),
138           'rrmemory'   =>_("round robin with memory, remember where we left off last ring pass")));
140     foreach($this->attributes as $key => $val){
141       $smarty->assign($val,$this->$val);  
143       if($this->$val == false){
144         $smarty->assign($val."CHK","");
145       }else{
146         $smarty->assign($val."CHK"," checked ");
147       }
149       if(chkacl($this->acl,$key)==""){
150         $smarty->assign($val."ACL","");
151       }else{
152         $smarty->assign($val."ACL"," disabled ");
153       }
154     }
155     return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE)));
156   }
159   /* Check formular input */
160   function check()
161   {
162     $message= array();
163   //  if(empty($this->description)){
164   //    $message[] = _("You must specify a description for this queue");
165   //  }
167     if($this->is_number_used()){
168       $message[] = $this->is_number_used();
169     }
171     if($this->generate_mysql_entension_entries()){
172       $message[] = $this->generate_mysql_entension_entries();
173     }
175     if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
176       $message[] = _("Timeout must be numeric");
177     }
178     if(!((is_numeric($this->goFonQueueRetry))||(empty($this->goFonQueueRetry)))){
179       $message[] = _("Retry must be numeric");
180     }
181     if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
182       $message[] = _("Max queue length must be numeric");
183     }
184     if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
185       $message[] = _("Announce frequency must be numeric");
186     }
187     if(count($this->telephoneNumber)==0){
188       $message[] = _("There must be least one queue number defined.");
189     }
191     return $message;
192   }
196   function generate_mysql_entension_entries($save = false)
197   {
199     $SQL = array();
200  
201     // Get Configuration for Mysql database Server
202     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
203     $s_parameter  ="";
205     // Connect to DB server
206     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
208     // Check if we are  connected correctly
209     if(!$r_con){
210       gosa_log(mysql_error());
211       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
212           $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
213     }
215     // Select database for Extensions
216     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
218     // Test if we have the database selected correctly
219     if(!$db){
220       gosa_log(mysql_error());
221       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
222     }
226     if($save){
227       $i = 0;
228       $prio = 11; 
230       if(empty($this->cn)){
231         $this->cn = $this->parent->by_object['ogroup']->cn;
232         $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
233       }
235       if(empty($this->description)){
236         $this->description = $this->parent->by_object['ogroup']->description;
237         $this->attrs['description'][0] = $this->parent->by_object['ogroup']->description;
238       }
239       
240       // Delete old Entries 
241       $delete = array();
242       if(is_array($this->old_phone_numbers)){
243         foreach($this->old_phone_numbers as $phone){
244           $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
245         }
246       }
247       $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
248       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']."  WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
249       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
251       /* Perform queries to delte old entries */
252       foreach($delete as $query){
253         if(!mysql_query($query)){
254           gosa_log(mysql_error());
255           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
256         }
257       }
259       /* Append new Member for this queue */ 
260       $i = 0;
261       foreach($this->parent->by_object['ogroup']->memberList as $member){
262         if(in_array("goFonAccount",$member['objectClass'])){
263           $i ++ ;
264           $queueuser[$i]['queue_name']  = $this->attrs['cn'][0]; 
265           $queueuser[$i]['interface']   = "SIP/".$member['uid'][0]; 
266           $queueuser[$i]['penalty']     = 1; 
267         }
268       }
270       /* Parse and Add members to query Array */
271       foreach($queueuser as $user){
272         $entries = "";
273         $values  = "";
274         foreach($user as $attr => $val){
275           $entries.= "`".$attr."`,"; 
276           $values .= "'".$val."',";
277         }
278         $values  = preg_replace("/,$/","",$values);
279         $entries = preg_replace("/,$/","",$entries );
280         
281         $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")"; 
282       }
283       
285       /* generate Extension entries, with priority  */
286       $i = 0;
287       foreach($this->telephoneNumber as $num){
288         
289         $prio --;
290         $a_ext[$i]['context']  = 'GOsa';
291         $a_ext[$i]['exten']    = $this->attrs['cn'][0];
292         $a_ext[$i]['priority'] = 1;
293         $a_ext[$i]['app']      = "Goto";
294         $a_ext[$i]['appdata']  = $num."|1";
295         $i ++ ; 
296         $a_ext[$i]['context']  = 'GOsa';
297         $a_ext[$i]['exten']    = $num;
298         $a_ext[$i]['priority'] = 1;
299         $a_ext[$i]['app']      = "Wait";
300         $a_ext[$i]['appdata']  = "2";
301         $i ++ ; 
302         $a_ext[$i]['context']  = 'GOsa';
303         $a_ext[$i]['exten']    = $num;
304         $a_ext[$i]['priority'] = 2;
305         $a_ext[$i]['app']      = "Set";
306         $a_ext[$i]['appdata']  = "LANGUAGE|".$this->goFonQueueLanguage;
307         $i ++ ; 
308         $a_ext[$i]['context']  = 'GOsa';
309         $a_ext[$i]['exten']    = $num;
310         $a_ext[$i]['priority'] = 3;
311         $a_ext[$i]['app']      = "Playback";
312         $a_ext[$i]['appdata']  = $this->goFonWelcomeMusic;
313         $i ++ ; 
314         $a_ext[$i]['context']  = 'GOsa';
315         $a_ext[$i]['exten']    = $num;
316         $a_ext[$i]['priority'] = 4;
317         $a_ext[$i]['app']      = "SetCIDName";
318         $a_ext[$i]['appdata']  = $this->attrs['description'][0];
319         $i ++ ; 
320         $a_ext[$i]['context']  = 'GOsa';
321         $a_ext[$i]['exten']    = $num;
322         $a_ext[$i]['priority'] = 5;
323         $a_ext[$i]['app']      = "Queue";
324         $a_ext[$i]['appdata']  =  $this->attrs['cn'][0].
325           "|".
326           $this->goFonDialOption_t.
327           $this->goFonDialOption_T.
328           $this->goFonDialOption_h.
329           $this->goFonDialOption_H.
330           "|".  // Optionalurl egal
331           "|".  // announceoverride
332           "|";  // Timeout
334 //        $i ++ ; 
335 //        $a_ext[$i]['context']  = 'GOsa';
336 //        $a_ext[$i]['exten']    = $num;
337 //        $a_ext[$i]['priority'] = 6;
338 //        $a_ext[$i]['app']      = "SetVar";
339 //        $a_ext[$i]['appdata']  = "Queue_Prio=".$prio;
340         $i ++ ;
342         /* Generate Priority Entry */
343         $queue["announce"]              = "";
344         $queue["monitor_join"]          = "";
345         $queue["monitor_format"]        = "";
346         $queue["queue_holdtime"]        = $this->goFonQueueAnnounce;
347         $queue["queue_lessthan"]        = "";   
348         $queue["announce_round_seconds"]= "";   
349         $queue["retry"]                 = $this->goFonQueueRetry;
350         $queue["wrapuptime"]            = "";
351         $queue["servicelevel"]          = "";
352         $queue["joinempty"]             = "";
353         $queue["leavewhenempty"]        = "";   
354         $queue["eventmemberstatus"]     = "";
355         $queue["eventwhencalled"]       = "";
356         $queue["reportholdtime"]        = "";
357         $queue["memberdelay"]           = "";
358         $queue["weight"]                = "";
359         $queue["timeoutrestart"]        = "";
361         $queue["context"]               = "default";
362         $queue["name"]                  = $this->attrs['cn'][0];  
363         $queue["timeout"]               = $this->goFonTimeOut; 
364         $queue["maxlen"]                = $this->goFonMaxLen;
365         $queue["strategy" ]             = $this->goFonQueueStrategy;
366         $queue["queue_thankyou"]        = $this->goFonQueueThankYou;   
367         $queue["queue_reporthold"]      = $this->goFonQueueReportHold; 
368         $queue["announce_frequency"]    = $this->goFonAnnounceFrequency;
369         $queue["queue_youarenext"]      = $this->goFonQueueYouAreNext;   
370         $queue["queue_thereare"]        = $this->goFonQueueThereAre;   
371         $queue["queue_callswaiting"]    = $this->goFonQueueCallsWaiting;
372         $queue["queue_minutes"]         = $this->goFonQueueMinutes;
373         $queue["queue_seconds"]         = $this->goFonQueueSeconds;   
374         $queue["announce_holdtime"]     = $this->goFonQueueAnnounceHoldtime;   
375         $queue["musiconhold"]           = $this->goFonMusiconHold;
377         $i++;
378       }
380       /* Parse and Add Extension entries */
381       foreach($a_ext as $ext){
382         $entries = "";
383         $values  = "";
384         foreach($ext as $attr => $val){
385           $entries.= "`".$attr."`,";
386           $values .= "'".$val."',";
387         }
388         $values  = preg_replace("/,$/","",$values);
389         $entries = preg_replace("/,$/","",$entries );
390         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
391       }
394       /* Parse and Add Queue */
395       $entries = "";
396       $values  = "";
397       foreach($queue as $attr=>$val){
398         if($val == "") continue;
399         $entries.= "`".$attr."`,";
400         $values .= "'".$val."',";
401       }
402       $values  = preg_replace("/,$/","",$values);
403       $entries = preg_replace("/,$/","",$entries );
404       $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_TABLE']." (".$entries.") VALUES (".$values.")";
406       foreach($SQL as $query){
407          if(!mysql_query($query)){
408           gosa_log(mysql_error());
409           print_red(mysql_error());
410           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
411         }
412       }
414     }
415     return(false);
416   }
420  /* This function checks if the given phonenumbers are available or already in use*/
422   function is_number_used()
423   {
424     $ldap= $this->config->get_ldap_link();
425     $ldap->cd($this->config->current['BASE']);
426     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue))", array("telephoneNumber","cn","uid"));
427     while($attrs = $ldap->fetch()) {
428       unset($attrs['telephoneNumber']['count']);
429       foreach($attrs['telephoneNumber'] as $tele){
430         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
431         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
432         $numbers[$tele]=$attrs;
433       }
434     }
436     foreach($this->telephoneNumber as $num){
437       if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
438         if(isset($numbers[$num]['uid'][0])){
439           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
440         }else{
441           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
442         }
443       }
444     }
445   }
451   function save_object()
452   {
453     plugin::save_object();  
454     if(isset($_POST['phonenumber'])){
455       foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H","goFonMusiconHold") as $val){
456         if(isset($_POST[$val])){
457           $this->$val = $_POST[$val];
458         }else{
459           $this->$val = false;
460         }
461       }
462     }
464   }
466   function save()
467   {
468     $ldap= $this->config->get_ldap_link();
470     plugin::save();
471     $this->attrs['goFonDialOption'] = "";
472     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
473       $this->attrs['goFonDialOption'].=$this->$val; 
474       unset($this->attrs[$val]); 
475     }
476     $this->generate_mysql_entension_entries(true);
477     if($this->attrs['goFonDialOption']=="") $this->attrs['goFonDialOption']=array();
479     /* Save data to LDAP */
480     $ldap->cd($this->dn);
481     $ldap->modify($this->attrs);
483     show_ldap_error($ldap->get_error());
485     /* Optionally execute a command after we're done */
486     if ($this->initially_was_account == $this->is_account){
487       if ($this->is_modified){
488         $this->handle_post_events("mofify");
489       }
490     } else {
491       $this->handle_post_events("add");
492     }
493   }
496   /* remove object from parent */
497   function remove_from_parent()
498   {
499     $SQL = array();
501     // Get Configuration for Mysql database Server
502     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
503     $s_parameter  ="";
505     // Connect to DB server
506     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
508     // Check if we are  connected correctly
509     if(!$r_con){
510       gosa_log(mysql_error());
511       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
512             $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
513     }
515     // Select database for Extensions
516     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
518     // Test if we have the database selected correctly
519     if(!$db){
520       gosa_log(mysql_error());
521       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
522     }
524     $tmp = array_flip($this->attributes);
525     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
526       unset($this->$val);
527       unset($this->attrs[$val]);
528       unset($tmp[$val]);
529     }   
530     foreach(array_flip($tmp) as $key => $val){
531       $tmp2[]=$val;
532     } 
533     $this->attributes = $tmp2;
535     $i = 0;
536     $prio = 11;
538     if(empty($this->cn)){
539       $this->cn = $this->parent->by_object['ogroup']->cn;
540       $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
541     }
543     if(empty($this->description)){
544       $this->description = $this->parent->by_object['ogroup']->description;
545       $this->attrs['description'][0] = $this->parent->by_object['ogroup']->description;
546     }
548     // Delete old Entries
549     $delete = array();
550     foreach($this->old_phone_numbers as $phone){
551       $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
552     }
553     $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
554     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']." WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
555     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
557     /* Perform queries to delte old entries */
558     foreach($delete as $query){
559       if(!mysql_query($query)){
560         gosa_log(mysql_error());
561         return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
562       }
563     }
567     /* Cancel if there's nothing to do here */
568     if (!$this->initially_was_account){
569       return;
570     }
572     /* include global link_info */
573     $ldap= $this->config->get_ldap_link();
575     /* Remove and write to LDAP */
576     plugin::remove_from_parent();
578     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
579         $this->attributes, "Save");
580     $ldap->cd($this->dn);
581     $ldap->modify($this->attrs);
582     show_ldap_error($ldap->get_error());
583   }
587 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
588 ?>