root/modules/branches/2.4/queues/install.php

Revision 5546, 10.2 kB (checked in by p_lindheimer, 5 years ago)

#2528 add context = 'ext-queues' when getting destination statement

Line 
1 <?php
2 global $db;
3
4 if (! function_exists("out")) {
5     function out($text) {
6         echo $text."<br />";
7     }
8 }
9
10 if (! function_exists("outn")) {
11     function outn($text) {
12         echo $text;
13     }
14 }
15
16 $results = array();
17 $sql = "SELECT args, extension, priority FROM extensions WHERE context = 'ext-queues' AND descr = 'jump'";
18 $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
19 if (!DB::IsError($results)) { // error - table must not be there
20     foreach ($results as $result) {
21         $old_dest  = $result['args'];
22         $extension = $result['extension'];
23         $priority  = $result['priority'];
24
25         $new_dest = merge_ext_followme(trim($old_dest));
26         if ($new_dest != $old_dest) {
27             $sql = "UPDATE extensions SET args = '$new_dest' WHERE extension = '$extension' AND priority = '$priority' AND context = 'ext-queues' AND descr = 'jump' AND args = '$old_dest'";
28             $results = $db->query($sql);
29             if(DB::IsError($results)) {
30                 die_freepbx($results->getMessage());
31             }
32         }
33     }
34 }
35
36 // Version 2.2.14 change - bump up priority on Goto because of inserted alert-info
37 //
38
39 $results = $db->query("UPDATE extensions SET priority = '7' WHERE context = 'ext-queues' AND priority = '6' AND application = 'Goto' AND descr = 'jump'");
40 if(DB::IsError($results)) {
41     echo $results->getMessage();
42     return false;
43 }
44
45     /** 2.4.0 Migrate away from legacy extensions table and queues table to queues_config and queues_details
46     */
47
48     $return_code = true;
49
50     outn(_("Checking for legacy queues table.."));
51     $sql = "SELECT * FROM `queues`";
52     $results = $db->query($sql);
53     if (DB::IsError($results)) {
54         out(_("NO table found, no migration to do just create tables"));
55         // Must not be a table so don't try to migrate
56         $migrate_queues_config = false;
57         $migrate_queues_details = false;
58     } else {
59         out(_("OK"));
60         $migrate_queues_config = true;
61         $migrate_queues_details = true;
62     }
63
64     // Create/Migrate the queues_details table, don't put IF NOT EXISTS so we
65     // can get the status in the error
66     //
67     $sql = "
68     CREATE TABLE `queues_details` (
69         `id` varchar( 45 ) NOT NULL default '-1',
70         `keyword` varchar( 30 ) NOT NULL default '',
71         `data` varchar( 150 ) NOT NULL default '',
72         `flags` int( 1 ) NOT NULL default '0',
73         PRIMARY KEY ( `id` , `keyword` , `data` )
74     )";
75
76     outn(_("Creating queues_details.."));
77     $results = $db->query($sql);
78     if (DB::IsError($results)) {
79         $migrate_queues_details = false;
80         if ($results->getCode() == DB_ERROR_ALREADY_EXISTS) {
81             out(_("already exists"));
82         } else {
83             out(_("ERROR: could not create table"));
84             $return_code = false;
85         }
86     } else if ($migrate_queues_details) {
87         out(_("OK"));
88         // Successfully created table so migrate the data next
89         //
90         $sql = "
91         INSERT INTO `queues_details`
92         SELECT *
93         FROM `queues`
94         WHERE
95         keyword NOT IN ('rtone', 'account', 'context')
96         ";
97
98         outn(_("Migrating to queues_details.."));
99         $results = $db->query($sql);
100         if (DB::IsError($results)) {
101             out(_("ERROR: could not migrate to queues_details"));
102             $return_code = false;
103         } else {
104             out(_("OK"));
105         }
106     } else {
107         out(_("OK"));
108     }
109     // Finished migrating to queues_details
110
111     // Create the queues_config table, don't put IF NOT EXISTS so we
112     // can get the status in the error
113     //
114     $sql = "
115     CREATE TABLE queues_config (
116       extension varchar(20) NOT NULL default '',
117       descr varchar(35) NOT NULL default '',
118       grppre varchar(100) NOT NULL default '',
119       alertinfo varchar(254) NOT NULL default '',
120       joinannounce varchar(254) NOT NULL default '',
121       ringing tinyint(1) NOT NULL default '0',
122       agentannounce varchar(254) NOT NULL default '',
123       maxwait varchar(8) NOT NULL default '',
124       `password` varchar(20) NOT NULL default '',
125       ivr_id varchar(8) NOT NULL default '0',
126       dest varchar(50) NOT NULL default '',
127       cwignore tinyint(1) NOT NULL default '0',
128     PRIMARY KEY  (extension)
129     )";
130
131     outn(_("Creating queues_config.."));
132     $results = $db->query($sql);
133     if (DB::IsError($results)) {
134         $migrate_queues_config = false;
135         if ($results->getCode() == DB_ERROR_ALREADY_EXISTS) {
136             out(_("already exists"));
137         } else {
138             out(_("ERROR: could not create table"));
139             $return_code = false;
140         }
141     } else if ($migrate_queues_config) {
142         out(_("OK"));
143         // Successfully created table so migrate the data next
144         //
145         $got_items = true;
146         outn(_("Migrating data to queues_config.."));
147         $sql = "SELECT id, data context FROM queues WHERE keyword = 'context'";
148         $context_results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
149         if(DB::IsError($context_results)) {
150             out(_("ERROR: accessing queues table obtaining context info, aborting"));
151             $return_code = false;
152             $got_items = false;
153         }
154         $sql = "SELECT id, data rtone FROM queues WHERE keyword = 'rtone'";
155         $rtone_results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
156         if(DB::IsError($context_results)) {
157             out(_("ERROR: accessing queues table obtaining rtone info, aborting"));
158             $return_code = false;
159             $got_items = false;
160         }
161         if ($got_items) {
162             // Got context & rtone, make a hash and then  get the list of IDs
163             //
164             $context_hash = array();
165             foreach ($context_results as $item) {
166                 $context_hash[$item['id']] = $item['context'];
167             }
168             $rtone_hash = array();
169             foreach ($rtone_results as $item) {
170                 $rtone_hash[$item['id']] = $item['rtone'];
171             }
172             $sql = "SELECT DISTINCT id FROM `queues`";
173             $queue_ids = $db->getAll($sql, DB_FETCHMODE_ASSOC);
174             if(DB::IsError($queue_ids)) {
175                 out(_("ERROR: accessing queues table obtaining id list, aborting"));
176                 $return_code = false;
177             } else {
178                 // Got ids, now we need to go through and get info from legacy table
179                 // but first lets create a hash with the context info
180                 foreach ($queue_ids as $item) {
181                     if (isset($context_hash[$item['id']])) {
182                         $queue_ids_hash[$item['id']]['ivr_id'] = ltrim('ivr-',$context_hash[$item['id']]);
183                     } else {
184                         $queue_ids_hash[$item['id']]['ivr_id'] = '';
185                     }
186                     if (isset($rtone_hash[$item['id']])) {
187                         $queue_ids_hash[$item['id']]['rtone'] = $rtone_hash[$item['id']];
188                     } else {
189                         $queue_ids_hash[$item['id']]['rtone'] = '0';
190                     }
191                 }
192                 // Now we have a queue_ids_hash with each unique id and the state of ivr_id. The
193                 // remaining information comes from the legacy extensions table
194                 //
195                 foreach ($queue_ids as $item) {
196                     $account = $item['id']; // don't run through addslashes() here
197
198                     $ivr_id = isset($queue_ids_hash['account']['ivr_id']) ? $queue_ids_hash['account']['ivr_id'] : '';
199                     $ivr_id = addslashes($ivr_id);
200                     $rtone  = isset($queue_ids_hash['account']['rtone']) ? $queue_ids_hash['account']['rtone'] : '';
201                     $rtone  = addslashes($rtone);
202
203                     // get CID Prefix
204                     //
205                     $sql = "SELECT args FROM extensions WHERE extension = '$account' AND context = 'ext-queues' AND application = 'SetCIDName'";
206                     list($args) = $db->getRow($sql);
207                     $prefix = explode('$',$args); //in table like prefix${CALLERID(name)}
208                     $grppre = isset($prefix[0]) ?    $prefix[0] : '';   
209                     $grppre = addslashes($grppre);
210
211                     // get ALERT_INFO
212                     //
213                     $sql = "SELECT args FROM extensions WHERE extension = '$account' AND context = 'ext-queues' AND application = 'SetVar' AND args LIKE '__ALERT_INFO=%'";
214                     list($args) = $db->getRow($sql);
215                     $alertinfo = substr($args,strlen("__ALERT_INFO="));
216                     $alertinfo = isset($alertinfo) ? addslashes($alertinfo) : '';
217
218                     // get maxwait time, agentannounce and name from Queue command
219                     //
220                     $sql = "SELECT args,descr FROM extensions WHERE extension = '$account' AND context = 'ext-queues' AND application = 'Queue'";
221                     list($args, $descr) = $db->getRow($sql);
222                     $maxwait = explode(',',$args);  //in table like queuenum,t,,,maxwait
223
224                     $agentannounce = isset($maxwait[3]) ? $maxwait[3] : '';
225                     $agentannounce = addslashes($agentannounce);
226                     $maxwait       = isset($maxwait[4]) ? $maxwait[4] : '';
227                     $maxwait       = addslashes($maxwait);
228                     $descr         = isset($descr) ? addslashes($descr) : '';
229
230                     // get joinannounce from Playback command
231                     //
232                     $sql = "SELECT args FROM extensions WHERE extension = '$account' AND context = 'ext-queues' and application = 'Playback'";
233                     list($args) = $db->getRow($sql);
234                     $joinannounce = isset($args) && $args !== NULL ? addslashes($args) : '';
235                     
236
237                     // get password from AddQueueMember command
238                     //
239                     $sql = "SELECT args FROM extensions WHERE extension = '$account*' AND context = 'ext-queues'";
240                     list($args) = $db->getRow($sql);
241                     $password_arr = explode(',',$args); //in table like agent-add,account,password
242                     $password = isset($password_arr[2]) ? $password_arr[2] : '';
243                     $password = addslashes($password);
244
245                     // get the failover destination (desc=jump)
246                     //
247                     $sql = "SELECT args FROM extensions WHERE extension = '".$account."' AND descr = 'jump' AND context = 'ext-queues'";
248                     list($args) = $db->getRow($sql);
249                     $dest = isset($args) ? addslashes($args) : '';
250
251                     // insert cwignore new to 2.4
252                     //
253                     $cwignore = '0';
254                     $account = addslashes($account);
255
256                     // Got everything we need for this id (account) so insert it into the queues_config table
257                     $sql =
258                     "INSERT INTO queues_config (extension, descr, grppre, alertinfo, joinannounce, ringing, agentannounce, maxwait, password, ivr_id, dest, cwignore)
259              VALUES
260                     ('$account', '$descr', '$grppre', '$alertinfo', '$joinannounce', '$rtone', '$agentannounce', '$maxwait', '$password', '$ivr_id', '$dest', '$cwignore')";
261                     $results = $db->query($sql);
262                     if (DB::IsError($results)) {
263                         outn(sprintf(_("ERROR: inserting data for row %s: %s.."),$account,$results->getMessage()));
264                         $return_code = false;
265                     }
266                 }
267             }
268             if ($return_code) {
269                 out(_("OK"));
270             } else {
271                 out(_("ERROR were encountered"));
272             }
273         }
274     } else {
275         out(_("OK"));
276     }
277     // Finished migrating to queues_config
278
279     // Now if all went well, we will remove the old queues table and entries in the extensions table
280     //
281     if ($return_code) {
282         outn(_("Dropping old queues table.."));
283         $sql = "DROP TABLE IF EXISTS queues";
284         $results = $db->query($sql);
285         if (DB::IsError($results)) {
286             out(sprintf(_("WARNING FAILED %s"),$results->getMessage()));
287         } else {
288             out(_("OK"));
289         }
290
291         outn(_("removing queues data extensions table.."));
292         $sql = "DELETE FROM extensions WHERE context = 'ext-queues'";
293         $results = $db->query($sql);
294         if (DB::IsError($results)) {
295             out(sprintf(_("WARNING FAILED %s"),$results->getMessage()));
296         } else {
297             out(_("OK"));
298         }
299     } else {
300         return $return_code;
301     }
302
303 ?>
304
Note: See TracBrowser for help on using the browser.