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 $goFonDialOption_t ="";
14 var $goFonDialOption_T ="";
15 var $goFonDialOption_h ="";
16 var $goFonDialOption_r ="";
17 var $goFonQueueAnnounce ="gonicus-berlin-welcome";
18 var $goFonDialOption_H ="";
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";
45 /* attribute list for save action */
46 var $attributes= array( "goFonTimeOut","goFonMaxLen","goFonAnnounceFrequency","goFonDialOption_t","goFonDialOption_T",
47 "goFonDialOption_h","goFonDialOption_r","cn","goFonHomeServer",
48 "goFonDialOption_H","goFonMusiconHold","goFonWelcomeMusic","goFonQueueReportHold","goFonQueueYouAreNext",
49 "goFonQueueThereAre","goFonQueueCallsWaiting","goFonQueueThankYou","goFonQueueMinutes","goFonQueueSeconds","goFonQueueLessThan",
50 "telephoneNumber","goFonQueueLanguage","goFonQueueStrategy","goFonQueueAnnounceHoldtime","goFonQueueAnnounce","goFonDialOption","goFonQueueRetry");
52 /* ObjectClass */
53 var $objectclasses= array("goFonQueue");
57 /* Pluigin initialization
58 * - Check currently selected and available home server.
59 * - Set default home server if necessary
60 * - Parse phone options flags
61 * - Get phone numbers
62 */
63 function phonequeue ($config, $dn= NULL)
64 {
65 plugin::plugin($config, $dn);
67 /* Check server configurations
68 * Load all server configuration in $this->goFonHomeServers if available
69 * and use first server as default if necessary.
70 */
71 if(array_key_exists('config',$_SESSION) &&
72 array_key_exists('SERVERS',$_SESSION['config']->data) &&
73 array_key_exists('FON',$_SESSION['config']->data['SERVERS']) &&
74 count($_SESSION['config']->data['SERVERS']['FON']) &&
75 is_callable("mysql_connect")
76 ) {
78 /* Set available server */
79 $this->goFonHomeServers = $_SESSION['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."),$this->goFonHomeServer, $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 gosa_log(@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 gosa_log(@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 gosa_log(@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 gosa_log(@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 /* Do we need to flip is_account state? */
197 if (isset($_POST['modify_state'])){
198 $this->is_account= !$this->is_account;
199 }
201 /* Show tab dialog headers */
202 if ($this->parent != NULL){
203 if ($this->is_account){
204 $display= $this->show_header(_("Remove the phone queue from this Account"),
205 _("Phone queue is enabled for this group. You can disable it by clicking below."));
206 } else {
207 $display= $this->show_header(_("Create phone queue"),
208 _("For this group the phone queues are disabled. You can enable them by clicking below."));
209 return ($display);
210 }
211 }
213 /* Add queue number */
214 if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
215 if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
216 $this->telephoneNumber[]=$_POST['phonenumber'];
217 }
218 }
220 /* Delete queue number */
221 if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
222 unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
223 }
225 $tmp = array();
226 foreach($this->telephoneNumber as $val){
227 if(!empty($val)){
228 $tmp[]= $val;
229 }
230 }
231 $this->telephoneNumber=$tmp;
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 }
253 /* Assign samrty variables */
254 $smarty= get_smarty();
255 $smarty->assign("goFonQueueLanguageOptions",array('de'=>_('German')));
256 $smarty->assign("goFonQueueStrategyOptions", $this->goFonQueueStrategyOptions);
258 foreach($this->attributes as $key => $val){
259 $smarty->assign($val,$this->$val);
260 if($this->$val == false){
261 $smarty->assign($val."CHK","");
262 }else{
263 $smarty->assign($val."CHK"," checked ");
264 }
265 if(chkacl($this->acl,$key)==""){
266 $smarty->assign($val."ACL","");
267 }else{
268 $smarty->assign($val."ACL"," disabled ");
269 }
270 }
272 /* Create array with goFonHomeServer */
273 $tmp = array();
274 foreach($this->goFonHomeServers as $dn => $val){
275 if(!is_numeric($dn)){
276 $tmp[$dn] = $val['SERVER'];
277 }
278 }
279 $smarty->assign("goFonHomeServers",$tmp);
281 return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE)));
282 }
285 /* Check formular input */
286 function check()
287 {
288 /* Call common method to give check the hook */
289 $message= plugin::check();
290 if(!count($this->goFonHomeServers)){
291 $message[] = _("There must be at least one server with an asterisk database to create a phone account.");
292 }
293 if(empty($this->goFonHomeServer)){
294 $message[] = _("Please select a valid goFonHomeServer.");
295 }
296 if($this->is_number_used()){
297 $message[] = $this->is_number_used();
298 }
299 if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
300 $message[] = _("Timeout must be numeric");
301 }
302 if(!((is_numeric($this->goFonQueueRetry))||(empty($this->goFonQueueRetry)))){
303 $message[] = _("Retry must be numeric");
304 }
305 if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
306 $message[] = _("Max queue length must be numeric");
307 }
308 if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
309 $message[] = _("Announce frequency must be numeric");
310 }
311 if(count($this->telephoneNumber)==0){
312 $message[] = _("There must be least one queue number defined.");
313 }
315 /* check if add to database could be successfull */
316 $str = $this->add_to_database();
317 if(!empty($str)){
318 $message[] = $str;
319 }
320 return $message;
321 }
324 /* This function removes the old database entries.
325 * If this entry should be removed or the home server has changed
326 * this function is called to ensure that all old entries will be deleted.
327 */
328 function remove_from_database($save = false)
329 {
330 /* Check if we must remove old entries */
331 if($this->initially_was_account){
333 /* Check if there is at least on server configuration */
334 if(!count($this->goFonHomeServers)){
335 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."));
336 }
338 /********************
339 * Get configuration and check it
340 ********************/
342 /* Check if databases are reachable, returns an error string if anything fails */
343 $error_str = $this->check_database_accessibility();
344 if($error_str){
345 return($error_str);
346 }
348 /* Connect to current database to be able to add new entries */
349 $cfg_Current = $this->goFonHomeServers[$this->init_HomeServer] ;
350 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
351 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
353 /* Create sql entries */
354 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
355 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."'; \n";
356 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';\n";
357 foreach($this->old_phone_numbers as $number){
358 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$number."';\n";
359 }
361 /* Execute the queries */
362 if($save){
363 foreach($delete as $sql){
364 $res = @mysql_query($sql,$res_cur);
365 if(!$res){
366 gosa_log(@mysql_error($res_cur));
367 return(_("Error while removing old queue entries from database.").
368 " "._("Please have a look a the gosa logfiles."));
369 }
370 }
371 }
372 }
373 }
376 /* This function handles the database entries for this
377 * queue.
378 * Existing entries will be updated if possible.
379 */
380 function add_to_database($save = false)
381 {
382 /* Check if there is at least on server configuration */
383 if(!count($this->goFonHomeServers)){
384 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."));
385 }
387 /********************
388 * Get configuration and check it
389 ********************/
391 /* Check if databases are reachable, returns an error string if anything fails */
392 $error_str = $this->check_database_accessibility();
393 if($error_str){
394 return($error_str);
395 }
397 /* Connect to current database to be able to add new entries */
398 $cfg_Current = $this->goFonHomeServers[$this->goFonHomeServer] ;
399 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
400 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
402 /* Connect to old home server and remove old entries if necessary */
403 if(($this->initially_was_account) && ($this->init_HomeServer != $this->goFonHomeServer)){
404 $str = $this->remove_from_database($save);
405 if(!empty($str)){
406 return($str);;
407 }
408 }
410 /* Ensure that we have the new cn in $this->cn and the old cn in $this->old_cn */
411 $this->cn = $this->parent->by_object['ogroup']->cn;
413 if($save){
415 /*****************
416 * Create queue table entry
417 *****************/
419 /* Check if QUEUE_TABLE entry exists.
420 * If this entry is missing - create it
421 * else update the entry with new values.
422 */
423 $query = "SELECT * FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."';";
424 $res = mysql_query($query,$res_cur);
425 if(!$res){
426 gosa_log(@mysql_error($res_cur));
427 return(_("Could not detect old queue entry, query failed.")." "._("Please have a look a the gosa logfiles."));
428 }
429 $cnt = mysql_affected_rows($res_cur);
431 /* Create queue table entry */
432 $queue["announce"] = "0";
433 $queue["monitor_join"] = "0";
434 $queue["monitor_format"] = "";
435 $queue["queue_holdtime"] = $this->goFonQueueAnnounce;
436 $queue["queue_lessthan"] = $this->goFonQueueLessThan;
437 $queue["announce_round_seconds"]= "0";
438 $queue["retry"] = $this->goFonQueueRetry;
439 $queue["wrapuptime"] = "0";
440 $queue["servicelevel"] = "0";
441 $queue["joinempty"] = "no";
442 $queue["leavewhenempty"] = "yes";
443 $queue["eventmemberstatus"] = "0";
444 $queue["eventwhencalled"] = "0";
445 $queue["reportholdtime"] = "1";
446 $queue["memberdelay"] = "0";
447 $queue["weight"] = "0";
448 $queue["timeoutrestart"] = "0";
450 $queue["context"] = "default";
451 $queue["name"] = $this->cn;
452 $queue["timeout"] = $this->goFonTimeOut;
453 $queue["maxlen"] = $this->goFonMaxLen;
454 $queue["strategy" ] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
455 $queue["queue_thankyou"] = $this->goFonQueueThankYou;
456 $queue["queue_reporthold"] = $this->goFonQueueReportHold;
457 $queue["announce_frequency"] = $this->goFonAnnounceFrequency;
458 $queue["queue_youarenext"] = $this->goFonQueueYouAreNext;
459 $queue["queue_thereare"] = $this->goFonQueueThereAre;
460 $queue["queue_callswaiting"] = $this->goFonQueueCallsWaiting;
461 $queue["queue_minutes"] = $this->goFonQueueMinutes;
462 $queue["queue_seconds"] = $this->goFonQueueSeconds;
463 $queue["announce_holdtime"] = $this->goFonQueueAnnounceHoldtime;
464 $queue["musiconhold"] = $this->goFonMusiconHold;
467 /* Check if we must create a new queue entry
468 * or if we can update an existing entry
469 * $cnt contains the number of entries matching this cn
470 */
472 /* Create new queue table entry
473 */
474 if($cnt == 0){
476 /* Parse and Add Queue */
477 $entries = "";
478 $values = "";
479 foreach($queue as $attr=>$val){
480 if($val == "") continue;
481 $entries.= "`".$attr."`,";
482 $values .= "'".$val."',";
483 }
484 $values = preg_replace("/,$/","",$values);
485 $entries = preg_replace("/,$/","",$entries );
486 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_TABLE']." (".$entries.") VALUES (".$values.");";
487 }elseif($cnt == 1){
489 /* Update queue table entry
490 */
491 $queue_old = @mysql_fetch_assoc($res);
492 foreach($queue_old as $name => $value){
493 if($queue[$name] == $value){
494 unset($queue[$name]);
495 }
496 }
498 /* Parse and Add Queue */
499 if(count($queue)){
500 $query = "UPDATE ".$cfg_Current['QUEUE_TABLE']." SET ";
501 foreach($queue as $key => $val){
502 $query.= "".$key."='".$val."',";
503 }
504 $query = preg_replace("/,$/","",$query);
505 $query.= " WHERE name='".$this->old_cn."';";
506 $SQL[] = $query;
507 }
508 }else{
509 return(sprintf(_("More than one entry in queue table found, that uses the name ('%s'). Please fix this issue manually first."),$this->cn));
510 }
513 /*****************
514 * Create queue member entries
515 *****************/
517 /* Add the queue member entries
518 * First we must remove all old user entries.
519 * to be able to add a clean set of members.
520 */
521 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->cn."';";
522 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';";
524 /* Append new Member for this queue */
525 $queueuser =array();
526 $i = 0;
527 $parent = $this->parent->by_object['ogroup'];
528 foreach($parent->memberList as $member => $mem_data){
529 $obj = $parent->objcache[$member];
530 if(isset($obj['objectClass'])){
531 if(in_array("goFonAccount",$obj['objectClass'])){
532 $i ++ ;
533 $queueuser[$i]['queue_name'] = $this->cn;
534 $queueuser[$i]['interface'] = "SIP/".$obj['uid'][0];
535 $queueuser[$i]['penalty'] = 1;
536 }
537 }
538 }
540 /* Parse and Add members to query Array */
541 if(is_array($queueuser)){
542 foreach($queueuser as $user){
543 $entries = "";
544 $values = "";
545 foreach($user as $attr => $val){
546 $entries.= "`".$attr."`,";
547 $values .= "'".$val."',";
548 }
549 $values = preg_replace("/,$/","",$values);
550 $entries = preg_replace("/,$/","",$entries );
552 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")";
553 }
554 }
557 /*****************
558 * Create extension entries
559 *****************/
561 /* Add the extension entries
562 * First we must remove all old entensions.
563 */
564 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->cn."';\n";
565 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
567 /* Delete old enxtension entries for the old telephone nubmer */
568 if(is_array($this->old_phone_numbers)){
569 foreach($this->old_phone_numbers as $phone){
570 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
571 }
572 }
574 /* Delete enxtension entries for telephone numbers */
575 if(is_array($this->telephoneNumber)){
576 foreach($this->telephoneNumber as $phone){
577 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
578 }
579 }
581 /* Create a extension entry fpr each telephoneNumber */
582 $i_insert_only_once = false;
583 $prio = 11; // This represents the priority for each telephoneNumber
584 foreach($this->telephoneNumber as $num){
586 /* The naming refrences */
587 if($i_insert_only_once == false){
588 $i_insert_only_once = true;
589 $a_ext[$i]['context'] = 'GOsa';
590 $a_ext[$i]['exten'] = $this->cn;
591 $a_ext[$i]['priority'] = 1;
592 $a_ext[$i]['app'] = "Goto";
593 $a_ext[$i]['appdata'] = $num."|1";
594 $i ++ ;
595 }
597 /* If there is currently no user for this queue
598 * Play no service sound file and return to default context.
599 */
600 if(count($queueuser)==0){
601 $a_ext[$i]['context'] = 'GOsa';
602 $a_ext[$i]['exten'] = $num;
603 $a_ext[$i]['priority'] = 1;
604 $a_ext[$i]['app'] = "SetLanguage";
605 $a_ext[$i]['appdata'] = "de";
606 $i ++ ;
608 $a_ext[$i]['context'] = 'GOsa';
609 $a_ext[$i]['exten'] = $num;
610 $a_ext[$i]['priority'] = 2;
611 $a_ext[$i]['app'] = "Playback";
612 $a_ext[$i]['appdata'] = "ss-noservice";
613 $i ++ ;
615 $a_ext[$i]['context'] = 'GOsa';
616 $a_ext[$i]['exten'] = $num;
617 $a_ext[$i]['priority'] = 3;
618 $a_ext[$i]['app'] = "Goto";
619 $a_ext[$i]['appdata'] = "default";
620 $i ++ ;
621 }else{
623 /* Dcrement priority to avoid using same priority twice */
624 $prio --;
626 /* Wait for 2 seconds */
627 $a_ext[$i]['context'] = 'GOsa';
628 $a_ext[$i]['exten'] = $num;
629 $a_ext[$i]['priority'] = 1;
630 $a_ext[$i]['app'] = "Wait";
631 $a_ext[$i]['appdata'] = "2";
632 $i ++ ;
634 /* Set language to queue language */
635 $a_ext[$i]['context'] = 'GOsa';
636 $a_ext[$i]['exten'] = $num;
637 $a_ext[$i]['priority'] = 2;
638 $a_ext[$i]['app'] = "SetLanguage";
639 $a_ext[$i]['appdata'] = $this->goFonQueueLanguage;
640 $i ++ ;
642 /* Play welcome sound file */
643 $a_ext[$i]['context'] = 'GOsa';
644 $a_ext[$i]['exten'] = $num;
645 $a_ext[$i]['priority'] = 3;
646 $a_ext[$i]['app'] = "Playback";
647 $a_ext[$i]['appdata'] = $this->goFonWelcomeMusic;
648 $i ++ ;
650 /* Set CID name */
651 $a_ext[$i]['context'] = 'GOsa';
652 $a_ext[$i]['exten'] = $num;
653 $a_ext[$i]['priority'] = 4;
654 $a_ext[$i]['app'] = "SetCIDName";
655 if(!empty($this->parent->by_object['ogroup']->description)){
656 $a_ext[$i]['appdata'] = $this->parent->by_object['ogroup']->description;
657 }else{
658 $a_ext[$i]['appdata'] = $this->cn." - ".$num;
659 }
660 $i ++ ;
662 /* Set queue priority */
663 $a_ext[$i]['context'] = 'GOsa';
664 $a_ext[$i]['exten'] = $num;
665 $a_ext[$i]['priority'] = 5;
666 $a_ext[$i]['app'] = "SetVar";
667 $a_ext[$i]['appdata'] = "QUEUE_PRIO=".$prio;
668 $i ++ ;
670 /* Open queue */
671 $a_ext[$i]['context'] = 'GOsa';
672 $a_ext[$i]['exten'] = $num;
673 $a_ext[$i]['priority'] = 6;
674 $a_ext[$i]['app'] = "Queue";
675 $a_ext[$i]['appdata'] = $this->cn;
676 "|".
677 $this->goFonDialOption_t.
678 $this->goFonDialOption_T.
679 $this->goFonDialOption_h.
680 $this->goFonDialOption_H.
681 $this->goFonDialOption_r;
682 }
684 $i++;
685 }
687 /* Parse and Add Extension entries */
688 foreach($a_ext as $ext){
689 $entries = "";
690 $values = "";
691 foreach($ext as $attr => $val){
692 $entries.= "`".$attr."`,";
693 $values .= "'".$val."',";
694 }
695 $values = preg_replace("/,$/","",$values);
696 $entries = preg_replace("/,$/","",$entries );
697 $SQL[]="INSERT INTO ".$cfg_Current['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
698 }
700 /* Do all collected mysql queries
701 */
702 foreach($SQL as $query)
703 $res = mysql_query($query,$res_cur);
704 if(!$res){
705 gosa_log(@mysql_error($res_cur));
706 return(_("Mysql query failed.")." "._("Please have a look a the gosa logfiles."));
707 }
708 }
709 @mysql_close($r_con);
710 return(false);
711 }
714 /* This function checks if the given phonenumbers
715 * are available or already in use
716 */
717 function is_number_used()
718 {
719 $ldap= $this->config->get_ldap_link();
720 $ldap->cd($this->config->current['BASE']);
721 $ldap->search("(&(& (!(uid=".$this->cn."))
722 (!(cn=".$this->cn.")))
723 (| (objectClass=goFonAccount)
724 (objectClass=goFonQueue)
725 (objectClass=goFonConference)))", array("telephoneNumber","cn","uid"));
726 while($attrs = $ldap->fetch()) {
727 unset($attrs['telephoneNumber']['count']);
728 foreach($attrs['telephoneNumber'] as $tele){
729 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
730 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
731 $numbers[$tele]=$attrs;
732 }
733 }
735 foreach($this->telephoneNumber as $num){
736 if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
737 if(isset($numbers[$num]['uid'][0])){
738 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
739 }else{
740 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
741 }
742 }
743 }
744 }
747 /* Get posted attributes */
748 function save_object()
749 {
750 plugin::save_object();
751 if(isset($_POST['phonenumber'])){
752 foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_h","goFonDialOption_r","goFonDialOption_H","goFonMusiconHold") as $val){
753 if(isset($_POST[$val])){
754 $this->$val = $_POST[$val];
755 }else{
756 $this->$val = false;
757 }
758 }
759 if(isset($_POST['goFonQueueAnnounceHoldtime'])){
760 $this->goFonQueueAnnounceHoldtime = "yes";
761 }else{
762 $this->goFonQueueAnnounceHoldtime = false;
763 }
764 }
765 }
768 function save()
769 {
770 $ldap= $this->config->get_ldap_link();
772 plugin::save();
774 /* Create dial option attribute */
775 $this->attrs['goFonDialOption'] = "";
776 foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_r","goFonDialOption_h","goFonDialOption_H","cn") as $val){
777 $this->attrs['goFonDialOption'].=$this->$val;
778 unset($this->attrs[$val]);
779 }
780 if(empty($this->attrs['goFonDialOption'])) {
781 $this->attrs['goFonDialOption']=array();
782 }
784 /* Set announce hold time to yes no .. */
785 if($this->goFonQueueAnnounceHoldtime != "no" ){
786 $this->attrs['goFonQueueAnnounceHoldtime'] = "yes";
787 }else{
788 $this->attrs['goFonQueueAnnounceHoldtime'] = "no";
789 }
791 /* Set strategy */
792 $this->attrs['goFonQueueStrategy'] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
794 /* Add database entry, display error and abort if this fails */
795 $str = $this->add_to_database(true);
796 if(!empty($str)){
797 print_red($str);
798 }
800 /* Save data to LDAP */
801 $ldap->cd($this->dn);
802 $this->cleanup();
803 $ldap->modify ($this->attrs);
805 show_ldap_error($ldap->get_error(), _("Saving phone queue failed"));
807 /* Optionally execute a command after we're done */
808 if ($this->initially_was_account == $this->is_account){
809 if ($this->is_modified){
810 $this->handle_post_events("modify");
811 }
812 } else {
813 $this->handle_post_events("add");
814 }
815 }
818 /* remove object from parent */
819 function remove_from_parent()
820 {
821 /* Cancel if nothing is to do here */
822 if (!$this->initially_was_account){
823 return;
824 }
826 /* Remove database entries,
827 * if fails display errors and abort
828 */
829 $str = $this->remove_from_database(true);
830 if(!empty($str)){
831 print_red($str);
832 return false;
833 }
835 /* Remove all temporary attributes */
836 $tmp = array_flip($this->attributes);
837 foreach(array("goFonDialOption_t","goFonDialOption_T","goFonDialOption_r","goFonDialOption_h","goFonDialOption_H","cn") as $val){
838 unset($this->$val);
839 unset($this->attrs[$val]);
840 unset($tmp[$val]);
841 }
842 foreach(array_flip($tmp) as $key => $val){
843 $tmp2[]=$val;
844 }
845 $this->attributes = $tmp2;
847 /* include global link_info */
848 $ldap= $this->config->get_ldap_link();
850 /* Remove and write to LDAP */
851 plugin::remove_from_parent();
853 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
854 $ldap->cd($this->dn);
855 $this->cleanup();
856 $ldap->modify ($this->attrs);
857 show_ldap_error($ldap->get_error(), _("Removing phone queue failed"));
858 }
861 function getCopyDialog()
862 {
863 $str = "";
864 $str .= _("Phone number");
865 $str .= " <input type='text' name='telephoneNumber' value='".$this->telephoneNumber."'>";
866 return($str);
867 }
870 function saveCopyDialog()
871 {
872 if(isset($_POST['telephoneNumber'])){
873 $this->telephoneNumber = $_POST['telephoneNumber'];
874 }
875 }
878 }
880 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
881 ?>