| 1 |
<?php |
|---|
| 2 |
/* $Id: functions.inc.php 4024 2007-06-09 03:09:16Z p_lindheimer $ */ |
|---|
| 3 |
|
|---|
| 4 |
// Class To Create, Access and Change DAYNIGHT objects in the dialplan |
|---|
| 5 |
// |
|---|
| 6 |
class dayNightObject { |
|---|
| 7 |
|
|---|
| 8 |
var $id; |
|---|
| 9 |
|
|---|
| 10 |
// contstructor |
|---|
| 11 |
function dayNightObject($item) { |
|---|
| 12 |
$this->id = $item; |
|---|
| 13 |
} |
|---|
| 14 |
|
|---|
| 15 |
function getState() { |
|---|
| 16 |
global $astman; |
|---|
| 17 |
|
|---|
| 18 |
if ($astman != null) { |
|---|
| 19 |
$mode = $astman->database_get("DAYNIGHT","C".$this->id); |
|---|
| 20 |
if ($mode != "DAY" && $mode != "NIGHT") { |
|---|
| 21 |
// TODO: should this return an error? |
|---|
| 22 |
return false; |
|---|
| 23 |
} else { |
|---|
| 24 |
return $mode; |
|---|
| 25 |
} |
|---|
| 26 |
} else { |
|---|
| 27 |
die_freepbx("No open connection to asterisk manager, can not access object."); |
|---|
| 28 |
} |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
function setState($state) { |
|---|
| 32 |
global $astman; |
|---|
| 33 |
|
|---|
| 34 |
if ($this->getState() === false) { |
|---|
| 35 |
die_freepbx("You must create the object before setting the state."); |
|---|
| 36 |
return false; |
|---|
| 37 |
} else { |
|---|
| 38 |
switch ($state) { |
|---|
| 39 |
case "DAY": |
|---|
| 40 |
case "NIGHT": |
|---|
| 41 |
if ($astman != null) { |
|---|
| 42 |
$astman->database_put("DAYNIGHT","C".$this->id,$state); |
|---|
| 43 |
} else { |
|---|
| 44 |
die_freepbx("No open connection to asterisk manager, can not access object."); |
|---|
| 45 |
} |
|---|
| 46 |
break; |
|---|
| 47 |
default: |
|---|
| 48 |
die_freepbx("Invalid state: $state"); |
|---|
| 49 |
break; |
|---|
| 50 |
} |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
function create($state="DAY") { |
|---|
| 55 |
global $astman; |
|---|
| 56 |
|
|---|
| 57 |
$current_state = $this->getState(); |
|---|
| 58 |
if ($current_state !== false) { |
|---|
| 59 |
die_freepbx("Object already exists and is in state: $current_state, you must delete it first"); |
|---|
| 60 |
return false; |
|---|
| 61 |
} else { |
|---|
| 62 |
switch ($state) { |
|---|
| 63 |
case "DAY": |
|---|
| 64 |
case "NIGHT": |
|---|
| 65 |
if ($astman != null) { |
|---|
| 66 |
$astman->database_put("DAYNIGHT","C".$this->id,$state); |
|---|
| 67 |
} else { |
|---|
| 68 |
die_freepbx("No open connection to asterisk manager, can not access object."); |
|---|
| 69 |
} |
|---|
| 70 |
break; |
|---|
| 71 |
default: |
|---|
| 72 |
die_freepbx("Invalid state: $state"); |
|---|
| 73 |
break; |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
function del() { |
|---|
| 79 |
global $astman; |
|---|
| 80 |
|
|---|
| 81 |
if ($astman != null) { |
|---|
| 82 |
$astman->database_del("DAYNIGHT","C".$this->id); |
|---|
| 83 |
} else { |
|---|
| 84 |
die_freepbx("No open connection to asterisk manager, can not access object."); |
|---|
| 85 |
} |
|---|
| 86 |
} |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
// The destinations this module provides |
|---|
| 90 |
// returns a associative arrays with keys 'destination' and 'description' |
|---|
| 91 |
function daynight_destinations() { |
|---|
| 92 |
|
|---|
| 93 |
$list = daynight_list(); |
|---|
| 94 |
foreach ($list as $item) { |
|---|
| 95 |
$dests = daynight_get_obj($item['ext']); |
|---|
| 96 |
if (!isset($dests['day']) || !isset($dests['night'])) { |
|---|
| 97 |
continue; |
|---|
| 98 |
} |
|---|
| 99 |
$description = $item['dest'] != ""?$item['dest']:"Day/Night Switch"; |
|---|
| 100 |
$description = "(".$item['ext'].") ".$description; |
|---|
| 101 |
$extens[] = array('destination' => 'app-daynight,'.$item['ext'].',1', 'description' => $description); |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
// return an associative array with destination and description |
|---|
| 105 |
if (isset($extens)) |
|---|
| 106 |
return $extens; |
|---|
| 107 |
else |
|---|
| 108 |
return null; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
function daynight_getdest($exten) { |
|---|
| 112 |
return array('app-daynight,'.$exten.',1'); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
function daynight_getdestinfo($dest) { |
|---|
| 116 |
global $active_modules; |
|---|
| 117 |
|
|---|
| 118 |
if (substr(trim($dest),0,13) == 'app-daynight,') { |
|---|
| 119 |
$exten = explode(',',$dest); |
|---|
| 120 |
$exten = $exten[1]; |
|---|
| 121 |
|
|---|
| 122 |
$thisexten = array(); |
|---|
| 123 |
$thislist = daynight_list($exten); |
|---|
| 124 |
foreach ($thislist as $item) { |
|---|
| 125 |
if ($item['ext'] == $exten) { |
|---|
| 126 |
$thisexten = $item; |
|---|
| 127 |
break; |
|---|
| 128 |
} |
|---|
| 129 |
} |
|---|
| 130 |
if (empty($thisexten)) { |
|---|
| 131 |
return array(); |
|---|
| 132 |
} else { |
|---|
| 133 |
//$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup'; |
|---|
| 134 |
return array('description' => 'Day/Night ('.$exten.') : '.$thisexten['dest'], |
|---|
| 135 |
'edit_url' => 'config.php?display=daynight&itemid='.urlencode($exten).'&action=edit', |
|---|
| 136 |
); |
|---|
| 137 |
} |
|---|
| 138 |
} else { |
|---|
| 139 |
return false; |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
function daynight_get_config($engine) { |
|---|
| 144 |
global $ext; |
|---|
| 145 |
|
|---|
| 146 |
switch($engine) { |
|---|
| 147 |
case "asterisk": |
|---|
| 148 |
|
|---|
| 149 |
$id = "app-daynight"; // The context to be included |
|---|
| 150 |
$ext->addInclude('from-internal-additional', $id); // Add the include from from-internal |
|---|
| 151 |
|
|---|
| 152 |
$list = daynight_list(); |
|---|
| 153 |
|
|---|
| 154 |
foreach ($list as $item) { |
|---|
| 155 |
$dests = daynight_get_obj($item['ext']); |
|---|
| 156 |
$ext->add($id, $item['ext'], '', new ext_gotoif('$["${DB(DAYNIGHT/C${EXTEN})}" = "NIGHT"]',$dests['night'],$dests['day'])); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
daynight_toggle(); |
|---|
| 160 |
|
|---|
| 161 |
break; |
|---|
| 162 |
} |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
function daynight_toggle() { |
|---|
| 166 |
global $ext; |
|---|
| 167 |
|
|---|
| 168 |
$fcc = new featurecode('daynight', 'toggle-mode'); |
|---|
| 169 |
$c = $fcc->getCodeActive(); |
|---|
| 170 |
unset($fcc); |
|---|
| 171 |
|
|---|
| 172 |
if (!empty($c)) { |
|---|
| 173 |
$id = "app-daynight-toggle"; // The context to be included |
|---|
| 174 |
|
|---|
| 175 |
$ext->addInclude('from-internal-additional', $id); // Add the include from from-internal |
|---|
| 176 |
|
|---|
| 177 |
$list = daynight_list(); |
|---|
| 178 |
$passwords = daynight_passwords(); |
|---|
| 179 |
foreach ($list as $item) { |
|---|
| 180 |
$index = $item['ext']; |
|---|
| 181 |
$ext->add($id, $c.$index, '', new ext_answer('')); |
|---|
| 182 |
$ext->add($id, $c.$index, '', new ext_wait('1')); |
|---|
| 183 |
|
|---|
| 184 |
if (isset($passwords[$index]) && trim($passwords[$index]) != "" && ctype_digit(trim($passwords[$index]))) { |
|---|
| 185 |
$ext->add($id, $c.$index, '', new ext_authenticate($passwords[$index])); |
|---|
| 186 |
} |
|---|
| 187 |
$ext->add($id, $c.$index, '', new ext_setvar('INDEX', $index)); |
|---|
| 188 |
$ext->add($id, $c.$index, '', new ext_goto($id.',s,1')); |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
$c='s'; |
|---|
| 192 |
$ext->add($id, $c, '', new ext_setvar('DAYNIGHTMODE', '${DB(DAYNIGHT/C${INDEX})}')); |
|---|
| 193 |
$ext->add($id, $c, '', new ext_gotoif('$["${DAYNIGHTMODE}" = "NIGHT"]', 'day', 'night')); |
|---|
| 194 |
|
|---|
| 195 |
$ext->add($id, $c, 'day', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'DAY')); |
|---|
| 196 |
$ext->add($id, $c, '', new ext_playback('beep&silence/1&day&reception&digits/${INDEX}&enabled')); |
|---|
| 197 |
$ext->add($id, $c, '', new ext_hangup('')); |
|---|
| 198 |
|
|---|
| 199 |
$ext->add($id, $c, 'night', new ext_setvar('DB(DAYNIGHT/C${INDEX})', 'NIGHT')); |
|---|
| 200 |
$ext->add($id, $c, '', new ext_playback('beep&silence/1&beep&silence/1&day&reception&digits/${INDEX}&disabled')); |
|---|
| 201 |
$ext->add($id, $c, '', new ext_hangup('')); |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
function daynight_get_avail() { |
|---|
| 206 |
global $db; |
|---|
| 207 |
|
|---|
| 208 |
$sql = "SELECT ext FROM daynight ORDER BY ext"; |
|---|
| 209 |
$results = $db->getCol($sql); |
|---|
| 210 |
if(DB::IsError($results)) { |
|---|
| 211 |
$results = array(); |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
for ($i=0;$i<=9;$i++) { |
|---|
| 215 |
if (!in_array($i,$results)) { |
|---|
| 216 |
$list[]=$i; |
|---|
| 217 |
} |
|---|
| 218 |
} |
|---|
| 219 |
return $list; |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
//get the existing daynight codes |
|---|
| 223 |
function daynight_list() { |
|---|
| 224 |
$results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'fc_description' ORDER BY ext","getAll",DB_FETCHMODE_ASSOC); |
|---|
| 225 |
if(is_array($results)){ |
|---|
| 226 |
foreach($results as $result){ |
|---|
| 227 |
$list[] = $result; |
|---|
| 228 |
} |
|---|
| 229 |
} |
|---|
| 230 |
if (isset($list)) { |
|---|
| 231 |
return $list; |
|---|
| 232 |
} else { |
|---|
| 233 |
return array(); |
|---|
| 234 |
} |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
//get the existing password codes |
|---|
| 238 |
function daynight_passwords() { |
|---|
| 239 |
$results = sql("SELECT ext, dest FROM daynight WHERE dmode = 'password'","getAll",DB_FETCHMODE_ASSOC); |
|---|
| 240 |
if(is_array($results)){ |
|---|
| 241 |
foreach($results as $result){ |
|---|
| 242 |
$list[$result['ext']] = $result['dest']; |
|---|
| 243 |
} |
|---|
| 244 |
} |
|---|
| 245 |
if (isset($list)) { |
|---|
| 246 |
return $list; |
|---|
| 247 |
} else { |
|---|
| 248 |
return array(); |
|---|
| 249 |
} |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
function daynight_edit($post, $id=0) { |
|---|
| 253 |
|
|---|
| 254 |
// TODO: Probably have separate add and edit (and change in page.daynight.php also) |
|---|
| 255 |
// Need to set the day/night mode in the system if new |
|---|
| 256 |
|
|---|
| 257 |
// Delete all the old dests |
|---|
| 258 |
sql("DELETE FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id'"); |
|---|
| 259 |
|
|---|
| 260 |
$day = isset($post[$post['goto0'].'0'])?$post[$post['goto0'].'0']:''; |
|---|
| 261 |
$night = isset($post[$post['goto1'].'1'])?$post[$post['goto1'].'1']:''; |
|---|
| 262 |
|
|---|
| 263 |
sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'day', '$day')"); |
|---|
| 264 |
sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'night', '$night')"); |
|---|
| 265 |
|
|---|
| 266 |
if (isset($post['password']) && trim($post['password'] != "")) { |
|---|
| 267 |
$password = trim($post['password']); |
|---|
| 268 |
sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'password', '$password')"); |
|---|
| 269 |
} |
|---|
| 270 |
$fc_description = isset($post['fc_description']) ? trim($post['fc_description']) : ""; |
|---|
| 271 |
sql("INSERT INTO daynight (ext, dmode, dest) VALUES ('$id', 'fc_description', '$fc_description')"); |
|---|
| 272 |
|
|---|
| 273 |
$dn = new dayNightObject($id); |
|---|
| 274 |
$dn->del(); |
|---|
| 275 |
$dn->create($post['state']); |
|---|
| 276 |
|
|---|
| 277 |
needreload(); |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
function daynight_del($id){ |
|---|
| 281 |
|
|---|
| 282 |
// TODO: delete ASTDB entry when deleting the mode |
|---|
| 283 |
// |
|---|
| 284 |
$results = sql("DELETE FROM daynight WHERE ext = \"$id\"","query"); |
|---|
| 285 |
|
|---|
| 286 |
$dn = new dayNightObject($id); |
|---|
| 287 |
$dn->del(); |
|---|
| 288 |
unset($dn); |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
function daynight_get_obj($id=0) { |
|---|
| 292 |
global $db; |
|---|
| 293 |
|
|---|
| 294 |
$sql = "SELECT dmode, dest FROM daynight WHERE dmode IN ('day', 'night', 'password', 'fc_description') AND ext = '$id' ORDER BY dmode"; |
|---|
| 295 |
$res = $db->getAll($sql, DB_FETCHMODE_ASSOC); |
|---|
| 296 |
if(DB::IsError($res)) { |
|---|
| 297 |
return null; |
|---|
| 298 |
} |
|---|
| 299 |
foreach($res as $pair) { |
|---|
| 300 |
$dmodes[$pair['dmode']] = $pair['dest']; |
|---|
| 301 |
} |
|---|
| 302 |
$dn = new dayNightObject($id); |
|---|
| 303 |
$dmodes['state'] = $dn->getState(); |
|---|
| 304 |
|
|---|
| 305 |
return $dmodes; |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
/* |
|---|
| 310 |
SELECT s1.ext ext, dest, dmode, s2.description descirption FROM daynight s1 |
|---|
| 311 |
INNER JOIN |
|---|
| 312 |
( |
|---|
| 313 |
SELECT ext, dest description FROM daynight WHERE dmode = 'fc_description') s2 |
|---|
| 314 |
ON s1.ext = s2.ext WHERE dmode in ('day','night') |
|---|
| 315 |
AND dest = '$dest' |
|---|
| 316 |
|
|---|
| 317 |
Provides: ext, dest, dmode, description |
|---|
| 318 |
*/ |
|---|
| 319 |
function daynight_check_destinations($dest=true) { |
|---|
| 320 |
global $active_modules; |
|---|
| 321 |
|
|---|
| 322 |
$destlist = array(); |
|---|
| 323 |
if (is_array($dest) && empty($dest)) { |
|---|
| 324 |
return $destlist; |
|---|
| 325 |
} |
|---|
| 326 |
$sql = " |
|---|
| 327 |
SELECT s1.ext ext, dest, dmode, s2.description description FROM daynight s1 |
|---|
| 328 |
INNER JOIN |
|---|
| 329 |
( |
|---|
| 330 |
SELECT ext, dest description FROM daynight WHERE dmode = 'fc_description') s2 |
|---|
| 331 |
ON s1.ext = s2.ext WHERE dmode in ('day','night') |
|---|
| 332 |
"; |
|---|
| 333 |
if ($dest !== true) { |
|---|
| 334 |
$sql .= "AND dest in ('".implode("','",$dest)."')"; |
|---|
| 335 |
} |
|---|
| 336 |
$results = sql($sql,"getAll",DB_FETCHMODE_ASSOC); |
|---|
| 337 |
|
|---|
| 338 |
//$type = isset($active_modules['announcement']['type'])?$active_modules['announcement']['type']:'setup'; |
|---|
| 339 |
|
|---|
| 340 |
foreach ($results as $result) { |
|---|
| 341 |
$thisdest = $result['dest']; |
|---|
| 342 |
$thisid = $result['ext']; |
|---|
| 343 |
$destlist[] = array( |
|---|
| 344 |
'dest' => $thisdest, |
|---|
| 345 |
'description' => 'Daynight: '.$result['description'].' ('.$result['dmode'].')', |
|---|
| 346 |
'edit_url' => 'config.php?display=daynight&itemid='.urlencode($thisid).'&action=edit', |
|---|
| 347 |
); |
|---|
| 348 |
} |
|---|
| 349 |
return $destlist; |
|---|
| 350 |
} |
|---|
| 351 |
?> |
|---|