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(array_key_exists('config',$_SESSION) &&
75 array_key_exists('SERVERS',$_SESSION['config']->data) &&
76 array_key_exists('FON',$_SESSION['config']->data['SERVERS']) &&
77 count($_SESSION['config']->data['SERVERS']['FON']) &&
78 is_callable("mysql_connect")
79 ) {
81 /* Set available server */
82 $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
84 /* Set default server */
85 if($this->dn == "new"){
86 $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
87 }
89 /* Remember inital home server, to be able to remove old entries */
90 $this->init_HomeServer = $this->goFonHomeServer;
92 /* get config */
93 if(!isset($this->goFonHomeServers[$this->goFonHomeServer])){
94 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'])));
95 $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
96 $this->init_HomeServer = $this->goFonHomeServers[0]['DN'];
97 }
98 $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer];
99 }
102 /* Variable init
103 * Load phone nubmers and parse dial options
104 */
105 if($this->is_account){
106 if(isset($this->attrs['telephoneNumber'])){
107 $this->telephoneNumber=$this->attrs['telephoneNumber'];
108 unset($this->telephoneNumber['count']);
109 }
110 for($i = 0; $i < strlen($this->goFonDialOption); $i++){
111 $name = "goFonDialOption".$this->goFonDialOption[$i];
112 $this->$name=$this->goFonDialOption[$i];
113 }
114 }
116 /* Set Queue announce hold time to true/false */
117 if(preg_match("/no/i",$this->goFonQueueAnnounceHoldtime)){
118 $this->goFonQueueAnnounceHoldtime=false;
119 }else{
120 $this->goFonQueueAnnounceHoldtime=true;
121 }
123 /* Define all available ringdown types */
124 $types= array('ringall' =>_("ring all"),
125 'roundrobin' =>_("round robin"),
126 'leastrecent'=>_("least recently called"),
127 'fewestcalls'=>_("fewest completed calls"),
128 'random' =>_("random"),
129 'rrmemory' =>_("round robin with memory"));
130 $i = 0;
131 foreach($types as $type => $name){
132 $i++;
133 $this->goFonQueueStrategyOptions[$i] =$name;
134 $this->goFonQueueStrategyOptionsR[$i] =$type;
135 $tmp[$type] = $i;
136 }
137 $this->goFonQueueStrategy= $tmp[$this->goFonQueueStrategy];
138 $this->old_cn = $this->cn;
139 $this->old_phone_numbers = $this->telephoneNumber;
140 }
143 /* This function ensures that the selected home server
144 * and the initially selected home server are reachable and accessible
145 */
146 function check_database_accessibility()
147 {
148 /* Check if mysql extension is available */
149 if(!is_callable("mysql_pconnect")){
150 return(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
151 }
153 /********************
154 * Check currently selected home server
155 ********************/
157 $cfg_Current = $this->goFonHomeServers[$this->goFonHomeServer];
158 $r_current = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
159 if(!$r_current){
160 gosa_log(@mysql_error($r_current));
161 return(sprintf(_("The MySQL home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
162 $cfg_Current['SERVER'],$cfg_Current['LOGIN']));
163 }
164 $db_current = @mysql_select_db($cfg_Current['DB'],$r_current);
165 if(!$db_current){
166 gosa_log(@mysql_error($r_current));
167 mysql_close($r_current);
168 return( sprintf(_("Can't select database '%s' on home server '%s'."),$cfg_Current['DB'],$cfg_Current['SERVER']));
169 }
171 /********************
172 * Check init home server
173 ********************/
175 if($this->initially_was_account){
176 $cfg_Init = $this->goFonHomeServers[$this->init_HomeServer] ;
177 $r_init = @mysql_pconnect($cfg_Init['SERVER'],$cfg_Init['LOGIN'],$cfg_Init['PASSWORD']);
178 if(!$r_init){
179 gosa_log(@mysql_error($r_init));
180 return(sprintf(_("The MySQL initial home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
181 $cfg_Init['SERVER'],$cfg_Init['LOGIN']));
182 }
183 $db_init = @mysql_select_db($cfg_Init['DB'],$r_init);
184 if(!$db_init){
185 gosa_log(@mysql_error($r_init));
186 mysql_close($r_init);
187 return( sprintf(_("Can't select database '%s' on initial home server '%s'."),$cfg_Init['DB'],$cfg_Init['SERVER']));
188 }
189 }
190 }
193 /* Display plugin ui */
194 function execute()
195 {
196 /* Call parent execute */
197 plugin::execute();
199 if($this->is_account && !$this->view_logged){
200 $this->view_logged = TRUE;
201 @log::log("view","ogroups/".get_class($this),$this->dn);
202 }
204 if(isset($_POST['modify_state'])){
205 if($this->is_account && $this->acl_is_removeable()){
206 $this->is_account= FALSE;
207 }elseif(!$this->is_account && $this->acl_is_createable()){
208 $this->is_account= TRUE;
209 }
210 }
212 /* Show tab dialog headers */
213 if ($this->parent != NULL){
214 if ($this->is_account){
215 $display= $this->show_disable_header(_("Remove the phone queue from this Account"),
216 _("Phone queue is enabled for this group. You can disable it by clicking below."));
217 } else {
218 $display= $this->show_enable_header(_("Create phone queue"),
219 _("For this group the phone queues are disabled. You can enable them by clicking below."));
220 return ($display);
221 }
222 }
224 if($this->acl_is_writeable("telephoneNumber")){
226 /* Add queue number */
227 if(isset($_POST['add_phonenumber'])&&(isset($_POST['phonenumber']))&&(!empty($_POST['phonenumber']))){
228 if((!in_array($_POST['phonenumber'],$this->telephoneNumber))&&(is_numeric($_POST['phonenumber']))){
229 $this->telephoneNumber[]=$_POST['phonenumber'];
230 }
231 }
233 /* Delete queue number */
234 if(isset($_POST['delete_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
235 unset($this->telephoneNumber[$_POST['goFonQueueNumber_List']]);
236 }
238 /* queue number up */
239 if(isset($_POST['up_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
240 if($_POST['goFonQueueNumber_List']>0){
241 $up = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
242 $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1];
243 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $down;
244 $this->telephoneNumber[$_POST['goFonQueueNumber_List']-1] = $up;
245 }
246 }
248 /* Queuenumber down */
249 if(isset($_POST['down_phonenumber'])&&(isset($_POST['goFonQueueNumber_List']))){
250 if(isset($this->telephoneNumber[($_POST['goFonQueueNumber_List']+1)])){
251 $up = $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1];
252 $down = $this->telephoneNumber[$_POST['goFonQueueNumber_List']];
253 $this->telephoneNumber[$_POST['goFonQueueNumber_List']+1] = $down;
254 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $up;
255 }
256 $this->telephoneNumber[$_POST['goFonQueueNumber_List']] = $up;
257 }
258 }
260 $tmp = array();
261 foreach($this->telephoneNumber as $val){
262 if(!empty($val)){
263 $tmp[]= $val;
264 }
265 }
266 $this->telephoneNumber=$tmp;
268 /* Assign samrty variables */
269 $smarty= get_smarty();
270 $smarty->assign("goFonQueueLanguageOptions",array('de'=>_('German')));
271 $smarty->assign("goFonQueueStrategyOptions", $this->goFonQueueStrategyOptions);
273 /* Set acls */
274 $tmp = $this->plInfo();
275 foreach($tmp['plProvidedAcls'] as $name => $translated){
276 $smarty->assign($name."ACL",$this->getacl($name));
277 }
279 foreach($this->attributes as $key => $val){
280 $smarty->assign($val,$this->$val);
281 if($this->$val == false){
282 $smarty->assign($val."CHK","");
283 }else{
284 $smarty->assign($val."CHK"," checked ");
285 }
286 }
288 /* Create array with goFonHomeServer */
289 $tmp = array();
290 foreach($this->goFonHomeServers as $dn => $val){
291 if(!is_numeric($dn)){
292 $tmp[$dn] = $val['SERVER'];
293 }
294 }
295 $smarty->assign("goFonHomeServers",$tmp);
297 return ($display.$smarty->fetch (get_template_path('phonequeue.tpl', TRUE)));
298 }
301 /* Check formular input */
302 function check()
303 {
304 /* Call common method to give check the hook */
305 $message= plugin::check();
306 if(!count($this->goFonHomeServers)){
307 $message[] = _("There must be at least one server with an asterisk database to create a phone queue.");
308 }
309 if(empty($this->goFonHomeServer)){
310 $message[] = _("Please select a valid goFonHomeServer.");
311 }
312 if($this->is_number_used()){
313 $message[] = $this->is_number_used();
314 }
315 if(!((is_numeric($this->goFonTimeOut))||(empty($this->goFonTimeOut)))){
316 $message[] = _("Timeout must be numeric");
317 }
318 if(!((is_numeric($this->goFonQueueRetry))||(empty($this->goFonQueueRetry)))){
319 $message[] = _("Retry must be numeric");
320 }
321 if(!((is_numeric($this->goFonMaxLen))||(empty($this->goFonMaxLen)))){
322 $message[] = _("Max queue length must be numeric");
323 }
324 if(!((is_numeric($this->goFonAnnounceFrequency))||(empty($this->goFonAnnounceFrequency)))){
325 $message[] = _("Announce frequency must be numeric");
326 }
327 if(count($this->telephoneNumber)==0){
328 $message[] = _("There must be least one queue number defined.");
329 }
331 /* check if add to database could be successfull */
332 $str = $this->add_to_database();
333 if(!empty($str)){
334 $message[] = $str;
335 }
336 return $message;
337 }
340 /* This function removes the old database entries.
341 * If this entry should be removed or the home server has changed
342 * this function is called to ensure that all old entries will be deleted.
343 */
344 function remove_from_database($save = false)
345 {
346 /* Check if we must remove old entries */
347 if($this->initially_was_account){
349 /* Check if there is at least on server configuration */
350 if(!count($this->goFonHomeServers)){
351 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."));
352 }
354 /********************
355 * Get configuration and check it
356 ********************/
358 /* Check if databases are reachable, returns an error string if anything fails */
359 $error_str = $this->check_database_accessibility();
360 if($error_str){
361 return($error_str);
362 }
364 /* Connect to current database to be able to add new entries */
365 $cfg_Current = $this->goFonHomeServers[$this->init_HomeServer] ;
366 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
367 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
369 /* Create sql entries */
370 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
371 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."'; \n";
372 $delete[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';\n";
373 foreach($this->old_phone_numbers as $number){
374 $delete[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$number."';\n";
375 }
377 /* Execute the queries */
378 if($save){
379 foreach($delete as $sql){
380 $res = @mysql_query($sql,$res_cur);
381 if(!$res){
382 gosa_log(@mysql_error($res_cur));
383 return(_("Error while removing old queue entries from database.").
384 " "._("Please have a look a the gosa logfiles."));
385 }
386 }
387 }
388 }
389 }
392 /* This function handles the database entries for this
393 * queue.
394 * Existing entries will be updated if possible.
395 */
396 function add_to_database($save = false)
397 {
398 /* Check if there is at least on server configuration */
399 if(!count($this->goFonHomeServers)){
400 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."));
401 }
403 /********************
404 * Get configuration and check it
405 ********************/
407 /* Check if databases are reachable, returns an error string if anything fails */
408 $error_str = $this->check_database_accessibility();
409 if($error_str){
410 return($error_str);
411 }
413 /* Connect to current database to be able to add new entries */
414 $cfg_Current = $this->goFonHomeServers[$this->goFonHomeServer] ;
415 $res_cur = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
416 $db_cur = @mysql_select_db($cfg_Current['DB'],$res_cur);
418 /* Connect to old home server and remove old entries if necessary */
419 if(($this->initially_was_account) && ($this->init_HomeServer != $this->goFonHomeServer)){
420 $str = $this->remove_from_database($save);
421 if(!empty($str)){
422 return($str);;
423 }
424 }
426 /* Ensure that we have the new cn in $this->cn and the old cn in $this->old_cn */
427 $this->cn = $this->parent->by_object['ogroup']->cn;
429 if($save){
431 /*****************
432 * Create queue table entry
433 *****************/
435 /* Check if QUEUE_TABLE entry exists.
436 * If this entry is missing - create it
437 * else update the entry with new values.
438 */
439 $query = "SELECT * FROM ".$cfg_Current['QUEUE_TABLE']." WHERE name='".$this->old_cn."';";
440 $res = mysql_query($query,$res_cur);
441 if(!$res){
442 gosa_log(@mysql_error($res_cur));
443 return(_("Could not detect old queue entry, query failed.")." "._("Please have a look a the gosa logfiles."));
444 }
445 $cnt = mysql_affected_rows($res_cur);
448 /* Create queue table entry
449 * Leave unused options empty.
450 */
451 $queue["announce"] = "";
452 $queue["monitor_join"] = "";
453 $queue["monitor_format"] = "";
454 $queue["announce_round_seconds"]= "";
455 $queue["wrapuptime"] = "";
456 $queue["servicelevel"] = "";
457 $queue["eventmemberstatus"] = "";
458 $queue["eventwhencalled"] = "";
459 $queue["memberdelay"] = "";
460 $queue["weight"] = "";
461 $queue["timeoutrestart"] = "";
463 $queue["queue_holdtime"] = $this->goFonQueueAnnounce;
464 $queue["queue_lessthan"] = $this->goFonQueueLessThan;
465 $queue["retry"] = $this->goFonQueueRetry;
466 $queue["reportholdtime"] = "1";
467 $queue["joinempty"] = "no";
468 $queue["leavewhenempty"] = "yes";
470 $queue["context"] = "default";
471 $queue["name"] = $this->cn;
472 $queue["timeout"] = $this->goFonTimeOut;
473 $queue["maxlen"] = $this->goFonMaxLen;
474 $queue["strategy" ] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
475 $queue["queue_thankyou"] = $this->goFonQueueThankYou;
476 $queue["queue_reporthold"] = $this->goFonQueueReportHold;
477 $queue["announce_frequency"] = $this->goFonAnnounceFrequency;
478 $queue["queue_youarenext"] = $this->goFonQueueYouAreNext;
479 $queue["queue_thereare"] = $this->goFonQueueThereAre;
480 $queue["queue_callswaiting"] = $this->goFonQueueCallsWaiting;
481 $queue["queue_minutes"] = $this->goFonQueueMinutes;
482 $queue["queue_seconds"] = $this->goFonQueueSeconds;
483 $queue["announce_holdtime"] = $this->goFonQueueAnnounceHoldtime;
484 $queue["musiconhold"] = $this->goFonMusiconHold;
487 /* Check if we must create a new queue entry
488 * or if we can update an existing entry
489 * $cnt contains the number of entries matching this cn
490 */
492 /* Create new queue table entry
493 */
494 if($cnt == 0){
496 /* Parse and Add Queue */
497 $entries = "";
498 $values = "";
499 foreach($queue as $attr=>$val){
500 if($val == "") continue;
501 $entries.= "`".$attr."`,";
502 $values .= "'".$val."',";
503 }
504 $values = preg_replace("/,$/","",$values);
505 $entries = preg_replace("/,$/","",$entries );
506 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_TABLE']." (".$entries.") VALUES (".$values.");";
507 }elseif($cnt == 1){
509 /* Update queue table entry
510 */
511 $queue_old = @mysql_fetch_assoc($res);
512 foreach($queue_old as $name => $value){
513 if(isset($queue[$name]) && $queue[$name] == $value){
514 unset($queue[$name]);
515 }
516 }
518 /* Parse and Add Queue */
519 if(count($queue)){
520 $query = "UPDATE ".$cfg_Current['QUEUE_TABLE']." SET ";
521 foreach($queue as $key => $val){
522 $query.= "".$key."='".$val."',";
523 }
524 $query = preg_replace("/,$/","",$query);
525 $query.= " WHERE name='".$this->old_cn."';";
526 $SQL[] = $query;
527 }
528 }else{
529 return(sprintf(_("More than one entry in queue table found, that uses the name ('%s'). Please fix this issue manually first."),$this->cn));
530 }
533 /*****************
534 * Create queue member entries
535 *****************/
537 /* Add the queue member entries
538 * First we must remove all old user entries.
539 * to be able to add a clean set of members.
540 */
541 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->cn."';";
542 $SQL[]= "DELETE FROM ".$cfg_Current['QUEUE_MEMBER_TABLE']." WHERE queue_name='".$this->old_cn."';";
544 /* Append new Member for this queue */
545 $queueuser =array();
546 $i = 0;
547 $parent = $this->parent->by_object['ogroup'];
550 $ldap = $this->config->get_ldap_link();
551 foreach($parent->memberList as $member => $mem_data){
552 $ldap->cat($member,array("goFonHomeServer","objectClass","dn","uid"));
553 if($ldap->count()){
555 $obj = $ldap->fetch();
557 /* Calculate server and account dependencies */
558 $is_acc = in_array("goFonAccount",$obj['objectClass']);
559 $is_home= isset($obj['goFonHomeServer'][0]) && $obj['goFonHomeServer'][0] == $this->goFonHomeServer;
561 /* Append user to list of queue member,
562 * only if user has phoneAccount extension && is on same home server */
563 if($is_acc && $is_home){
564 $i ++ ;
565 $queueuser[$i]['queue_name'] = $this->cn;
566 $queueuser[$i]['interface'] = "SIP/".$obj['uid'][0];
567 $queueuser[$i]['penalty'] = 1;
568 }
569 }
570 }
572 /* Parse and Add members to query Array */
573 if(is_array($queueuser)){
574 foreach($queueuser as $user){
575 $entries = "";
576 $values = "";
577 foreach($user as $attr => $val){
578 $entries.= "`".$attr."`,";
579 $values .= "'".$val."',";
580 }
581 $values = preg_replace("/,$/","",$values);
582 $entries = preg_replace("/,$/","",$entries );
584 $SQL[]="INSERT INTO ".$cfg_Current['QUEUE_MEMBER_TABLE']." (".$entries.") VALUES (".$values.")";
585 }
586 }
589 /*****************
590 * Create extension entries
591 *****************/
593 /* Add the extension entries
594 * First we must remove all old entensions.
595 */
596 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->cn."';\n";
597 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$this->old_cn."';\n";
599 /* Delete old enxtension entries for the old telephone nubmer */
600 if(is_array($this->old_phone_numbers)){
601 foreach($this->old_phone_numbers as $phone){
602 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
603 }
604 }
606 /* Delete enxtension entries for telephone numbers */
607 if(is_array($this->telephoneNumber)){
608 foreach($this->telephoneNumber as $phone){
609 $SQL[]= "DELETE FROM ".$cfg_Current['EXT_TABLE']." WHERE exten='".$phone."';\n";
610 }
611 }
613 /* Create a extension entry fpr each telephoneNumber */
614 $i_insert_only_once = false;
615 $prio = 11; // This represents the priority for each telephoneNumber
616 foreach($this->telephoneNumber as $num){
618 /* The naming refrences */
619 if($i_insert_only_once == false){
620 $i_insert_only_once = true;
621 $a_ext[$i]['context'] = 'GOsa';
622 $a_ext[$i]['exten'] = $this->cn;
623 $a_ext[$i]['priority'] = 1;
624 $a_ext[$i]['app'] = "Goto";
625 $a_ext[$i]['appdata'] = $num."|1";
626 $i ++ ;
627 }
629 /* If there is currently no user for this queue
630 * Play no service sound file and return to default context.
631 */
632 if(count($queueuser)==0){
633 $a_ext[$i]['context'] = 'GOsa';
634 $a_ext[$i]['exten'] = $num;
635 $a_ext[$i]['priority'] = 1;
636 $a_ext[$i]['app'] = "SetLanguage";
637 $a_ext[$i]['appdata'] = "de";
638 $i ++ ;
640 $a_ext[$i]['context'] = 'GOsa';
641 $a_ext[$i]['exten'] = $num;
642 $a_ext[$i]['priority'] = 2;
643 $a_ext[$i]['app'] = "Playback";
644 $a_ext[$i]['appdata'] = "ss-noservice";
645 $i ++ ;
647 $a_ext[$i]['context'] = 'GOsa';
648 $a_ext[$i]['exten'] = $num;
649 $a_ext[$i]['priority'] = 3;
650 $a_ext[$i]['app'] = "Goto";
651 $a_ext[$i]['appdata'] = "default";
652 $i ++ ;
653 }else{
655 /* Dcrement priority to avoid using same priority twice */
656 $prio --;
658 /* Wait for 2 seconds */
659 $a_ext[$i]['context'] = 'GOsa';
660 $a_ext[$i]['exten'] = $num;
661 $a_ext[$i]['priority'] = 1;
662 $a_ext[$i]['app'] = "Wait";
663 $a_ext[$i]['appdata'] = "2";
664 $i ++ ;
666 /* Set language to queue language */
667 $a_ext[$i]['context'] = 'GOsa';
668 $a_ext[$i]['exten'] = $num;
669 $a_ext[$i]['priority'] = 2;
670 $a_ext[$i]['app'] = "SetLanguage";
671 $a_ext[$i]['appdata'] = $this->goFonQueueLanguage;
672 $i ++ ;
674 /* Play welcome sound file */
675 $a_ext[$i]['context'] = 'GOsa';
676 $a_ext[$i]['exten'] = $num;
677 $a_ext[$i]['priority'] = 3;
678 $a_ext[$i]['app'] = "Playback";
679 $a_ext[$i]['appdata'] = $this->goFonWelcomeMusic;
680 $i ++ ;
682 /* Set CID name */
683 $a_ext[$i]['context'] = 'GOsa';
684 $a_ext[$i]['exten'] = $num;
685 $a_ext[$i]['priority'] = 4;
686 $a_ext[$i]['app'] = "SetCIDName";
687 if(!empty($this->parent->by_object['ogroup']->description)){
688 $a_ext[$i]['appdata'] = $this->parent->by_object['ogroup']->description;
689 }else{
690 $a_ext[$i]['appdata'] = $this->cn." - ".$num;
691 }
692 $i ++ ;
694 /* Set queue priority */
695 $a_ext[$i]['context'] = 'GOsa';
696 $a_ext[$i]['exten'] = $num;
697 $a_ext[$i]['priority'] = 5;
698 $a_ext[$i]['app'] = "SetVar";
699 $a_ext[$i]['appdata'] = "QUEUE_PRIO=".$prio;
700 $i ++ ;
702 /* Open queue */
703 $a_ext[$i]['context'] = 'GOsa';
704 $a_ext[$i]['exten'] = $num;
705 $a_ext[$i]['priority'] = 6;
706 $a_ext[$i]['app'] = "Queue";
707 $a_ext[$i]['appdata'] = $this->cn;
708 "|".
709 $this->goFonDialOptiont.
710 $this->goFonDialOptionT.
711 $this->goFonDialOptionh.
712 $this->goFonDialOptionH.
713 $this->goFonDialOptionr;
714 }
716 $i++;
717 }
719 /* Parse and Add Extension entries */
720 foreach($a_ext as $ext){
721 $entries = "";
722 $values = "";
723 foreach($ext as $attr => $val){
724 $entries.= "`".$attr."`,";
725 $values .= "'".$val."',";
726 }
727 $values = preg_replace("/,$/","",$values);
728 $entries = preg_replace("/,$/","",$entries );
729 $SQL[]="INSERT INTO ".$cfg_Current['EXT_TABLE']." (".$entries.") VALUES (".$values.")";
730 }
732 /* Do all collected mysql queries
733 */
734 foreach($SQL as $query)
735 $res = mysql_query($query,$res_cur);
736 if(!$res){
737 gosa_log(@mysql_error($res_cur));
738 return(_("Mysql query failed.")." "._("Please have a look a the gosa logfiles."));
739 }
740 }
741 @mysql_close($r_con);
742 return(false);
743 }
746 /* This function checks if the given phonenumbers
747 * are available or already in use
748 */
749 function is_number_used()
750 {
751 $ldap= $this->config->get_ldap_link();
752 $ldap->cd($this->config->current['BASE']);
753 $ldap->search("(&(& (!(uid=".$this->cn."))
754 (!(cn=".$this->cn.")))
755 (| (objectClass=goFonAccount)
756 (objectClass=goFonQueue)
757 (objectClass=goFonConference)))", array("telephoneNumber","cn","uid"));
758 while($attrs = $ldap->fetch()) {
759 unset($attrs['telephoneNumber']['count']);
760 foreach($attrs['telephoneNumber'] as $tele){
761 if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
762 if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
763 $numbers[$tele]=$attrs;
764 }
765 }
767 foreach($this->telephoneNumber as $num){
768 if((isset($numbers[$num]))&&(($numbers[$num]['cn'][0]!= $this->attrs['cn'][0]))){
769 if(isset($numbers[$num]['uid'][0])){
770 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
771 }else{
772 return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
773 }
774 }
775 }
776 }
779 /* Get posted attributes */
780 function save_object()
781 {
782 plugin::save_object();
783 if(isset($_POST['phonenumber'])){
784 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionh","goFonDialOptionr","goFonDialOptionH","goFonMusiconHold") as $val){
785 if(isset($_POST[$val])){
786 $this->$val = $_POST[$val];
787 }else{
788 $this->$val = false;
789 }
790 }
791 if(isset($_POST['goFonQueueAnnounceHoldtime'])){
792 $this->goFonQueueAnnounceHoldtime = "yes";
793 }else{
794 $this->goFonQueueAnnounceHoldtime = false;
795 }
796 }
797 }
800 function save()
801 {
802 $ldap= $this->config->get_ldap_link();
804 plugin::save();
806 /* Create dial option attribute */
807 $this->attrs['goFonDialOption'] = "";
808 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionr","goFonDialOptionh","goFonDialOptionH","cn") as $val){
809 $this->attrs['goFonDialOption'].=$this->$val;
810 unset($this->attrs[$val]);
811 }
812 if(empty($this->attrs['goFonDialOption'])) {
813 $this->attrs['goFonDialOption']=array();
814 }
816 /* Set announce hold time to yes no .. */
817 if($this->goFonQueueAnnounceHoldtime != "no" ){
818 $this->attrs['goFonQueueAnnounceHoldtime'] = "yes";
819 }else{
820 $this->attrs['goFonQueueAnnounceHoldtime'] = "no";
821 }
823 /* Set strategy */
824 $this->attrs['goFonQueueStrategy'] = $this->goFonQueueStrategyOptionsR[$this->goFonQueueStrategy];
826 /* Add database entry, display error and abort if this fails */
827 $str = $this->add_to_database(true);
828 if(!empty($str)){
829 print_red($str);
830 }
832 /* Save data to LDAP */
833 $ldap->cd($this->dn);
834 $this->cleanup();
835 $ldap->modify ($this->attrs);
837 if($this->initially_was_account){
838 @log::log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
839 }else{
840 @log::log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
841 }
843 show_ldap_error($ldap->get_error(), _("Saving phone queue failed"));
845 /* Optionally execute a command after we're done */
846 if ($this->initially_was_account == $this->is_account){
847 if ($this->is_modified){
848 $this->handle_post_events("modify");
849 }
850 } else {
851 $this->handle_post_events("add");
852 }
853 }
856 /* remove object from parent */
857 function remove_from_parent()
858 {
859 /* Cancel if nothing is to do here */
860 if (!$this->initially_was_account){
861 return;
862 }
864 /* Remove database entries,
865 * if fails display errors and abort
866 */
867 $str = $this->remove_from_database(true);
868 if(!empty($str)){
869 print_red($str);
870 return false;
871 }
873 /* Remove all temporary attributes */
874 $tmp = array_flip($this->attributes);
875 foreach(array("goFonDialOptiont","goFonDialOptionT","goFonDialOptionr","goFonDialOptionh","goFonDialOptionH","cn") as $val){
876 unset($this->$val);
877 unset($this->attrs[$val]);
878 unset($tmp[$val]);
879 }
880 foreach(array_flip($tmp) as $key => $val){
881 $tmp2[]=$val;
882 }
883 $this->attributes = $tmp2;
885 /* include global link_info */
886 $ldap= $this->config->get_ldap_link();
888 /* Remove and write to LDAP */
889 plugin::remove_from_parent();
891 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save");
892 $ldap->cd($this->dn);
893 $this->cleanup();
894 $ldap->modify ($this->attrs);
895 @log::log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
896 show_ldap_error($ldap->get_error(), _("Removing phone queue failed"));
897 }
900 function getCopyDialog()
901 {
902 $str = "";
903 $str .= _("Phone number");
904 $str .= " <input type='text' name='telephoneNumber' value='".$this->telephoneNumber."'>";
905 return($str);
906 }
909 function saveCopyDialog()
910 {
911 if(isset($_POST['telephoneNumber'])){
912 $this->telephoneNumber = $_POST['telephoneNumber'];
913 }
914 }
917 function plInfo()
918 {
919 return (array(
920 "plShortName" => _("Phone"),
921 "plDescription" => _("Phone group"),
922 "plSelfModify" => FALSE,
923 "plDepends" => array(),
924 "plPriority" => 3,
925 "plSection" => array("administration"),
926 "plCategory" => array("ogroups"),
927 "plProvidedAcls"=> array(
929 "goFonTimeOut" => _("Timeout"),
930 "goFonMaxLen" => _("Max queue length"),
931 "goFonHomeServer" => _("Home server"),
932 "goFonAnnounceFrequency" => _("Announce frequency"),
933 "goFonDialOptiont" => _("Allow the called user to transfer his call"),
934 "goFonDialOptionT" => _("Allows calling user to transfer call"),
935 "goFonDialOptionh" => _("Allow the called to hangup by pressing *"),
936 "goFonDialOptionr" => _("Ring instead of playing background music"),
937 "goFonDialOptionH" => _("Allows calling to hangup by pressing *"),
939 "goFonMusiconHold" => _("Music on hold"),
940 "goFonWelcomeMusic" => _("Welcome music"),
941 "goFonQueueReportHold" => _("Report hold time"),
942 "goFonQueueYouAreNext" => _("'You are next' sound"),
943 "goFonQueueThereAre" => _("'There are' sound"),
944 "goFonQueueCallsWaiting" => _("'Call waiting' sound"),
945 "goFonQueueThankYou" => _("'Thank you' sound"),
946 "goFonQueueMinutes" => _("'Minutes' sound"),
947 "goFonQueueSeconds" => _("'Seconds' sound"),
948 "goFonQueueLessThan" => _("'Less than' sound"),
949 "telephoneNumber" => _("Queue phone number"),
950 "goFonQueueLanguage" => _("Language"),
951 "goFonQueueStrategy" => _("Method"),
952 "goFonQueueAnnounceHoldtime"=> _("Announce holdtime"),
953 "goFonQueueAnnounce" => _("Announce"),
954 "goFonQueueRetry" => _("Retry"))
955 ));
956 }
958 }
960 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
961 ?>