customcontexts/install.php

171 lines
6.6 KiB
PHP

<?php /* $Id: install.php $ */
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
global $db;
global $amp_conf;
if (! function_exists("out")) {
function out($text) {
echo $text."<br />";
}
}
if (! function_exists("outn")) {
function outn($text) {
echo $text;
}
}
// TODO: returning false will fail the install with #4345 checked in
//
if (!function_exists('timeconditions_timegroups_add_group_timestrings')) {
out(_('Time Conditions Module required and not present .. aborting install'));
return false;
}
$sql[] ="CREATE TABLE `customcontexts_contexts` (
`context` varchar(100) NOT NULL DEFAULT '',
`description` varchar(100) NOT NULL DEFAULT '',
`dialrules` varchar(1000) DEFAULT NULL,
`faildestination` varchar(250) DEFAULT NULL,
`featurefaildestination` varchar(250) DEFAULT NULL,
`failpin` varchar(100) DEFAULT NULL,
`failpincdr` tinyint(1) NOT NULL DEFAULT '0',
`featurefailpin` varchar(100) DEFAULT NULL,
`featurefailpincdr` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
$sql[] ="CREATE TABLE `customcontexts_contexts_list` (
`context` varchar(100) NOT NULL DEFAULT '',
`description` varchar(100) NOT NULL DEFAULT '',
`locked` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
$sql[] ="INSERT INTO `customcontexts_contexts_list` (`context`, `description`, `locked`) VALUES
('from-internal', 'Default Internal Context', 1),
('from-internal-additional', 'Internal Dialplan', 0),
('outbound-allroutes', 'Outbound Routes', 0);";
$sql[] ="CREATE TABLE `customcontexts_includes` (
`context` varchar(100) NOT NULL DEFAULT '',
`include` varchar(100) NOT NULL DEFAULT '',
`timegroupid` int DEFAULT NULL,
`sort` int NOT NULL DEFAULT '0',
`missing` tinyint(1) NOT NULL DEFAULT '0',
`userules` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
// $sql[] ="ALTER IGNORE TABLE `customcontexts_includes` ADD `timegroupid` INT NULL AFTER `include`";
$sql[] ="CREATE TABLE `customcontexts_includes_list` (
`context` varchar(100) NOT NULL DEFAULT '',
`include` varchar(100) NOT NULL DEFAULT '',
`description` varchar(100) NOT NULL DEFAULT '',
`sort` int NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
// $sql[] ="ALTER IGNORE TABLE `customcontexts_includes_list` ADD `missing` BOOL NOT NULL DEFAULT '0'";
$sql[] ="INSERT INTO `customcontexts_includes_list` (`context`, `include`, `description`, `sort`) VALUES
('from-internal', 'from-internal-additional', 'ENTIRE Basic Internal Dialplan', 0),
('from-internal', 'from-internal-custom', 'Custom Internal Dialplan', 0),
('from-internal', 'parkedcalls', 'Call Parking', 0),
('from-internal-additional', 'outbound-allroutes', 'ALL OUTBOUND ROUTES', 0);";
// $sql[] ="UPDATE `customcontexts_includes_list` SET `description` = 'ALL OUTBOUND ROUTES' WHERE `context` = 'from-internal-additional' AND `include` = 'outbound-allroutes'";
$sql[] ="CREATE TABLE `customcontexts_module` (
`id` varchar(50) NOT NULL DEFAULT '',
`value` varchar(100) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;";
$sql[] ="INSERT INTO `customcontexts_module` (`id`, `value`) VALUES
('displaysortforincludes', '1'),
('moduledisplayname', 'Custom Contexts'),
('modulerawname', 'customcontexts'),
('moduleversion', '0.3.2');";
$sql[] ="ALTER TABLE `customcontexts_contexts`
ADD PRIMARY KEY (`context`),
ADD UNIQUE KEY `description` (`description`);";
$sql[] ="ALTER TABLE `customcontexts_contexts_list`
ADD PRIMARY KEY (`context`),
ADD UNIQUE KEY `description` (`description`);";
$sql[] ="ALTER TABLE `customcontexts_includes`
ADD PRIMARY KEY (`context`,`include`),
ADD KEY `sort` (`sort`);";
$sql[] ="ALTER TABLE `customcontexts_includes_list`
ADD PRIMARY KEY (`context`,`include`);";
$sql[] ="ALTER TABLE `customcontexts_module`
ADD PRIMARY KEY (`id`);
COMMIT;";
// $sql[] ="UPDATE `customcontexts_module` set `value` = '0.3.2' where `id` = 'moduleversion';";
foreach ($sql as $q){
$db->query($q);
if(DB::IsError($q)) {
out("FATAL: ".$q->getDebugInfo()."\n");
}
}
customcontexts_updatedb();
//bring db up to date on install/upgrade
function customcontexts_updatedb() {
global $db;
$sql = "ALTER IGNORE TABLE `customcontexts_includes` ADD `timegroupid` INT NULL AFTER `include` ;";
$db->query($sql);
$sql = "ALTER IGNORE TABLE `customcontexts_includes_list` ADD `missing` BOOL NOT NULL DEFAULT '0';";
$db->query($sql);
$sql = "ALTER IGNORE TABLE `customcontexts_contexts` ADD `dialrules` VARCHAR( 1000 ) NULL;";
$db->query($sql);
$sql = "ALTER IGNORE TABLE `customcontexts_includes` ADD `userules` VARCHAR( 10 ) NULL ;";
$db->query($sql);
$sql = "ALTER IGNORE TABLE `customcontexts_contexts` ADD `faildestination` VARCHAR( 250 ) NULL , ADD `featurefaildestination` VARCHAR( 250 ) NULL ;";
$db->query($sql);
//0.3.0
$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';";
$db->query($sql);
//0.3.2
$sql = "ALTER IGNORE TABLE `customcontexts_includes_list` ADD `sort` INT NOT NULL DEFAULT '0';";
$db->query($sql);
}
$tgs = $db->getAll('SELECT * FROM customcontexts_timegroups',DB_FETCHMODE_ASSOC);
if(!DB::IsError($tgs)) {
outn(_("migrating customcontexts_timegroups if needed.."));
foreach ($tgs as $tg) {
$tg_strings = sql('SELECT time FROM customcontexts_timegroups_detail WHERE timegroupid = '.$tg['id'].' ORDER BY id','getCol','time');
$tg_id = timeconditions_timegroups_add_group_timestrings($tg['description'],$tg_strings);
sql("UPDATE customcontexts_includes set timegroupid = $tg_id WHERE timegroupid = {$tg['id']}");
}
out(_("done"));
outn(_("removing customcontexts_timegroups and customcontexts_tiemgroups_detail tables.."));
unset($sql);
$sql[] = "DROP TABLE IF EXISTS `customcontexts_timegroups`";
$sql[] = "DROP TABLE IF EXISTS `customcontexts_timegroups_detail`";
foreach ($sql as $q){
$db->query($q);
}
out(_("done"));
}
?>