root/modules/branches/2.4/queues/functions.inc.php

Revision 6415, 19.5 kB (checked in by p_lindheimer, 5 years ago)

Merged revisions 6413-6414 via svnmerge from re #3083
http://svn.freepbx.org/modules/branches/2.5

........

r6413 | p_lindheimer | 2008-08-17 11:47:47 -0700 (Sun, 17 Aug 2008) | 1 line


fixes #3083, setting ringinuse causes transfered call to keep agent as unavailable, removing since it is not needed for FreePBX standard agents and was only included for people doing non standard things like agents.conf or sip channel agents

........

r6414 | p_lindheimer | 2008-08-17 11:49:48 -0700 (Sun, 17 Aug 2008) | 1 line


Module Publish Script: queues 2.5.2.3

........

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php /* $id:$ */
2
3 class queues_conf {
4     // return an array of filenames to write
5     // files named like pinset_N
6     function get_filename() {
7         return "queues_additional.conf";
8     }
9     
10     // return the output that goes in each of the files
11     function generateConf() {
12
13         global $db;
14         global $version;
15
16         $additional = "";
17         $output = "";
18         // Asterisk 1.4 does not like blank assignments so just don't put them there
19         //
20         $ver12 = version_compare($version, '1.4', 'lt');
21         
22         // legacy but in case someone was using this we will leave it
23         //
24         $sql = "SELECT keyword,data FROM queues_details WHERE id='-1' AND keyword <> 'account'";
25         $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
26         if(DB::IsError($results)) {
27            die($results->getMessage());
28         }
29         foreach ($results as $result) {
30             if (!$ver12 && trim($result['data']) == '') {
31                 continue;
32             }
33             $additional .= $result['keyword']."=".$result['data']."\n";
34         }
35
36         $results = queues_list();
37         foreach ($results as $result) {
38             $output .= "[".$result[0]."]\n";
39
40             // passing 2nd param 'true' tells queues_get to send back only queue_conf required params
41             // and nothing else
42             //
43             $results2 = queues_get($result[0], true);
44
45             // memebers is an array of members so we set it asside and remove it
46             // and then generate each later
47             //
48             $members = $results2['member'];
49             unset($results2['member']);
50
51             foreach ($results2 as $keyword => $data) {
52                 if ($ver12){
53                     $output .= $keyword."=".$data."\n";
54                 }else{
55                     switch($keyword){
56                         case (trim($data) == ''):
57                         case 'monitor-join':
58                             break;
59                         case 'monitor-format':
60                             if (strtolower($data) != 'no'){
61                                 $output .= "monitor-type=mixmonitor\n";
62                                 $output .= $keyword."=".$data."\n";
63                             }
64                             break;
65                         case 'ringinuse':
66                             break;
67                         default:
68                             $output .= $keyword."=".$data."\n";
69                             break;
70                     }
71                 }
72             }
73
74             // Now pull out all the memebers, one line for each
75             //
76             foreach ($members as $member) {
77                 $output .= "member=".$member."\n";
78             }
79             $output .= $additional."\n";
80         }
81
82         // Before returning the results, do an integrity check to see
83         // if there are any truncated compound recrodings and if so
84         // crate a noticication.
85         //
86         $nt = notifications::create($db);
87
88         $compound_recordings = queues_check_compoundrecordings();
89         if (empty($compound_recordings)) {
90             $nt->delete('queues', 'COMPOUNDREC');
91         } else {
92             $str = _("Warning, there are compound recordings configured in one or more Queue configurations. Queues can not play these so they have been truncated to the first sound file. You should correct this problem.<br />Details:<br /><br />");
93             foreach ($compound_recordings as $item) {
94                 $str .= sprintf(_("Queue - %s (%s): %s<br />"), $item['extension'], $item['descr'], $item['error']);
95             }
96             $nt->add_error('queues', 'COMPOUNDREC', _("Compound Recordings in Queues Detected"), $str);
97         }
98         return $output;
99     }
100 }
101
102 // The destinations this module provides
103 // returns a associative arrays with keys 'destination' and 'description'
104 function queues_destinations() {
105     //get the list of all exisiting
106     $results = queues_list();
107     
108     //return an associative array with destination and description
109     if (isset($results)) {
110         foreach($results as $result){
111                 $extens[] = array('destination' => 'ext-queues,'.$result['0'].',1', 'description' => $result['1'].' <'.$result['0'].'>');
112         }
113     }
114     
115     if (isset($extens))
116         return $extens;
117     else
118         return null;
119 }
120
121 function queues_getdest($exten) {
122     return array('ext-queues,'.$exten.',1');
123 }
124
125 function queues_getdestinfo($dest) {
126     global $active_modules;
127
128     if (substr(trim($dest),0,11) == 'ext-queues,') {
129         $exten = explode(',',$dest);
130         $exten = $exten[1];
131         $thisexten = queues_get($exten);
132         if (empty($thisexten)) {
133             return array();
134         } else {
135             //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
136             return array('description' => 'Queue '.$exten.' : '.$thisexten['name'],
137                          'edit_url' => 'config.php?display=queues&extdisplay='.urlencode($exten),
138                                   );
139         }
140     } else {
141         return false;
142     }
143 }
144
145 /*     Generates dialplan for "queues" components (extensions & inbound routing)
146     We call this with retrieve_conf
147 */
148 function queues_get_config($engine) {
149     global $ext// is this the best way to pass this?
150     switch($engine) {
151         case "asterisk":
152             /* queue extensions */
153             $ext->addInclude('from-internal-additional','ext-queues');
154             $qlist = queues_list();
155             if (is_array($qlist)) {
156                 foreach($qlist as $item) {
157                     
158                     $exten = $item[0];
159                     $q = queues_get($exten);
160
161                     $grppre = (isset($q['prefix'])?$q['prefix']:'');
162                     $alertinfo = (isset($q['alertinfo'])?$q['alertinfo']:'');
163                     
164                     $ext->add('ext-queues', $exten, '', new ext_macro('user-callerid'));
165                     $ext->add('ext-queues', $exten, '', new ext_answer(''));
166
167                     // block voicemail until phone is answered at which point a macro should be called on the answering
168                     // line to clear this flag so that subsequent transfers can occur.
169                     //
170                     $ext->add('ext-queues', $exten, '', new ext_setvar('__BLKVM_OVERRIDE', 'BLKVM/${EXTEN}/${CHANNEL}'));
171                     $ext->add('ext-queues', $exten, '', new ext_setvar('__BLKVM_BASE', '${EXTEN}'));
172                     $ext->add('ext-queues', $exten, '', new ext_setvar('DB(${BLKVM_OVERRIDE})', 'TRUE'));
173                     $ext->add('ext-queues', $exten, '', new ext_setvar('_DIAL_OPTIONS', '${DIAL_OPTIONS}M(auto-blkvm)'));
174
175                     // Inform all the children NOT to send calls to destinations or voicemail
176                     //
177                     $ext->add('ext-queues', $exten, '', new ext_setvar('__NODEST', '${EXTEN}'));
178
179                     // deal with group CID prefix
180                     // Use the same variable as ringgroups/followme so that we can manage chaines of calls
181                     // but strip only if you plan on setting a new one
182                     //
183                     if ($grppre != '') {
184                         $ext->add('ext-queues', $exten, '', new ext_gotoif('$["foo${RGPREFIX}" = "foo"]', 'REPCID'));
185                         $ext->add('ext-queues', $exten, '', new ext_gotoif('$["${RGPREFIX}" != "${CALLERID(name):0:${LEN(${RGPREFIX})}}"]', 'REPCID'));
186                         $ext->add('ext-queues', $exten, '', new ext_noop('Current RGPREFIX is ${RGPREFIX}....stripping from Caller ID'));
187                         $ext->add('ext-queues', $exten, '', new ext_setvar('CALLERID(name)', '${CALLERID(name):${LEN(${RGPREFIX})}}'));
188                         $ext->add('ext-queues', $exten, '', new ext_setvar('_RGPREFIX', ''));
189                         $ext->add('ext-queues', $exten, 'REPCID', new ext_noop('CALLERID(name) is ${CALLERID(name)}'));
190                         $ext->add('ext-queues', $exten, '', new ext_setvar('_RGPREFIX', $grppre));
191                         $ext->add('ext-queues', $exten, '', new ext_setvar('CALLERID(name)','${RGPREFIX}${CALLERID(name)}'));
192                     }
193
194                     // Set Alert_Info
195                     if ($alertinfo != '') {
196                         $ext->add('ext-queues', $exten, '', new ext_setvar('__ALERT_INFO', str_replace(';', '\;', $alertinfo)));
197                     }
198
199                     $ext->add('ext-queues', $exten, '', new ext_setvar('MONITOR_FILENAME','/var/spool/asterisk/monitor/q${EXTEN}-${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}-${UNIQUEID}'));
200                     $joinannounce = (isset($q['joinannounce'])?$q['joinannounce']:'');
201                     if($joinannounce != "") {
202                         $ext->add('ext-queues', $exten, '', new ext_playback($joinannounce));
203                     }
204                     $options = 't';
205                     if ($q['rtone'] == 1)
206                         $options .= 'r';
207                     if (isset($q['music'])) {
208                          $ext->add('ext-queues', $exten, '', new ext_setvar('__MOHCLASS', $q['music']));
209                     }
210                     // Set CWIGNORE  if enabled so that busy agents don't have another line key ringing and
211                     // stalling the ACD.
212                     if ($q['cwignore']) {
213                          $ext->add('ext-queues', $exten, '', new ext_setvar('_CWIGNORE', 'TRUE'));
214                     }
215                     $agentannounce = (isset($q['agentannounce'])?$q['agentannounce']:'');
216                     $ext->add('ext-queues', $exten, '', new ext_queue($exten,$options,'',$agentannounce,$q['maxwait']));
217  
218                     $ext->add('ext-queues', $exten, '', new ext_dbdel('${BLKVM_OVERRIDE}'));
219                      // If we are here, disable the NODEST as we want things to resume as normal
220                      //
221                      $ext->add('ext-queues', $exten, '', new ext_setvar('__NODEST', ''));
222     
223                     // destination field in 'incoming' database is backwards from what ext_goto expects
224                     $goto_context = strtok($q['goto'],',');
225                     $goto_exten = strtok(',');
226                     $goto_pri = strtok(',');
227                     
228                     $ext->add('ext-queues', $exten, '', new ext_goto($goto_pri,$goto_exten,$goto_context));
229                     
230                     //dynamic agent login/logout
231                     $ext->add('ext-queues', $exten."*", '', new ext_macro('agent-add',$exten.",".$q['password']));
232                     $ext->add('ext-queues', $exten."**", '', new ext_macro('agent-del',$exten.",".$exten));
233                 }
234             }
235         break;
236     }
237 }
238
239 function queues_timeString($seconds, $full = false) {
240     if ($seconds == 0) {
241         return "0 ".($full ? "seconds" : "s");
242     }
243
244     $minutes = floor($seconds / 60);
245     $seconds = $seconds % 60;
246
247     $hours = floor($minutes / 60);
248     $minutes = $minutes % 60;
249
250     $days = floor($hours / 24);
251     $hours = $hours % 24;
252
253     if ($full) {
254          return substr(
255                       ($days ? $days." day".(($days == 1) ? "" : "s").", " : "").
256                       ($hours ? $hours." hour".(($hours == 1) ? "" : "s").", " : "").
257                       ($minutes ? $minutes." minute".(($minutes == 1) ? "" : "s").", " : "").
258                       ($seconds ? $seconds." second".(($seconds == 1) ? "" : "s").", " : ""),
259                       0, -2);
260     } else {
261         return substr(($days ? $days."d, " : "").($hours ? $hours."h, " : "").($minutes ? $minutes."m, " : "").($seconds ? $seconds."s, " : ""), 0, -2);
262     }
263 }
264
265 function queues_add($account,$name,$password,$prefix,$goto,$agentannounce,$members,$joinannounce,$maxwait,$alertinfo='',$cwignore='no') {
266     global $db;
267
268     if (trim($account) == '') {
269         echo "<script>javascript:alert('"._("Bad Queue Number, can not be blank")."');</script>";
270         return false;
271     }
272
273     //add to extensions table
274     if (empty($agentannounce) || $agentannounce == 'None') {
275         $agentannounce="";
276     }
277
278 $fields = array(
279     array($account,'maxlen',($_REQUEST['maxlen'])?$_REQUEST['maxlen']:'0',0),
280     array($account,'joinempty',($_REQUEST['joinempty'])?$_REQUEST['joinempty']:'yes',0),
281     array($account,'leavewhenempty',($_REQUEST['leavewhenempty'])?$_REQUEST['leavewhenempty']:'no',0),
282     array($account,'strategy',($_REQUEST['strategy'])?$_REQUEST['strategy']:'ringall',0),
283     array($account,'timeout',(isset($_REQUEST['timeout']))?$_REQUEST['timeout']:'15',0),
284     array($account,'retry',(isset($_REQUEST['retry']) && $_REQUEST['retry'] != '')?$_REQUEST['retry']:'5',0),
285     array($account,'wrapuptime',($_REQUEST['wrapuptime'])?$_REQUEST['wrapuptime']:'0',0),
286     array($account,'announce-frequency',($_REQUEST['announcefreq'])?$_REQUEST['announcefreq']:'0',0),
287     array($account,'announce-holdtime',($_REQUEST['announceholdtime'])?$_REQUEST['announceholdtime']:'no',0),
288     array($account,'queue-youarenext',($_REQUEST['announceposition']=='no')?'silence/1':'queue-youarenext',0),  //if no, play no sound
289     array($account,'queue-thereare',($_REQUEST['announceposition']=='no')?'silence/1':'queue-thereare',0),  //if no, play no sound
290     array($account,'queue-callswaiting',($_REQUEST['announceposition']=='no')?'silence/1':'queue-callswaiting',0),  //if no, play no sound
291     array($account,'queue-thankyou',($_REQUEST['announceposition']=='no')?'':'queue-thankyou',0),  //if no, play no sound
292     array($account,'periodic-announce-frequency',($_REQUEST['pannouncefreq'])?$_REQUEST['pannouncefreq']:'0',0),
293     array($account,'monitor-format',($_REQUEST['monitor-format'])?$_REQUEST['monitor-format']:'',0),
294     array($account,'monitor-join','yes',0),
295     array($account,'eventwhencalled',($_REQUEST['eventwhencalled'])?$_REQUEST['eventwhencalled']:'no',0),
296     array($account,'eventmemberstatus',($_REQUEST['eventmemberstatus'])?$_REQUEST['eventmemberstatus']:'no',0),
297 );
298
299     if ($_REQUEST['music'] != 'inherit') {
300         $fields[] = array($account,'music',($_REQUEST['music'])?$_REQUEST['music']:'default',0);
301     }
302
303     //there can be multiple members
304     if (isset($members)) {
305         $count = 0;
306         foreach ($members as $member) {
307             $fields[] = array($account,'member',$member,$count);
308             $count++;
309         }
310     }
311
312     $compiled = $db->prepare('INSERT INTO queues_details (id, keyword, data, flags) values (?,?,?,?)');
313     $result = $db->executeMultiple($compiled,$fields);
314     if(DB::IsError($result)) {
315         die_freepbx($result->getMessage()."<br><br>error adding to queues_details table");   
316     }
317     $extension        = $account;
318     $descr         = isset($name) ? addslashes($name):'';
319     $grppre        = isset($prefix) ? addslashes($prefix):'';
320     $alertinfo     = isset($alertinfo) ? addslashes($alertinfo):'';
321     $joinannounce  = strtolower($joinannounce) != 'none' ? $joinannounce:'';
322     $ringing       = isset($_REQUEST['rtone']) ? $_REQUEST['rtone']:'';
323     $agentannounce = strtolower($agentannounce) != 'none' ? $agentannounce:'';
324     $maxwait       = isset($maxwait) ? $maxwait:'';
325     $password      = isset($password) ? $password:'';
326     $ivr_id        = isset($_REQUEST['announcemenu']) ? $_REQUEST['announcemenu']:'none';
327     $dest          = isset($goto) ? $goto:'';
328     $cwignore      = isset($cwignore) ? $cwignore:'0';
329
330     // Assumes it has just been deleted
331     $sql = "INSERT INTO queues_config (extension, descr, grppre, alertinfo, joinannounce, ringing, agentannounce, maxwait, password, ivr_id, dest, cwignore)
332              VALUES ('$extension', '$descr', '$grppre', '$alertinfo', '$joinannounce', '$ringing', '$agentannounce', '$maxwait', '$password', '$ivr_id', '$dest', '$cwignore')    ";
333     $results = sql($sql);
334     return true;
335 }
336
337 function queues_del($account) {
338     global $db;
339     
340     $sql = "DELETE FROM queues_details WHERE id = '$account'";
341     $result = $db->query($sql);
342     if(DB::IsError($result)) {
343         die_freepbx($result->getMessage().$sql);
344     }
345     $sql = "DELETE FROM queues_config WHERE extension = '$account'";
346     $result = $db->query($sql);
347     if(DB::IsError($result)) {
348         die_freepbx($result->getMessage().$sql);
349     }
350
351 }
352
353 //get the existing queue extensions
354 function queues_list() {
355     global $db;
356     $sql = "SELECT extension, descr FROM queues_config ORDER BY extension";
357     $results = $db->getAll($sql);
358     if(DB::IsError($results)) {
359         $results = array();
360     }
361
362     foreach($results as $result){
363         if (checkRange($result[0])){
364             $extens[] = array($result[0],$result[1]);
365         }
366     }
367     if (isset($extens)) {
368         return $extens;
369     } else {
370         return array();
371     }
372 }
373
374 function queues_check_extensions($exten=true) {
375     global $active_modules;
376
377     $extenlist = array();
378     if (is_array($exten) && empty($exten)) {
379         return $extenlist;
380     }
381     $sql = "SELECT extension, descr FROM queues_config ";
382     if (is_array($exten)) {
383         $sql .= "WHERE extension in ('".implode("','",$exten)."')";
384     }
385     $sql .= " ORDER BY extension";
386     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
387
388     //$type = isset($active_modules['queues']['type'])?$active_modules['queues']['type']:'setup';
389     foreach ($results as $result) {
390         $thisexten = $result['extension'];
391         $extenlist[$thisexten]['description'] = _("Queue: ").$result['descr'];
392         $extenlist[$thisexten]['status'] = 'INUSE';
393         $extenlist[$thisexten]['edit_url'] = 'config.php?display=queues&extdisplay='.urlencode($thisexten);
394     }
395     return $extenlist;
396 }
397
398 function queues_check_destinations($dest=true) {
399     global $active_modules;
400
401     $destlist = array();
402     if (is_array($dest) && empty($dest)) {
403         return $destlist;
404     }
405     $sql = "SELECT extension, descr, dest FROM queues_config";
406     if ($dest !== true) {
407         $sql .= " WHERE dest in ('".implode("','",$dest)."')";
408     }
409     $sql .= " ORDER BY extension";
410
411     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
412
413     //$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup';
414
415     foreach ($results as $result) {
416         $thisdest = $result['dest'];
417         $thisid   = $result['extension'];
418         $destlist[] = array(
419             'dest' => $thisdest,
420             'description' => 'Queue: '.$result['descr'].'('.$thisid.')',
421             'edit_url' => 'config.php?display=queues&extdisplay='.urlencode($thisid),
422         );
423     }
424     return $destlist;
425 }
426
427 function queues_check_compoundrecordings() {
428     global $db;
429
430     $compound_recordings = array();
431     $sql = "SELECT extension, descr, agentannounce, ivr_id FROM queues_config WHERE (ivr_id != 'none' AND ivr_id != '') OR agentannounce != ''";
432     $results = sql($sql, "getAll",DB_FETCHMODE_ASSOC);
433
434     if (function_exists('ivr_list')) {
435         $ivr_details = ivr_list();
436         foreach ($ivr_details as $item) {
437             $ivr_hash[$item['ivr_id']] = $item;
438         }
439         $check_ivr = true;
440     } else {
441         $check_ivr = false;
442     }
443
444     foreach ($results as $result) {
445         if (strpos($result['agentannounce'],"&") !== false) {
446             $compound_recordings[] = array(
447                                            'extension' => $result['extension'],
448                                                                  'descr' => $result['descr'],
449                                                                  'error' => _("Agent Announce Msg"),
450                                                              );
451         }
452         if ($result['ivr_id'] != 'none' && $result['ivr_id'] != '' && $check_ivr) {
453             if (strpos($ivr_hash[$result['ivr_id']]['announcement'],"&") !== false) {
454                 $compound_recordings[] = array(
455                                                'extension' => $result['extension'],
456                                                                      'descr' => $result['descr'],
457                                                                      'error' => sprintf(_("IVR Announce: %s"),$ivr_hash[$result['ivr_id']]['displayname']),
458                                                                  );
459             }
460         }
461     }
462     return $compound_recordings;
463 }
464
465
466 function queues_get($account, $queues_conf_only=false) {
467     global $db;
468     
469     if ($account == "")
470     {
471         return array();
472     }
473
474     $account = q($account);
475     //get all the variables for the queue
476     $sql = "SELECT keyword,data FROM queues_details WHERE id = $account";
477     $results = $db->getAssoc($sql);
478     if (empty($results)) {
479         return array();
480     }
481
482     //okay, but there can be multiple member variables ... do another select for them
483     $sql = "SELECT data FROM queues_details WHERE id = $account AND keyword = 'member' order by flags";
484     $results['member'] = $db->getCol($sql);
485     
486     //if 'queue-youarenext=queue-youarenext', then assume we want to announce position
487     if (!$queues_conf_only) {
488         if(isset($results['queue-youarenext']) && $results['queue-youarenext'] == 'queue-youarenext') {
489             $results['announce-position'] = 'yes';
490         } else {
491             $results['announce-position'] = 'no';
492         }
493     }
494     
495     //if 'eventmemberstatusoff=Yes', then assume we want to 'eventmemberstatus=no'
496     if(isset($results['eventmemberstatusoff'])) {
497         if (strtolower($results['eventmemberstatusoff']) == 'yes') {
498             $results['eventmemberstatus'] = 'no';
499         } else {
500             $results['eventmemberstatus'] = 'yes';
501         }
502     } elseif (!isset($results['eventmemberstatus'])){
503         $results['eventmemberstatus'] = 'no';
504     }
505
506     if ($queues_conf_only) {
507         $sql = "SELECT ivr_id FROM queues_config WHERE extension = $account";
508         $config = sql($sql, "getRow",DB_FETCHMODE_ASSOC);
509
510         // We need to strip off all but the first sound file of any compound sound files
511         //
512         $agentannounce_arr        = explode("&", $config['agentannounce']);
513         $results['agentannounce'] = $agentannounce_arr[0];
514     } else {
515         $sql = "SELECT * FROM queues_config WHERE extension = $account";
516         $config = sql($sql, "getRow",DB_FETCHMODE_ASSOC);
517
518         $results['prefix']        = $config['grppre'];
519         $results['alertinfo']     = $config['alertinfo'];
520         $results['agentannounce'] = $config['agentannounce'];
521         $results['maxwait']       = $config['maxwait'];
522         $results['name']          = $config['descr'];
523         $results['joinannounce']  = $config['joinannounce'];
524         $results['password']      = $config['password'];
525         $results['goto']          = $config['dest'];
526         $results['announcemenu']  = $config['ivr_id'];
527         $results['rtone']         = $config['ringing'];
528         $results['cwignore']      = $config['cwignore'];
529     }
530
531     $results['context'] = '';
532     $results['periodic-announce'] = '';
533
534     if ($config['ivr_id'] != 'none' && $config['ivr_id'] != '') {
535         if (function_exists('ivr_get_details')) {
536             $results['context'] = "ivr-".$config['ivr_id'];
537             $arr = ivr_get_details($config['ivr_id']);
538             if( isset($arr['announcement']) && $arr['announcement'] != '') {
539
540                 // We need to strip off all but the first sound file of any compound sound files
541                 //
542                 $periodic_arr = explode("&", $arr['announcement']);
543                 $results['periodic-announce'] = $periodic_arr[0];
544             }
545         }
546     }
547
548     return $results;
549 }
550 ?>
551
Note: See TracBrowser for help on using the browser.