root/modules/branches/2.10/ivr/functions.inc.php

Revision 14238, 25.8 kB (checked in by p_lindheimer, 11 months ago)

fixes #5882 always put the return extenion in the IVRs as it is used as a destination from sub IVR or Announcements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
3  /* $Id$ */
4
5 // The destinations this module provides
6 // returns a associative arrays with keys 'destination' and 'description'
7 function ivr_destinations() {
8     global $module_page;
9     
10     //get the list of IVR's
11     $results = ivr_get_details();
12
13     // return an associative array with destination and description
14     if (isset($results)) {
15         foreach($results as $result){
16             $name = $result['name'] ? $result['name'] : 'IVR ' . $result['id'];
17             $extens[] = array('destination' => 'ivr-'.$result['id'].',s,1', 'description' => $name);
18         }
19     }
20     if (isset($extens)) {
21         return $extens;
22     } else {
23         return null;
24     }
25
26 }
27
28 //dialplan generator
29 function ivr_get_config($engine) {
30     global $ext;
31
32     switch($engine) {
33         case "asterisk":
34             $ddial_contexts = array();
35             $ivrlist = ivr_get_details();
36             if(!is_array($ivrlist)) {
37                 break;
38             }
39             
40             if (function_exists('queues_list')) {
41                 //draw a list of ivrs included by any queues
42                 $queues = queues_list(true);
43                 $qivr = array();
44                 foreach ($queues as $q) {
45                     $thisq = queues_get($q[0]);
46                     if ($thisq['context'] && strpos($thisq['context'], 'ivr-') === 0) {
47                         $qivr[] = str_replace('ivr-', '', $thisq['context']);
48                     }
49                 }
50             }
51             
52             foreach($ivrlist as $ivr) {
53                 $c = 'ivr-' . $ivr['id'];
54                 $ivr = ivr_get_details($ivr['id']);
55                 $ext->addSectionComment($c, $ivr['name'] ? $ivr['name'] : 'IVR ' . $ivr['id']);
56                 
57                 if ($ivr['directdial']) {
58                     if ($ivr['directdial'] == 'ext-local') {
59                         $ext->addInclude($c, 'from-did-direct-ivr'); //generated in core module
60                     } else {
61                         //generated by directory
62                         $ext->addInclude($c, 'from-ivr-directory-' . $ivr['directdial']);
63                         $directdial_contexts[$ivr['directdial']] = $ivr['directdial'];
64                     }
65                 }
66
67                 //set variables for loops when used
68                 if ($ivr['timeout_loops'] != 'disabled' && $ivr['timeout_loops'] > 0) {
69                     $ext->add($c, 's', '', new ext_setvar('TIMEOUT_LOOPCOUNT', 0));
70                 }
71                 if ($ivr['invalid_loops'] != 'disabled' && $ivr['invalid_loops'] > 0) {
72                     $ext->add($c, 's', '', new ext_setvar('INVALID_LOOPCOUNT', 0));
73                 }
74                 
75                 $ext->add($c, 's', '', new ext_setvar('_IVR_CONTEXT_${CONTEXT}', '${IVR_CONTEXT}'));
76                 $ext->add($c, 's', '', new ext_setvar('_IVR_CONTEXT', '${CONTEXT}'));
77                 if ($ivr['retvm']) {
78                     $ext->add($c, 's', '', new ext_setvar('__IVR_RETVM', 'RETURN'));
79                 } else {
80                     //TODO: do we need to set anything at all?
81                     $ext->add($c, 's', '', new ext_setvar('__IVR_RETVM', ''));
82                 }
83                 
84                 $ext->add($c, 's', '', new ext_gotoif('$["${CDR(disposition)}" = "ANSWERED"]','skip'));
85                 $ext->add($c, 's', '', new ext_answer(''));
86                 $ext->add($c, 's', '', new ext_wait('1'));
87
88                 $ivr_announcement = recordings_get_file($ivr['announcement']);
89                 $ext->add($c, 's', 'skip', new ext_set('IVR_MSG', $ivr_announcement));
90
91                 $ext->add($c, 's', 'start', new ext_digittimeout(3));
92                 //$ext->add($ivr_id, 's', '', new ext_responsetimeout($ivr['timeout_time']));
93
94                 $ext->add($c, 's', '', new ext_execif('$["${IVR_MSG}" != ""]','Background','${IVR_MSG}'));
95                 $ext->add($c, 's', '', new ext_waitexten($ivr['timeout_time']));
96                 
97
98                 // Actually add the IVR entires now
99                 $entries = ivr_get_entries($ivr['id']);
100
101                 if ($entries) {
102                     foreach($entries as $e) {
103                         //dont set a t or i if there already defined above
104                         if ($e['selection'] == 't' && $ivr['timeout_loops'] != 'disabled') {
105                              continue;
106                         }
107                         if ($e['selection'] == 'i' && $ivr['invalid_loops'] != 'disabled') {
108                              continue;
109                         }
110                         
111                         //only display these two lines if the ivr is included in any queues
112                         if (function_exists('queues_list') && in_array($ivr['id'], $qivr)) {
113                             $ext->add($c, $e['selection'],'', new ext_macro('blkvm-clr'));
114                             $ext->add($c, $e['selection'], '', new ext_setvar('__NODEST', ''));
115                         }
116
117                         if ($e['ivr_ret']) {
118                             $ext->add($c, $e['selection'], '',
119                                 new ext_gotoif('$["x${IVR_CONTEXT_${CONTEXT}}" = "x"]',
120                                     $e['dest'] . ':${IVR_CONTEXT_${CONTEXT}},return,1'));
121                         } else {
122                             $ext->add($c, $e['selection'],'', new ext_goto($e['dest']));
123                         }
124                     }
125                 }
126         
127                 // add invalid destination if required
128                 if ($ivr['invalid_loops'] != 'disabled') {
129                     if ($ivr['invalid_loops'] > 0) {
130                         $ext->add($c, 'i', '', new ext_set('INVALID_LOOPCOUNT', '$[${INVALID_LOOPCOUNT}+1]'));
131                         $ext->add($c, 'i', '',    new ext_gotoif('$[${INVALID_LOOPCOUNT} > ' . $ivr['invalid_loops'] . ']','final'));
132                         switch ($ivr['invalid_retry_recording']) {
133                             case 'default':
134                                 $invalid_annoucement = 'no-valid-responce-pls-try-again';
135                                 break;
136                             case '':
137                                 $invalid_annoucement = '';
138                                 break;
139                             default:
140                                 $invalid_annoucement = recordings_get_file($ivr['invalid_retry_recording']);
141                                 break;
142                         }
143
144                         if ($ivr['invalid_append_announce'] || $invalid_annoucement == '') {
145                             $invalid_annoucement .= '&' . $ivr_announcement;
146                         }
147                         $ext->add($c, 'i', '', new ext_set('IVR_MSG', trim($invalid_annoucement, '&')));
148                         $ext->add($c, 'i', '', new ext_goto('s,start'));
149                     }
150
151                     $label = 'final';
152                     switch ($ivr['invalid_recording']) {
153                         case 'default':
154                             $ext->add($c, 'i', $label, new ext_playback('no-valid-responce-transfering'));
155                             $label ='';
156                             break;
157                         case '':
158                             break;
159                         default:
160                             $ext->add($c, 'i', $label,
161                                 new ext_playback(recordings_get_file($ivr['invalid_recording'])));
162                             $label = '';
163                             break;
164                     }
165                     $ext->add($c, 'i', $label, new ext_goto($ivr['invalid_destination']));
166                 } else {
167                     // If no invalid destination provided we need to do something
168                     $ext->add($c, 'i', '', new ext_playback('sorry-youre-having-problems'));
169                     $ext->add($c, 'i', '', new ext_goto('1','hang'));
170                 }
171
172                 // Apply timeout destination if required
173                 if ($ivr['timeout_loops'] != 'disabled') {
174                     if ($ivr['timeout_loops'] > 0) {
175                         $ext->add($c, 't', '', new ext_set('TIMEOUT_LOOPCOUNT', '$[${TIMEOUT_LOOPCOUNT}+1]'));
176                         $ext->add($c, 't', '', new ext_gotoif('$[${TIMEOUT_LOOPCOUNT} > ' . $ivr['timeout_loops'] . ']','final'));
177
178                         switch ($ivr['timeout_retry_recording']) {
179                             case 'default':
180                                 $timeout_annoucement = 'no-valid-responce-pls-try-again';
181                                 break;
182                             case '':
183                                 $timeout_annoucement = '';
184                                 break;
185                             default:
186                                 $timeout_annoucement = recordings_get_file($ivr['timeout_retry_recording']);
187                                 break;
188                         }
189
190                         if ($ivr['timeout_append_announce'] || $timeout_annoucement == '') {
191                             $timeout_annoucement .= '&' . $ivr_announcement;
192                         }
193                         $ext->add($c, 't', '', new ext_set('IVR_MSG', trim($timeout_annoucement, '&')));
194                         $ext->add($c, 't', '', new ext_goto('s,start'));
195                     }
196                     
197                     $label = 'final';
198                     switch ($ivr['timeout_recording']) {
199                         case 'default':
200                             $ext->add($c, 't', $label, new ext_playback('no-valid-responce-transfering'));
201                             $label = '';
202                             break;
203                         case '':
204                             break;
205                         default:
206                             $ext->add($c, 't', $label,
207                                 new ext_playback(recordings_get_file($ivr['timeout_recording'])));
208                             $label = '';
209                             break;
210                     }
211                     $ext->add($c, 't', $label, new ext_goto($ivr['timeout_destination']));
212                 } else {
213                     // If no invalid destination provided we need to do something
214                     $ext->add($c, 't', '', new ext_playback('sorry-youre-having-problems'));
215                     $ext->add($c, 't', '', new ext_goto('1','hang'));
216                 }
217                 
218                 // these need to be reset or inheritance problems makes them go away in some conditions
219                 //and infinite inheritance creates other problems
220                 $ext->add($c, 'return', '', new ext_setvar('_IVR_CONTEXT', '${CONTEXT}'));
221                 $ext->add($c, 'return', '', new ext_setvar('_IVR_CONTEXT_${CONTEXT}', '${IVR_CONTEXT_${CONTEXT}}'));
222                 $ext->add($c, 'return', '', new ext_set('IVR_MSG', $ivr_announcement));
223                 $ext->add($c, 'return', '', new ext_goto('s,start'));
224             
225                 //h extension
226                 $ext->add($c, 'h', '', new ext_hangup(''));
227                 $ext->add($c, 'hang', '', new ext_playback('vm-goodbye'));
228                 $ext->add($c, 'hang', '', new ext_hangup(''));
229             }
230             
231             
232             //generate from-ivr-directory contexts for direct dialing a directory entire
233             if (!empty($directdial_contexts)) {
234                 foreach($directdial_contexts as $dir_id) {
235                     $c = 'from-ivr-directory-' . $dir_id;
236                     $entries = function_exists('directory_get_dir_entries') ? directory_get_dir_entries($dir_id) : array();
237                     foreach ($entries as $dstring) {
238                         $exten = $dstring['dial'] == '' ? $dstring['foreign_id'] : $dstring['dial'];
239                         if ($exten == '' || $exten == 'custom') {
240                             continue;
241                         }
242                         $ext->add($c, $exten, '', new ext_macro('blkvm-clr'));
243                         $ext->add($c, $exten, '', new ext_setvar('__NODEST', ''));
244                         $ext->add($c, $exten, '', new ext_goto('1', $exten, 'from-internal'));
245                     }
246                 }
247             }
248         break;
249     }
250 }
251
252 //replaces ivr_list(), returns all details of any ivr
253 function ivr_get_details($id = '') {
254     global $db;
255
256     $sql = "SELECT *, announcement announcement_id FROM ivr_details";
257     if ($id) {
258         $sql .= ' where  id = "' . $id . '"';
259     }
260     $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
261     if($db->IsError($res)) {
262         die_freepbx($res->getDebugInfo());
263     }
264
265     return $id && isset($res[0]) ? $res[0] : $res;
266 }
267
268 //get all ivr entires
269 function ivr_get_entries($id) {
270     global $db;
271     
272     //+0 to convert string to an integer
273     $sql = "SELECT * FROM ivr_entries WHERE ivr_id = ? ORDER BY selection + 0";
274     $res = $db->getAll($sql, array($id), DB_FETCHMODE_ASSOC);
275     if ($db->IsError($res)) {
276         die_freepbx($res->getDebugInfo());
277     }
278     return $res;
279 }
280
281
282 //draw ivr options page
283 function ivr_configpageload() {
284     global $currentcomponent, $display;
285     $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
286     $id     = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
287
288     if ($action  == 'add') {
289         $currentcomponent->addguielem('_top', new gui_pageheading('title', _('Add IVR')), 0);
290
291         $deet = array('id', 'name', 'description', 'announcement', 'directdial',
292                     'invalid_loops', 'invalid_retry_recording',
293                     'invalid_recording', 'invalid_destination',
294                     'timeout_loops', 'timeout_time',
295                     'timeout_retry_recording', 'timeout_recording', 'timeout_destination',
296                     'retvm');
297     
298         //keep vairables set on new ivr's
299         foreach ($deet as $d) {
300             switch ($d){
301                 case 'invalid_loops':
302                 case 'timeout_loops';
303                     $ivr[$d] = 3;
304                     break;
305                 case 'announcement':
306                     $ivr[$d] = '';
307                     break;
308                 case 'invalid_recording':
309                 case 'invalid_retry_recording':
310                 case 'timeout_retry_recording':
311                 case 'timeout_recording':
312                     $ivr[$d] = 'default';
313                     break;
314                 case 'timeout_time':
315                     $ivr[$d] = 10;
316                     break;
317                 default:
318                 $ivr[$d] = '';
319                     break;
320             }
321         }
322     } else {
323         $ivr = ivr_get_details($id);
324
325         $label = sprintf(_("Edit IVR: %s"), $ivr['name'] ? $ivr['name'] : 'ID '.$ivr['id']);
326         $currentcomponent->addguielem('_top', new gui_pageheading('title', $label), 0);
327         
328         //display usage
329         $usage_list            = framework_display_destination_usage(ivr_getdest($ivr['id']));
330         if (!empty($usage_list)) {
331             $usage_list_text    = isset($usage_list['text']) ? $usage_list['text'] : '';
332             $usage_list_tooltip    = isset($usage_list['tooltip']) ? $usage_list['tooltip'] : '';
333             $currentcomponent->addguielem('_top',
334                 new gui_link_label('usage', $usage_list_text, $usage_list_tooltip), 0);
335         }
336         
337         //display delete link
338         $label = sprintf(_("Delete IVR: %s"), $ivr['name'] ? $ivr['name'] : 'ID '.$ivr['id']);
339         $del                 = '<span><img width="16" height="16" border="0" title="'
340                             . $label . '" alt="" src="images/core_delete.png"/>&nbsp;' . $label . '</span>';
341         $currentcomponent->addguielem('_top',
342             new gui_link('del', $del, $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . '&action=delete',
343                 true, false), 0);
344     }
345     
346     //general options
347     $gen_section = _('IVR General Options');
348     $currentcomponent->addguielem($gen_section,
349         new gui_textbox('name', stripslashes($ivr['name']), _('IVR Name'), _('Name of this IVR.')));
350     $currentcomponent->addguielem($gen_section,
351         new gui_textbox('description', stripslashes($ivr['description']),
352         _('IVR Description'), _('Description of this ivr.')));
353
354     
355     //dtmf options
356     $section = _('IVR Options (DTMF)');
357     
358     //build recordings select list
359     $currentcomponent->addoptlistitem('recordings', '', _('None'));
360     foreach(recordings_list() as $r){
361         $currentcomponent->addoptlistitem('recordings', $r['id'], $r['displayname']);
362     }
363     $currentcomponent->setoptlistopts('recordings', 'sort', false);
364     
365     //build repeat_loops select list and defualt it to 3
366     //while addoptlist is not usually required, declaring this is the only way to prevent sorting on the list
367     $currentcomponent->addoptlist('ivr_repeat_loops', false);
368     $currentcomponent->addoptlistitem('ivr_repeat_loops', 'disabled', 'Disabled');
369     for($i=0; $i <11; $i++){
370         $currentcomponent->addoptlistitem('ivr_repeat_loops', $i, $i);
371     }
372
373     //greating to be played on entry to the ivr
374     $currentcomponent->addguielem($section,
375         new gui_selectbox('announcement', $currentcomponent->getoptlist('recordings'),
376             $ivr['announcement'], _('Announcement'), _('Greeting to be played on entry to the Ivr.'), false));
377
378
379     
380     //direct dial
381     $currentcomponent->addoptlistitem('directdial', '', _('Disabled'));
382     $currentcomponent->addoptlistitem('directdial', 'ext-local', _('Extensions'));
383     
384     $currentcomponent->addgeneralarrayitem('directdial_help', 'disabled', _('Completely disabled'));
385     $currentcomponent->addgeneralarrayitem('directdial_help', 'local', _('Enabled for all extensions on a system'));
386
387     $currentcomponent->addguielem($section,
388         new gui_selectbox('directdial', $currentcomponent->getoptlist('directdial'),
389         $ivr['directdial'], _('Direct Dial'), _('Provides options for callers to direct dial an extension. Direct dialing can be:')
390         . ul($currentcomponent->getgeneralarray('directdial_help')), false));
391     
392     //add default to the recordings list. We dont want this for the general announcement, so we do it here
393     $currentcomponent->addoptlistitem('recordings', 'default', _('Default'));
394     //$currentcomponent->addguielem($section, new gui_textbox('timeout_time', stripslashes($ivr['timeout_time']), _('Timeout'), _('Amount of time to be concidered a timeout')));
395     $currentcomponent->addguielem($section, new guielement('timeout_time',
396         '<tr class="IVROptionsDTMF"><td>' . fpbx_label(_('Timeout'), _('Amount of time to be considered a timeout')).'</td><td><input type="number" name="timeout_time" value="'
397                     . $ivr['timeout_time']
398                     .'" required></td></tr>'));
399     //invalid
400     $currentcomponent->addguielem($section,
401         new gui_selectbox('invalid_loops', $currentcomponent->getoptlist('ivr_repeat_loops'),
402         $ivr['invalid_loops'], _('Invalid Retries'), _('Number of times to retry when receiving an invalid/unmatched response from the caller'), false));
403     $currentcomponent->addguielem($section,
404         new gui_selectbox('invalid_retry_recording', $currentcomponent->getoptlist('recordings'),
405         $ivr['invalid_retry_recording'], _('Invalid Retry Recording'), _('Prompt to be played when an invalid/unmatched response is received, before prompting the caller to try again'), false));
406
407     $currentcomponent->addguielem($section,
408         new gui_checkbox('invalid_append_announce', $ivr['invalid_append_announce'], _('Append Original Annoucement'), _('After playing the Invalid Retry Recording the system will replay the main IVR Annoucement')));
409
410     $currentcomponent->addguielem($section,
411         new gui_selectbox('invalid_recording', $currentcomponent->getoptlist('recordings'),
412         $ivr['invalid_recording'], _('Invalid Recording'), _('Prompt to be played before sending the caller to an alternate destination due to the caller pressing 0 or receiving the maximum amount of invalid/unmatched responses (as determined by Invalid Retries)'), false));
413     $currentcomponent->addguielem($section,
414         new gui_drawselects('invalid_destination', 'invalid', $ivr['invalid_destination'], _('Invalid Destination'),
415          _('Destination to send the call to after Invalid Recording is played.'), true));
416     
417     //timeout
418     $currentcomponent->addguielem($section,
419         new gui_selectbox('timeout_loops', $currentcomponent->getoptlist('ivr_repeat_loops'),
420         $ivr['timeout_loops'], _('Timeout Retries'), _('Number of times to retry when no DTMF is heard and the IVR choice timesout.'), false));
421     $currentcomponent->addguielem($section,
422         new gui_selectbox('timeout_retry_recording', $currentcomponent->getoptlist('recordings'),
423         $ivr['timeout_retry_recording'], _('Timeout Retry Recording'), _('Prompt to be played when an invalid/unmatched response is received, before prompting the caller to try again'), false));
424
425     $currentcomponent->addguielem($section,
426         new gui_checkbox('timeout_append_announce', $ivr['timeout_append_announce'], _('Append Original Annoucement'), _('After playing the Timeout Retry Recording the system will replay the main IVR Annoucement')));
427
428     $currentcomponent->addguielem($section,
429         new gui_selectbox('timeout_recording', $currentcomponent->getoptlist('recordings'),
430         $ivr['timeout_recording'], _('Timeout Recording'), _('Prompt to be played before sending the caller to an alternate destination due to the caller pressing 0 or receiving the maximum amount of invalid/unmatched responses (as determined by Invalid Retries)'), false));
431     $currentcomponent->addguielem($section,
432         new gui_drawselects('timeout_destination', 'timeout',
433         $ivr['timeout_destination'], _('Timeout Destination'), _('Destination to send the call to after Invalid Recording is played.'), true));
434     
435     //return to ivr
436     $currentcomponent->addguielem($section,
437         new gui_checkbox('retvm', $ivr['retvm'], _('Return to IVR after VM'), _('If checked, upon exiting voicemail a caller will be returned to this IVR if they got a users voicemail')));
438         
439     /*$currentcomponent->addguielem($section,
440         new gui_checkbox('say_extension', $dir['say_extension'], _('Announce Extension'),
441         _('When checked, the extension number being transferred to will be announced prior to the transfer'),true));*/
442     $currentcomponent->addguielem($section, new gui_hidden('id', $ivr['id']));
443     $currentcomponent->addguielem($section, new gui_hidden('action', 'save'));
444
445
446         
447     $section = _('IVR Entries');
448     //draw the entries part of the table. A bit hacky perhaps, but hey - it works!
449     $currentcomponent->addguielem($section, new guielement('rawhtml', ivr_draw_entries($ivr['id']), ''), 6);
450 }
451
452 function ivr_configpageinit($pagename) {
453     global $currentcomponent;
454     $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
455     $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
456
457     if($pagename == 'ivr'){
458         $currentcomponent->addprocessfunc('ivr_configprocess');
459         
460         //dont show page if there is no action set
461         if ($action && $action != 'delete' || $id) {           
462             $currentcomponent->addguifunc('ivr_configpageload');
463         }
464         
465     return true;
466     }
467 }
468
469 //prosses received arguments
470 function ivr_configprocess(){
471     if (isset($_REQUEST['display']) && $_REQUEST['display'] == 'ivr'){
472         global $db;
473         //get variables
474
475         $get_var = array('id', 'name', 'description', 'announcement',
476                         'directdial', 'invalid_loops', 'invalid_retry_recording',
477                         'invalid_destination', 'invalid_recording',
478                         'retvm', 'timeout_time', 'timeout_recording',
479                         'timeout_retry_recording', 'timeout_destination', 'timeout_loops',
480                         'timeout_append_announce', 'invalid_append_announce');
481         foreach($get_var as $var){
482             $vars[$var] = isset($_REQUEST[$var])     ? $_REQUEST[$var]        : '';
483         }
484         $vars['timeout_append_announce'] = empty($vars['timeout_append_announce']) ? '0' : '1';
485         $vars['invalid_append_announce'] = empty($vars['invalid_append_announce']) ? '0' : '1';
486
487         $action        = isset($_REQUEST['action'])    ? $_REQUEST['action']    : '';
488         $entries    = isset($_REQUEST['entries'])    ? $_REQUEST['entries']    : '';
489
490         switch ($action) {
491             case 'save':
492             
493                 //get real dest
494                 $_REQUEST['id'] = $vars['id'] = ivr_save_details($vars);
495                 ivr_save_entries($vars['id'], $entries);
496                 needreload();
497                 //$_REQUEST['action'] = 'edit';
498                 redirect_standard_continue('id');
499             break;
500             case 'delete':
501                 ivr_delete($vars['id']);
502                 needreload();
503                 redirect_standard_continue();
504             break;
505         }
506     }
507 }
508
509 //save ivr settings
510 function ivr_save_details($vals){
511     global $db, $amp_conf;
512
513     foreach($vals as $key => $value) {
514         $vals[$key] = $db->escapeSimple($value);
515     }
516
517     if ($vals['id']) {
518         $sql = 'REPLACE INTO ivr_details (id, name, description, announcement,
519                 directdial, invalid_loops, invalid_retry_recording,
520                 invalid_destination, invalid_recording,
521                 retvm, timeout_time, timeout_recording,
522                 timeout_retry_recording, timeout_destination, timeout_loops,
523                 timeout_append_announce, invalid_append_announce)
524                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
525         $foo = $db->query($sql, $vals);
526         if($db->IsError($foo)) {
527             die_freepbx(print_r($vals,true).' '.$foo->getDebugInfo());
528         }
529     } else {
530         unset($vals['id']);
531         $sql = 'INSERT INTO ivr_details (name, description, announcement,
532                 directdial, invalid_loops, invalid_retry_recording,
533                 invalid_destination,  invalid_recording,
534                 retvm, timeout_time, timeout_recording,
535                 timeout_retry_recording, timeout_destination, timeout_loops,
536                 timeout_append_announce, invalid_append_announce)
537                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
538                 
539         $foo = $db->query($sql, $vals);
540         if($db->IsError($foo)) {
541             die_freepbx(print_r($vals,true).' '.$foo->getDebugInfo());
542         }
543         $sql = ( ($amp_conf["AMPDBENGINE"]=="sqlite3") ? 'SELECT last_insert_rowid()' : 'SELECT LAST_INSERT_ID()');
544         $vals['id'] = $db->getOne($sql);
545         if ($db->IsError($foo)){
546             die_freepbx($foo->getDebugInfo());
547         }
548     }
549
550     return $vals['id'];
551 }
552
553 //save ivr entires
554 function ivr_save_entries($id, $entries){
555     global $db;
556     $id = $db->escapeSimple($id);
557     sql('DELETE FROM ivr_entries WHERE ivr_id = "' . $id . '"');
558
559     if ($entries) {
560         for ($i = 0; $i < count($entries['ext']); $i++) {
561             //make sure there is an extension & goto set - otherwise SKIP IT
562             if (trim($entries['ext'][$i]) != '' && $entries['goto'][$i]) {
563                 $d[] = array(
564                             'ivr_id'    => $id,
565                             'selection'     => $entries['ext'][$i],
566                             'dest'        => $entries['goto'][$i],
567                             'ivr_ret'    => (isset($entries['ivr_ret'][$i]) ? $entries['ivr_ret'][$i] : '')
568                         );
569             }
570
571         }
572         $sql = $db->prepare('INSERT INTO ivr_entries VALUES (?, ?, ?, ?)');
573         $res = $db->executeMultiple($sql, $d);
574         if ($db->IsError($res)){
575             die_freepbx($res->getDebugInfo());
576         }
577     }
578     
579     return true;
580 }
581
582 //draw uvr entires table header
583 function ivr_draw_entries_table_header_ivr() {
584     return  array(_('Ext'), _('Destination'), fpbx_label(_('Return'), _('Return to IVR')), _('Delete'));
585 }
586
587 //draw actualy entires
588 function ivr_draw_entries($id){
589     $headers        = mod_func_iterator('draw_entries_table_header_ivr');
590     $ivr_entries    = ivr_get_entries($id);
591
592     if ($ivr_entries) {
593         foreach ($ivr_entries as $k => $e) {
594             $entries[$k]= $e;
595             $array = array('id' => $id, 'ext' => $e['selection']);
596             $entries[$k]['hooks'] = mod_func_iterator('draw_entries_ivr', $array);
597         }
598     }
599     
600     $entries['blank'] = array('selection' => '', 'dest' => '', 'ivr_ret' => '');
601     //assign to a vatriable first so that it can be passed by reference
602     $array = array('id' => '', 'ext' => '');
603     $entries['blank']['hooks'] = mod_func_iterator('draw_entries_ivr', $array);
604     
605     return load_view(dirname(__FILE__) . '/views/entries.php',
606                 array(
607                     'headers'    => $headers,
608                     'entries'    =>  $entries
609                 )
610             );
611
612 }
613
614 //delete an ivr + entires
615 function ivr_delete($id) {
616     global $db;
617     sql('DELETE FROM ivr_details WHERE id = "' . $db->escapeSimple($id) . '"');
618     sql('DELETE FROM ivr_entries WHERE ivr_id = "' . $db->escapeSimple($id) . '"');
619 }
620 //----------------------------------------------------------------------------
621 // Dynamic Destination Registry and Recordings Registry Functions
622 function ivr_check_destinations($dest=true) {
623     global $active_modules;
624
625     $destlist = array();
626     if (is_array($dest) && empty($dest)) {
627         return $destlist;
628     }
629     $sql = "SELECT dest, name, selection, a.id id FROM ivr_details a INNER JOIN ivr_entries d ON a.id = d.ivr_id  ";
630     if ($dest !== true) {
631         $sql .= "WHERE dest in ('".implode("','",$dest)."')";
632     }
633     $sql .= "ORDER BY name";
634     $results = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
635
636     foreach ($results as $result) {
637         $thisdest = $result['dest'];
638         $thisid   = $result['id'];
639         $name = $result['name'] ? $result['name'] : 'IVR ' . $thisid;
640         $destlist[] = array(
641             'dest' => $thisdest,
642             'description' => sprintf(_("IVR: %s / Option: %s"),$name,$result['selection']),
643             'edit_url' => 'config.php?display=ivr&action=edit&id='.urlencode($thisid),
644         );
645     }
646     return $destlist;
647 }
648
649
650
651 function ivr_change_destination($old_dest, $new_dest) {
652     global $db;
653      $sql = "UPDATE ivr_entires SET dest = '$new_dest' WHERE dest = '$old_dest'";
654      $db->query($sql);
655
656 }
657
658
659 function ivr_getdest($exten) {
660     return array('ivr-'.$exten.',s,1');
661 }
662
663 function ivr_getdestinfo($dest) {
664     global $active_modules;
665
666     if (substr(trim($dest),0,4) == 'ivr-') {
667         $exten = explode(',',$dest);
668         $exten = substr($exten[0],4);
669
670         $thisexten = ivr_get_details($exten);
671         if (empty($thisexten)) {
672             return array();
673         } else {
674             //$type = isset($active_modules['ivr']['type'])?$active_modules['ivr']['type']:'setup';
675             return array('description' => sprintf(_("IVR: %s"), ($thisexten['name'] ? $thisexten['name'] : $thisexten['id'])),
676                          'edit_url' => 'config.php?display=ivr&action=edit&id='.urlencode($exten),
677                                   );
678         }
679     } else {
680         return false;
681     }
682 }
683
684 function ivr_recordings_usage($recording_id) {
685     global $active_modules;
686
687     //$results = sql("SELECT `ivr`, `name` FROM `ivr` WHERE `announcement` = '$recording_id' || `timeout_id` = '$recording_id' || `invalid_id` = '$recording_id'","getAll",DB_FETCHMODE_ASSOC);
688     $results = sql("SELECT `id`, `name` FROM `ivr_details` WHERE '$recording_id' IN('announcement', 'invalid_retry_recording', 'invalid_recording', 'timeout_recording', 'timeout_retry_recording')", "getAll",DB_FETCHMODE_ASSOC);
689     if (empty($results)) {
690         return array();
691     } else {
692         foreach ($results as $result) {
693             $usage_arr[] = array(
694                 'url_query' => 'config.php?display=ivr&action=edit&id='.urlencode($result['id']),
695                 'description' => sprintf(_("IVR: %s"), ($result['name'] ? $result['name'] : $result['id'])),
696             );
697         }
698         return $usage_arr;
699     }
700 }
701
702 ?>
703
Note: See TracBrowser for help on using the browser.