1 <?php
3 class phonequeue extends plugin
4 {
5 /* plugin specific values */
6 var $mail= "";
7 var $cn= "";
8 var $old_cn ="";
10 var $goFonTimeOut ="20";
11 var $goFonMaxLen ="20"; //
12 var $goFonAnnounceFrequency ="60"; // Annouce Frequency in seconds
13 var $goFonDialOptiont ="";
14 var $goFonDialOptionT ="";
15 var $goFonDialOptionh ="";
16 var $goFonDialOptionr ="";
17 var $goFonDialOptionH ="";
18 var $goFonQueueAnnounce ="gonicus-berlin-welcome";
19 var $goFonMusiconHold ="default";
20 var $goFonWelcomeMusic ="gonicus-berlin-welcome";
21 var $goFonQueueReportHold ="yes";
22 var $goFonQueueYouAreNext ="queue-youarenext";
23 var $goFonQueueThereAre ="queue-thereare";
24 var $goFonQueueCallsWaiting ="queue-callswaiting";
25 var $goFonQueueThankYou ="queue-thankyou";
26 var $goFonQueueMinutes ="queue-minutes";
27 var $goFonQueueSeconds ="queue-seconds";
28 var $goFonQueueLessThan ="queue-lessthan";
29 var $goFonQueueLanguage ="queue-holdtime";
30 var $goFonQueueStrategy ="ringall";
31 var $goFonQueueAnnounceHoldtime="yes";
32 var $telephoneNumber =array();
33 var $goFonQueueMember =array();
34 var $goFonDialOption ="";
35 var $goFonQueueRetry =5;
37 var $goFonQueueStrategyOptions=array();
38 var $goFonQueueStrategyOptionsR=array();
40 var $old_phone_numbers =array();
42 var $goFonHomeServer = "0";
43 var $init_HomeServer = "0";
44 var $goFonHomeServers = array();
46 var $view_logged = FALSE;
48 /* attribute list for save action */
49 var $attributes= array( "goFonTimeOut","goFonMaxLen","goFonAnnounceFrequency","goFonDialOptiont","goFonDialOptionT",
50 "goFonDialOptionh","goFonDialOptionr","cn","goFonHomeServer",
51 "goFonDialOptionH","goFonMusiconHold","goFonWelcomeMusic","goFonQueueReportHold","goFonQueueYouAreNext",
52 "goFonQueueThereAre","goFonQueueCallsWaiting","goFonQueueThankYou","goFonQueueMinutes","goFonQueueSeconds","goFonQueueLessThan",
53 "telephoneNumber","goFonQueueLanguage","goFonQueueStrategy","goFonQueueAnnounceHoldtime","goFonQueueAnnounce","goFonDialOption","goFonQueueRetry");
55 /* ObjectClass */
56 var $objectclasses= array("goFonQueue");
60 /* Pluigin initialization
61 * - Check currently selected and available home server.
62 * - Set default home server if necessary
63 * - Parse phone options flags
64 * - Get phone numbers
65 */
66 function phonequeue (&$config, $dn= NULL)
67 {
68 plugin::plugin($config, $dn);
70 /* Check server configurations
71 * Load all server configuration in $this->goFonHomeServers if available
72 * and use first server as default if necessary.
73 */
74 if(isset($config->data['SERVERS']['FON']) &&
75 count($config->data['SERVERS']['FON']) &&
76 is_callable("mysql_connect")){
78 /* Set available server */
79 $this->goFonHomeServers = $config->data['SERVERS']['FON'];
81 /* Set default server */
82 if($this->dn == "new"){
83 $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
84 }
86 /* Remember inital home server, to be able to remove old entries */
87 $this->init_HomeServer = $this->goFonHomeServer;
89 /* get config */
90 if(!isset($this->goFonHomeServers[$this->goFonHomeServer])){
91 msg_dialog::display(_("Obsolete entry"), sprintf(_("The current home server is not available anymore. It will be moved to '%s' if you save this entry!"), preg_replace("/,/",", ",$this->goFonHomeServers[0]['DN'])), ERROR_DIALOG);
92 $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
93 $this->init_HomeServer = $this->goFonHomeServers[0]['DN'];
94 }
95 $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer];
96 }
99 /* Variable init
100 * Load phone nubmers and parse dial options
101 */
102 if($this->is_account){
103 if(isset($this->attrs['telephoneNumber'])){
104 $this->telephoneNumber=$this->attrs['telephoneNumber'];
105 unset($this->telephoneNumber['count']);
106 }
107 for($i = 0; $i < strlen($this->goFonDialOption); $i++){
108 $name = "goFonDialOption".$this->goFonDialOption[$i];
109 $this->$name=$this->goFonDialOption[$i];
110 }
111 }
113 /* Set Queue announce hold time to true/false */
114 if(preg_match("/no/i",$this->goFonQueueAnnounceHoldtime)){
115 $this->goFonQueueAnnounceHoldtime=false;
116 }else{
117 $this->goFonQueueAnnounceHoldtime=true;
118 }
120 /* Define all available ringdown types */
121 $types= array('ringall' =>_("ring all"),
122 'roundrobin' =>_("round robin"),
123 'leastrecent'=>_("least recently called"),
124 'fewestcalls'=>_("fewest completed calls"),
125 'random' =>_("random"),
126 'rrmemory' =>_("round robin with memory"));
127 $i = 0;
128 foreach($types as $type => $name){
129 $i++;
130 $this->goFonQueueStrategyOptions[$i] =$name;
131 $this->goFonQueueStrategyOptionsR[$i] =$type;
132 $tmp[$type] = $i;
133 }
134 $this->goFonQueueStrategy= $tmp[$this->goFonQueueStrategy];
135 $this->old_cn = $this->cn;
136 $this->old_phone_numbers = $this->telephoneNumber;
137 }
140 /* This function ensures that the selected home server
141 * and the initially selected home server are reachable and accessible
142 */
143 function check_database_accessibility()
144 {
145 /* Check if mysql extension is available */
146 if(!is_callable("mysql_pconnect")){
147 return(sprintf(_("Missing %s PHP extension!"), "mysql"));
148 }
150 /********************
151 * Check currently selected home server
152 ********************/
154 $cfg_Current = $this->goFonHomeServers[$this->goFonHomeServer];
155 $r_current = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
156 if(!$r_current){
157 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_current));
158 return(sprintf(_("Cannot connect to %s database on server '%s'!"), "GOfon", $cfg_Current['SERVER']));
159 }
160 $db_current = @mysql_select_db($cfg_Current['DB'],$r_current);
161 if(!$db_current){
162 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_current));
163 mysql_close($r_current);
164 return(sprintf(_("Cannot select %s database on server '%s'!"), "GOfon", $cfg_Current['SERVER']));
165 }
167 /********************
168 * Check init home server
169 ********************/
171 if($this->initially_was_account){
172 $cfg_Init = $this->goFonHomeServers[$this->init_HomeServer] ;
173 $r_init = @mysql_pconnect($cfg_Init['SERVER'],$cfg_Init['LOGIN'],$cfg_Init['PASSWORD']);
174 if(!$r_init){
175 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_init));
176 return(sprintf(_("Cannot connect to %s database on server '%s'!"), "GOfon", $cfg_Init['SERVER']));
177 }
178 $db_init = @mysql_select_db($cfg_Init['DB'],$r_init);
179 if(!$db_init){
180 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_init));
181 mysql_close($r_init);
182 return(sprintf(_("Cannot select %s database on server '%s'!"), "GOfon", $cfg_Init['SERVER']));
183 }
184 }
185 }
188 /* Display plugin ui */
189 function execute()
190 {
191 /* Call parent execute */
192 plugin::execute();
194 if($this->is_account && !$this->view_logged){
195 $this->view_logged = TRUE;
196 new log("view","ogroups/".get_class($this),$this->dn);
197 }
199 if(isset($_POST['modify_state'])){
200 if($this->is_account && $this->acl_is_removeable()){
201 $this->is_account= FALSE;
202 }elseif(!$this->is_account && $this->acl_is_createable()){
203 $this->is_account= TRUE;
204 }
205 }
207 /* Show tab dialog headers */
208 if ($this->parent !== NULL){
209 if ($this->is_account){
210 $display= $this->show_disable_header(_("Remove the phone queue from this Account"),
211 _("Phone queue is enabled for this group. You can disable it by clicking below."));
212 } else {
213 $display= $this->show_enable_header(_("Create phone queue"),
214 _("For this group the phone queues are disabled. You can enable them by clicking below."));
215 return ($display);
216 }
217 }
219 if($this->acl_is_writeable("telephoneNumber")){
221 /* Add queue number */
222 if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
223 if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
224 $this->telephoneNumber[]=$_POST['phonenumber'];
225 }
226 }
228 /* Delete queue number */
229 if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
230 unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
231 }
233 /* queue number up */
234 if(isset($_POST['up_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
235 if($_POST['goFonQueueNumber_List']>0){
236 $up = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
237 $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1];
238 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $down;
239 $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1] = $up;
240 }
241 }
243 /* Queuenumber down */
244 if(isset($_POST['down_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
245 if(isset($this->telephoneNumber[($_POST['goFonQueueNumber_List']+1)])){
246 $up = $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1];
247 $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
248 $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1] = $down;
249 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $up;
250 }
251 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $up;
252 }
253 }
255 $tmp = array();
256 foreach($this->telephoneNumber as $val){
257 if(!empty($val)){
258 $tmp[]= $val;
259 }
260 }
261 $this->telephoneNumber=$tmp;
263 /* Assign samrty variables */
264 $smarty= get_smarty();
265 $smarty->assign("goFonQueueLanguageOptions",get_languages(FALSE,TRUE));
266 $smarty->assign("goFonQueueStrategyOptions", $this->goFonQueueStrategyOptions);
268 /* Set acls */
269 $tmp = $this->plInfo();
270 foreach($tmp['plProvidedAcls'] as $name => $translated){
271 $smarty->assign($name."ACL",$this->getacl($name));
272 }
274 foreach($this->attributes as $key => $val){
275 $smarty->assign($val,$this->$val);
276 if($this->$val == false){
277 $smarty->assign($val."CHK","");
278 }else{
279 $smarty->assign($val."CHK"," checked ");
280 }
281 }
283 /* Create array with goFonHomeServer */
284 $tmp = array();
285 foreach($this->goFonHomeServers as $dn => $val){
286 if(!is_numeric($dn)){
287 $tmp[$dn] = $val['SERVER'];
288 }
289 }
290 $smarty->assign("goFonHomeServers",$tmp);
292 return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE,dirname(__FILE__))));
293 }
296 /* Check formular input */
297 function check()
298 {
299 /* Call common method to give check the hook */
300 $message= plugin::check();
301 if(!count($this->goFonHomeServers)){
302 $message[] = _("There must be at least one server with an asterisk database to create a phone queue.");
303 }
304 if(empty($this->goFonHomeServer)){
305 $message[] = _("Please select a valid goFonHomeServer.");
306 }
307 if($this->is_number_used()){
308 $message[] = $this->is_number_used();
309 }
310 if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
311 $message[] = _("Timeout must be numeric");
312 }
313 if(!((is_numeric($this->goFonQueueRetry))||(empty($this->goFonQueueRetry)))){
314 $message[] = _("Retry must be numeric");
315 }
316 if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
317 $message[] = _("Max queue length must be numeric");
318 }
319 if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
320 $message[] = _("Announce frequency must be numeric");
321 }
322 if(count($this->telephoneNumber)==0){
323 $message[] = _("There must be least one queue number defined.");
324 }
326 /* check if add to database could be successfull */
327 $str = $this->add_to_database();
328 if(!empty($str)){
329 $message[] = $str;
330 }
331 return $message;
332 }
335 /* This function removes the old database entries.
336 * If this entry should be removed or the home server has changed
337 * this function is called to ensure that all old entries will be deleted.
338 */
339 function remove_from_database($save = false)
340 {
341 /* Check if we must remove old entries */
342 if($this->initially_was_account){
344 /* Check if there is at least on server configuration */
345 if(!count($this->goFonHomeServers)){
346 return( _("There is currently no asterisk server defined!"));
347 }
349 /********************
350 * Get configuration and check it
351 ********************/
353 /* Check if databases are reachable, returns an error string if anything fails */
354 $error_str = $this->check_database_accessibility();
355 if($error_str){
356 return($error_str);
357 }
359 /* Connect to current database to be able to add new entries */
360 $cfg_Current = $this->goFonHomeServers[$this->init_HomeServer] ;
361 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
362 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
364 /* Create sql entries */
365 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
366 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."'; \n";
367 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';\n";
368 foreach($this->old_phone_numbers as $number){
369 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$number."';\n";
370 }
372 /* Execute the queries */
373 if($save){
374 foreach($delete as $sql){
375 $res = @mysql_query($sql,$res_cur);
376 if(!$res){
377 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_current));
378 return(_("Cannot remove queue entries from database!"));
379 }
380 }
381 }
382 }
383 }
386 /* This function handles the database entries for this
387 * queue.
388 * Existing entries will be updated if possible.
389 */
390 function add_to_database($save = false)
391 {
392 /* Check if there is at least on server configuration */
393 if(!count($this->goFonHomeServers)){
394 return( _("There is currently no asterisk server defined!"));
395 }
397 /********************
398 * Get configuration and check it
399 ********************/
401 /* Check if databases are reachable, returns an error string if anything fails */
402 $error_str = $this->check_database_accessibility();
403 if($error_str){
404 return($error_str);
405 }
407 /* Connect to current database to be able to add new entries */
408 $cfg_Current = $this->goFonHomeServers[$this->goFonHomeServer] ;
409 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
410 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
412 /* Connect to old home server and remove old entries if necessary */
413 if(($this->initially_was_account) && ($this->init_HomeServer != $this->goFonHomeServer)){
414 $str = $this->remove_from_database($save);
415 if(!empty($str)){
416 return($str);;
417 }
418 }
420 /* Ensure that we have the new cn in $this->cn and the old cn in $this->old_cn */
421 $this->cn = $this->parent->by_object['ogroup']->cn;
423 if($save){
425 /*****************
426 * Create queue table entry
427 *****************/
429 /* Check if QUEUE_TABLE entry exists.
430 * If this entry is missing - create it
431 * else update the entry with new values.
432 */
433 $query = "SELECT * FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."';";
434 $res = mysql_query($query,$res_cur);
435 if(!$res){
436 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($res_cur));
437 return(_("Cannot find old queue entry!"));
438 }
439 $cnt = mysql_affected_rows($res_cur);
442 /* Create queue table entry
443 * Leave unused options empty.
444 */
445 $queue["announce"] = "";
446 $queue["monitor_join"] = "";
447 $queue["monitor_format"] = "";
448 $queue["announce_round_seconds"]= "";
449 $queue["wrapuptime"] = "";
450 $queue["servicelevel"] = "";
451 $queue["eventmemberstatus"] = "";
452 $queue["eventwhencalled"] = "";
453 $queue["memberdelay"] = "";
454 $queue["weight"] = "";
455 $queue["timeoutrestart"] = "";
457 $queue["queue_holdtime"] = $this->goFonQueueAnnounce;
458 $queue["queue_lessthan"] = $this->goFonQueueLessThan;
459 $queue["retry"] = $this->goFonQueueRetry;
460 $queue["reportholdtime"] = "1";
461 $queue["joinempty"] = "no";
462 $queue["leavewhenempty"] = "yes";
464 $queue["context"] = "default";
465 $queue["name"] = $this->cn;
466 $queue["timeout"] = $this->goFonTimeOut;
467 $queue["maxlen"] = $this->goFonMaxLen;
468 $queue["strategy" ] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
469 $queue["queue_thankyou"] = $this->goFonQueueThankYou;
470 $queue["queue_reporthold"] = $this->goFonQueueReportHold;
471 $queue["announce_frequency"] = $this->goFonAnnounceFrequency;
472 $queue["queue_youarenext"] = $this->goFonQueueYouAreNext;
473 $queue["queue_thereare"] = $this->goFonQueueThereAre;
474 $queue["queue_callswaiting"] = $this->goFonQueueCallsWaiting;
475 $queue["queue_minutes"] = $this->goFonQueueMinutes;
476 $queue["queue_seconds"] = $this->goFonQueueSeconds;
477 $queue["announce_holdtime"] = $this->goFonQueueAnnounceHoldtime;
478 $queue["musiconhold"] = $this->goFonMusiconHold;
481 /* Check if we must create a new queue entry
482 * or if we can update an existing entry
483 * $cnt contains the number of entries matching this cn
484 */
486 /* Create new queue table entry
487 */
488 if($cnt == 0){
490 /* Parse and Add Queue */
491 $entries = "";
492 $values = "";
493 foreach($queue as $attr=>$val){
494 if($val == "") continue;
495 $entries.= "`".$attr."`,";
496 $values .= "'".$val."',";
497 }
498 $values = preg_replace("/,$/","",$values);
499 $entries = preg_replace("/,$/","",$entries );
500 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_TABLE']." (".$entries.") VALUES (".$values.");";
501 }elseif($cnt == 1){
503 /* Update queue table entry
504 */
505 $queue_old = @mysql_fetch_assoc($res);
506 foreach($queue_old as $name => $value){
507 if(isset($queue[$name]) && $queue[$name] == $value){
508 unset($queue[$name]);
509 }
510 }
512 /* Parse and Add Queue */
513 if(count($queue)){
514 $query = "UPDATE ".$cfg_Current['QUEUE_TABLE']." SET ";
515 foreach($queue as $key => $val){
516 $query.= "".$key."='".$val."',";
517 }
518 $query = preg_replace("/,$/","",$query);
519 $query.= " WHERE name='".$this->old_cn."';";
520 $SQL[] = $query;
521 }
522 }else{
523 return(sprintf(_("Found more than one entry named '%s' in queue table. Please inform your system administrator!"),$this->cn));
524 }
527 /*****************
528 * Create queue member entries
529 *****************/
531 /* Add the queue member entries
532 * First we must remove all old user entries.
533 * to be able to add a clean set of members.
534 */
535 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->cn."';";
536 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';";
538 /* Append new Member for this queue */
539 $queueuser =array();
540 $i = 0;
541 $parent = $this->parent->by_object['ogroup'];
544 $ldap = $this->config->get_ldap_link();
545 foreach($parent->memberList as $member => $mem_data){
546 $ldap->cat($member,array("goFonHomeServer","objectClass","dn","uid"));
547 if($ldap->count()){
549 $obj = $ldap->fetch();
551 /* Calculate server and account dependencies */
552 $is_acc = in_array("goFonAccount",$obj['objectClass']);
553 $is_home= isset($obj['goFonHomeServer'][0]) && $obj['goFonHomeServer'][0] == $this->goFonHomeServer;
555 /* Append user to list of queue member,
556 * only if user has phoneAccount extension && is on same home server */
557 if($is_acc && $is_home){
558 $i ++ ;
559 $queueuser[$i]['queue_name'] = $this->cn;
560 $queueuser[$i]['interface'] = "SIP/".$obj['uid'][0];
561 $queueuser[$i]['penalty'] = 1;
562 }
563 }
564 }
566 /* Parse and Add members to query Array */
567 if(is_array($queueuser)){
568 foreach($queueuser as $user){
569 $entries = "";
570 $values = "";
571 foreach($user as $attr => $val){
572 $entries.= "`".$attr."`,";
573 $values .= "'".$val."',";
574 }
575 $values = preg_replace("/,$/","",$values);
576 $entries = preg_replace("/,$/","",$entries );
578 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")";
579 }
580 }
583 /*****************
584 * Create extension entries
585 *****************/
587 /* Add the extension entries
588 * First we must remove all old entensions.
589 */
590 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->cn."';\n";
591 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
593 /* Delete old enxtension entries for the old telephone nubmer */
594 if(is_array($this->old_phone_numbers)){
595 foreach($this->old_phone_numbers as $phone){
596 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
597 }
598 }
600 /* Delete enxtension entries for telephone numbers */
601 if(is_array($this->telephoneNumber)){
602 foreach($this->telephoneNumber as $phone){
603 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
604 }
605 }
607 /* Create a extension entry fpr each telephoneNumber */
608 $i_insert_only_once = false;
609 $prio = 11; // This represents the priority for each telephoneNumber
610 foreach($this->telephoneNumber as $num){
612 /* The naming refrences */
613 if($i_insert_only_once == false){
614 $i_insert_only_once = true;
615 $a_ext[$i]['context'] = 'GOsa';
616 $a_ext[$i]['exten'] = $this->cn;
617 $a_ext[$i]['priority'] = 1;
618 $a_ext[$i]['app'] = "Goto";
619 $a_ext[$i]['appdata'] = $num."|1";
620 $i ++ ;
621 }
623 /* If there is currently no user for this queue
624 * Play no service sound file and return to default context.
625 */
626 if(count($queueuser)==0){
627 $a_ext[$i]['context'] = 'GOsa';
628 $a_ext[$i]['exten'] = $num;
629 $a_ext[$i]['priority'] = 1;
630 $a_ext[$i]['app'] = "SetLanguage";
631 $a_ext[$i]['appdata'] = "de";
632 $i ++ ;
634 $a_ext[$i]['context'] = 'GOsa';
635 $a_ext[$i]['exten'] = $num;
636 $a_ext[$i]['priority'] = 2;
637 $a_ext[$i]['app'] = "Playback";
638 $a_ext[$i]['appdata'] = "ss-noservice";
639 $i ++ ;
641 $a_ext[$i]['context'] = 'GOsa';
642 $a_ext[$i]['exten'] = $num;
643 $a_ext[$i]['priority'] = 3;
644 $a_ext[$i]['app'] = "Goto";
645 $a_ext[$i]['appdata'] = "default";
646 $i ++ ;
647 }else{
649 /* Dcrement priority to avoid using same priority twice */
650 $prio --;
652 /* Wait for 2 seconds */
653 $a_ext[$i]['context'] = 'GOsa';
654 $a_ext[$i]['exten'] = $num;
655 $a_ext[$i]['priority'] = 1;
656 $a_ext[$i]['app'] = "Wait";
657 $a_ext[$i]['appdata'] = "2";
658 $i ++ ;
660 /* Set language to queue language */
661 $a_ext[$i]['context'] = 'GOsa';
662 $a_ext[$i]['exten'] = $num;
663 $a_ext[$i]['priority'] = 2;
664 $a_ext[$i]['app'] = "SetLanguage";
665 $a_ext[$i]['appdata'] = $this->goFonQueueLanguage;
666 $i ++ ;
668 /* Play welcome sound file */
669 $a_ext[$i]['context'] = 'GOsa';
670 $a_ext[$i]['exten'] = $num;
671 $a_ext[$i]['priority'] = 3;
672 $a_ext[$i]['app'] = "Playback";
673 $a_ext[$i]['appdata'] = $this->goFonWelcomeMusic;
674 $i ++ ;
676 /* Set CID name */
677 $a_ext[$i]['context'] = 'GOsa';
678 $a_ext[$i]['exten'] = $num;
679 $a_ext[$i]['priority'] = 4;
680 $a_ext[$i]['app'] = "SetCIDName";
681 if(!empty($this->parent->by_object['ogroup']->description)){
682 $a_ext[$i]['appdata'] = $this->parent->by_object['ogroup']->description;
683 }else{
684 $a_ext[$i]['appdata'] = $this->cn." - ".$num;
685 }
686 $i ++ ;
688 /* Set queue priority */
689 $a_ext[$i]['context'] = 'GOsa';
690 $a_ext[$i]['exten'] = $num;
691 $a_ext[$i]['priority'] = 5;
692 $a_ext[$i]['app'] = "SetVar";
693 $a_ext[$i]['appdata'] = "QUEUE_PRIO=".$prio;
694 $i ++ ;
696 /* Open queue */
697 $a_ext[$i]['context'] = 'GOsa';
698 $a_ext[$i]['exten'] = $num;
699 $a_ext[$i]['priority'] = 6;
700 $a_ext[$i]['app'] = "Queue";
701 $a_ext[$i]['appdata'] = $this->cn;
702 "|".
703 $this->goFonDialOptiont.
704 $this->goFonDialOptionT.
705 $this->goFonDialOptionh.
706 $this->goFonDialOptionH.
707 $this->goFonDialOptionr;
708 }
710 $i++;
711 }
713 /* Parse and Add Extension entries */
714 foreach($a_ext as $ext){
715 $entries = "";
716 $values = "";
717 foreach($ext as $attr => $val){
718 $entries.= "`".$attr."`,";
719 $values .= "'".$val."',";
720 }
721 $values = preg_replace("/,$/","",$values);
722 $entries = preg_replace("/,$/","",$entries );
723 $SQL[]="INSERT INTO ".$cfg_Current['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
724 }
726 /* Do all collected mysql queries
727 */
728 foreach($SQL as $query)
729 $res = mysql_query($query,$res_cur);
730 if(!$res){
731 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($res_cur));
732 return(_("Mysql query failed.")." "._("Please have a look a the gosa logfiles."));
733 }
734 }
735 @mysql_close($r_con);
736 return(false);
737 }
740 /* This function checks if the given phonenumbers
741 * are available or already in use
742 */
743 function is_number_used()
744 {
745 $ldap= $this->config->get_ldap_link();
746 $ldap->cd($this->config->current['BASE']);
747 $ldap->search("(&(& (!(uid=".$this->cn."))
748 (!(cn=".$this->cn.")))
749 (| (objectClass=goFonAccount)
750 (objectClass=goFonQueue)
751 (objectClass=goFonConference)))", array("telephoneNumber","cn","uid"));
752 while($attrs = $ldap->fetch()) {
753 unset($attrs['telephoneNumber']['count']);
754 foreach($attrs['telephoneNumber'] as $tele){
755 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
756 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
757 $numbers[$tele]=$attrs;
758 }
759 }
761 foreach($this->telephoneNumber as $num){
762 if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
763 if(isset($numbers[$num]['uid'][0])){
764 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
765 }else{
766 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
767 }
768 }
769 }
770 }
773 /* Get posted attributes */
774 function save_object()
775 {
776 plugin::save_object();
777 if(isset($_POST['phonenumber'])){
778 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionh","goFonDialOptionr","goFonDialOptionH","goFonMusiconHold") as $val){
779 if(isset($_POST[$val])){
780 $this->$val = $_POST[$val];
781 }else{
782 $this->$val = false;
783 }
784 }
785 if(isset($_POST['goFonQueueAnnounceHoldtime'])){
786 $this->goFonQueueAnnounceHoldtime = "yes";
787 }else{
788 $this->goFonQueueAnnounceHoldtime = false;
789 }
790 }
791 }
794 function save()
795 {
796 $ldap= $this->config->get_ldap_link();
798 plugin::save();
800 /* Create dial option attribute */
801 $this->attrs['goFonDialOption'] = "";
802 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionr","goFonDialOptionh","goFonDialOptionH","cn") as $val){
803 $this->attrs['goFonDialOption'].=$this->$val;
804 unset($this->attrs[$val]);
805 }
806 if(empty($this->attrs['goFonDialOption'])) {
807 $this->attrs['goFonDialOption']=array();
808 }
810 /* Set announce hold time to yes no .. */
811 if($this->goFonQueueAnnounceHoldtime != "no" ){
812 $this->attrs['goFonQueueAnnounceHoldtime'] = "yes";
813 }else{
814 $this->attrs['goFonQueueAnnounceHoldtime'] = "no";
815 }
817 /* Set strategy */
818 $this->attrs['goFonQueueStrategy'] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
820 /* Add database entry, display error and abort if this fails */
821 $str = $this->add_to_database(true);
822 if(!empty($str)){
823 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
824 }
826 /* Save data to LDAP */
827 $ldap->cd($this->dn);
828 $this->cleanup();
829 $ldap->modify ($this->attrs);
831 if($this->initially_was_account){
832 new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
833 }else{
834 new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
835 }
837 if (!$ldap->success()){
838 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
839 }
841 /* Optionally execute a command after we're done */
842 if ($this->initially_was_account == $this->is_account){
843 if ($this->is_modified){
844 $this->handle_post_events("modify");
845 }
846 } else {
847 $this->handle_post_events("add");
848 }
849 }
852 /* remove object from parent */
853 function remove_from_parent()
854 {
855 /* Cancel if nothing is to do here */
856 if (!$this->initially_was_account){
857 return;
858 }
860 /* Remove database entries,
861 * if fails display errors and abort
862 */
863 $str = $this->remove_from_database(true);
864 if(!empty($str)){
865 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
866 return false;
867 }
869 /* Remove all temporary attributes */
870 $tmp = array_flip($this->attributes);
871 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionr","goFonDialOptionh","goFonDialOptionH","cn") as $val){
872 unset($this->$val);
873 unset($this->attrs[$val]);
874 unset($tmp[$val]);
875 }
876 foreach(array_flip($tmp) as $key => $val){
877 $tmp2[]=$val;
878 }
879 $this->attributes = $tmp2;
881 /* include global link_info */
882 $ldap= $this->config->get_ldap_link();
884 /* Remove and write to LDAP */
885 plugin::remove_from_parent();
887 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
888 $ldap->cd($this->dn);
889 $this->cleanup();
890 $ldap->modify ($this->attrs);
891 new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
892 if (!$ldap->success()){
893 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
894 }
895 }
898 function getCopyDialog()
899 {
900 $str = "";
901 $str .= _("Phone number");
902 $str .= " <input type='text' name='telephoneNumber' value='".$this->telephoneNumber."'>";
903 return($str);
904 }
907 function saveCopyDialog()
908 {
909 if(isset($_POST['telephoneNumber'])){
910 $this->telephoneNumber = $_POST['telephoneNumber'];
911 }
912 }
915 static function plInfo()
916 {
917 return (array(
918 "plShortName" => _("Phone"),
919 "plDescription" => _("Phone group"),
920 "plSelfModify" => FALSE,
921 "plDepends" => array(),
922 "plPriority" => 3,
923 "plSection" => array("administration"),
924 "plCategory" => array("ogroups"),
925 "plProvidedAcls"=> array(
927 "goFonTimeOut" => _("Timeout"),
928 "goFonMaxLen" => _("Max queue length"),
929 "goFonHomeServer" => _("Home server"),
930 "goFonAnnounceFrequency" => _("Announce frequency"),
931 "goFonDialOptiont" => _("Allow the called user to transfer his call"),
932 "goFonDialOptionT" => _("Allows calling user to transfer call"),
933 "goFonDialOptionh" => _("Allow the called to hangup by pressing *"),
934 "goFonDialOptionr" => _("Ring instead of playing background music"),
935 "goFonDialOptionH" => _("Allows calling to hangup by pressing *"),
937 "goFonMusiconHold" => _("Music on hold"),
938 "goFonWelcomeMusic" => _("Welcome music"),
939 "goFonQueueReportHold" => _("Report hold time"),
940 "goFonQueueYouAreNext" => _("'You are next' sound"),
941 "goFonQueueThereAre" => _("'There are' sound"),
942 "goFonQueueCallsWaiting" => _("'Call waiting' sound"),
943 "goFonQueueThankYou" => _("'Thank you' sound"),
944 "goFonQueueMinutes" => _("'Minutes' sound"),
945 "goFonQueueSeconds" => _("'Seconds' sound"),
946 "goFonQueueLessThan" => _("'Less than' sound"),
947 "telephoneNumber" => _("Queue phone number"),
948 "goFonQueueLanguage" => _("Language"),
949 "goFonQueueStrategy" => _("Method"),
950 "goFonQueueAnnounceHoldtime"=> _("Announce holdtime"),
951 "goFonQueueAnnounce" => _("Announce"),
952 "goFonQueueRetry" => _("Retry"))
953 ));
954 }
956 }
958 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
959 ?>