root/modules/branches/2.9/ivr/install.php

Revision 11109, 8.4 kB (checked in by p_lindheimer, 2 years ago)

remove functions now always included by utility.functions.php

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
Line 
1 <?php
2 require_once dirname(__FILE__)."/functions.inc.php";
3
4 global $db;
5 global $amp_conf;
6
7 if($amp_conf["AMPDBENGINE"] == "sqlite3")  {
8   $sql = "
9     CREATE TABLE IF NOT EXISTS ivr (
10       ivr_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
11       displayname VARCHAR(50),
12       deptname VARCHAR(50),
13       enable_directory VARCHAR(8),
14       enable_directdial VARCHAR(8),
15       timeout INT,
16       announcement VARCHAR(255),
17       dircontext VARCHAR ( 50 ) DEFAULT 'default',
18       alt_timeout VARCHAR(8),
19       alt_invalid VARCHAR(8),
20       `loops` TINYINT(1) NOT NULL DEFAULT 2
21     );
22   ";
23 }
24 else  {
25   $sql = "
26     CREATE TABLE IF NOT EXISTS ivr (
27       ivr_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY  ,
28       displayname VARCHAR(50),
29       deptname VARCHAR(50),
30       enable_directory VARCHAR(8),
31       enable_directdial VARCHAR(8),
32       timeout INT,
33       announcement VARCHAR(255),
34       dircontext VARCHAR ( 50 ) DEFAULT 'default',
35       alt_timeout VARCHAR(8),
36       alt_invalid VARCHAR(8),
37       `loops` TINYINT(1) NOT NULL DEFAULT 2
38     );
39   ";
40 }
41 sql($sql);
42
43 $sql = "
44 CREATE TABLE IF NOT EXISTS ivr_dests
45 (
46   `ivr_id` INT NOT NULL,
47   `selection` VARCHAR(10),
48   `dest` VARCHAR(50),
49   `ivr_ret` TINYINT(1) NOT NULL DEFAULT 0
50 )
51 ";
52 sql($sql);
53
54 // Now, we need to check for upgrades.
55 // V1.0, old IVR. You shouldn't see this, but check for it anyway, and assume that it's 2.0
56 // V2.0, Original Release
57 // V2.1, added 'directorycontext' to the schema
58 // v2.2, announcement changed to support filenames instead of ID's from recordings table
59 //
60
61 $ivr_modcurrentvers = modules_getversion('ivr');
62
63 if($amp_conf["AMPDBENGINE"] != "sqlite3")  { // As of 2.5 these are all in the sqlite3 schema
64   // Add the col
65   $sql = "SELECT dircontext FROM ivr";
66   $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
67   if(DB::IsError($check)) {
68     // add new field
69     $sql = 'ALTER TABLE ivr ADD COLUMN dircontext VARCHAR ( 50 ) DEFAULT "default"';
70     $result = $db->query($sql);
71     if(DB::IsError($result)) {
72       die_freepbx($result->getDebugInfo());
73     }
74   }
75
76   if ($ivr_modcurrentvers !== null && version_compare($ivr_modcurrentvers, "2.2", "<")) {
77     // Change existing records
78     $existing = sql("SELECT DISTINCT announcement FROM ivr WHERE displayname <> '__install_done' AND announcement IS NOT NULL", "getAll");
79     foreach ($existing as $item) {
80       $recid = $item[0];
81       $sql = "SELECT filename FROM recordings WHERE id = '$recid' AND displayname <> '__invalid'";
82       $recordings = sql($sql, "getRow");
83       if (is_array($recordings)) {
84         $filename = (isset($recordings[0]) ? $recordings[0] : '');
85         if ($filename != '') {
86           $sql = "UPDATE ivr SET announcement = '".str_replace("'", "''", $filename)."' WHERE announcement = '$recid'";
87           $upcheck = $db->query($sql);
88           if(DB::IsError($upcheck))
89           die_freepbx($upcheck->getDebugInfo());
90         }
91       }
92     }
93   }
94 }
95 // Version 2.5.7 adds auto-return to IVR
96 $sql = "SELECT ivr_ret FROM ivr_dests";
97 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
98 if(DB::IsError($check)) {
99   // add new field
100   $sql = "ALTER TABLE ivr_dests ADD ivr_ret TINYINT(1) NOT NULL DEFAULT 0;";
101   $result = $db->query($sql);
102   if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
103 }
104
105 $results = array();
106 $sql = "SELECT ivr_id, selection, dest FROM ivr_dests";
107 $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
108 if (!DB::IsError($results)) { // error - table must not be there
109   foreach ($results as $result) {
110     $old_dest  = $result['dest'];
111     $ivr_id    = $result['ivr_id'];
112     $selection = $result['selection'];
113
114     $new_dest = merge_ext_followme(trim($old_dest));
115     if ($new_dest != $old_dest) {
116       $sql = "UPDATE ivr_dests SET dest = '$new_dest' WHERE ivr_id = $ivr_id AND selection = '$selection' AND dest = '$old_dest'";
117       $results = $db->query($sql);
118       if(DB::IsError($results)) {
119         die_freepbx($results->getMessage());
120       }
121     }
122   }
123 }
124
125 // Version 2.5.17 adds improved i and t destination handling
126 $sql = "SELECT alt_timeout FROM ivr";
127 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
128 if(DB::IsError($check)) {
129   // add new field
130   $sql = "ALTER TABLE ivr ADD alt_timeout VARCHAR(8);";
131   $result = $db->query($sql);
132   if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
133 }
134 $sql = "SELECT alt_invalid FROM ivr";
135 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
136 if(DB::IsError($check)) {
137   // add new field
138   $sql = "ALTER TABLE ivr ADD alt_invalid VARCHAR(8);";
139   $result = $db->query($sql);
140   if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
141 }
142 $sql = "SELECT `loops` FROM ivr";
143 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
144 if(DB::IsError($check)) {
145   // add new field
146   $sql = "ALTER TABLE ivr ADD `loops` TINYINT(1) NOT NULL DEFAULT 2;";
147   $result = $db->query($sql);
148   if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
149 }
150
151
152
153 // Version 2.5 migrate to recording ids
154 //
155 outn(_("Checking if announcements need migration.."));
156 $sql = "SELECT announcement_id FROM ivr";
157 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
158 if(DB::IsError($check)) {
159   //  Add announcement_id field
160   //
161   out(_("migrating"));
162   outn(_("adding announcement_id field.."));
163   $sql = "ALTER TABLE ivr ADD announcement_id INTEGER";
164   $result = $db->query($sql);
165   if(DB::IsError($result)) {
166     out(_("fatal error"));
167     die_freepbx($result->getDebugInfo());
168   } else {
169     out(_("ok"));
170   }
171
172   // Get all the valudes and replace them with announcement_id
173   //
174   outn(_("migrate to recording ids.."));
175   $sql = "SELECT `ivr_id`, `announcement` FROM `ivr`";
176   $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
177   if(DB::IsError($results)) {
178     out(_("fatal error"));
179     die_freepbx($results->getDebugInfo());
180   }
181   $migrate_arr = array();
182   $count = 0;
183   foreach ($results as $row) {
184     if (trim($row['announcement']) != '') {
185       $rec_id = recordings_get_or_create_id($row['announcement'], 'ivr');
186       $migrate_arr[] = array($rec_id, $row['ivr_id']);
187       $count++;
188     }
189   }
190   if ($count) {
191     $compiled = $db->prepare('UPDATE `ivr` SET `announcement_id` = ? WHERE `ivr_id` = ?');
192     $result = $db->executeMultiple($compiled,$migrate_arr);
193     if(DB::IsError($result)) {
194       out(_("fatal error"));
195       die_freepbx($result->getDebugInfo());
196     }
197   }
198   out(sprintf(_("migrated %s entries"),$count));
199
200   // Now remove the old recording field replaced by new id field
201   //
202   outn(_("dropping announcement field.."));
203   $sql = "ALTER TABLE `ivr` DROP `announcement`";
204   $result = $db->query($sql);
205   if(DB::IsError($result)) {
206     out(_("no announcement field???"));
207   } else {
208     out(_("ok"));
209   }
210
211 } else {
212   out(_("already migrated"));
213 }
214
215 // Version 2.5.19 add invalid and timeout messages
216 //
217 outn(_("Checking for timeout_id.."));
218 $sql = "SELECT timeout_id FROM ivr";
219 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
220 if(DB::IsError($check)) {
221   //  Add timeout_id field
222   //
223   $sql = "ALTER TABLE ivr ADD timeout_id INTEGER DEFAULT null";
224   $result = $db->query($sql);
225   if(DB::IsError($result)) {
226     out(_("fatal error"));
227     die_freepbx($result->getDebugInfo());
228   } else {
229     out(_("added"));
230   }
231 } else {
232   out(_("not needed"));
233 }
234 outn(_("Checking for invalid_id.."));
235 $sql = "SELECT invalid_id FROM ivr";
236 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
237 if(DB::IsError($check)) {
238   //  Add invalid_id field
239   //
240   $sql = "ALTER TABLE ivr ADD invalid_id INTEGER DEFAULT null";
241   $result = $db->query($sql);
242   if(DB::IsError($result)) {
243     out(_("fatal error"));
244     die_freepbx($result->getDebugInfo());
245   } else {
246     out(_("added"));
247   }
248 } else {
249   out(_("not needed"));
250 }
251
252 outn(_("Checking for retvm.."));
253 $sql = "SELECT retvm FROM ivr";
254 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
255 if(DB::IsError($check)) {
256   //  Add retvm field
257   //
258   $sql = "ALTER TABLE ivr ADD retvm VARCHAR(8);";
259   $result = $db->query($sql);
260   if(DB::IsError($result)) {
261     out(_("fatal error"));
262     die_freepbx($result->getDebugInfo());
263   } else {
264     out(_("added"));
265   }
266 } else {
267   out(_("not needed"));
268 }
269
270 $count = sql('SELECT COUNT(*) FROM `ivr` WHERE `enable_directory` = "CHECKED"','getOne');
271 if ($count) {
272   global $db;
273   $notifications =& notifications::create($db);
274   $extext = sprintf(_("There are %s IVRs that have the legacy Directory dialing enabled. This has been deprecated and will be removed from future releases. You should convert your IVRs to use the Directory module for this functionality and assign an IVR destination to a desired Directory. You can install the Directory module from the Online Module Repository"),$count);
275   $notifications->add_notice('ivr', 'DIRECTORY_DEPRECATED', sprintf(_('Deprecated Directory used by %s IVRs'),$count), $extext, '', true, true);
276   out(_("posting notice about deprecated functionality"));
277 }
278
279 // This used to be called from page.ivr.php every time, it should not be needed, it should
280 // be called once and be done with.
281 //
282 ivr_init();
283
284 ?>
Note: See TracBrowser for help on using the browser.