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

Revision 11003, 17.4 kB (checked in by p_lindheimer, 2 years ago)

make category madatory even for modules, makes things more clear re #4740

Line 
1 <?php
2 global $db;
3 global $amp_conf;
4
5 // Add Feature Codes for Toggle Queues - Using *45
6 $fcc = new featurecode('queues', 'que_toggle');
7 $fcc->setDescription('Queue Toggle');
8 $fcc->setDefault('*45');
9 $fcc->update();
10 unset($fcc);
11
12 if (! function_exists("out")) {
13     function out($text) {
14         echo $text."<br />";
15     }
16 }
17
18 if (! function_exists("outn")) {
19     function outn($text) {
20         echo $text;
21     }
22 }
23
24 $results = array();
25 $sql = "SELECT args, extension, priority FROM extensions WHERE context = 'ext-queues' AND descr = 'jump'";
26 $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
27 if (!DB::IsError($results)) { // error - table must not be there
28     foreach ($results as $result) {
29         $old_dest  = $result['args'];
30         $extension = $result['extension'];
31         $priority  = $result['priority'];
32
33         $new_dest = merge_ext_followme(trim($old_dest));
34         if ($new_dest != $old_dest) {
35             $sql = "UPDATE extensions SET args = '$new_dest' WHERE extension = '$extension' AND priority = '$priority' AND context = 'ext-queues' AND descr = 'jump' AND args = '$old_dest'";
36             $results = $db->query($sql);
37             if(DB::IsError($results)) {
38                 die_freepbx($results->getMessage());
39             }
40         }
41     }
42 }
43
44 // Version 2.2.14 change - bump up priority on Goto because of inserted alert-info
45 //
46
47 $results = $db->query("UPDATE extensions SET priority = '7' WHERE context = 'ext-queues' AND priority = '6' AND application = 'Goto' AND descr = 'jump'");
48 if(DB::IsError($results)) {
49     echo $results->getMessage();
50     return false;
51 }
52
53     /** 2.4.0 Migrate away from legacy extensions table and queues table to queues_config and queues_details
54     */
55
56     $return_code = true;
57
58     outn(_("Checking for legacy queues table.."));
59     $sql = "SELECT * FROM `queues`";
60     $results = $db->query($sql);
61     if (DB::IsError($results)) {
62         out(_("NO table found, no migration to do just create tables"));
63         // Must not be a table so don't try to migrate
64         $migrate_queues_config = false;
65         $migrate_queues_details = false;
66     } else {
67         out(_("OK"));
68         $migrate_queues_config = true;
69         $migrate_queues_details = true;
70     }
71
72     // Create/Migrate the queues_details table, don't put IF NOT EXISTS so we
73     // can get the status in the error
74     //
75     $sql = "
76     CREATE TABLE IF NOT EXISTS `queues_details` (
77         `id` varchar( 45 ) NOT NULL default '-1',
78         `keyword` varchar( 30 ) NOT NULL default '',
79         `data` varchar( 150 ) NOT NULL default '',
80         `flags` int( 1 ) NOT NULL default '0',
81         PRIMARY KEY ( `id` , `keyword` , `data` )
82     )";
83
84     outn(_("Creating queues_details.."));
85     $results = $db->query($sql);
86     if (DB::IsError($results)) {
87         $migrate_queues_details = false;
88         if ($results->getCode() == DB_ERROR_ALREADY_EXISTS) {
89             out(_("already exists"));
90         } else {
91             out(_("ERROR: could not create table"));
92             $return_code = false;
93         }
94     } else if ($migrate_queues_details) {
95         out(_("OK"));
96         // Successfully created table so migrate the data next
97         //
98         $sql = "
99         INSERT INTO `queues_details`
100         SELECT *
101         FROM `queues`
102         WHERE
103         keyword NOT IN ('rtone', 'account', 'context')
104         ";
105
106         outn(_("Migrating to queues_details.."));
107         $results = $db->query($sql);
108         if (DB::IsError($results)) {
109             out(_("ERROR: could not migrate to queues_details"));
110             $return_code = false;
111         } else {
112             out(_("OK"));
113         }
114     } else {
115         out(_("OK"));
116     }
117     // Finished migrating to queues_details
118
119     // Create the queues_config table, don't put IF NOT EXISTS so we
120     // can get the status in the error
121     //
122     // for sqlite3, create the final table template since sqlite3
123     // support officially begins at 2.5 release.
124     if($amp_conf["AMPDBENGINE"] == "sqlite3")  {
125
126         $sql = "
127         CREATE TABLE IF NOT EXISTS queues_config (
128           extension varchar(20) NOT NULL default '',
129           descr varchar(35) NOT NULL default '',
130           grppre varchar(100) NOT NULL default '',
131           alertinfo varchar(254) NOT NULL default '',
132           joinannounce_id int,
133           ringing tinyint(1) NOT NULL default '0',
134           agentannounce_id int,
135           maxwait varchar(8) NOT NULL default '',
136           `password` varchar(20) NOT NULL default '',
137           ivr_id varchar(8) NOT NULL default '0',
138           dest varchar(50) NOT NULL default '',
139           cwignore tinyint(1) NOT NULL default '0',
140           `qregex` VARCHAR( 255 ) NULL,
141             `queuewait` TINYINT( 1 ) DEFAULT 0,
142             `use_queue_context` TINYINT( 1 ) DEFAULT 0,
143             `togglehint` TINYINT( 1 ) DEFAULT 0,
144         PRIMARY KEY  (extension)
145         )
146         ";
147     }
148     else  {
149         $sql = "
150         CREATE TABLE IF NOT EXISTS queues_config (
151           extension varchar(20) NOT NULL default '',
152           descr varchar(35) NOT NULL default '',
153           grppre varchar(100) NOT NULL default '',
154           alertinfo varchar(254) NOT NULL default '',
155           joinannounce varchar(254) NOT NULL default '',
156           ringing tinyint(1) NOT NULL default '0',
157           agentannounce varchar(254) NOT NULL default '',
158           maxwait varchar(8) NOT NULL default '',
159           `password` varchar(20) NOT NULL default '',
160           ivr_id varchar(8) NOT NULL default '0',
161           dest varchar(50) NOT NULL default '',
162           cwignore tinyint(1) NOT NULL default '0',
163             `queuewait` TINYINT( 1 ) DEFAULT 0,
164             `use_queue_context` TINYINT( 1 ) DEFAULT 0,
165             `togglehint` TINYINT( 1 ) DEFAULT 0,
166         PRIMARY KEY  (extension)
167         )
168         ";
169
170     }
171
172     outn(_("Creating queues_config.."));
173     $results = $db->query($sql);
174     if (DB::IsError($results)) {
175         $migrate_queues_config = false;
176         if ($results->getCode() == DB_ERROR_ALREADY_EXISTS) {
177             out(_("already exists"));
178         } else {
179             out(_("ERROR: could not create table"));
180             $return_code = false;
181         }
182     } else if ($migrate_queues_config) {
183         out(_("OK"));
184         // Successfully created table so migrate the data next
185         //
186         $got_items = true;
187         outn(_("Migrating data to queues_config.."));
188         $sql = "SELECT id, data context FROM queues WHERE keyword = 'context'";
189         $context_results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
190         if(DB::IsError($context_results)) {
191             out(_("ERROR: accessing queues table obtaining context info, aborting"));
192             $return_code = false;
193             $got_items = false;
194         }
195         $sql = "SELECT id, data rtone FROM queues WHERE keyword = 'rtone'";
196         $rtone_results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
197         if(DB::IsError($context_results)) {
198             out(_("ERROR: accessing queues table obtaining rtone info, aborting"));
199             $return_code = false;
200             $got_items = false;
201         }
202         if ($got_items) {
203             // Got context & rtone, make a hash and then  get the list of IDs
204             //
205             $context_hash = array();
206             foreach ($context_results as $item) {
207                 $context_hash[$item['id']] = $item['context'];
208             }
209             $rtone_hash = array();
210             foreach ($rtone_results as $item) {
211                 $rtone_hash[$item['id']] = $item['rtone'];
212             }
213             $sql = "SELECT DISTINCT id FROM `queues`";
214             $queue_ids = $db->getAll($sql, DB_FETCHMODE_ASSOC);
215             if(DB::IsError($queue_ids)) {
216                 out(_("ERROR: accessing queues table obtaining id list, aborting"));
217                 $return_code = false;
218             } else {
219                 // Got ids, now we need to go through and get info from legacy table
220                 // but first lets create a hash with the context info
221                 foreach ($queue_ids as $item) {
222                     if (isset($context_hash[$item['id']])) {
223                         $queue_ids_hash[$item['id']]['ivr_id'] = ltrim('ivr-',$context_hash[$item['id']]);
224                     } else {
225                         $queue_ids_hash[$item['id']]['ivr_id'] = '';
226                     }
227                     if (isset($rtone_hash[$item['id']])) {
228                         $queue_ids_hash[$item['id']]['rtone'] = $rtone_hash[$item['id']];
229                     } else {
230                         $queue_ids_hash[$item['id']]['rtone'] = '0';
231                     }
232                 }
233                 // Now we have a queue_ids_hash with each unique id and the state of ivr_id. The
234                 // remaining information comes from the legacy extensions table
235                 //
236                 foreach ($queue_ids as $item) {
237                     $account = $item['id']; // don't run through addslashes() here
238
239                     $ivr_id = isset($queue_ids_hash['account']['ivr_id']) ? $queue_ids_hash['account']['ivr_id'] : '';
240                     $ivr_id = addslashes($ivr_id);
241                     $rtone  = isset($queue_ids_hash['account']['rtone']) ? $queue_ids_hash['account']['rtone'] : '';
242                     $rtone  = addslashes($rtone);
243
244                     // get CID Prefix
245                     //
246                     $sql = "SELECT args FROM extensions WHERE extension = '$account' AND context = 'ext-queues' AND application = 'SetCIDName'";
247                     list($args) = $db->getRow($sql);
248                     $prefix = explode('$',$args); //in table like prefix${CALLERID(name)}
249                     $grppre = isset($prefix[0]) ?    $prefix[0] : '';   
250                     $grppre = addslashes($grppre);
251
252                     // get ALERT_INFO
253                     //
254                     $sql = "SELECT args FROM extensions WHERE extension = '$account' AND context = 'ext-queues' AND application = 'SetVar' AND args LIKE '__ALERT_INFO=%'";
255                     list($args) = $db->getRow($sql);
256                     $alertinfo = substr($args,strlen("__ALERT_INFO="));
257                     $alertinfo = isset($alertinfo) ? addslashes($alertinfo) : '';
258
259                     // get maxwait time, agentannounce and name from Queue command
260                     //
261                     $sql = "SELECT args,descr FROM extensions WHERE extension = '$account' AND context = 'ext-queues' AND application = 'Queue'";
262                     list($args, $descr) = $db->getRow($sql);
263                     $maxwait = explode(',',$args);  //in table like queuenum,t,,,maxwait
264
265                     $agentannounce = isset($maxwait[3]) ? $maxwait[3] : '';
266                     $agentannounce = addslashes($agentannounce);
267                     $maxwait       = isset($maxwait[4]) ? $maxwait[4] : '';
268                     $maxwait       = addslashes($maxwait);
269                     $descr         = isset($descr) ? addslashes($descr) : '';
270
271                     // get joinannounce from Playback command
272                     //
273                     $sql = "SELECT args FROM extensions WHERE extension = '$account' AND context = 'ext-queues' and application = 'Playback'";
274                     list($args) = $db->getRow($sql);
275                     $joinannounce = isset($args) && $args !== NULL ? addslashes($args) : '';
276                     
277
278                     // get password from AddQueueMember command
279                     //
280                     $sql = "SELECT args FROM extensions WHERE extension = '$account*' AND context = 'ext-queues'";
281                     list($args) = $db->getRow($sql);
282                     $password_arr = explode(',',$args); //in table like agent-add,account,password
283                     $password = isset($password_arr[2]) ? $password_arr[2] : '';
284                     $password = addslashes($password);
285
286                     // get the failover destination (desc=jump)
287                     //
288                     $sql = "SELECT args FROM extensions WHERE extension = '".$account."' AND descr = 'jump' AND context = 'ext-queues'";
289                     list($args) = $db->getRow($sql);
290                     $dest = isset($args) ? addslashes($args) : '';
291
292                     // insert cwignore new to 2.4
293                     //
294                     $cwignore = '0';
295                     $account = addslashes($account);
296
297                     // Got everything we need for this id (account) so insert it into the queues_config table
298                     $sql =
299                     "INSERT INTO queues_config (extension, descr, grppre, alertinfo, joinannounce, ringing, agentannounce, maxwait, password, ivr_id, dest, cwignore)
300              VALUES
301                     ('$account', '$descr', '$grppre', '$alertinfo', '$joinannounce', '$rtone', '$agentannounce', '$maxwait', '$password', '$ivr_id', '$dest', '$cwignore')";
302                     $results = $db->query($sql);
303                     if (DB::IsError($results)) {
304                         outn(sprintf(_("ERROR: inserting data for row %s: %s.."),$account,$results->getMessage()));
305                         $return_code = false;
306                     }
307                 }
308             }
309             if ($return_code) {
310                 out(_("OK"));
311             } else {
312                 out(_("ERROR were encountered"));
313             }
314         }
315     } else {
316         out(_("OK"));
317     }
318     // Finished migrating to queues_config
319
320     // Now if all went well, we will remove the old queues table and entries in the extensions table
321     //
322     if ($return_code) {
323         outn(_("Dropping old queues table.."));
324         $sql = "DROP TABLE IF EXISTS queues";
325         $results = $db->query($sql);
326         if (DB::IsError($results)) {
327             out(sprintf(_("WARNING FAILED %s"),$results->getMessage()));
328         } else {
329             out(_("OK"));
330         }
331
332         outn(_("removing queues data extensions table.."));
333         $sql = "DELETE FROM extensions WHERE context = 'ext-queues'";
334         $results = $db->query($sql);
335         if (DB::IsError($results)) {
336             out(sprintf(_("WARNING FAILED %s"),$results->getMessage()));
337         } else {
338             out(_("OK"));
339         }
340     } else {
341         return $return_code;
342     }
343
344     // Version 2.5 upgrade
345     outn(_("checking for qregex field.."));
346     $sql = "SELECT `qregex` FROM queues_config";
347     $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
348     if(DB::IsError($check)) {
349         // add new field
350         $sql = "ALTER TABLE queues_config ADD `qregex` VARCHAR( 255 ) NULL ;";
351         $result = $db->query($sql);
352         if(DB::IsError($result)) {
353             die_freepbx($result->getDebugInfo());
354         }
355         out(_("OK"));
356     } else {
357         out(_("already exists"));
358     }
359
360 // Version 2.5 migrate to recording ids
361 // Note: we purposely did not chnage the inital creation of the
362 //       recording ids as it is safer with all the complex
363 //       migration code to simply stick with what works and
364 //       then convert it here even if new.
365 //
366 outn(_("Checking if recordings need migration.."));
367 $sql = "SELECT agentannounce_id FROM queues_config";
368 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
369 if(DB::IsError($check)) {
370     //  Add recording_id field
371     //
372     out(_("migrating"));
373     outn(_("adding agentannounce_id field.."));
374   $sql = "ALTER TABLE queues_config ADD agentannounce_id INTEGER";
375   $result = $db->query($sql);
376   if(DB::IsError($result)) {
377         out(_("fatal error"));
378         die_freepbx($result->getDebugInfo());
379     } else {
380         out(_("ok"));
381     }
382     outn(_("adding joinannounce_id field.."));
383   $sql = "ALTER TABLE queues_config ADD joinannounce_id INTEGER";
384   $result = $db->query($sql);
385   if(DB::IsError($result)) {
386         out(_("fatal error"));
387         die_freepbx($result->getDebugInfo());
388     } else {
389         out(_("ok"));
390     }
391
392     // Get all the valudes and replace them with recording_id
393     //
394     outn(_("migrate agentannounce to ids.."));
395   $sql = "SELECT `extension`, `agentannounce` FROM `queues_config`";
396     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
397     if(DB::IsError($results)) {
398         out(_("fatal error"));
399         die_freepbx($results->getDebugInfo());   
400     }
401     $migrate_arr = array();
402     $count = 0;
403     foreach ($results as $row) {
404         if (trim($row['agentannounce']) != '') {
405             $rec_id = recordings_get_or_create_id($row['agentannounce'], 'queues');
406             $migrate_arr[] = array($rec_id, $row['extension']);
407             $count++;
408         }
409     }
410     if ($count) {
411         $compiled = $db->prepare('UPDATE `queues_config` SET `agentannounce_id` = ? WHERE `extension` = ?');
412         $result = $db->executeMultiple($compiled,$migrate_arr);
413         if(DB::IsError($result)) {
414             out(_("fatal error"));
415             die_freepbx($result->getDebugInfo());   
416         }
417     }
418     out(sprintf(_("migrated %s entries"),$count));
419
420     outn(_("migrate joinannounce to ids.."));
421   $sql = "SELECT `extension`, `joinannounce` FROM `queues_config`";
422     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
423     if(DB::IsError($results)) {
424         out(_("fatal error"));
425         die_freepbx($results->getDebugInfo());   
426     }
427     $migrate_arr = array();
428     $count = 0;
429     foreach ($results as $row) {
430         if (trim($row['joinannounce']) != '') {
431             $rec_id = recordings_get_or_create_id($row['joinannounce'], 'queues');
432             $migrate_arr[] = array($rec_id, $row['extension']);
433             $count++;
434         }
435     }
436     if ($count) {
437         $compiled = $db->prepare('UPDATE `queues_config` SET `joinannounce_id` = ? WHERE `extension` = ?');
438         $result = $db->executeMultiple($compiled,$migrate_arr);
439         if(DB::IsError($result)) {
440             out(_("fatal error"));
441             die_freepbx($result->getDebugInfo());   
442         }
443     }
444     out(sprintf(_("migrated %s entries"),$count));
445
446     // Now remove the old recording field replaced by new id field
447     //
448     outn(_("dropping agentannounce field.."));
449   // sqlite doesn't support drop syntax, but since we already CREATE'd the table properly, these don't need to be executed anyway
450   if($amp_conf["AMPDBENGINE"] != "sqlite3")  {
451       $sql = "ALTER TABLE `queues_config` DROP `agentannounce`";
452       $result = $db->query($sql);
453       if(DB::IsError($result)) {
454             out(_("no agentannounce field???"));
455         } else {
456             out(_("ok"));
457         }
458         outn(_("dropping joinannounce field.."));
459       $sql = "ALTER TABLE `queues_config` DROP `joinannounce`";
460       $result = $db->query($sql);
461       if(DB::IsError($result)) {
462             out(_("no joinannounce field???"));
463         } else {
464             out(_("ok"));
465         }
466     } else {
467         out(_("already migrated"));
468     }
469   }
470
471 outn(_("checking for queuewait field.."));
472 $sql = "SELECT `queuewait` FROM queues_config";
473 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
474 if(DB::IsError($check)) {
475     // add new field
476     $sql = "ALTER TABLE queues_config ADD `queuewait` TINYINT( 1 ) DEFAULT 0";
477     $result = $db->query($sql);
478     if(DB::IsError($result)) {
479         die_freepbx($result->getDebugInfo());
480     }
481     out(_("OK"));
482 } else {
483     out(_("already exists"));
484 }
485
486 outn(_("checking for use_queue_context field.."));
487 $sql = "SELECT `use_queue_context` FROM queues_config";
488 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
489 if(DB::IsError($check)) {
490     // add new field
491     $sql = "ALTER TABLE queues_config ADD `use_queue_context` TINYINT( 1 ) DEFAULT 0";
492     $result = $db->query($sql);
493     if(DB::IsError($result)) {
494         die_freepbx($result->getDebugInfo());
495     }
496     out(_("OK"));
497 } else {
498     out(_("already exists"));
499 }
500
501 outn(_("checking for togglehint field.."));
502 $sql = "SELECT `togglehint` FROM queues_config";
503 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
504 if(DB::IsError($check)) {
505     // add new field
506     $sql = "ALTER TABLE queues_config ADD `togglehint` TINYINT( 1 ) DEFAULT 0";
507     $result = $db->query($sql);
508     if(DB::IsError($result)) {
509         die_freepbx($result->getDebugInfo());
510     }
511     out(_("OK"));
512 } else {
513     out(_("already exists"));
514 }
515
516 $freepbx_conf =& freepbx_conf::create();
517
518   // USEQUEUESTATE
519   //
520   $set['value'] = false;
521   $set['defaultval'] =& $set['value'];
522   $set['readonly'] = 0;
523   $set['hidden'] = 0;
524   $set['level'] = 3;
525   $set['module'] = 'queues';
526   $set['category'] = 'System Setup';
527   $set['emptyok'] = 0;
528   $set['description'] = 'Setting this flag will generate the required dialplan to integrate with the following Asterisk patch: <b>https://issues.asterisk.org/view.php?id=15168</b>. This setting is obsolete on Asterisk 1.8+ systems where the hint state is now standard and always used. This asterisk patch is only available on Asterisk 1.4, trying to use this setting on Asterisk 1.6 will break some queue behavior and should be avoided';
529   $set['type'] = CONF_TYPE_BOOL;
530   $freepbx_conf->define_conf_setting('USEQUEUESTATE',$set,true);
531
Note: See TracBrowser for help on using the browser.