| 1 |
<?php |
|---|
| 2 |
//for translation only |
|---|
| 3 |
if (false) { |
|---|
| 4 |
_("Findme Follow Toggle"); |
|---|
| 5 |
} |
|---|
| 6 |
|
|---|
| 7 |
global $db; |
|---|
| 8 |
global $amp_conf; |
|---|
| 9 |
global $astman; |
|---|
| 10 |
|
|---|
| 11 |
$sql = " |
|---|
| 12 |
CREATE TABLE IF NOT EXISTS `findmefollow` |
|---|
| 13 |
( |
|---|
| 14 |
`grpnum` VARCHAR( 20 ) NOT NULL , |
|---|
| 15 |
`strategy` VARCHAR( 50 ) NOT NULL , |
|---|
| 16 |
`grptime` SMALLINT NOT NULL , |
|---|
| 17 |
`grppre` VARCHAR( 100 ) NULL , |
|---|
| 18 |
`grplist` VARCHAR( 255 ) NOT NULL , |
|---|
| 19 |
`annmsg_id` INTEGER, |
|---|
| 20 |
`postdest` VARCHAR( 255 ) NULL , |
|---|
| 21 |
`dring` VARCHAR ( 255 ) NULL , |
|---|
| 22 |
`remotealert_id` INTEGER, |
|---|
| 23 |
`needsconf` VARCHAR ( 10 ), |
|---|
| 24 |
`toolate_id` INTEGER, |
|---|
| 25 |
`pre_ring` SMALLINT NOT NULL DEFAULT 0, |
|---|
| 26 |
PRIMARY KEY (`grpnum`) |
|---|
| 27 |
) |
|---|
| 28 |
"; |
|---|
| 29 |
$check = $db->query($sql); |
|---|
| 30 |
if(DB::IsError($check)) { |
|---|
| 31 |
die_freepbx("Can not create annoucment table"); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
//TODO: Also need to create all the states if enabled |
|---|
| 35 |
|
|---|
| 36 |
$fcc = new featurecode('findmefollow', 'fmf_toggle'); |
|---|
| 37 |
$fcc->setDescription('Findme Follow Toggle'); |
|---|
| 38 |
$fcc->setDefault('*21'); |
|---|
| 39 |
$fcc->update(); |
|---|
| 40 |
unset($fcc); |
|---|
| 41 |
|
|---|
| 42 |
// Adding support for a pre_ring before follow-me group |
|---|
| 43 |
$sql = "SELECT pre_ring FROM findmefollow"; |
|---|
| 44 |
$check = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 45 |
if(DB::IsError($check)) { |
|---|
| 46 |
// add new field |
|---|
| 47 |
$sql = "ALTER TABLE findmefollow ADD pre_ring SMALLINT( 6 ) NOT NULL DEFAULT 0 ;"; |
|---|
| 48 |
$result = $db->query($sql); |
|---|
| 49 |
if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); } |
|---|
| 50 |
} |
|---|
| 51 |
// If there is no needsconf then this is a really old upgrade. We create the 2 old fields |
|---|
| 52 |
// here and then the migration code below will change them as needed but will work properly |
|---|
| 53 |
// since it now has the fields it is expecting |
|---|
| 54 |
// |
|---|
| 55 |
$sql = "SELECT needsconf FROM findmefollow"; |
|---|
| 56 |
$check = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 57 |
if(DB::IsError($check)) { |
|---|
| 58 |
// add new field |
|---|
| 59 |
$sql = "ALTER TABLE findmefollow ADD remotealert VARCHAR( 80 ) NULL ;"; |
|---|
| 60 |
$result = $db->query($sql); |
|---|
| 61 |
if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); } |
|---|
| 62 |
|
|---|
| 63 |
$sql = "ALTER TABLE findmefollow ADD needsconf VARCHAR( 10 ) NULL ;"; |
|---|
| 64 |
$result = $db->query($sql); |
|---|
| 65 |
if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); } |
|---|
| 66 |
|
|---|
| 67 |
$sql = "ALTER TABLE findmefollow ADD toolate VARCHAR( 80 ) NULL ;"; |
|---|
| 68 |
$result = $db->query($sql); |
|---|
| 69 |
if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); } |
|---|
| 70 |
} |
|---|
| 71 |
// Version 2.1 upgrade. Add support for ${DIALOPTS} override, playing MOH |
|---|
| 72 |
$sql = "SELECT ringing FROM findmefollow"; |
|---|
| 73 |
$check = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 74 |
if(DB::IsError($check)) { |
|---|
| 75 |
// add new field |
|---|
| 76 |
$sql = "ALTER TABLE findmefollow ADD ringing VARCHAR( 80 ) NULL ;"; |
|---|
| 77 |
$result = $db->query($sql); |
|---|
| 78 |
if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); } |
|---|
| 79 |
} |
|---|
| 80 |
// increase size for older installs, ignore sqlite3, doesn't support ALTER...CHANGE syntax, table created properly above |
|---|
| 81 |
if($amp_conf["AMPDBENGINE"] != "sqlite3") { |
|---|
| 82 |
$db->query("ALTER TABLE findmefollow CHANGE dring dring VARCHAR( 255 ) NULL"); |
|---|
| 83 |
} |
|---|
| 84 |
$results = array(); |
|---|
| 85 |
$sql = "SELECT grpnum, postdest FROM findmefollow"; |
|---|
| 86 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 87 |
if (!DB::IsError($results)) { // error - table must not be there |
|---|
| 88 |
foreach ($results as $result) { |
|---|
| 89 |
$old_dest = $result['postdest']; |
|---|
| 90 |
$grpnum = $result['grpnum']; |
|---|
| 91 |
|
|---|
| 92 |
$new_dest = merge_ext_followme(trim($old_dest)); |
|---|
| 93 |
if ($new_dest != $old_dest) { |
|---|
| 94 |
$sql = "UPDATE findmefollow SET postdest = '$new_dest' WHERE grpnum = '$grpnum' AND postdest = '$old_dest'"; |
|---|
| 95 |
$results = $db->query($sql); |
|---|
| 96 |
if(DB::IsError($results)) { |
|---|
| 97 |
die_freepbx($results->getMessage()); |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
// this function builds the AMPUSER/<grpnum>/followme tree for each user who has a group number |
|---|
| 104 |
// it's purpose is to convert after an upgrade |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
// TODO, is this needed...? |
|---|
| 108 |
// is this global...? what if we include this files |
|---|
| 109 |
// from a function...? |
|---|
| 110 |
|
|---|
| 111 |
$sql = "SELECT * FROM findmefollow"; |
|---|
| 112 |
$userresults = sql($sql,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| 113 |
|
|---|
| 114 |
//add details to astdb |
|---|
| 115 |
if ($astman) { |
|---|
| 116 |
foreach($userresults as $usr) { |
|---|
| 117 |
extract($usr); |
|---|
| 118 |
|
|---|
| 119 |
$astman->database_put("AMPUSER",$grpnum."/followme/prering",isset($pre_ring)?$pre_ring:''); |
|---|
| 120 |
$astman->database_put("AMPUSER",$grpnum."/followme/grptime",isset($grptime)?$grptime:''); |
|---|
| 121 |
$astman->database_put("AMPUSER",$grpnum."/followme/grplist",isset($grplist)?$grplist:''); |
|---|
| 122 |
$confvalue = ($needsconf == 'CHECKED')?'ENABLED':'DISABLED'; |
|---|
| 123 |
$astman->database_put("AMPUSER",$grpnum."/followme/grpconf",isset($needsconf)?$confvalue:''); |
|---|
| 124 |
$ddial = $astman->database_get("AMPUSER",$grpnum."/followme/ddial"); |
|---|
| 125 |
$ddial = ($ddial == 'EXTENSION' || $ddial == 'DIRECT')?$ddial:'DIRECT'; |
|---|
| 126 |
$astman->database_put("AMPUSER",$grpnum."/followme/ddial",$ddial); |
|---|
| 127 |
} |
|---|
| 128 |
} else { |
|---|
| 129 |
echo _("Cannot connect to Asterisk Manager with ").$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"]; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
// Version 2.4.13 change (#1961) |
|---|
| 133 |
// Ignore sqlite3, doesn't support ALTER...CHANGE syntax, table created properly above |
|---|
| 134 |
if($amp_conf["AMPDBENGINE"] != "sqlite3") { |
|---|
| 135 |
$sql = "ALTER TABLE `findmefollow` CHANGE `grpnum` `grpnum` VARCHAR( 20 ) NOT NULL"; |
|---|
| 136 |
$results = $db->query($sql); |
|---|
| 137 |
if(DB::IsError($results)) { |
|---|
| 138 |
echo $results->getMessage(); |
|---|
| 139 |
return false; |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
// Version 2.5 migrate to recording ids |
|---|
| 144 |
// |
|---|
| 145 |
// Do not do upgrades for sqlite3. Assume full support begins in 2.5 and our CREATE syntax is correct |
|---|
| 146 |
if($amp_conf["AMPDBENGINE"] != "sqlite3") { |
|---|
| 147 |
outn(_("Checking if recordings need migration..")); |
|---|
| 148 |
$sql = "SELECT annmsg_id FROM findmefollow"; |
|---|
| 149 |
$check = $db->getRow($sql, DB_FETCHMODE_ASSOC); |
|---|
| 150 |
if(DB::IsError($check)) { |
|---|
| 151 |
// Add recording_id field |
|---|
| 152 |
// |
|---|
| 153 |
out(_("migrating")); |
|---|
| 154 |
outn(_("adding annmsg_id field..")); |
|---|
| 155 |
$sql = "ALTER TABLE findmefollow ADD annmsg_id INTEGER"; |
|---|
| 156 |
$result = $db->query($sql); |
|---|
| 157 |
if(DB::IsError($result)) { |
|---|
| 158 |
out(_("fatal error")); |
|---|
| 159 |
die_freepbx($result->getDebugInfo()); |
|---|
| 160 |
} else { |
|---|
| 161 |
out(_("ok")); |
|---|
| 162 |
} |
|---|
| 163 |
outn(_("adding remotealert_id field..")); |
|---|
| 164 |
$sql = "ALTER TABLE findmefollow ADD remotealert_id INTEGER"; |
|---|
| 165 |
$result = $db->query($sql); |
|---|
| 166 |
if(DB::IsError($result)) { |
|---|
| 167 |
out(_("fatal error")); |
|---|
| 168 |
die_freepbx($result->getDebugInfo()); |
|---|
| 169 |
} else { |
|---|
| 170 |
out(_("ok")); |
|---|
| 171 |
} |
|---|
| 172 |
outn(_("adding toolate_id field..")); |
|---|
| 173 |
$sql = "ALTER TABLE findmefollow ADD toolate_id INTEGER"; |
|---|
| 174 |
$result = $db->query($sql); |
|---|
| 175 |
if(DB::IsError($result)) { |
|---|
| 176 |
out(_("fatal error")); |
|---|
| 177 |
die_freepbx($result->getDebugInfo()); |
|---|
| 178 |
} else { |
|---|
| 179 |
out(_("ok")); |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
// Get all the valudes and replace them with recording_id |
|---|
| 183 |
// |
|---|
| 184 |
outn(_("migrate annmsg to ids..")); |
|---|
| 185 |
$sql = "SELECT `grpnum`, `annmsg` FROM `findmefollow`"; |
|---|
| 186 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 187 |
if(DB::IsError($results)) { |
|---|
| 188 |
out(_("fatal error")); |
|---|
| 189 |
die_freepbx($results->getDebugInfo()); |
|---|
| 190 |
} |
|---|
| 191 |
$migrate_arr = array(); |
|---|
| 192 |
$count = 0; |
|---|
| 193 |
foreach ($results as $row) { |
|---|
| 194 |
if (trim($row['annmsg']) != '') { |
|---|
| 195 |
$rec_id = recordings_get_or_create_id($row['annmsg'], 'findmefollow'); |
|---|
| 196 |
$migrate_arr[] = array($rec_id, $row['grpnum']); |
|---|
| 197 |
$count++; |
|---|
| 198 |
} |
|---|
| 199 |
} |
|---|
| 200 |
if ($count) { |
|---|
| 201 |
$compiled = $db->prepare('UPDATE `findmefollow` SET `annmsg_id` = ? WHERE `grpnum` = ?'); |
|---|
| 202 |
$result = $db->executeMultiple($compiled,$migrate_arr); |
|---|
| 203 |
if(DB::IsError($result)) { |
|---|
| 204 |
out(_("fatal error")); |
|---|
| 205 |
die_freepbx($result->getDebugInfo()); |
|---|
| 206 |
} |
|---|
| 207 |
} |
|---|
| 208 |
out(sprintf(_("migrated %s entries"),$count)); |
|---|
| 209 |
|
|---|
| 210 |
outn(_("migrate remotealert to ids..")); |
|---|
| 211 |
$sql = "SELECT `grpnum`, `remotealert` FROM `findmefollow`"; |
|---|
| 212 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 213 |
if(DB::IsError($results)) { |
|---|
| 214 |
out(_("fatal error")); |
|---|
| 215 |
die_freepbx($results->getDebugInfo()); |
|---|
| 216 |
} |
|---|
| 217 |
$migrate_arr = array(); |
|---|
| 218 |
$count = 0; |
|---|
| 219 |
foreach ($results as $row) { |
|---|
| 220 |
if (trim($row['remotealert']) != '') { |
|---|
| 221 |
$rec_id = recordings_get_or_create_id($row['remotealert'], 'findmefollow'); |
|---|
| 222 |
$migrate_arr[] = array($rec_id, $row['grpnum']); |
|---|
| 223 |
$count++; |
|---|
| 224 |
} |
|---|
| 225 |
} |
|---|
| 226 |
if ($count) { |
|---|
| 227 |
$compiled = $db->prepare('UPDATE `findmefollow` SET `remotealert_id` = ? WHERE `grpnum` = ?'); |
|---|
| 228 |
$result = $db->executeMultiple($compiled,$migrate_arr); |
|---|
| 229 |
if(DB::IsError($result)) { |
|---|
| 230 |
out(_("fatal error")); |
|---|
| 231 |
die_freepbx($result->getDebugInfo()); |
|---|
| 232 |
} |
|---|
| 233 |
} |
|---|
| 234 |
out(sprintf(_("migrated %s entries"),$count)); |
|---|
| 235 |
|
|---|
| 236 |
outn(_("migrate toolate to ids..")); |
|---|
| 237 |
$sql = "SELECT `grpnum`, `toolate` FROM `findmefollow`"; |
|---|
| 238 |
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 239 |
if(DB::IsError($results)) { |
|---|
| 240 |
out(_("fatal error")); |
|---|
| 241 |
die_freepbx($results->getDebugInfo()); |
|---|
| 242 |
} |
|---|
| 243 |
$migrate_arr = array(); |
|---|
| 244 |
$count = 0; |
|---|
| 245 |
foreach ($results as $row) { |
|---|
| 246 |
if (trim($row['toolate']) != '') { |
|---|
| 247 |
$rec_id = recordings_get_or_create_id($row['toolate'], 'findmefollow'); |
|---|
| 248 |
$migrate_arr[] = array($rec_id, $row['grpnum']); |
|---|
| 249 |
$count++; |
|---|
| 250 |
} |
|---|
| 251 |
} |
|---|
| 252 |
if ($count) { |
|---|
| 253 |
$compiled = $db->prepare('UPDATE `findmefollow` SET `toolate_id` = ? WHERE `grpnum` = ?'); |
|---|
| 254 |
$result = $db->executeMultiple($compiled,$migrate_arr); |
|---|
| 255 |
if(DB::IsError($result)) { |
|---|
| 256 |
out(_("fatal error")); |
|---|
| 257 |
die_freepbx($result->getDebugInfo()); |
|---|
| 258 |
} |
|---|
| 259 |
} |
|---|
| 260 |
out(sprintf(_("migrated %s entries"),$count)); |
|---|
| 261 |
|
|---|
| 262 |
// Now remove the old recording field replaced by new id field |
|---|
| 263 |
// |
|---|
| 264 |
outn(_("dropping annmsg field..")); |
|---|
| 265 |
$sql = "ALTER TABLE `findmefollow` DROP `annmsg`"; |
|---|
| 266 |
$result = $db->query($sql); |
|---|
| 267 |
if(DB::IsError($result)) { |
|---|
| 268 |
out(_("no annmsg field???")); |
|---|
| 269 |
} else { |
|---|
| 270 |
out(_("ok")); |
|---|
| 271 |
} |
|---|
| 272 |
outn(_("dropping remotealert field..")); |
|---|
| 273 |
$sql = "ALTER TABLE `findmefollow` DROP `remotealert`"; |
|---|
| 274 |
$result = $db->query($sql); |
|---|
| 275 |
if(DB::IsError($result)) { |
|---|
| 276 |
out(_("no remotealert field???")); |
|---|
| 277 |
} else { |
|---|
| 278 |
out(_("ok")); |
|---|
| 279 |
} |
|---|
| 280 |
outn(_("dropping toolate field..")); |
|---|
| 281 |
$sql = "ALTER TABLE `findmefollow` DROP `toolate`"; |
|---|
| 282 |
$result = $db->query($sql); |
|---|
| 283 |
if(DB::IsError($result)) { |
|---|
| 284 |
out(_("no toolate field???")); |
|---|
| 285 |
} else { |
|---|
| 286 |
out(_("ok")); |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
} else { |
|---|
| 290 |
out(_("already migrated")); |
|---|
| 291 |
} |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
?> |
|---|