root/freepbx/trunk/amp_conf/htdocs/admin/modules/ivr/functions.inc.php

Revision 1483, 11.1 kB (checked in by qldrob, 7 years ago)

recordings_init doesn't exist any more.

  • 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                 runModuleSQL('ivr', 'uninstall');
19                 if (runModuleSQL('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         if (!isset($results[0])) {
27                 // Note: There's an invalid entry created, __invalid, after this is run,
28                 // so as long as this has been run _once_, there will always be a result.
29
30         // Read old IVR format, part of xtns..
31         $sql = "SELECT context,descr FROM extensions WHERE extension = 's' AND application LIKE 'DigitTimeout' AND context LIKE '".$dept."aa_%' ORDER BY context,priority";
32         $unique_aas = $db->getAll($sql);
33         if (isset($unique_aas)) {
34             foreach($unique_aas as $aa){
35                 // This gets all the menu options
36                 $id = ivr_get_ivr_id($aa[1]);
37                 // Save the old name, with a link to the new name, for upgrading
38                 $ivr_newname[$aa[0]] = "ivr-$id";
39                 // Get the old config
40                 $sql = "SELECT extension,args from extensions where application='Goto' and context='{$aa[0]}'";
41                 $cmds = $db->getAll($sql, DB_FETCHMODE_ASSOC);
42                 if (isset($cmds)) {
43                     // There were some actions, so loop through them
44                     foreach ($cmds as $cmd) {
45                         $arr=explode(',', $cmd['args']);
46                         // s == old stuff. We don't care.
47                         if ($arr[0] != 's')
48                             ivr_add_command($id,$cmd['extension'],$cmd['args']);
49                     }
50                 }
51             }
52             // Now. Upgrade all the links inside the old IVR's
53             if (isset($ivr_newname)) {
54                 // Some IVR's were upgraded
55                 $sql = "SELECT * FROM ivr_dests WHERE dest LIKE '%aa_%'";
56                 $dests = $db->getAll($sql, DB_FETCHMODE_ASSOC);
57                 if (isset($dests)) {
58                     foreach ($dests as $dest) {
59                         $arr=explode(',', $dest['dest']);
60                         sql("UPDATE ivr_dests set dest='".$ivr_newname[$arr[0]].",s,1' where ivr_id='".$dest['ivr_id']."' and selection='".$dest['selection']."'");
61                     }
62                 }
63             }
64
65             // Upgrade everything using IVR as a destination. Ick.
66
67             // Are queue's using an ivr failover?
68             // ***FIXME*** if upgrading queues away from legacy cruft.
69             $queues = $db->getAll("select extensions,args from extensions where args LIKE '%aa_%' and context='ext-queues' and priority='6'");
70             if (count($res) != 0) {
71                 foreach ($queues as $q) {
72                     $arr=explode(',', $q['args']);
73                     sql("UPDATE extensions set args='".$ivr_newname[$arr[0]].",s,1' where context='ext-queues' and priority='6' and extension='".$q['extension']."'");
74                                 }
75             }
76
77             // Now process everything else
78             foreach (array_keys($ivr_newname) as $old) {
79                 // Timeconditions
80                 sql("UPDATE timeconditions set truegoto='".$ivr_newname[$arr[0]].",s,1' where truegoto='$old,s,1'");
81                 sql("UPDATE timeconditions set falsegoto='".$ivr_newname[$arr[0]].",s,1' where falsegoto='$old,s,1'");
82                 // Inbound Routes
83                 sql("UPDATE incoming set destination='".$ivr_newname[$arr[0]].",s,1' where destination='$old,s,1'");
84                 // Ring Groups
85                 sql("UPDATE ringgroups set postdest='".$ivr_newname[$arr[0]].",s,1' where postdest='$old,s,1'");
86             }
87         }
88         // Note, the __install_done line is for internal version checking - the second field
89         // should be incremented and checked if the database ever changes.
90                 $result = sql("INSERT INTO ivr (displayname, deptname) VALUES ('__install_done', '1')");
91         needreload();
92         }
93     // Now, we need to check for upgrades.
94     // V1.0, old IVR. You shouldn't see this, but check for it anyway, and assume that it's 2.0
95     // V2.0, Original Release
96     // V2.1, added 'directorycontext' to the schema
97     //
98     if (modules_getversion('ivr') == "1.0" || modules_getversion('ivr') == "2.0") {
99         // Add the col
100         sql('ALTER TABLE ivr ADD COLUMN dircontext VARCHAR ( 50 ) DEFAULT "default"');
101         modules_setversion('ivr', '1.1');
102     }
103 }
104
105 // The destinations this module provides
106 // returns a associative arrays with keys 'destination' and 'description'
107 function ivr_destinations() {
108     //get the list of IVR's
109     $results = ivr_list();
110
111     // return an associative array with destination and description
112     if (isset($results)) {
113         foreach($results as $result){
114             $extens[] = array('destination' => 'ivr-'.$result['ivr_id'].',s,1', 'description' => $result['displayname']);
115         }
116     }
117     if (isset($extens))
118         return $extens;
119     else
120         return null;
121 }
122
123 function ivr_get_config($engine) {
124         global $ext;
125         global $conferences_conf;
126
127     switch($engine) {
128         case "asterisk":
129             $ivrlist = ivr_list();
130             if(is_array($ivrlist)) {
131                 foreach($ivrlist as $item) {
132                     $id = "ivr-".$item['ivr_id'];
133                     $details = ivr_get_details($item['ivr_id']);
134                     if (!empty($details['enable_directdial']))
135                                             $ext->addInclude($id,'ext-local');
136                     // I'm not sure I like the ability of people to send voicemail from the IVR.
137                     // Make it a config option, possibly?
138                                         // $ext->addInclude($item[0],'app-messagecenter');
139                     if (!empty($details['enable_directory']))
140                                             $ext->addInclude($id,'app-directory');
141                                         $ext->add($id, 'h', '', new ext_hangup(''));
142                                         $ext->add($id, 's', '', new ext_setvar('LOOPCOUNT', 0));
143                                         $ext->add($id, 's', '', new ext_setvar('__DIR-CONTEXT', $details['dircontext']));
144                                         $ext->add($id, 's', '', new ext_answer(''));
145                                         $ext->add($id, 's', '', new ext_wait('1'));
146                                         $ext->add($id, 's', 'begin', new ext_digittimeout(3));
147                                         $ext->add($id, 's', '', new ext_responsetimeout($details['timeout']));
148                     if(function_exists('recordings_get')) {
149                         $recording = recordings_get($details['announcement']);
150                         $ext->add($id, 's', '', new ext_background($recording['filename']));
151                     }
152                                         $ext->add($id, 'hang', '', new ext_playback('vm-goodbye'));
153                                         $ext->add($id, 'hang', '', new ext_hangup(''));
154
155                                         $default_t=true;
156                     // Actually add the IVR commands now.
157                     $dests = ivr_get_dests($item['ivr_id']);
158                     if (!empty($dests)) {
159                         foreach($dests as $dest) {
160                             if ($dest['selection'] == 't') $timeout=true;
161                             if ($dest['selection'] == 'i') $invalid=true;
162                             $ext->add($id, $dest['selection'],'', new ext_goto($dest['dest']));
163                         }
164                     }
165                     // Apply invalid if required
166                     if (!isset($invalid)) {
167                         $ext->add($id, 'i', '', new ext_playback('invalid'));
168                         $ext->add($id, 'i', '', new ext_goto('loop,1'));
169                         $addloop=true;
170                     }
171                     if (!isset($timeout)) {
172                         $ext->add($id, 't', '', new ext_goto('loop,1'));
173                         $addloop=true;
174                     }
175                     if (isset($addloop)) {
176                         $ext->add($id, 'loop', '', new ext_setvar('LOOPCOUNT','$[${LOOPCOUNT} + 1]'));   
177                         $ext->add($id, 'loop', '', new ext_gotoif('$[${LOOPCOUNT} > 2]','hang,1'));
178                         $ext->add($id, 'loop', '', new ext_goto($id.',s,begin'));
179                     }
180                     $ext->add($id, 'fax', '', new ext_goto('ext-fax,in_fax,1'));
181                                 }
182                         }
183                 break;
184         }
185 }
186
187
188
189 function ivr_get_ivr_id($name) {
190     global $db;
191     $res = $db->getRow("SELECT ivr_id from ivr where displayname='$name'");
192     if (count($res) == 0) {
193         // It's not there. Create it and return the ID
194         sql("INSERT INTO ivr (displayname, enable_directory, enable_directdial, timeout)  values('$name', 'CHECKED', 'CHECKED', 10)");
195         $res = $db->getRow("SELECT ivr_id from ivr where displayname='$name'");
196         needreload();
197     }
198     return ($res[0]);
199     
200 }
201
202 function ivr_add_command($id, $cmd, $dest) {
203     global $db;
204     // Does it already exist?
205     $res = $db->getRow("SELECT * from ivr_dests where ivr_id='$id' and selection='$cmd'");
206     if (count($res) == 0) {
207         // Just add it.
208         sql("INSERT INTO ivr_dests VALUES('$id', '$cmd', '$dest')");
209     } else {
210         // Update it.
211         sql("UPDATE ivr_dests SET dest='$dest' where ivr_id='$id' and selection='$cmd'");
212     }
213     needreload();
214 }
215 function ivr_do_edit($id, $post) {
216
217     $displayname = isset($post['displayname'])?$post['displayname']:'';
218     $timeout = isset($post['timeout'])?$post['timeout']:'';
219     $ena_directory = isset($post['ena_directory'])?$post['ena_directory']:'';
220     $ena_directdial = isset($post['ena_directdial'])?$post['ena_directdial']:'';
221     $annmsg = isset($post['annmsg'])?$post['annmsg']:'';
222     $dircontext = isset($post['dircontext'])?$post['dircontext']:'';
223
224     if (!empty($ena_directory))
225         $ena_directory='CHECKED';
226
227
228     if (!empty($ena_directdial))
229         $ena_directdial='CHECKED';
230     
231     sql("UPDATE ivr SET displayname='$displayname', enable_directory='$ena_directory', enable_directdial='$ena_directdial', timeout='$timeout', announcement='$annmsg', dircontext='$dircontext' WHERE ivr_id='$id'");
232
233     // Delete all the old dests
234     sql("DELETE FROM ivr_dests where ivr_id='$id'");
235     // Now, lets find all the goto's in the post. Destinations return goto_indicateN => foo and get fooN for the dest.
236     // Is that right, or am I missing something?
237     foreach(array_keys($post) as $var) {
238         if (preg_match('/goto_indicate(\d+)/', $var, $match)) {
239             // This is a really horrible line of code. take N, and get value of fooN. See above. Note we
240             // get match[1] from the preg_match above
241             $dest = $post[$post[$var].$match[1]];
242             $cmd = $post['option'.$match[1]];
243             // Debugging if it all goes pear shaped.
244             // print "I think pushing $cmd does $dest<br>\n";
245             if (strlen($cmd))
246                 ivr_add_command($id, $cmd, $dest);
247         }
248     }
249     needreload();
250 }
251
252
253 function ivr_list() {
254     global $db;
255
256     $sql = "SELECT * FROM ivr where displayname <> '__install_done' ORDER BY displayname";
257         $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
258         if(DB::IsError($res)) {
259         return null;
260         }
261         return $res;
262 }
263
264 function ivr_get_details($id) {
265     global $db;
266
267     $sql = "SELECT * FROM ivr where ivr_id='$id'";
268         $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
269         if(DB::IsError($res)) {
270         return null;
271         }
272         return $res[0];
273 }
274
275 function ivr_get_dests($id) {
276     global $db;
277
278     $sql = "SELECT selection, dest FROM ivr_dests where ivr_id='$id' ORDER BY selection";
279         $res = $db->getAll($sql, DB_FETCHMODE_ASSOC);
280         if(DB::IsError($res)) {
281                 return null;
282         }
283         return $res;
284 }
285     
286 function ivr_get_name($id) {
287     $res = ivr_get_details($id);
288     if (isset($res['displayname'])) {
289         return $res['displayname'];
290     } else {
291         return null;
292     }
293 }
294 ?>
295
Note: See TracBrowser for help on using the browser.