| 1 |
<?php /* $Id: install.php $ */ |
|---|
| 2 |
|
|---|
| 3 |
if (!function_exists("timeconditions_timegroups_add_group_timestrings")) { |
|---|
| 4 |
function timeconditions_timegroups_add_group_timestrings($description,$timestrings) { |
|---|
| 5 |
global $db; |
|---|
| 6 |
|
|---|
| 7 |
$sql = "insert timegroups_groups(description) VALUES ('$description')"; |
|---|
| 8 |
$db->query($sql); |
|---|
| 9 |
$timegroup=mysql_insert_id(); |
|---|
| 10 |
timeconditions_timegroups_edit_timestrings($timegroup,$timestrings); |
|---|
| 11 |
return $timegroup; |
|---|
| 12 |
} |
|---|
| 13 |
} |
|---|
| 14 |
|
|---|
| 15 |
if (!function_exists("timeconditions_timegroups_get_times")) { |
|---|
| 16 |
function timeconditions_timegroups_get_times($timegroup) { |
|---|
| 17 |
global $db; |
|---|
| 18 |
|
|---|
| 19 |
$sql = "select id, time from timegroups_details where timegroupid = $timegroup"; |
|---|
| 20 |
$results = $db->getAll($sql); |
|---|
| 21 |
if(DB::IsError($results)) { |
|---|
| 22 |
$results = null; |
|---|
| 23 |
} |
|---|
| 24 |
foreach ($results as $val) { |
|---|
| 25 |
$tmparray[] = array($val[0], $val[1]); |
|---|
| 26 |
} |
|---|
| 27 |
return $tmparray; |
|---|
| 28 |
} |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
if (!function_exists("timeconditions_timegroups_edit_timestrings")) { |
|---|
| 32 |
function timeconditions_timegroups_edit_timestrings($timegroup,$timestrings) { |
|---|
| 33 |
global $db; |
|---|
| 34 |
|
|---|
| 35 |
$sql = "delete from timegroups_details where timegroupid = $timegroup"; |
|---|
| 36 |
$db->query($sql); |
|---|
| 37 |
foreach ($timestrings as $key=>$val) { |
|---|
| 38 |
$time = $val; |
|---|
| 39 |
if (isset($time) && $time != '' && $time <> '*|*|*|*') { |
|---|
| 40 |
$sql = "insert timegroups_details (timegroupid, time) values ($timegroup, '$time')"; |
|---|
| 41 |
$db->query($sql); |
|---|
| 42 |
} |
|---|
| 43 |
} |
|---|
| 44 |
} |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
if (! function_exists("out")) { |
|---|
| 48 |
function out($text) { |
|---|
| 49 |
echo $text."<br />"; |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
if (! function_exists("outn")) { |
|---|
| 54 |
function outn($text) { |
|---|
| 55 |
echo $text; |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
if($amp_conf["AMPDBENGINE"] == "sqlite3") { |
|---|
| 60 |
$sql = " |
|---|
| 61 |
CREATE TABLE IF NOT EXISTS timeconditions ( |
|---|
| 62 |
`timeconditions_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, |
|---|
| 63 |
`displayname` VARCHAR( 50 ) , |
|---|
| 64 |
`time` INT( 11 ) , |
|---|
| 65 |
`truegoto` VARCHAR( 50 ) , |
|---|
| 66 |
`falsegoto` VARCHAR( 50 ), |
|---|
| 67 |
`deptname` VARCHAR( 50 ) |
|---|
| 68 |
) |
|---|
| 69 |
"; |
|---|
| 70 |
} |
|---|
| 71 |
else { |
|---|
| 72 |
$sql = " |
|---|
| 73 |
CREATE TABLE IF NOT EXISTS timeconditions ( |
|---|
| 74 |
`timeconditions_id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, |
|---|
| 75 |
`displayname` VARCHAR( 50 ) , |
|---|
| 76 |
`time` INT( 11 ) , |
|---|
| 77 |
`truegoto` VARCHAR( 50 ) , |
|---|
| 78 |
`falsegoto` VARCHAR( 50 ), |
|---|
| 79 |
`deptname` VARCHAR( 50 ) |
|---|
| 80 |
) |
|---|
| 81 |
"; |
|---|
| 82 |
} |
|---|
| 83 |
$check = $db->query($sql); |
|---|
| 84 |
if(DB::IsError($check)) { |
|---|
| 85 |
die_freepbx("Can not create `timeconditions` table: " . $check->getMessage() . "\n"); |
|---|
| 86 |
} |
|---|
| 87 |
if($amp_conf["AMPDBENGINE"] == "sqlite3") { |
|---|
| 88 |
|
|---|
| 89 |
$sql = " |
|---|
| 90 |
CREATE TABLE IF NOT EXISTS `timegroups_groups` ( |
|---|
| 91 |
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, |
|---|
| 92 |
`description` varchar(50) NOT NULL default '', |
|---|
| 93 |
UNIQUE (`description`) |
|---|
| 94 |
) |
|---|
| 95 |
"; |
|---|
| 96 |
} |
|---|
| 97 |
else { |
|---|
| 98 |
$sql = " |
|---|
| 99 |
CREATE TABLE IF NOT EXISTS `timegroups_groups` ( |
|---|
| 100 |
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, |
|---|
| 101 |
`description` varchar(50) NOT NULL default '', |
|---|
| 102 |
UNIQUE KEY `display` (`description`) |
|---|
| 103 |
) AUTO_INCREMENT = 1 |
|---|
| 104 |
"; |
|---|
| 105 |
} |
|---|
| 106 |
$check = $db->query($sql); |
|---|
| 107 |
if(DB::IsError($check)) { |
|---|
| 108 |
die_freepbx("Can not create `timeconditions` table: " . $check->getMessage() . "\n"); |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
if($amp_conf["AMPDBENGINE"] == "sqlite3") { |
|---|
| 112 |
|
|---|
| 113 |
$sql = " |
|---|
| 114 |
CREATE TABLE IF NOT EXISTS `timegroups_details` ( |
|---|
| 115 |
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, |
|---|
| 116 |
`timegroupid` int(11) NOT NULL default '0', |
|---|
| 117 |
`time` varchar(100) NOT NULL default '' |
|---|
| 118 |
) |
|---|
| 119 |
"; |
|---|
| 120 |
} |
|---|
| 121 |
else { |
|---|
| 122 |
$sql = " |
|---|
| 123 |
CREATE TABLE IF NOT EXISTS `timegroups_details` ( |
|---|
| 124 |
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, |
|---|
| 125 |
`timegroupid` int(11) NOT NULL default '0', |
|---|
| 126 |
`time` varchar(100) NOT NULL default '' |
|---|
| 127 |
) |
|---|
| 128 |
"; |
|---|
| 129 |
} |
|---|
| 130 |
$check = $db->query($sql); |
|---|
| 131 |
if(DB::IsError($check)) { |
|---|
| 132 |
die_freepbx("Can not create `timeconditions` table: " . $check->getMessage() . "\n"); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
// Merge old findmefollow destinations to extension |
|---|
| 136 |
// |
|---|
| 137 |
$results = array(); |
|---|
| 138 |
$sql = "SELECT timeconditions_id, truegoto, falsegoto FROM timeconditions"; |
|---|
| 139 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 140 |
if (!DB::IsError($results)) { // error - table must not be there |
|---|
| 141 |
foreach ($results as $result) { |
|---|
| 142 |
$old_false_dest = $result['falsegoto']; |
|---|
| 143 |
$old_true_dest = $result['truegoto']; |
|---|
| 144 |
$timeconditions_id = $result['timeconditions_id']; |
|---|
| 145 |
|
|---|
| 146 |
$new_false_dest = merge_ext_followme(trim($old_false_dest)); |
|---|
| 147 |
$new_true_dest = merge_ext_followme(trim($old_true_dest)); |
|---|
| 148 |
if (($new_true_dest != $old_true_dest) || ($new_false_dest != $old_false_dest)) { |
|---|
| 149 |
$sql = "UPDATE timeconditions SET truegoto = '$new_true_dest', falsegoto = '$new_false_dest' WHERE timeconditions_id = $timeconditions_id AND truegoto = '$old_true_dest' AND falsegoto ='$old_false_dest'"; |
|---|
| 150 |
$results = $db->query($sql); |
|---|
| 151 |
if(DB::IsError($results)) { |
|---|
| 152 |
die_freepbx($results->getMessage()); |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
/* Upgrade to 2.5 |
|---|
| 159 |
* Migrate time conditions to new time conditions groups |
|---|
| 160 |
*/ |
|---|
| 161 |
timeconditions_updatedb(); |
|---|
| 162 |
|
|---|
| 163 |
/* Alter the time field to int now that it refernces the id field in groups |
|---|
| 164 |
*/ |
|---|
| 165 |
// sqlite3 support as of 2.5 has correct table structure built into the CREATE |
|---|
| 166 |
if($amp_conf["AMPDBENGINE"] != "sqlite3") { |
|---|
| 167 |
outn(_("converting timeconditions time field to int..")); |
|---|
| 168 |
$sql = "ALTER TABLE `timeconditions` CHANGE `time` `time` INT (11)"; |
|---|
| 169 |
$results = $db->query($sql); |
|---|
| 170 |
if(DB::IsError($results)) { |
|---|
| 171 |
out(_("ERROR: failed to convert field ").$results->getMessage()); |
|---|
| 172 |
} else { |
|---|
| 173 |
out(_("OK")); |
|---|
| 174 |
} |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
// bring db up to date on install/upgrade |
|---|
| 178 |
// |
|---|
| 179 |
function timeconditions_updatedb() { |
|---|
| 180 |
$modinfo = module_getinfo('timeconditions'); |
|---|
| 181 |
if (is_array($modinfo)) { |
|---|
| 182 |
$ver = $modinfo['timeconditions']['dbversion']; |
|---|
| 183 |
|
|---|
| 184 |
// If previous version was older than 2.5 then migrate the timeconditions to groups |
|---|
| 185 |
// |
|---|
| 186 |
if (version_compare_freepbx($ver,'2.5','lt')) { |
|---|
| 187 |
outn(_("Checking for old timeconditions to upgrade..")); |
|---|
| 188 |
$upgradelist = timeconditions_list_forupgrade(); |
|---|
| 189 |
if (isset($upgradelist)) { |
|---|
| 190 |
// we have old conditions to upgrade |
|---|
| 191 |
// |
|---|
| 192 |
out(_("starting migration")); |
|---|
| 193 |
foreach($upgradelist as $upgrade) { |
|---|
| 194 |
$times[] = $upgrade['time']; |
|---|
| 195 |
$newid = timeconditions_timegroups_add_group_timestrings('migrated-'.$upgrade['displayname'],$times); |
|---|
| 196 |
timeconditions_set_timegroupid($upgrade['timeconditions_id'],$newid); |
|---|
| 197 |
$newtimes = timeconditions_timegroups_get_times($newid); |
|---|
| 198 |
out(sprintf(_("Upgraded %s and created group %s"), $upgrade['displayname'], 'migrated-'.$upgrade['displayname'])); |
|---|
| 199 |
if (!is_array($newtimes)) { |
|---|
| 200 |
out(sprintf(_("%sWARNING:%s No time defined for this condition, please review"),"<font color='red'>","</font>")); |
|---|
| 201 |
} |
|---|
| 202 |
unset($times); |
|---|
| 203 |
} |
|---|
| 204 |
} else { |
|---|
| 205 |
out(_("no upgrade needed")); |
|---|
| 206 |
} |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
function timeconditions_list_forupgrade() { |
|---|
| 212 |
$results = sql("SELECT * FROM timeconditions","getAll",DB_FETCHMODE_ASSOC); |
|---|
| 213 |
if(is_array($results)){ |
|---|
| 214 |
foreach($results as $result){ |
|---|
| 215 |
$list[] = $result; |
|---|
| 216 |
} |
|---|
| 217 |
} |
|---|
| 218 |
if (isset($list)) { |
|---|
| 219 |
return $list; |
|---|
| 220 |
} else { |
|---|
| 221 |
return null; |
|---|
| 222 |
} |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
function timeconditions_set_timegroupid($id, $timegroup) { |
|---|
| 226 |
sql("UPDATE timeconditions SET time = $timegroup WHERE timeconditions_id = $id;"); |
|---|
| 227 |
} |
|---|
| 228 |
?> |
|---|