05cd9278073f7ea427e0754c1ab33816425f9ef2
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 print_red(sprintf(_("The specified home server '%s' is not available in GOsa server configuration. Saving this account will create a new entry on the server '%s'. Use cancel if you do not want to create a new entry while ignoring old accounts."), preg_replace("/,/",", ",$this->goFonHomeServer), preg_replace("/,/",", ",$this->goFonHomeServers[0]['DN'])));
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(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
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(_("The MySQL home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
159 $cfg_Current['SERVER'],$cfg_Current['LOGIN']));
160 }
161 $db_current = @mysql_select_db($cfg_Current['DB'],$r_current);
162 if(!$db_current){
163 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_current));
164 mysql_close($r_current);
165 return( sprintf(_("Can't select database '%s' on home server '%s'."),$cfg_Current['DB'],$cfg_Current['SERVER']));
166 }
168 /********************
169 * Check init home server
170 ********************/
172 if($this->initially_was_account){
173 $cfg_Init = $this->goFonHomeServers[$this->init_HomeServer] ;
174 $r_init = @mysql_pconnect($cfg_Init['SERVER'],$cfg_Init['LOGIN'],$cfg_Init['PASSWORD']);
175 if(!$r_init){
176 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_init));
177 return(sprintf(_("The MySQL initial home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
178 $cfg_Init['SERVER'],$cfg_Init['LOGIN']));
179 }
180 $db_init = @mysql_select_db($cfg_Init['DB'],$r_init);
181 if(!$db_init){
182 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_init));
183 mysql_close($r_init);
184 return( sprintf(_("Can't select database '%s' on initial home server '%s'."),$cfg_Init['DB'],$cfg_Init['SERVER']));
185 }
186 }
187 }
190 /* Display plugin ui */
191 function execute()
192 {
193 /* Call parent execute */
194 plugin::execute();
196 if($this->is_account && !$this->view_logged){
197 $this->view_logged = TRUE;
198 new log("view","ogroups/".get_class($this),$this->dn);
199 }
201 if(isset($_POST['modify_state'])){
202 if($this->is_account && $this->acl_is_removeable()){
203 $this->is_account= FALSE;
204 }elseif(!$this->is_account && $this->acl_is_createable()){
205 $this->is_account= TRUE;
206 }
207 }
209 /* Show tab dialog headers */
210 if ($this->parent !== NULL){
211 if ($this->is_account){
212 $display= $this->show_disable_header(_("Remove the phone queue from this Account"),
213 _("Phone queue is enabled for this group. You can disable it by clicking below."));
214 } else {
215 $display= $this->show_enable_header(_("Create phone queue"),
216 _("For this group the phone queues are disabled. You can enable them by clicking below."));
217 return ($display);
218 }
219 }
221 if($this->acl_is_writeable("telephoneNumber")){
223 /* Add queue number */
224 if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
225 if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
226 $this->telephoneNumber[]=$_POST['phonenumber'];
227 }
228 }
230 /* Delete queue number */
231 if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
232 unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
233 }
235 /* queue number up */
236 if(isset($_POST['up_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
237 if($_POST['goFonQueueNumber_List']>0){
238 $up = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
239 $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1];
240 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $down;
241 $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1] = $up;
242 }
243 }
245 /* Queuenumber down */
246 if(isset($_POST['down_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
247 if(isset($this->telephoneNumber[($_POST['goFonQueueNumber_List']+1)])){
248 $up = $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1];
249 $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
250 $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1] = $down;
251 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $up;
252 }
253 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $up;
254 }
255 }
257 $tmp = array();
258 foreach($this->telephoneNumber as $val){
259 if(!empty($val)){
260 $tmp[]= $val;
261 }
262 }
263 $this->telephoneNumber=$tmp;
265 /* Assign samrty variables */
266 $smarty= get_smarty();
267 $smarty->assign("goFonQueueLanguageOptions",get_languages(FALSE,TRUE));
268 $smarty->assign("goFonQueueStrategyOptions", $this->goFonQueueStrategyOptions);
270 /* Set acls */
271 $tmp = $this->plInfo();
272 foreach($tmp['plProvidedAcls'] as $name => $translated){
273 $smarty->assign($name."ACL",$this->getacl($name));
274 }
276 foreach($this->attributes as $key => $val){
277 $smarty->assign($val,$this->$val);
278 if($this->$val == false){
279 $smarty->assign($val."CHK","");
280 }else{
281 $smarty->assign($val."CHK"," checked ");
282 }
283 }
285 /* Create array with goFonHomeServer */
286 $tmp = array();
287 foreach($this->goFonHomeServers as $dn => $val){
288 if(!is_numeric($dn)){
289 $tmp[$dn] = $val['SERVER'];
290 }
291 }
292 $smarty->assign("goFonHomeServers",$tmp);
294 return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE,dirname(__FILE__))));
295 }
298 /* Check formular input */
299 function check()
300 {
301 /* Call common method to give check the hook */
302 $message= plugin::check();
303 if(!count($this->goFonHomeServers)){
304 $message[] = _("There must be at least one server with an asterisk database to create a phone queue.");
305 }
306 if(empty($this->goFonHomeServer)){
307 $message[] = _("Please select a valid goFonHomeServer.");
308 }
309 if($this->is_number_used()){
310 $message[] = $this->is_number_used();
311 }
312 if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
313 $message[] = _("Timeout must be numeric");
314 }
315 if(!((is_numeric($this->goFonQueueRetry))||(empty($this->goFonQueueRetry)))){
316 $message[] = _("Retry must be numeric");
317 }
318 if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
319 $message[] = _("Max queue length must be numeric");
320 }
321 if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
322 $message[] = _("Announce frequency must be numeric");
323 }
324 if(count($this->telephoneNumber)==0){
325 $message[] = _("There must be least one queue number defined.");
326 }
328 /* check if add to database could be successfull */
329 $str = $this->add_to_database();
330 if(!empty($str)){
331 $message[] = $str;
332 }
333 return $message;
334 }
337 /* This function removes the old database entries.
338 * If this entry should be removed or the home server has changed
339 * this function is called to ensure that all old entries will be deleted.
340 */
341 function remove_from_database($save = false)
342 {
343 /* Check if we must remove old entries */
344 if($this->initially_was_account){
346 /* Check if there is at least on server configuration */
347 if(!count($this->goFonHomeServers)){
348 return( _("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). Your settings can't be saved to asterisk database."));
349 }
351 /********************
352 * Get configuration and check it
353 ********************/
355 /* Check if databases are reachable, returns an error string if anything fails */
356 $error_str = $this->check_database_accessibility();
357 if($error_str){
358 return($error_str);
359 }
361 /* Connect to current database to be able to add new entries */
362 $cfg_Current = $this->goFonHomeServers[$this->init_HomeServer] ;
363 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
364 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
366 /* Create sql entries */
367 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
368 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."'; \n";
369 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';\n";
370 foreach($this->old_phone_numbers as $number){
371 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$number."';\n";
372 }
374 /* Execute the queries */
375 if($save){
376 foreach($delete as $sql){
377 $res = @mysql_query($sql,$res_cur);
378 if(!$res){
379 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($r_current));
380 return(_("Error while removing old queue entries from database.").
381 " "._("Please have a look a the gosa logfiles."));
382 }
383 }
384 }
385 }
386 }
389 /* This function handles the database entries for this
390 * queue.
391 * Existing entries will be updated if possible.
392 */
393 function add_to_database($save = false)
394 {
395 /* Check if there is at least on server configuration */
396 if(!count($this->goFonHomeServers)){
397 return( _("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). Your settings can't be saved to asterisk database."));
398 }
400 /********************
401 * Get configuration and check it
402 ********************/
404 /* Check if databases are reachable, returns an error string if anything fails */
405 $error_str = $this->check_database_accessibility();
406 if($error_str){
407 return($error_str);
408 }
410 /* Connect to current database to be able to add new entries */
411 $cfg_Current = $this->goFonHomeServers[$this->goFonHomeServer] ;
412 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
413 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
415 /* Connect to old home server and remove old entries if necessary */
416 if(($this->initially_was_account) && ($this->init_HomeServer != $this->goFonHomeServer)){
417 $str = $this->remove_from_database($save);
418 if(!empty($str)){
419 return($str);;
420 }
421 }
423 /* Ensure that we have the new cn in $this->cn and the old cn in $this->old_cn */
424 $this->cn = $this->parent->by_object['ogroup']->cn;
426 if($save){
428 /*****************
429 * Create queue table entry
430 *****************/
432 /* Check if QUEUE_TABLE entry exists.
433 * If this entry is missing - create it
434 * else update the entry with new values.
435 */
436 $query = "SELECT * FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."';";
437 $res = mysql_query($query,$res_cur);
438 if(!$res){
439 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($res_cur));
440 return(_("Could not detect old queue entry, query failed.")." "._("Please have a look a the gosa logfiles."));
441 }
442 $cnt = mysql_affected_rows($res_cur);
445 /* Create queue table entry
446 * Leave unused options empty.
447 */
448 $queue["announce"] = "";
449 $queue["monitor_join"] = "";
450 $queue["monitor_format"] = "";
451 $queue["announce_round_seconds"]= "";
452 $queue["wrapuptime"] = "";
453 $queue["servicelevel"] = "";
454 $queue["eventmemberstatus"] = "";
455 $queue["eventwhencalled"] = "";
456 $queue["memberdelay"] = "";
457 $queue["weight"] = "";
458 $queue["timeoutrestart"] = "";
460 $queue["queue_holdtime"] = $this->goFonQueueAnnounce;
461 $queue["queue_lessthan"] = $this->goFonQueueLessThan;
462 $queue["retry"] = $this->goFonQueueRetry;
463 $queue["reportholdtime"] = "1";
464 $queue["joinempty"] = "no";
465 $queue["leavewhenempty"] = "yes";
467 $queue["context"] = "default";
468 $queue["name"] = $this->cn;
469 $queue["timeout"] = $this->goFonTimeOut;
470 $queue["maxlen"] = $this->goFonMaxLen;
471 $queue["strategy" ] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
472 $queue["queue_thankyou"] = $this->goFonQueueThankYou;
473 $queue["queue_reporthold"] = $this->goFonQueueReportHold;
474 $queue["announce_frequency"] = $this->goFonAnnounceFrequency;
475 $queue["queue_youarenext"] = $this->goFonQueueYouAreNext;
476 $queue["queue_thereare"] = $this->goFonQueueThereAre;
477 $queue["queue_callswaiting"] = $this->goFonQueueCallsWaiting;
478 $queue["queue_minutes"] = $this->goFonQueueMinutes;
479 $queue["queue_seconds"] = $this->goFonQueueSeconds;
480 $queue["announce_holdtime"] = $this->goFonQueueAnnounceHoldtime;
481 $queue["musiconhold"] = $this->goFonMusiconHold;
484 /* Check if we must create a new queue entry
485 * or if we can update an existing entry
486 * $cnt contains the number of entries matching this cn
487 */
489 /* Create new queue table entry
490 */
491 if($cnt == 0){
493 /* Parse and Add Queue */
494 $entries = "";
495 $values = "";
496 foreach($queue as $attr=>$val){
497 if($val == "") continue;
498 $entries.= "`".$attr."`,";
499 $values .= "'".$val."',";
500 }
501 $values = preg_replace("/,$/","",$values);
502 $entries = preg_replace("/,$/","",$entries );
503 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_TABLE']." (".$entries.") VALUES (".$values.");";
504 }elseif($cnt == 1){
506 /* Update queue table entry
507 */
508 $queue_old = @mysql_fetch_assoc($res);
509 foreach($queue_old as $name => $value){
510 if(isset($queue[$name]) && $queue[$name] == $value){
511 unset($queue[$name]);
512 }
513 }
515 /* Parse and Add Queue */
516 if(count($queue)){
517 $query = "UPDATE ".$cfg_Current['QUEUE_TABLE']." SET ";
518 foreach($queue as $key => $val){
519 $query.= "".$key."='".$val."',";
520 }
521 $query = preg_replace("/,$/","",$query);
522 $query.= " WHERE name='".$this->old_cn."';";
523 $SQL[] = $query;
524 }
525 }else{
526 return(sprintf(_("More than one entry in queue table found, that uses the name ('%s'). Please fix this issue manually first."),$this->cn));
527 }
530 /*****************
531 * Create queue member entries
532 *****************/
534 /* Add the queue member entries
535 * First we must remove all old user entries.
536 * to be able to add a clean set of members.
537 */
538 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->cn."';";
539 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';";
541 /* Append new Member for this queue */
542 $queueuser =array();
543 $i = 0;
544 $parent = $this->parent->by_object['ogroup'];
547 $ldap = $this->config->get_ldap_link();
548 foreach($parent->memberList as $member => $mem_data){
549 $ldap->cat($member,array("goFonHomeServer","objectClass","dn","uid"));
550 if($ldap->count()){
552 $obj = $ldap->fetch();
554 /* Calculate server and account dependencies */
555 $is_acc = in_array("goFonAccount",$obj['objectClass']);
556 $is_home= isset($obj['goFonHomeServer'][0]) && $obj['goFonHomeServer'][0] == $this->goFonHomeServer;
558 /* Append user to list of queue member,
559 * only if user has phoneAccount extension && is on same home server */
560 if($is_acc && $is_home){
561 $i ++ ;
562 $queueuser[$i]['queue_name'] = $this->cn;
563 $queueuser[$i]['interface'] = "SIP/".$obj['uid'][0];
564 $queueuser[$i]['penalty'] = 1;
565 }
566 }
567 }
569 /* Parse and Add members to query Array */
570 if(is_array($queueuser)){
571 foreach($queueuser as $user){
572 $entries = "";
573 $values = "";
574 foreach($user as $attr => $val){
575 $entries.= "`".$attr."`,";
576 $values .= "'".$val."',";
577 }
578 $values = preg_replace("/,$/","",$values);
579 $entries = preg_replace("/,$/","",$entries );
581 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")";
582 }
583 }
586 /*****************
587 * Create extension entries
588 *****************/
590 /* Add the extension entries
591 * First we must remove all old entensions.
592 */
593 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->cn."';\n";
594 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
596 /* Delete old enxtension entries for the old telephone nubmer */
597 if(is_array($this->old_phone_numbers)){
598 foreach($this->old_phone_numbers as $phone){
599 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
600 }
601 }
603 /* Delete enxtension entries for telephone numbers */
604 if(is_array($this->telephoneNumber)){
605 foreach($this->telephoneNumber as $phone){
606 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
607 }
608 }
610 /* Create a extension entry fpr each telephoneNumber */
611 $i_insert_only_once = false;
612 $prio = 11; // This represents the priority for each telephoneNumber
613 foreach($this->telephoneNumber as $num){
615 /* The naming refrences */
616 if($i_insert_only_once == false){
617 $i_insert_only_once = true;
618 $a_ext[$i]['context'] = 'GOsa';
619 $a_ext[$i]['exten'] = $this->cn;
620 $a_ext[$i]['priority'] = 1;
621 $a_ext[$i]['app'] = "Goto";
622 $a_ext[$i]['appdata'] = $num."|1";
623 $i ++ ;
624 }
626 /* If there is currently no user for this queue
627 * Play no service sound file and return to default context.
628 */
629 if(count($queueuser)==0){
630 $a_ext[$i]['context'] = 'GOsa';
631 $a_ext[$i]['exten'] = $num;
632 $a_ext[$i]['priority'] = 1;
633 $a_ext[$i]['app'] = "SetLanguage";
634 $a_ext[$i]['appdata'] = "de";
635 $i ++ ;
637 $a_ext[$i]['context'] = 'GOsa';
638 $a_ext[$i]['exten'] = $num;
639 $a_ext[$i]['priority'] = 2;
640 $a_ext[$i]['app'] = "Playback";
641 $a_ext[$i]['appdata'] = "ss-noservice";
642 $i ++ ;
644 $a_ext[$i]['context'] = 'GOsa';
645 $a_ext[$i]['exten'] = $num;
646 $a_ext[$i]['priority'] = 3;
647 $a_ext[$i]['app'] = "Goto";
648 $a_ext[$i]['appdata'] = "default";
649 $i ++ ;
650 }else{
652 /* Dcrement priority to avoid using same priority twice */
653 $prio --;
655 /* Wait for 2 seconds */
656 $a_ext[$i]['context'] = 'GOsa';
657 $a_ext[$i]['exten'] = $num;
658 $a_ext[$i]['priority'] = 1;
659 $a_ext[$i]['app'] = "Wait";
660 $a_ext[$i]['appdata'] = "2";
661 $i ++ ;
663 /* Set language to queue language */
664 $a_ext[$i]['context'] = 'GOsa';
665 $a_ext[$i]['exten'] = $num;
666 $a_ext[$i]['priority'] = 2;
667 $a_ext[$i]['app'] = "SetLanguage";
668 $a_ext[$i]['appdata'] = $this->goFonQueueLanguage;
669 $i ++ ;
671 /* Play welcome sound file */
672 $a_ext[$i]['context'] = 'GOsa';
673 $a_ext[$i]['exten'] = $num;
674 $a_ext[$i]['priority'] = 3;
675 $a_ext[$i]['app'] = "Playback";
676 $a_ext[$i]['appdata'] = $this->goFonWelcomeMusic;
677 $i ++ ;
679 /* Set CID name */
680 $a_ext[$i]['context'] = 'GOsa';
681 $a_ext[$i]['exten'] = $num;
682 $a_ext[$i]['priority'] = 4;
683 $a_ext[$i]['app'] = "SetCIDName";
684 if(!empty($this->parent->by_object['ogroup']->description)){
685 $a_ext[$i]['appdata'] = $this->parent->by_object['ogroup']->description;
686 }else{
687 $a_ext[$i]['appdata'] = $this->cn." - ".$num;
688 }
689 $i ++ ;
691 /* Set queue priority */
692 $a_ext[$i]['context'] = 'GOsa';
693 $a_ext[$i]['exten'] = $num;
694 $a_ext[$i]['priority'] = 5;
695 $a_ext[$i]['app'] = "SetVar";
696 $a_ext[$i]['appdata'] = "QUEUE_PRIO=".$prio;
697 $i ++ ;
699 /* Open queue */
700 $a_ext[$i]['context'] = 'GOsa';
701 $a_ext[$i]['exten'] = $num;
702 $a_ext[$i]['priority'] = 6;
703 $a_ext[$i]['app'] = "Queue";
704 $a_ext[$i]['appdata'] = $this->cn;
705 "|".
706 $this->goFonDialOptiont.
707 $this->goFonDialOptionT.
708 $this->goFonDialOptionh.
709 $this->goFonDialOptionH.
710 $this->goFonDialOptionr;
711 }
713 $i++;
714 }
716 /* Parse and Add Extension entries */
717 foreach($a_ext as $ext){
718 $entries = "";
719 $values = "";
720 foreach($ext as $attr => $val){
721 $entries.= "`".$attr."`,";
722 $values .= "'".$val."',";
723 }
724 $values = preg_replace("/,$/","",$values);
725 $entries = preg_replace("/,$/","",$entries );
726 $SQL[]="INSERT INTO ".$cfg_Current['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
727 }
729 /* Do all collected mysql queries
730 */
731 foreach($SQL as $query)
732 $res = mysql_query($query,$res_cur);
733 if(!$res){
734 new log("debug","ogroups/".get_class($this),"",array(),@mysql_error($res_cur));
735 return(_("Mysql query failed.")." "._("Please have a look a the gosa logfiles."));
736 }
737 }
738 @mysql_close($r_con);
739 return(false);
740 }
743 /* This function checks if the given phonenumbers
744 * are available or already in use
745 */
746 function is_number_used()
747 {
748 $ldap= $this->config->get_ldap_link();
749 $ldap->cd($this->config->current['BASE']);
750 $ldap->search("(&(& (!(uid=".$this->cn."))
751 (!(cn=".$this->cn.")))
752 (| (objectClass=goFonAccount)
753 (objectClass=goFonQueue)
754 (objectClass=goFonConference)))", array("telephoneNumber","cn","uid"));
755 while($attrs = $ldap->fetch()) {
756 unset($attrs['telephoneNumber']['count']);
757 foreach($attrs['telephoneNumber'] as $tele){
758 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
759 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
760 $numbers[$tele]=$attrs;
761 }
762 }
764 foreach($this->telephoneNumber as $num){
765 if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
766 if(isset($numbers[$num]['uid'][0])){
767 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
768 }else{
769 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
770 }
771 }
772 }
773 }
776 /* Get posted attributes */
777 function save_object()
778 {
779 plugin::save_object();
780 if(isset($_POST['phonenumber'])){
781 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionh","goFonDialOptionr","goFonDialOptionH","goFonMusiconHold") as $val){
782 if(isset($_POST[$val])){
783 $this->$val = $_POST[$val];
784 }else{
785 $this->$val = false;
786 }
787 }
788 if(isset($_POST['goFonQueueAnnounceHoldtime'])){
789 $this->goFonQueueAnnounceHoldtime = "yes";
790 }else{
791 $this->goFonQueueAnnounceHoldtime = false;
792 }
793 }
794 }
797 function save()
798 {
799 $ldap= $this->config->get_ldap_link();
801 plugin::save();
803 /* Create dial option attribute */
804 $this->attrs['goFonDialOption'] = "";
805 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionr","goFonDialOptionh","goFonDialOptionH","cn") as $val){
806 $this->attrs['goFonDialOption'].=$this->$val;
807 unset($this->attrs[$val]);
808 }
809 if(empty($this->attrs['goFonDialOption'])) {
810 $this->attrs['goFonDialOption']=array();
811 }
813 /* Set announce hold time to yes no .. */
814 if($this->goFonQueueAnnounceHoldtime != "no" ){
815 $this->attrs['goFonQueueAnnounceHoldtime'] = "yes";
816 }else{
817 $this->attrs['goFonQueueAnnounceHoldtime'] = "no";
818 }
820 /* Set strategy */
821 $this->attrs['goFonQueueStrategy'] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
823 /* Add database entry, display error and abort if this fails */
824 $str = $this->add_to_database(true);
825 if(!empty($str)){
826 print_red($str);
827 }
829 /* Save data to LDAP */
830 $ldap->cd($this->dn);
831 $this->cleanup();
832 $ldap->modify ($this->attrs);
834 if($this->initially_was_account){
835 new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
836 }else{
837 new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
838 }
840 show_ldap_error($ldap->get_error(), _("Saving phone queue failed"));
842 /* Optionally execute a command after we're done */
843 if ($this->initially_was_account == $this->is_account){
844 if ($this->is_modified){
845 $this->handle_post_events("modify");
846 }
847 } else {
848 $this->handle_post_events("add");
849 }
850 }
853 /* remove object from parent */
854 function remove_from_parent()
855 {
856 /* Cancel if nothing is to do here */
857 if (!$this->initially_was_account){
858 return;
859 }
861 /* Remove database entries,
862 * if fails display errors and abort
863 */
864 $str = $this->remove_from_database(true);
865 if(!empty($str)){
866 print_red($str);
867 return false;
868 }
870 /* Remove all temporary attributes */
871 $tmp = array_flip($this->attributes);
872 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionr","goFonDialOptionh","goFonDialOptionH","cn") as $val){
873 unset($this->$val);
874 unset($this->attrs[$val]);
875 unset($tmp[$val]);
876 }
877 foreach(array_flip($tmp) as $key => $val){
878 $tmp2[]=$val;
879 }
880 $this->attributes = $tmp2;
882 /* include global link_info */
883 $ldap= $this->config->get_ldap_link();
885 /* Remove and write to LDAP */
886 plugin::remove_from_parent();
888 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
889 $ldap->cd($this->dn);
890 $this->cleanup();
891 $ldap->modify ($this->attrs);
892 new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
893 show_ldap_error($ldap->get_error(), _("Removing phone queue failed"));
894 }
897 function getCopyDialog()
898 {
899 $str = "";
900 $str .= _("Phone number");
901 $str .= " <input type='text' name='telephoneNumber' value='".$this->telephoneNumber."'>";
902 return($str);
903 }
906 function saveCopyDialog()
907 {
908 if(isset($_POST['telephoneNumber'])){
909 $this->telephoneNumber = $_POST['telephoneNumber'];
910 }
911 }
914 static function plInfo()
915 {
916 return (array(
917 "plShortName" => _("Phone"),
918 "plDescription" => _("Phone group"),
919 "plSelfModify" => FALSE,
920 "plDepends" => array(),
921 "plPriority" => 3,
922 "plSection" => array("administration"),
923 "plCategory" => array("ogroups"),
924 "plProvidedAcls"=> array(
926 "goFonTimeOut" => _("Timeout"),
927 "goFonMaxLen" => _("Max queue length"),
928 "goFonHomeServer" => _("Home server"),
929 "goFonAnnounceFrequency" => _("Announce frequency"),
930 "goFonDialOptiont" => _("Allow the called user to transfer his call"),
931 "goFonDialOptionT" => _("Allows calling user to transfer call"),
932 "goFonDialOptionh" => _("Allow the called to hangup by pressing *"),
933 "goFonDialOptionr" => _("Ring instead of playing background music"),
934 "goFonDialOptionH" => _("Allows calling to hangup by pressing *"),
936 "goFonMusiconHold" => _("Music on hold"),
937 "goFonWelcomeMusic" => _("Welcome music"),
938 "goFonQueueReportHold" => _("Report hold time"),
939 "goFonQueueYouAreNext" => _("'You are next' sound"),
940 "goFonQueueThereAre" => _("'There are' sound"),
941 "goFonQueueCallsWaiting" => _("'Call waiting' sound"),
942 "goFonQueueThankYou" => _("'Thank you' sound"),
943 "goFonQueueMinutes" => _("'Minutes' sound"),
944 "goFonQueueSeconds" => _("'Seconds' sound"),
945 "goFonQueueLessThan" => _("'Less than' sound"),
946 "telephoneNumber" => _("Queue phone number"),
947 "goFonQueueLanguage" => _("Language"),
948 "goFonQueueStrategy" => _("Method"),
949 "goFonQueueAnnounceHoldtime"=> _("Announce holdtime"),
950 "goFonQueueAnnounce" => _("Announce"),
951 "goFonQueueRetry" => _("Retry"))
952 ));
953 }
955 }
957 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
958 ?>