Code

Queue extension entry modified
[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             =""; 
11   var $goFonMaxLen              ="";
12   var $goFonAnnounceFrequency   ="";
13   var $goFonDialOption_t        ="";
14   var $goFonDialOption_T        ="";
15   var $goFonDialOption_h        ="";
16   var $goFonAnnounce            ="";
17   var $goFonDialOption_H        ="";
18   var $goFonMusiconHold         ="";
19   var $goFonWelcomeMusic        ="";
20   var $goFonQueueReportHold     ="";
21   var $goFonQueueYouAreNext     ="";
22   var $goFonQueueThereAre       ="";
23   var $goFonQueueCallsWaiting   ="";
24   var $goFonQueueThankYou       ="";
25   var $goFonQueueMinutes        =""; 
26   var $goFonQueueSeconds        ="";
27   var $goFonLanguage            ="";
28   var $goFonStrategy            ="";
29   var $goFonAnnounceHoldtime    ="";
30   var $telephoneNumber          =array();
31   var $goFonQueueMember         =array(); 
32   var $goFonDialOption          ="";
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     $this->telephoneNumber=$this->attrs['telephoneNumber'];
55     unset($this->telephoneNumber['count']); 
58     for($i = 0; $i < strlen($this->goFonDialOption); $i++){
59       $name = "goFonDialOption_".$this->goFonDialOption[$i];
60       $this->$name=$this->goFonDialOption[$i];
61     }
62     
63     $this->old_phone_numbers = $this->telephoneNumber;
64   }
67   function execute()
68   {
69     /* Do we need to flip is_account state? */
70     if (isset($_POST['modify_state'])){
71       $this->is_account= !$this->is_account;
72     }
74     /* Show tab dialog headers */
75     if ($this->parent != NULL){
76       if ($this->is_account){
77         $display= $this->show_header(_("Remove the phone queue from this Account"),
78             _("Phone queue is enabled for this group. You can disable it by clicking below."));
79       } else {
80         $display= $this->show_header(_("Create phone queue"), _("For this group the phone queues are disabled. You can enable them by clicking below."));
81         return ($display);
82       }
83     }
85     /* Add queue number */ 
86     if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
87       if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
88         $this->telephoneNumber[]=$_POST['phonenumber'];
89       }
90     }
92     /* Delete queue number */ 
93     if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
94       unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
95     }
96   
97     $tmp = array();
98     foreach($this->telephoneNumber as $val){
99       if(!empty($val)){
100         $tmp[]= $val;
101       }
102     }  
103     $this->telephoneNumber=$tmp;
105     /* queue number up */ 
106     if(isset($_POST['up_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
107       if($_POST['goFonQueueNumber_List']>0){
108         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
109         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1];
110         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]    = $down; 
111         $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1]  = $up;
112       }
113     }
115     /* Queuenumber down */ 
116     if(isset($_POST['down_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
117       if(isset($this->telephoneNumber[($_POST['goFonQueueNumber_List']+1)])){
118         $up   = $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1];
119         $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
120         $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1]    = $down; 
121         $this->telephoneNumber[$_POST['goFonQueueNumber_List']]  = $up;
122       }
123     }
126     $smarty= get_smarty();
128     $smarty->assign("goFonLanguageOptions",array('de'=>_('Germany'),'ur'=>_('Uruguai')));
129     $smarty->assign("goFonAnnounceHoldtimeOptions",array('no'=>_("No"),'yes'=>_('Yes')));
130     $smarty->assign("goFonStrategyOptions",array('ringall'    =>_("ring all available channels until one answers"),
131           'roundrobin' =>_("take turns ringing each available interface"),
132           'leastrecent'=>_("ring interface which was least recently called by this queue"),
133           'fewestcalls'=>_("ring the one with fewest completed calls from this queue"),
134           'random'     =>_("ring random interface"),
135           'rrmemory'   =>_("round robin with memory, remember where we left off last ring pass")));
137     foreach($this->attributes as $key => $val){
138       $smarty->assign($val,$this->$val);  
140       if($this->$val == false){
141         $smarty->assign($val."CHK","");
142       }else{
143         $smarty->assign($val."CHK"," checked ");
144       }
146       if(chkacl($this->acl,$key)==""){
147         $smarty->assign($val."ACL","");
148       }else{
149         $smarty->assign($val."ACL"," disabled ");
150       }
151     }
152     return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE)));
153   }
156   /* Check formular input */
157   function check()
158   {
159     $message= array();
161     if($this->is_number_used()){
162       $message[] = $this->is_number_used();
163     }
165     if($this->generate_mysql_entension_entries()){
166       $message[] = $this->generate_mysql_entension_entries();
167     }
169     if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
170       $message[] = _("Timeout must be numeric");
171     }
172     if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
173       $message[] = _("Queue length must be numeric");
174     }
175     if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
176       $message[] = _("Announce frequency must be numeric");
177     }
178     if(count($this->telephoneNumber)==0){
179       $message[] = _("There must be least one queue number defined.");
180     }
182     return $message;
183   }
187   function generate_mysql_entension_entries($save = false)
188   {
190     $SQL = array();
191  
192     // Get Configuration for Mysql database Server
193     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
194     $s_parameter  ="";
196     // Connect to DB server
197     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
199     // Check if we are  connected correctly
200     if(!$r_con){
201       gosa_log(mysql_error());
202       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
203           $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
204     }
206     // Select database for Extensions
207     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
209     // Test if we have the database selected correctly
210     if(!$db){
211       gosa_log(mysql_error());
212       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
213     }
217     if($save){
218       $i = 0;
219       $prio = 11; 
221       if(empty($this->cn)){
222         $this->cn = $this->parent->by_object['ogroup']->cn;
223         $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
224       }
226       if(empty($this->description)){
227         $this->description = $this->parent->by_object['ogroup']->description;
228         $this->attrs['description'][0] = $this->parent->by_object['ogroup']->description;
229       }
230       
231       // Delete old Entries 
232       $delete = array();
233       foreach($this->old_phone_numbers as $phone){
234         $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
235       }
236       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']." WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
237       $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
239       /* Perform queries to delte old entries */
240       foreach($delete as $query){
241         if(!mysql_query($query)){
242           gosa_log(mysql_error());
243           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
244         }
245       }
247       /* Append new Member for this queue */ 
248       $i = 0;
249       foreach($this->parent->by_object['ogroup']->memberList as $member){
250         if(in_array("goFonAccount",$member['objectClass'])){
251           $i ++ ;
252           $queueuser[$i]['queue_name']  = $this->attrs['cn'][0]; 
253           $queueuser[$i]['interface']   = "SIP/".$member['uid'][0]; 
254           $queueuser[$i]['penalty']     = 1; 
255         }
256       }
258       /* Parse and Add members to query Array */
259       foreach($queueuser as $user){
260         $entries = "";
261         $values  = "";
262         foreach($user as $attr => $val){
263           $entries.= "`".$attr."`,"; 
264           $values .= "'".$val."',";
265         }
266         $values  = preg_replace("/,$/","",$values);
267         $entries = preg_replace("/,$/","",$entries );
268         
269         $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")"; 
270       }
271       
273       /* generate Extension entries, with priority  */
274       $i = 0;
275       foreach($this->telephoneNumber as $num){
276         
277         $prio --;
278         $a_ext[$i]['context']  = 'GOsa';
279         $a_ext[$i]['exten']    = $num;
280         $a_ext[$i]['priority'] = 1;
281         $a_ext[$i]['app']      = "Wait";
282         $a_ext[$i]['appdata']  = "2";
283         $i ++ ; 
284         $a_ext[$i]['context']  = 'GOsa';
285         $a_ext[$i]['exten']    = $num;
286         $a_ext[$i]['priority'] = 2;
287         $a_ext[$i]['app']      = "Set";
288         $a_ext[$i]['appdata']  = "LANGUAGE|".$this->goFonLanguage;
289         $i ++ ; 
290         $a_ext[$i]['context']  = 'GOsa';
291         $a_ext[$i]['exten']    = $num;
292         $a_ext[$i]['priority'] = 3;
293         $a_ext[$i]['app']      = "Playback";
294         $a_ext[$i]['appdata']  = $this->goFonWelcomeMusic;
295         $i ++ ; 
296         $a_ext[$i]['context']  = 'GOsa';
297         $a_ext[$i]['exten']    = $num;
298         $a_ext[$i]['priority'] = 4;
299         $a_ext[$i]['app']      = "SetCIDName";
300         $a_ext[$i]['appdata']  = $this->attrs['description'][0];
301         $i ++ ; 
302         $a_ext[$i]['context']  = 'GOsa';
303         $a_ext[$i]['exten']    = $num;
304         $a_ext[$i]['priority'] = 5;
305         $a_ext[$i]['app']      = "Queue";
306         $a_ext[$i]['appdata']  =  $this->attrs['cn'][0].
307           "|".
308           $this->goFonDialOption_t.
309           $this->goFonDialOption_T.
310           $this->goFonDialOption_h.
311           $this->goFonDialOption_H.
312           "|".  // Optionalurl egal
313           "|".  // announceoverride
314           "|".  // Timeout
315           $this->goFonTimeOut;     
317         $i ++ ; 
318         $a_ext[$i]['context']  = 'GOsa';
319         $a_ext[$i]['exten']    = $num;
320         $a_ext[$i]['priority'] = 6;
321         $a_ext[$i]['app']      = "SetVar";
322         $a_ext[$i]['appdata']  = "Queue_Prio=".$prio;
323         $i ++ ;
325         /* Generate Priority Entry */
326         $queue["announce"]              = "";
327         $queue["monitor_join"]          = "";
328         $queue["monitor_format"]        = "";
329         $queue["queue_holdtime"]        = $this->goFonAnnounce;
330         $queue["queue_lessthan"]        = "";   
331         $queue["announce_round_seconds"]= "";   
332         $queue["retry"]                 = "";
333         $queue["wrapuptime"]            = "";
334         $queue["servicelevel"]          = "";
335         $queue["joinempty"]             = "";
336         $queue["leavewhenempty"]        = "";   
337         $queue["eventmemberstatus"]     = "";
338         $queue["eventwhencalled"]       = "";
339         $queue["reportholdtime"]        = "";
340         $queue["memberdelay"]           = "";
341         $queue["weight"]                = "";
342         $queue["timeoutrestart"]        = "";
344         $queue["context"]               = "default";
345         $queue["name"]                  = $this->attrs['cn'][0];  
346         $queue["timeout"]               = $this->goFonTimeOut;
347         $queue["maxlen"]                = $this->goFonMaxLen;
348         $queue["strategy" ]             = $this->goFonStrategy;
349         $queue["queue_thankyou"]        = $this->goFonQueueThankYou;   
350         $queue["queue_reporthold"]      = $this->goFonQueueReportHold; 
351         $queue["announce_frequency"]    = $this->goFonAnnounceFrequency;
352         $queue["queue_youarenext"]      = $this->goFonQueueYouAreNext;   
353         $queue["queue_thereare"]        = $this->goFonQueueThereAre;   
354         $queue["queue_callswaiting"]    = $this->goFonQueueCallsWaiting;
355         $queue["queue_minutes"]         = $this->goFonQueueMinutes;
356         $queue["queue_seconds"]         = $this->goFonQueueSeconds;   
357         $queue["announce_holdtime"]     = $this->goFonAnnounceHoldtime;   
358         $queue["musiconhold"]           = $this->goFonMusiconHold;
360         $i++;
361       }
363       /* Parse and Add Extension entries */
364       foreach($a_ext as $ext){
365         $entries = "";
366         $values  = "";
367         foreach($ext as $attr => $val){
368           $entries.= "`".$attr."`,";
369           $values .= "'".$val."',";
370         }
371         $values  = preg_replace("/,$/","",$values);
372         $entries = preg_replace("/,$/","",$entries );
373         $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
374       }
377       /* Parse and Add Queue */
378       $entries = "";
379       $values  = "";
380       foreach($queue as $attr=>$val){
381         if($val == "") continue;
382         $entries.= "`".$attr."`,";
383         $values .= "'".$val."',";
384       }
385       $values  = preg_replace("/,$/","",$values);
386       $entries = preg_replace("/,$/","",$entries );
387       $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_TABLE']." (".$entries.") VALUES (".$values.")";
389       foreach($SQL as $query){
390          if(!mysql_query($query)){
391           gosa_log(mysql_error());
392           print_red(mysql_error());
393           return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
394         }
395       }
397     }
398     return(false);
399   }
402   function is_number_used()
403   {
404     $ldap= $this->config->get_ldap_link();
405     $ldap->search("(objectClass=goFonAccount)", array("telephoneNumber","uid","cn"));
406     while($attrs = $ldap->fetch()) {
407       unset($attrs['telephoneNumber']['count']);
408       $usednumber[$attrs['uid'][0]]= $attrs['telephoneNumber'];
409       foreach($attrs['telephoneNumber'] as $tele){
410         $numbers[$tele]=$attrs;
411       }
412     }
415     foreach($this->telephoneNumber as $num){
416       if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->cn)||($numbers[$num]['uid'][0]!=$this->uid))){
417         if(isset($numbers[$num]['uid'][0])){
418           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
419         }else{
420           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
421         }
422       }
423     }
424   }
430   function save_object()
431   {
432     plugin::save_object();  
433     if(isset($_POST['phonenumber'])){
434       foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H","goFonMusiconHold") as $val){
435         if(isset($_POST[$val])){
436           $this->$val = $_POST[$val];
437         }else{
438           $this->$val = false;
439         }
440       }
441     }
443   }
445   function save()
446   {
447     $ldap= $this->config->get_ldap_link();
449     plugin::save();
450     $this->attrs['goFonDialOption'] = "";
451     foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H") as $val){
452       $this->attrs['goFonDialOption'].=$this->$val; 
453       unset($this->attrs[$val]); 
454     }
455     $this->generate_mysql_entension_entries(true);
456     if($this->attrs['goFonDialOption']=="") $this->attrs['goFonDialOption']=array();
458     /* Save data to LDAP */
459     $ldap->cd($this->dn);
460     $ldap->modify($this->attrs);
462     show_ldap_error($ldap->get_error());
464     /* Optionally execute a command after we're done */
465     if ($this->initially_was_account == $this->is_account){
466       if ($this->is_modified){
467         $this->handle_post_events("mofify");
468       }
469     } else {
470       $this->handle_post_events("add");
471     }
472   }
475   /* remove object from parent */
476   function remove_from_parent()
477   {
478     $SQL = array();
480     // Get Configuration for Mysql database Server
481     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
482     $s_parameter  ="";
484     // Connect to DB server
485     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
487     // Check if we are  connected correctly
488     if(!$r_con){
489       gosa_log(mysql_error());
490       return (sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
491             $a_SETUP['SERVER'],$a_SETUP['LOGIN']));
492     }
494     // Select database for Extensions
495     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
497     // Test if we have the database selected correctly
498     if(!$db){
499       gosa_log(mysql_error());
500       return( sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
501     }
503     $i = 0;
504     $prio = 11;
506     if(empty($this->cn)){
507       $this->cn = $this->parent->by_object['ogroup']->cn;
508       $this->attrs['cn'][0] = $this->parent->by_object['ogroup']->cn;
509     }
511     if(empty($this->description)){
512       $this->description = $this->parent->by_object['ogroup']->description;
513       $this->attrs['description'][0] = $this->parent->by_object['ogroup']->description;
514     }
516     // Delete old Entries
517     $delete = array();
518     foreach($this->old_phone_numbers as $phone){
519       $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
520     }
521     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_TABLE']." WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
522     $delete[]=    "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
524     /* Perform queries to delte old entries */
525     foreach($delete as $query){
526       if(!mysql_query($query)){
527         gosa_log(mysql_error());
528         return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
529       }
530     }
534     /* Cancel if there's nothing to do here */
535     if (!$this->initially_was_account){
536       return;
537     }
539     /* include global link_info */
540     $ldap= $this->config->get_ldap_link();
542     /* Remove and write to LDAP */
543     plugin::remove_from_parent();
545     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
546         $this->attributes, "Save");
547     $ldap->cd($this->dn);
548     $ldap->modify($this->attrs);
549     show_ldap_error($ldap->get_error());
550   }
554 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
555 ?>