Code

78950fd286f64b2b82b148cc4f7cd07273b937ee
[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   ="90";
13   var $goFonDialOption_t        ="t";
14   var $goFonDialOption_T        ="T";
15   var $goFonDialOption_h        ="h";
16   var $goFonAnnounce            ="";
17   var $goFonDialOption_H        ="H";
18   var $goFonMusiconHold         ="vm-for";
19   var $goFonWelcomeMusic        ="random";
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 $goFonLanguage            ="queue-holdtime";
28   var $goFonStrategy            ="rrmemory";
29   var $goFonAnnounceHoldtime    ="yes";
30   var $telephoneNumber          =array();
31   var $goFonQueueMember         =array(); 
32   var $goFonDialOption          ="tThH";
34   var $old_phone_numbers        =array();
36   /* attribute list for save action */
37   var $attributes= array( "goFonTimeOut","goFonMaxLen","goFonAnnounceFrequency","goFonDialOption_t","goFonDialOption_T","goFonDialOption_h",
38       "goFonDialOption_H","goFonMusiconHold","goFonWelcomeMusic","goFonQueueReportHold","goFonQueueYouAreNext",
39       "goFonQueueThereAre","goFonQueueCallsWaiting","goFonQueueThankYou","goFonQueueMinutes","goFonQueueSeconds",
40       "telephoneNumber","goFonLanguage","goFonStrategy","goFonAnnounceHoldtime","goFonAnnounce","goFonDialOption");
41   /* ObjectClass */
42   var $objectclasses= array("goFonQueue");
44   function phonequeue ($config, $dn= NULL)
45   {
46     plugin::plugin($config, $dn);
48     /* Include config object */
49     $this->config= $config;
51     /* Save initial account state */
52     $this->initially_was_account= $this->is_account;
54     if(isset($this->attrs['telephoneNumber'])){
55       $this->telephoneNumber=$this->attrs['telephoneNumber'];
56       unset($this->telephoneNumber['count']); 
57     }
60     for($i = 0; $i < strlen($this->goFonDialOption); $i++){
61       $name = "goFonDialOption_".$this->goFonDialOption[$i];
62       $this->$name=$this->goFonDialOption[$i];
63     }
64     
65     $this->old_phone_numbers = $this->telephoneNumber;
66   }
69   function execute()
70   {
71     /* Do we need to flip is_account state? */
72     if (isset($_POST['modify_state'])){
73       $this->is_account= !$this->is_account;
74     }
76     /* Show tab dialog headers */
77     if ($this->parent != NULL){
78       if ($this->is_account){
79         $display= $this->show_header(_("Remove the phone queue from this Account"),
80             _("Phone queue is enabled for this group. You can disable it by clicking below."));
81       } else {
82         $display= $this->show_header(_("Create phone queue"), _("For this group the phone queues are disabled. You can enable them by clicking below."));
83         return ($display);
84       }
85     }
87     /* Add queue number */ 
88     if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
89       if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
90         $this->telephoneNumber[]=$_POST['phonenumber'];
91       }
92     }
94     /* Delete queue number */ 
95     if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
96       unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
97     }
98   
99     $tmp = array();
100     foreach($this->telephoneNumber as $val){
101       if(!empty($val)){
102         $tmp[]= $val;
103       }
104     }  
105     $this->telephoneNumber=$tmp;
107     /* queue number up */ 
108     if(isset($_POST['up_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
109       if($_POST['goFonQueueNumber_List']>0){
110         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
111         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1];
112         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]    = $down; 
113         $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1]  = $up;
114       }
115     }
117     /* Queuenumber down */ 
118     if(isset($_POST['down_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
119       if(isset($this->telephoneNumber[($_POST['goFonQueueNumber_List']+1)])){
120         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1];
121         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
122         $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1]    = $down; 
123         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]  = $up;
124       }
125     }
128     $smarty= get_smarty();
130     $smarty->assign("goFonLanguageOptions",array('de'=>_('Germany'),'ur'=>_('Uruguai')));
131     $smarty->assign("goFonAnnounceHoldtimeOptions",array('no'=>_("No"),'yes'=>_('Yes')));
132     $smarty->assign("goFonStrategyOptions",array('ringall'    =>_("ring all available channels until one answers"),
133           'roundrobin' =>_("take turns ringing each available interface"),
134           'leastrecent'=>_("ring interface which was least recently called by this queue"),
135           'fewestcalls'=>_("ring the one with fewest completed calls from this queue"),
136           'random'     =>_("ring random interface"),
137           'rrmemory'   =>_("round robin with memory, remember where we left off last ring pass")));
139     foreach($this->attributes as $key => $val){
140       $smarty->assign($val,$this->$val);  
142       if($this->$val == false){
143         $smarty->assign($val."CHK","");
144       }else{
145         $smarty->assign($val."CHK"," checked ");
146       }
148       if(chkacl($this->acl,$key)==""){
149         $smarty->assign($val."ACL","");
150       }else{
151         $smarty->assign($val."ACL"," disabled ");
152       }
153     }
154     return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE)));
155   }
158   /* Check formular input */
159   function check()
160   {
161     $message= array();
162   //  if(empty($this->description)){
163   //    $message[] = _("You must specify a description for this queue");
164   //  }
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->goFonMaxLen))||(empty($this->goFonMaxLen)))){
178       $message[] = _("Queue length must be numeric");
179     }
180     if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
181       $message[] = _("Announce frequency must be numeric");
182     }
183     if(count($this->telephoneNumber)==0){
184       $message[] = _("There must be least one queue number defined.");
185     }
187     return $message;
188   }
192   function generate_mysql_entension_entries($save = false)
193   {
195     $SQL = array();
196  
197     // Get Configuration for Mysql database Server
198     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
199     $s_parameter  ="";
201     // Connect to DB server
202     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
204     // Check if we are  connected correctly
205     if(!$r_con){
206       gosa_log(mysql_error());
207       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
208           $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
209     }
211     // Select database for Extensions
212     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
214     // Test if we have the database selected correctly
215     if(!$db){
216       gosa_log(mysql_error());
217       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
218     }
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       if(empty($this->description)){
232         $this->description = $this->parent->by_object['ogroup']->description;
233         $this->attrs['description'][0] = $this->parent->by_object['ogroup']->description;
234       }
235       
236       // Delete old Entries 
237       $delete = array();
238       if(is_array($this->old_phone_numbers)){
239         foreach($this->old_phone_numbers as $phone){
240           $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
241         }
242       }
243       $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
244       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']."  WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
245       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
247       /* Perform queries to delte old entries */
248       foreach($delete as $query){
249         if(!mysql_query($query)){
250           gosa_log(mysql_error());
251           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
252         }
253       }
255       /* Append new Member for this queue */ 
256       $i = 0;
257       foreach($this->parent->by_object['ogroup']->memberList as $member){
258         if(in_array("goFonAccount",$member['objectClass'])){
259           $i ++ ;
260           $queueuser[$i]['queue_name']  = $this->attrs['cn'][0]; 
261           $queueuser[$i]['interface']   = "SIP/".$member['uid'][0]; 
262           $queueuser[$i]['penalty']     = 1; 
263         }
264       }
266       /* Parse and Add members to query Array */
267       foreach($queueuser as $user){
268         $entries = "";
269         $values  = "";
270         foreach($user as $attr => $val){
271           $entries.= "`".$attr."`,"; 
272           $values .= "'".$val."',";
273         }
274         $values  = preg_replace("/,$/","",$values);
275         $entries = preg_replace("/,$/","",$entries );
276         
277         $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")"; 
278       }
279       
281       /* generate Extension entries, with priority  */
282       $i = 0;
283       foreach($this->telephoneNumber as $num){
284         
285         $prio --;
286         $a_ext[$i]['context']  = 'GOsa';
287         $a_ext[$i]['exten']    = $this->attrs['cn'][0];
288         $a_ext[$i]['priority'] = 1;
289         $a_ext[$i]['app']      = "Goto";
290         $a_ext[$i]['appdata']  = $num."|1";
291         $i ++ ; 
292         $a_ext[$i]['context']  = 'GOsa';
293         $a_ext[$i]['exten']    = $num;
294         $a_ext[$i]['priority'] = 1;
295         $a_ext[$i]['app']      = "Wait";
296         $a_ext[$i]['appdata']  = "2";
297         $i ++ ; 
298         $a_ext[$i]['context']  = 'GOsa';
299         $a_ext[$i]['exten']    = $num;
300         $a_ext[$i]['priority'] = 2;
301         $a_ext[$i]['app']      = "Set";
302         $a_ext[$i]['appdata']  = "LANGUAGE|".$this->goFonLanguage;
303         $i ++ ; 
304         $a_ext[$i]['context']  = 'GOsa';
305         $a_ext[$i]['exten']    = $num;
306         $a_ext[$i]['priority'] = 3;
307         $a_ext[$i]['app']      = "Playback";
308         $a_ext[$i]['appdata']  = $this->goFonWelcomeMusic;
309         $i ++ ; 
310         $a_ext[$i]['context']  = 'GOsa';
311         $a_ext[$i]['exten']    = $num;
312         $a_ext[$i]['priority'] = 4;
313         $a_ext[$i]['app']      = "SetCIDName";
314         $a_ext[$i]['appdata']  = $this->attrs['description'][0];
315         $i ++ ; 
316         $a_ext[$i]['context']  = 'GOsa';
317         $a_ext[$i]['exten']    = $num;
318         $a_ext[$i]['priority'] = 5;
319         $a_ext[$i]['app']      = "Queue";
320         $a_ext[$i]['appdata']  =  $this->attrs['cn'][0].
321           "|".
322           $this->goFonDialOption_t.
323           $this->goFonDialOption_T.
324           $this->goFonDialOption_h.
325           $this->goFonDialOption_H.
326           "|".  // Optionalurl egal
327           "|".  // announceoverride
328           "|".  // Timeout
329           $this->goFonTimeOut;     
331         $i ++ ; 
332         $a_ext[$i]['context']  = 'GOsa';
333         $a_ext[$i]['exten']    = $num;
334         $a_ext[$i]['priority'] = 6;
335         $a_ext[$i]['app']      = "SetVar";
336         $a_ext[$i]['appdata']  = "Queue_Prio=".$prio;
337         $i ++ ;
339         /* Generate Priority Entry */
340         $queue["announce"]              = "";
341         $queue["monitor_join"]          = "";
342         $queue["monitor_format"]        = "";
343         $queue["queue_holdtime"]        = $this->goFonAnnounce;
344         $queue["queue_lessthan"]        = "";   
345         $queue["announce_round_seconds"]= "";   
346         $queue["retry"]                 = "";
347         $queue["wrapuptime"]            = "";
348         $queue["servicelevel"]          = "";
349         $queue["joinempty"]             = "";
350         $queue["leavewhenempty"]        = "";   
351         $queue["eventmemberstatus"]     = "";
352         $queue["eventwhencalled"]       = "";
353         $queue["reportholdtime"]        = "";
354         $queue["memberdelay"]           = "";
355         $queue["weight"]                = "";
356         $queue["timeoutrestart"]        = "";
358         $queue["context"]               = "default";
359         $queue["name"]                  = $this->attrs['cn'][0];  
360         $queue["timeout"]               = $this->goFonTimeOut;
361         $queue["maxlen"]                = $this->goFonMaxLen;
362         $queue["strategy" ]             = $this->goFonStrategy;
363         $queue["queue_thankyou"]        = $this->goFonQueueThankYou;   
364         $queue["queue_reporthold"]      = $this->goFonQueueReportHold; 
365         $queue["announce_frequency"]    = $this->goFonAnnounceFrequency;
366         $queue["queue_youarenext"]      = $this->goFonQueueYouAreNext;   
367         $queue["queue_thereare"]        = $this->goFonQueueThereAre;   
368         $queue["queue_callswaiting"]    = $this->goFonQueueCallsWaiting;
369         $queue["queue_minutes"]         = $this->goFonQueueMinutes;
370         $queue["queue_seconds"]         = $this->goFonQueueSeconds;   
371         $queue["announce_holdtime"]     = $this->goFonAnnounceHoldtime;   
372         $queue["musiconhold"]           = $this->goFonMusiconHold;
374         $i++;
375       }
377       /* Parse and Add Extension entries */
378       foreach($a_ext as $ext){
379         $entries = "";
380         $values  = "";
381         foreach($ext as $attr => $val){
382           $entries.= "`".$attr."`,";
383           $values .= "'".$val."',";
384         }
385         $values  = preg_replace("/,$/","",$values);
386         $entries = preg_replace("/,$/","",$entries );
387         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
388       }
391       /* Parse and Add Queue */
392       $entries = "";
393       $values  = "";
394       foreach($queue as $attr=>$val){
395         if($val == "") continue;
396         $entries.= "`".$attr."`,";
397         $values .= "'".$val."',";
398       }
399       $values  = preg_replace("/,$/","",$values);
400       $entries = preg_replace("/,$/","",$entries );
401       $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_TABLE']." (".$entries.") VALUES (".$values.")";
403       foreach($SQL as $query){
404          if(!mysql_query($query)){
405           gosa_log(mysql_error());
406           print_red(mysql_error());
407           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
408         }
409       }
411     }
412     return(false);
413   }
417  /* This function checks if the given phonenumbers are available or already in use*/
419   function is_number_used()
420   {
421     $ldap= $this->config->get_ldap_link();
422     $ldap->cd($this->config->current['BASE']);
423     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue))", array("telephoneNumber","cn","uid"));
424     while($attrs = $ldap->fetch()) {
425       unset($attrs['telephoneNumber']['count']);
426       foreach($attrs['telephoneNumber'] as $tele){
427         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
428         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
429         $numbers[$tele]=$attrs;
430       }
431     }
433     foreach($this->telephoneNumber as $num){
434       if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
435         if(isset($numbers[$num]['uid'][0])){
436           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
437         }else{
438           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
439         }
440       }
441     }
442   }
448   function save_object()
449   {
450     plugin::save_object();  
451     if(isset($_POST['phonenumber'])){
452       foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H","goFonMusiconHold") as $val){
453         if(isset($_POST[$val])){
454           $this->$val = $_POST[$val];
455         }else{
456           $this->$val = false;
457         }
458       }
459     }
461   }
463   function save()
464   {
465     $ldap= $this->config->get_ldap_link();
467     plugin::save();
468     $this->attrs['goFonDialOption'] = "";
469     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
470       $this->attrs['goFonDialOption'].=$this->$val; 
471       unset($this->attrs[$val]); 
472     }
473     $this->generate_mysql_entension_entries(true);
474     if($this->attrs['goFonDialOption']=="") $this->attrs['goFonDialOption']=array();
476     /* Save data to LDAP */
477     $ldap->cd($this->dn);
478     $ldap->modify($this->attrs);
480     show_ldap_error($ldap->get_error());
482     /* Optionally execute a command after we're done */
483     if ($this->initially_was_account == $this->is_account){
484       if ($this->is_modified){
485         $this->handle_post_events("mofify");
486       }
487     } else {
488       $this->handle_post_events("add");
489     }
490   }
493   /* remove object from parent */
494   function remove_from_parent()
495   {
496     $SQL = array();
498     // Get Configuration for Mysql database Server
499     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
500     $s_parameter  ="";
502     // Connect to DB server
503     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
505     // Check if we are  connected correctly
506     if(!$r_con){
507       gosa_log(mysql_error());
508       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
509             $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
510     }
512     // Select database for Extensions
513     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
515     // Test if we have the database selected correctly
516     if(!$db){
517       gosa_log(mysql_error());
518       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
519     }
522     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
523       unset($this->$val);
524       unset($this->attrs[$val]);
525     }    
527     $i = 0;
528     $prio = 11;
530     if(empty($this->cn)){
531       $this->cn = $this->parent->by_object['ogroup']->cn;
532       $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
533     }
535     if(empty($this->description)){
536       $this->description = $this->parent->by_object['ogroup']->description;
537       $this->attrs['description'][0] = $this->parent->by_object['ogroup']->description;
538     }
540     // Delete old Entries
541     $delete = array();
542     foreach($this->old_phone_numbers as $phone){
543       $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
544     }
545     $delete[]=    "DELETE FROM ".$a_SETUP['EXT_TABLE']."    WHERE exten='".$this->attrs['cn'][0]."';\n";
546     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']." WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
547     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
549     /* Perform queries to delte old entries */
550     foreach($delete as $query){
551       if(!mysql_query($query)){
552         gosa_log(mysql_error());
553         return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
554       }
555     }
559     /* Cancel if there's nothing to do here */
560     if (!$this->initially_was_account){
561       return;
562     }
564     /* include global link_info */
565     $ldap= $this->config->get_ldap_link();
567     /* Remove and write to LDAP */
568     plugin::remove_from_parent();
570     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
571         $this->attributes, "Save");
572     $ldap->cd($this->dn);
573     $ldap->modify($this->attrs);
574     show_ldap_error($ldap->get_error());
575   }
579 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
580 ?>