4603d726eaef0d7df2ebd9e23936e175cad75a6e
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 $goFonAnnounce ="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 $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 }
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 }
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'=>_('German'),'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[] = _("Retry must be numeric");
176 }
177 if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
178 $message[] = _("Max 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();
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 }
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 );
277 $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")";
278 }
281 /* generate Extension entries, with priority */
282 $i = 0;
283 foreach($this->telephoneNumber as $num){
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
330 // $i ++ ;
331 // $a_ext[$i]['context'] = 'GOsa';
332 // $a_ext[$i]['exten'] = $num;
333 // $a_ext[$i]['priority'] = 6;
334 // $a_ext[$i]['app'] = "SetVar";
335 // $a_ext[$i]['appdata'] = "Queue_Prio=".$prio;
336 $i ++ ;
338 /* Generate Priority Entry */
339 $queue["announce"] = "";
340 $queue["monitor_join"] = "";
341 $queue["monitor_format"] = "";
342 $queue["queue_holdtime"] = $this->goFonAnnounce;
343 $queue["queue_lessthan"] = "";
344 $queue["announce_round_seconds"]= "";
345 $queue["retry"] = $this->goFonTimeOut;
346 $queue["wrapuptime"] = "";
347 $queue["servicelevel"] = "";
348 $queue["joinempty"] = "";
349 $queue["leavewhenempty"] = "";
350 $queue["eventmemberstatus"] = "";
351 $queue["eventwhencalled"] = "";
352 $queue["reportholdtime"] = "";
353 $queue["memberdelay"] = "";
354 $queue["weight"] = "";
355 $queue["timeoutrestart"] = "";
357 $queue["context"] = "default";
358 $queue["name"] = $this->attrs['cn'][0];
359 $queue["timeout"] = 20;
360 $queue["maxlen"] = $this->goFonMaxLen;
361 $queue["strategy" ] = $this->goFonStrategy;
362 $queue["queue_thankyou"] = $this->goFonQueueThankYou;
363 $queue["queue_reporthold"] = $this->goFonQueueReportHold;
364 $queue["announce_frequency"] = $this->goFonAnnounceFrequency;
365 $queue["queue_youarenext"] = $this->goFonQueueYouAreNext;
366 $queue["queue_thereare"] = $this->goFonQueueThereAre;
367 $queue["queue_callswaiting"] = $this->goFonQueueCallsWaiting;
368 $queue["queue_minutes"] = $this->goFonQueueMinutes;
369 $queue["queue_seconds"] = $this->goFonQueueSeconds;
370 $queue["announce_holdtime"] = $this->goFonAnnounceHoldtime;
371 $queue["musiconhold"] = $this->goFonMusiconHold;
373 $i++;
374 }
376 /* Parse and Add Extension entries */
377 foreach($a_ext as $ext){
378 $entries = "";
379 $values = "";
380 foreach($ext as $attr => $val){
381 $entries.= "`".$attr."`,";
382 $values .= "'".$val."',";
383 }
384 $values = preg_replace("/,$/","",$values);
385 $entries = preg_replace("/,$/","",$entries );
386 $SQL[]="INSERT INTO ".$a_SETUP['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
387 }
390 /* Parse and Add Queue */
391 $entries = "";
392 $values = "";
393 foreach($queue as $attr=>$val){
394 if($val == "") continue;
395 $entries.= "`".$attr."`,";
396 $values .= "'".$val."',";
397 }
398 $values = preg_replace("/,$/","",$values);
399 $entries = preg_replace("/,$/","",$entries );
400 $SQL[]="INSERT INTO ".$a_SETUP['QUEUE_TABLE']." (".$entries.") VALUES (".$values.")";
402 foreach($SQL as $query){
403 if(!mysql_query($query)){
404 gosa_log(mysql_error());
405 print_red(mysql_error());
406 return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
407 }
408 }
410 }
411 return(false);
412 }
416 /* This function checks if the given phonenumbers are available or already in use*/
418 function is_number_used()
419 {
420 $ldap= $this->config->get_ldap_link();
421 $ldap->cd($this->config->current['BASE']);
422 $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue))", array("telephoneNumber","cn","uid"));
423 while($attrs = $ldap->fetch()) {
424 unset($attrs['telephoneNumber']['count']);
425 foreach($attrs['telephoneNumber'] as $tele){
426 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
427 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
428 $numbers[$tele]=$attrs;
429 }
430 }
432 foreach($this->telephoneNumber as $num){
433 if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
434 if(isset($numbers[$num]['uid'][0])){
435 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
436 }else{
437 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
438 }
439 }
440 }
441 }
447 function save_object()
448 {
449 plugin::save_object();
450 if(isset($_POST['phonenumber'])){
451 foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_H","goFonMusiconHold") as $val){
452 if(isset($_POST[$val])){
453 $this->$val = $_POST[$val];
454 }else{
455 $this->$val = false;
456 }
457 }
458 }
460 }
462 function save()
463 {
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();
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 if(empty($this->description)){
540 $this->description = $this->parent->by_object['ogroup']->description;
541 $this->attrs['description'][0] = $this->parent->by_object['ogroup']->description;
542 }
544 // Delete old Entries
545 $delete = array();
546 foreach($this->old_phone_numbers as $phone){
547 $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$phone."';\n";
548 }
549 $delete[]= "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->attrs['cn'][0]."';\n";
550 $delete[]= "DELETE FROM ".$a_SETUP['QUEUE_TABLE']." WHERE name=\"".$this->attrs['cn'][0]."\"; \n";
551 $delete[]= "DELETE FROM ".$a_SETUP['QUEUE_MEMBER_TABLE']." WHERE queue_name=\"".$this->attrs['cn'][0]."\";\n";
553 /* Perform queries to delte old entries */
554 foreach($delete as $query){
555 if(!mysql_query($query)){
556 gosa_log(mysql_error());
557 return(mysql_error(). sprintf(_("Can't delete in Database %s, on Server %s."),$a_SETUP['DB'],$a_SETUP['SERVER']));
558 }
559 }
563 /* Cancel if there's nothing to do here */
564 if (!$this->initially_was_account){
565 return;
566 }
568 /* include global link_info */
569 $ldap= $this->config->get_ldap_link();
571 /* Remove and write to LDAP */
572 plugin::remove_from_parent();
574 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
575 $this->attributes, "Save");
576 $ldap->cd($this->dn);
577 $ldap->modify($this->attrs);
578 show_ldap_error($ldap->get_error());
579 }
581 }
583 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
584 ?>