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

Revision 5714, 12.3 kB (checked in by p_lindheimer, 4 years ago)

Merged revisions 5712-5713 via svnmerge from
http://svn.freepbx.org/modules/branches/2.4

........

r5712 | p_lindheimer | 2008-02-18 20:34:11 -0800 (Mon, 18 Feb 2008) | 1 line


#2687 breakout from Queue to Company Directory blocks voicemail

........

r5713 | p_lindheimer | 2008-02-18 20:35:07 -0800 (Mon, 18 Feb 2008) | 1 line


Module Publish Script: ivr 2.5.16.2

........

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2  /* $Id$ */
3
4
5 function ivr_init() {
6     global $db;
7
8     // Check to make sure that install.sql has been run
9     $sql = "SELECT deptname from ivr where displayname='__install_done' LIMIT 1";
10
11     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
12
13     if (DB::IsError($results)) {
14             // It couldn't locate the table. This is bad. Lets try to re-create it, just
15             // in case the user has had the brilliant idea to delete it.
16             // runModuleSQL taken from page.module.php. It's inclusion here is probably
17             // A bad thing. It should be, I think, globally available.
18             localrunModuleSQL('ivr', 'uninstall');
19             if (localrunModuleSQL('ivr', 'install')==false) {
20                     echo _("There is a problem with install.sql, cannot re-create databases. Contact support\n");
21                     die;
22             } else {
23                     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
24             }
25     }
26    
27     if (!isset($results[0])) {
28         // Note: There's an invalid entry created, __invalid, after this is run,
29         // so as long as this has been run _once_, there will always be a result.
30
31     // Read old IVR format, part of xtns..
32     $sql = "SELECT context,descr FROM extensions WHERE extension = 's' AND application LIKE 'DigitTimeout' AND context LIKE '".$dept."aa_%' ORDER BY context,priority";
33     $unique_aas = $db->getAll($sql);
34     if (isset($unique_aas)) {
35       foreach($unique_aas as $aa){
36         // This gets all the menu options
37         $id = ivr_get_ivr_id($aa[1]);
38         // Save the old name, with a link to the new name, for upgrading
39         $ivr_newname[$aa[0]] = "ivr-$id";
40         // Get the old config
41         $sql = "SELECT extension,args from extensions where application='Goto' and context='{$aa[0]}'";
42         $cmds = $db->getAll($sql, DB_FETCHMODE_ASSOC);
43         if (isset($cmds)) {
44           // There were some actions, so loop through them
45           foreach ($cmds as $cmd) {
46             $arr=explode(',', $cmd['args']);
47             // s == old stuff. We don't care.
48             if ($arr[0] != 's')
49               ivr_add_command($id,$cmd['extension'],$cmd['args'],0);
50           }
51         }
52       }
53       // Now. Upgrade all the links inside the old IVR's
54       if (isset($ivr_newname)) {
55         // Some IVR's were upgraded
56         $sql = "SELECT * FROM ivr_dests WHERE dest LIKE '%aa_%'";
57         $dests = $db->getAll($sql, DB_FETCHMODE_ASSOC);
58         if (isset($dests)) {
59           foreach ($dests as $dest) {
60             $arr=explode(',', $dest['dest']);
61             sql("UPDATE ivr_dests set dest='".$ivr_newname[$arr[0]].",s,1' where ivr_id='".$dest['ivr_id']."' and selection='".$dest['selection']."'");
62           }
63         }
64       }
65  
66       // Upgrade everything using IVR as a destination. Ick.
67  
68       // Are queue's using an ivr failover?
69       // ***FIXME*** if upgrading queues away from legacy cruft.
70       $queues = $db->getAll("select extensions,args from extensions where args LIKE '%aa_%' and context='ext-queues' and priority='6'");
71       if (count($res) != 0) {
72         foreach ($queues as $q) {
73           $arr=explode(',', $q['args']);
74           sql("UPDATE extensions set args='".$ivr_newname[$arr[0]].",s,1' where context='ext-queues' and priority='6' and extension='".$q['extension']."'");
75                               }
76       }
77  
78       // Now process everything else - if there's anything to process.
79       if (isset($ivr_newname) && is_array($ivr_newname)) {
80         foreach (array_keys($ivr_newname) as $old) {
81           // Timeconditions
82           sql("UPDATE timeconditions set truegoto='".$ivr_newname[$arr[0]].",s,1' where truegoto='$old,s,1'");
83           sql("UPDATE timeconditions set falsegoto='".$ivr_newname[$arr[0]].",s,1' where falsegoto='$old,s,1'");
84           // Inbound Routes
85           sql("UPDATE incoming set destination='".$ivr_newname[$arr[0]].",s,1' where destination='$old,s,1'");
86           // Ring Groups
87           sql("UPDATE ringgroups set postdest='".$ivr_newname[$arr[0]].",s,1' where postdest='$old,s,1'");
88         }
89       }
90     }
91     // Note, the __install_done line is for internal version checking - the second field
92     // should be incremented and checked if the database ever changes.
93     $result = sql("INSERT INTO ivr (displayname, deptname) VALUES ('__install_done', '1')");
94     needreload();
95     }
96 }
97
98 // The destinations this module provides
99 // returns a associative arrays with keys 'destination' and 'description'
100 function ivr_destinations() {
101   //get the list of IVR's
102   $results = ivr_list();
103
104   // return an associative array with destination and description
105   if (isset($results)) {
106     foreach($results as $result){
107       $extens[] = array('destination' => 'ivr-'.$result['ivr_id'].',s,1', 'description' => $result['displayname']);
108     }
109   }
110   if (isset($extens))
111     return $extens;
112   else
113     return null;
114 }
115
116 function ivr_get_config($engine) {
117         global $ext;
118         global $conferences_conf;
119
120   switch($engine) {
121     case "asterisk":
122       $ivrlist = ivr_list();
123       if(is_array($ivrlist)) {
124         foreach($ivrlist as $item) {
125           $id = "ivr-".$item['ivr_id'];
126           $details = ivr_get_details($item['ivr_id']);
127
128           $announcement = (isset($details['announcement']) ? $details['announcement'] : '');
129
130           if (!empty($details['enable_directdial'])) {
131             // MODIFIED (PL)
132             // always include ext-findmefollow whether or not the module is currenlty
133             // enabled since subsequent activations should work without regenerating the
134             // ivr. (and no harm done if context does not exist.
135             //
136             $ext->addInclude($id,'ext-findmefollow');
137                                           $ext->addInclude($id,'ext-local');
138           }
139           // I'm not sure I like the ability of people to send voicemail from the IVR.
140           // Make it a config option, possibly?
141                                         // $ext->addInclude($item[0],'app-messagecenter');
142           if (!empty($details['enable_directory'])) {
143             $ext->addInclude($id,'app-directory');
144             $dir = featurecodes_getFeatureCode('infoservices', 'directory');
145             $ext->add($id, '#' ,'', new ext_dbdel('${BLKVM_OVERRIDE}'));
146             $ext->add($id, '#' ,'', new ext_setvar('__NODEST', ''));
147             $ext->add($id, '#', '', new ext_goto("app-directory,$dir,1"));
148           }
149
150           $ext->add($id, 'h', '', new ext_hangup(''));
151           $ext->add($id, 's', '', new ext_setvar('LOOPCOUNT', 0));
152           $ext->add($id, 's', '', new ext_setvar('__DIR-CONTEXT', $details['dircontext']));
153           $ext->add($id, 's', '', new ext_setvar('_IVR_CONTEXT_${CONTEXT}', '${IVR_CONTEXT}'));
154           $ext->add($id, 's', '', new ext_setvar('_IVR_CONTEXT', '${CONTEXT}'));
155           $ext->add($id, 's', '', new ext_gotoif('$["${CDR(disposition)}" = "ANSWERED"]','begin'));
156           $ext->add($id, 's', '', new ext_answer(''));
157           $ext->add($id, 's', '', new ext_wait('1'));
158           $ext->add($id, 's', 'begin', new ext_digittimeout(3));
159           $ext->add($id, 's', '', new ext_responsetimeout($details['timeout']));
160           if ($announcement != '') {
161             $ext->add($id, 's', '', new ext_background($announcement));
162           }
163           $ext->add($id, 's', '', new ext_waitexten());
164           $ext->add($id, 'hang', '', new ext_playback('vm-goodbye'));
165           $ext->add($id, 'hang', '', new ext_hangup(''));
166
167           $default_t=true;
168
169           // Actually add the IVR commands now.
170           $dests = ivr_get_dests($item['ivr_id']);
171           $timeout=false;
172           $invalid=false;
173           $addloop=false;
174           if (!empty($dests)) {
175             foreach($dests as $dest) {
176               if ($dest['selection'] == 't') $timeout=true;
177               if ($dest['selection'] == 'i') $invalid=true;
178               $ext->add($id, $dest['selection'],'', new ext_dbdel('${BLKVM_OVERRIDE}'));
179               $ext->add($id, $dest['selection'],'', new ext_setvar('__NODEST', ''));
180
181               // if the goto goes loops back to this ivr, then don't go to the begining or it will break the return to previous ivr info
182               //
183               $dest_context = trim(strtok($dest['dest'],",|"));
184               if ($dest_context == $id) {
185                 $dest['dest'] = $id.',s,begin';
186               }
187
188               if ($dest['ivr_ret']) {
189                 $ext->add($id, $dest['selection'],'', new ext_gotoif('$["x${IVR_CONTEXT_${CONTEXT}}" = "x"]', $dest['dest'].':${IVR_CONTEXT_${CONTEXT}},return,1'));
190               } else {
191                 $ext->add($id, $dest['selection'],'', new ext_goto($dest['dest']));
192               }
193             }
194           }
195           // Apply invalid if required
196           if (!$invalid) {
197             $ext->add($id, 'i', '', new ext_playback('invalid'));
198             $ext->add($id, 'i', '', new ext_goto('loop,1'));
199             $addloop=true;
200           }
201           if (!$timeout) {
202             $ext->add($id, 't', '', new ext_goto('loop,1'));
203             $addloop=true;
204           }
205           if ($addloop) {
206             $ext->add($id, 'loop', '', new ext_setvar('LOOPCOUNT','$[${LOOPCOUNT} + 1]')); 
207             $ext->add($id, 'loop', '', new ext_gotoif('$[${LOOPCOUNT} > 2]','hang,1'));
208             $ext->add($id, 'loop', '', new ext_goto($id.',s,begin'));
209
210             // these need to be reset or inheritance problems makes them go away in some conditions and infinite inheritance creates other problems
211             //
212             $ext->add($id, 'return', '', new ext_setvar('_IVR_CONTEXT', '${CONTEXT}'));
213             $ext->add($id, 'return', '', new ext_setvar('_IVR_CONTEXT_${CONTEXT}', '${IVR_CONTEXT_${CONTEXT}}'));
214             $ext->add($id, 'return', '', new ext_goto($id.',s,begin'));
215           }
216           $ext->add($id, 'fax', '', new ext_goto('ext-fax,in_fax,1'));
217         }
218       }
219     break;
220   }
221 }
222
223
224
225 function ivr_get_ivr_id($name) {
226   global $db;
227   $res = $db->getRow("SELECT ivr_id from ivr where displayname='$name'");
228   if (count($res) == 0) {
229     // It's not there. Create it and return the ID
230     sql("INSERT INTO ivr (displayname, enable_directory, enable_directdial, timeout)  values('$name', 'CHECKED', 'CHECKED', 10)");
231     $res = $db->getRow("SELECT ivr_id from ivr where displayname='$name'");
232     needreload();
233   }
234   return ($res[0]);
235  
236 }
237
238 function ivr_add_command($id, $cmd, $dest, $ivr_ret) {
239   global $db;
240   // Does it already exist?
241   $res = $db->getRow("SELECT * from ivr_dests where ivr_id='$id' and selection='$cmd'");
242   $ivr_ret = $ivr_ret ? 1 : 0;
243   if (count($res) == 0) {
244     // Just add it.
245     sql("INSERT INTO ivr_dests VALUES('$id', '$cmd', '$dest', '$ivr_ret')");
246   } else {
247     // Update it.
248     sql("UPDATE ivr_dests SET dest='$dest', ivr_ret='$ivr_ret' where ivr_id='$id' and selection='$cmd'");
249   }
250   needreload();
251 }
252 function ivr_do_edit($id, $post) {
253
254   $displayname = isset($post['displayname'])?$post['displayname']:'';
255   $timeout = isset($post['timeout'])?$post['timeout']:'';
256   $ena_directory = isset($post['ena_directory'])?$post['ena_directory']:'';
257   $ena_directdial = isset($post['ena_directdial'])?$post['ena_directdial']:'';
258   $annmsg = isset($post['annmsg'])?$post['annmsg']:'';
259   $dircontext = isset($post['dircontext'])?$post['dircontext']:'';
260
261   if (!empty($ena_directory))
262     $ena_directory='CHECKED';
263
264
265   if (!empty($ena_directdial))
266     $ena_directdial='CHECKED';
267  
268   sql("UPDATE ivr SET displayname='$displayname', enable_directory='$ena_directory', enable_directdial='$ena_directdial', timeout='$timeout', announcement='$annmsg', dircontext='$dircontext' WHERE ivr_id='$id'");
269
270   // Delete all the old dests
271   sql("DELETE FROM ivr_dests where ivr_id='$id'");
272   // Now, lets find all the goto's in the post. Destinations return gotoN => foo and get fooN for the dest.
273   // Is that right, or am I missing something?
274   foreach(array_keys($post) as $var) {
275     if (preg_match('/goto(\d+)/', $var, $match)) {
276       // This is a really horrible line of code. take N, and get value of fooN. See above. Note we
277       // get match[1] from the preg_match above
278       $dest = $post[$post[$var].$match[1]];
279       $cmd = $post['option'.$match[1]];
280       $ivr_ret = $post['ivr_ret'.$match[1]];
281       // Debugging if it all goes pear shaped.
282       // print "I think pushing $cmd does $dest<br>\n";
283       if (strlen($cmd))
284         ivr_add_command($id, $cmd, $dest, $ivr_ret);
285     }
286   }
287   needreload();
288 }
289
290
291 function ivr_list() {
292   global $db;
293
294   $sql = "SELECT * FROM ivr where displayname <> '__install_done' ORDER BY displayname";
295         $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
296         if(DB::IsError($res)) {
297     return null;
298         }
299         return $res;
300 }
301
302 function ivr_get_details($id) {
303   global $db;
304
305   $sql = "SELECT * FROM ivr where ivr_id='$id'";
306         $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
307         if(DB::IsError($res)) {
308     return null;
309         }
310         return $res[0];
311 }
312
313 function ivr_get_dests($id) {
314   global $db;
315
316   $sql = "SELECT selection, dest, ivr_ret FROM ivr_dests where ivr_id='$id' ORDER BY selection";
317         $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
318         if(DB::IsError($res)) {
319                 return null;
320         }
321         return $res;
322 }
323  
324 function ivr_get_name($id) {
325   $res = ivr_get_details($id);
326   if (isset($res['displayname'])) {
327     return $res['displayname'];
328   } else {
329     return null;
330   }
331 }
332 ?>
Note: See TracBrowser for help on using the browser.