root/contributed_modules/modules/customcontexts/install.php

Revision 9875, 8.6 kB (checked in by p_lindheimer, 3 years ago)

tweak moshe's change to keep outbound route orders conisten with route seq when pri is equal, bump up num of possible priorities to accmodate lots of new modules, minor tweaks re #4335

Line 
1 <?php /* $Id: install.php $ */
2 /*
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13  
14 global $db;
15 global $amp_conf;
16
17 if (! function_exists("out")) {
18     function out($text) {
19         echo $text."<br />";
20     }
21 }
22
23 if (! function_exists("outn")) {
24     function outn($text) {
25         echo $text;
26     }
27 }
28
29 // TODO: returning false will fail the install with #4345 checked in
30 //
31 if (!function_exists('timeconditions_timegroups_add_group_timestrings')) {
32   out(_('Time Conditions Module required and not present .. aborting install'));
33   return false;
34 }
35
36 $sql[] ="CREATE TABLE IF NOT EXISTS `customcontexts_contexts` (
37                 `context` varchar(100) NOT NULL default '',
38                 `description` varchar(100) NOT NULL default '',
39                 PRIMARY KEY  (`context`),
40                 UNIQUE KEY `description` (`description`)
41             )";
42
43
44 $sql[] ="CREATE TABLE IF NOT EXISTS `customcontexts_contexts_list` (
45                 `context` varchar(100) NOT NULL default '',
46                 `description` varchar(100) NOT NULL default '',
47                 `locked` tinyint(1) NOT NULL default '0',
48                 PRIMARY KEY  (`context`),
49                 UNIQUE KEY `description` (`description`)
50                 )";
51
52 $sql[] ="INSERT IGNORE INTO `customcontexts_contexts_list`
53                 (`context`, `description`, `locked`)
54                 VALUES ('from-internal', 'Default Internal Context', 1),
55                 ('from-internal-additional', 'Internal Dialplan', 0),
56                 ('outbound-allroutes', 'Outbound Routes', 0)";
57
58 $sql[] ="CREATE TABLE IF NOT EXISTS `customcontexts_includes` (
59                 `context` varchar(100) NOT NULL default '',
60                 `include` varchar(100) NOT NULL default '',
61                 `timegroupid` int(11) default NULL,
62                 `sort` int(11) NOT NULL default '0',
63                 PRIMARY KEY  (`context`,`include`),
64                 KEY `sort` (`sort`)
65 )";
66
67 $sql[] ="ALTER IGNORE TABLE `customcontexts_includes` ADD `timegroupid` INT NULL AFTER `include`";
68
69 $sql[] ="CREATE TABLE IF NOT EXISTS `customcontexts_includes_list` (
70                 `context` varchar(100) NOT NULL default '',
71                 `include` varchar(100) NOT NULL default '',
72                 `description` varchar(100) NOT NULL default '',
73                 PRIMARY KEY  (`context`,`include`)
74                 )";
75
76 $sql[] ="ALTER IGNORE TABLE `customcontexts_includes_list` ADD `missing` BOOL NOT NULL DEFAULT '0'";
77
78
79 $sql[] ="INSERT IGNORE INTO `customcontexts_includes_list` (`context`, `include`, `description`) VALUES ('from-internal', 'parkedcalls', 'Call Parking'),
80                 ('from-internal', 'from-internal-custom', 'Custom Internal Dialplan')";
81
82 $sql[] ="INSERT IGNORE INTO `customcontexts_includes_list`
83                     (`context`, `include`, `description`)
84                     VALUES ('from-internal-additional', 'outbound-allroutes', 'ALL OUTBOUND ROUTES'),
85                     ('from-internal', 'from-internal-additional', 'ENTIRE Basic Internal Dialplan')";
86
87 $sql[] ="UPDATE `customcontexts_includes_list` SET `description` = 'ALL OUTBOUND ROUTES' WHERE  `context` = 'from-internal-additional' AND `include` = 'outbound-allroutes'";
88
89 $sql[] ="CREATE TABLE IF NOT EXISTS `customcontexts_module` (
90                 `id` varchar(50) NOT NULL default '',
91                 `value` varchar(100) NOT NULL default '',
92                 PRIMARY KEY  (`id`)
93                 )";
94
95 $sql[] ="INSERT IGNORE INTO `customcontexts_module` (`id`, `value`) VALUES ('modulerawname', 'customcontexts'),
96                 ('moduledisplayname', 'Custom Contexts'),
97                 ('moduleversion', '0.3.2'),
98                 ('displaysortforincludes', 1)";
99
100 $sql[] ="UPDATE `customcontexts_module` set `value` = '0.3.2' where `id` = 'moduleversion';";
101
102 foreach ($sql as $q){
103     $db->query($q);
104         if(DB::IsError($q)) {
105             out("FATAL: ".$q->getDebugInfo()."\n");   
106         }
107 }
108
109 customcontexts_updatedb();
110
111 //bring db up to date on install/upgrade
112 function customcontexts_updatedb() {
113     global $db;
114     $sql = "ALTER IGNORE TABLE `customcontexts_includes` ADD `timegroupid` INT NULL AFTER `include` ;";
115     $db->query($sql);
116     $sql = "ALTER IGNORE TABLE `customcontexts_includes_list` ADD `missing` BOOL NOT NULL DEFAULT '0';";
117     $db->query($sql);
118     $sql = "ALTER IGNORE TABLE `customcontexts_contexts` ADD `dialrules` VARCHAR( 1000 ) NULL;";
119     $db->query($sql);
120     $sql = "ALTER IGNORE TABLE `customcontexts_includes` ADD `userules` VARCHAR( 10 ) NULL ;";
121     $db->query($sql);
122     $sql = "ALTER IGNORE TABLE `customcontexts_contexts` ADD `faildestination` VARCHAR( 250 ) NULL , ADD `featurefaildestination` VARCHAR( 250 ) NULL ;";
123     $db->query($sql);
124 //0.3.0
125     $sql = "ALTER IGNORE TABLE `customcontexts_contexts` ADD `failpin` VARCHAR( 100 ) NULL , ADD `failpincdr` BOOL NOT NULL DEFAULT '0', ADD `featurefailpin` VARCHAR( 100 ) NULL , ADD `featurefailpincdr` BOOL NOT NULL DEFAULT '0';";
126     $db->query($sql);
127 //0.3.2
128     $sql = "ALTER IGNORE TABLE `customcontexts_includes_list` ADD `sort` INT NOT NULL DEFAULT '0';";
129     $db->query($sql);
130 }
131 $ver = modules_getversion('customcontexts');
132 outn(_("checking if migration required..."));
133 if ($ver !== null && version_compare($ver, "2.8.0beta1.0", "<")) {
134         outn(_("migrating.."));
135         /*
136          * We need to now migrate from from the old format of dispname_id where the only supported dispname
137          * so far has been "routing" and the "id" used was the imperfect nnn-name. As it truns out, it was
138          * possible to have the same route name perfiously so we will try to detect that. This was really ugly
139          * so if we can't find stuff we will simply report errors and let the user go back and fix things.
140          */
141         $sql = "SELECT * FROM customcontexts_includes_list WHERE context = 'outbound-allroutes'";
142         $includes = $db->getAll($sql, DB_FETCHMODE_ASSOC);
143         if(DB::IsError($includes) || !isset($includes)) {
144             out(_("Unknown error fetching table data or no data to migrate"));
145             out(_("Migration aborted"));
146         } else {
147             /*
148              * If there are any rows then lets get our route information. We will force this module to depend on
149              * the new core, so we can count on the APIs being available. If there are indentical names, then
150              * oh well...
151              */
152             $routes = core_routing_list();
153             $newincludes = array();
154             foreach ($includes as $inc => $myinclude) {
155                 $include = explode('-',$myinclude['include'],3);
156                 $include[1] = (int)$include[1];
157                 foreach ($routes as $rt => $route) {
158                     //if we have a trunk with the same name match it and take it out of the list
159                     if ($include[2] == $route['name']){
160                         $newincludes[] = array('new' => 'outrt-'.$route['route_id'],
161                                                                     'sort' => $route['seq'], 'old' => $myinclude['include']);
162                         //unset the routes so we dont search them again
163                         unset($includes[$inc]);
164                         unset($routes[$rt]);
165                     }
166                 }   
167             }
168
169             //alert user of unmigrated routes
170             foreach ($includes as $include) {
171                 out(_('FAILED to migrating route '.$include['description'].'. NO MATCH FOUND'));
172                 outn(_("Continuing..."));
173             }
174
175             // We new have all the indices, so lets save them
176             $sql = $db->prepare('UPDATE customcontexts_includes_list SET include = ?, sort = ? WHERE include = ?');
177             $result = $db->executeMultiple($sql,$newincludes);
178             if(DB::IsError($result)) {
179                 out("FATAL: ".$result->getDebugInfo()."\n".'error updating customcontexts_includes_list table. Aborting!');   
180             } else {
181                 //now update the customcontexts_includes table
182                 foreach ($newincludes as $inc => $newinclude){
183                     unset($newincludes[$inc]['sort']);
184             }
185                 $sql = $db->prepare('UPDATE customcontexts_includes SET include = ? WHERE include = ?');
186                 $result = $db->executeMultiple($sql,$newincludes);
187                 if(DB::IsError($result)) {
188                     out("FATAL: ".$result->getDebugInfo()."\n".'error updating customcontexts_includes table. Aborting!');   
189                 } else {
190                 out(_("done! Reload FreePBX to update all settings."));               
191             }
192         }
193     }
194 } else {
195   out(_("not needed"));
196 }
197 $tgs = $db->getAll('SELECT * FROM customcontexts_timegroups',DB_FETCHMODE_ASSOC);
198 if(!DB::IsError($tgs)) {
199   outn(_("migrating customcontexts_timegroups if needed.."));               
200   foreach ($tgs as $tg) {
201     $tg_strings = sql('SELECT time FROM customcontexts_timegroups_detail WHERE timegroupid = '.$tg['id'].' ORDER BY id','getCol','time');
202     $tg_id = timeconditions_timegroups_add_group_timestrings($tg['description'],$tg_strings);
203     sql("UPDATE customcontexts_includes set timegroupid = $tg_id WHERE timegroupid = {$tg['id']}");
204   }
205   out(_("done"));               
206   outn(_("removing customcontexts_timegroups and customcontexts_tiemgroups_detail tables.."));               
207   unset($sql);
208   $sql[] = "DROP TABLE IF EXISTS `customcontexts_timegroups`";
209   $sql[] = "DROP TABLE IF EXISTS `customcontexts_timegroups_detail`";
210   foreach ($sql as $q){
211       $db->query($q);
212   }
213   out(_("done"));               
214 }
215 ?>
216
Note: See TracBrowser for help on using the browser.