From e907af9f5f9cdd3318ca0a79a9ac590454c0ab76 Mon Sep 17 00:00:00 2001 From: Adam Zammit Date: Fri, 22 Nov 2013 16:04:22 +1100 Subject: [PATCH] Convert use of operator table for extensions to extension table --- admin/extensionstatus.php | 14 ++++----- admin/operatorlist.php | 39 ++++++++++++++++++------- admin/operators.php | 50 ++++++++++++++++++++------------ functions/functions.operator.php | 42 ++++++++++++++------------- functions/functions.voip.php | 20 ++++++------- 5 files changed, 98 insertions(+), 67 deletions(-) diff --git a/admin/extensionstatus.php b/admin/extensionstatus.php index d0c7a297..ba2800ea 100644 --- a/admin/extensionstatus.php +++ b/admin/extensionstatus.php @@ -45,24 +45,24 @@ include ("../functions/functions.xhtml.php"); xhtml_head(T_("Display extension status"),true,array("../css/table.css")); -$sql= "SELECT o.firstName, o.extension, CASE o.voip_status WHEN 0 THEN '" . T_("VoIP Offline") . "' ELSE '" . T_("VoIP Online") . "' END as voip_status, CASE ca.state WHEN 0 THEN '" . T_("Not called") . "' WHEN 1 THEN '" . T_("Requesting call") . "' WHEN 2 THEN '" . T_("Ringing") . "' WHEN 3 THEN '" . T_("Answered") . "' WHEN 4 THEN '" . T_("Requires coding") . "' ELSE '" . T_("Done") . "' END as state, CONCAT('' , c.case_id, '') as case_id, SEC_TO_TIME(TIMESTAMPDIFF(SECOND,cal.start,CONVERT_TZ(NOW(),'SYSTEM','UTC'))) as calltime, voip_status as vs - FROM operator as o +$sql= "SELECT CONCAT('',o.firstName,'') as firstName, e.extension, CASE e.status WHEN 0 THEN '" . T_("VoIP Offline") . "' ELSE '" . T_("VoIP Online") . "' END as status, CASE ca.state WHEN 0 THEN '" . T_("Not called") . "' WHEN 1 THEN '" . T_("Requesting call") . "' WHEN 2 THEN '" . T_("Ringing") . "' WHEN 3 THEN '" . T_("Answered") . "' WHEN 4 THEN '" . T_("Requires coding") . "' ELSE '" . T_("Done") . "' END as state, CONCAT('' , c.case_id, '') as case_id, SEC_TO_TIME(TIMESTAMPDIFF(SECOND,cal.start,CONVERT_TZ(NOW(),'SYSTEM','UTC'))) as calltime, e.status as vs + FROM extension as e + LEFT JOIN `operator` as o ON (o.operator_id = e.current_operator_id) LEFT JOIN `case` as c ON (c.current_operator_id = o.operator_id) LEFT JOIN `call_attempt` as cal ON (cal.operator_id = o.operator_id AND cal.end IS NULL and cal.case_id = c.case_id) LEFT JOIN `call` as ca ON (ca.case_id = c.case_id AND ca.operator_id = o.operator_id AND ca.outcome_id= 0 AND ca.call_attempt_id = cal.call_attempt_id) - WHERE o.voip = 1 - ORDER BY o.operator_id ASC"; + ORDER BY e.extension_id ASC"; $rs = $db->GetAll($sql); if (!empty($rs)) { - xhtml_table($rs,array("extension","firstName","voip_status","case_id","state","calltime"),array(T_("Extension"),T_("Operator"),T_("VoIP Status"),T_("Case ID"),T_("Call state"),T_("Time on call")),"tclass",array("vs" => "1")); + xhtml_table($rs,array("extension","firstName","firstName","status","case_id","state","calltime"),array(T_("Extension"),T_("Operator"),T_("Assignment"),T_("VoIP Status"),T_("Case ID"),T_("Call state"),T_("Time on call")),"tclass",array("vs" => "1")); } else - print "

" . T_("No operators") . "

"; - + print "

" . T_("No extensions") . "

"; + xhtml_foot(); ?> diff --git a/admin/operatorlist.php b/admin/operatorlist.php index 8b3c195d..7468b311 100644 --- a/admin/operatorlist.php +++ b/admin/operatorlist.php @@ -67,8 +67,6 @@ if (isset($_POST['submit'])) firstName = " . $db->qstr($_POST['firstName']) . ", chat_user = " . $db->qstr($_POST['chat_user']) . ", chat_password = " . $db->qstr($_POST['chat_password']) . ", - extension = " . $db->qstr($_POST['extension']) . ", - extension_password = " . $db->qstr($_POST['extension_password']) . ", Time_zone_name = " . $db->qstr($_POST['timezone']) . ", voip = $voip, enabled = $enabled, chat_enable = $chat_enable WHERE operator_id = $operator_id"; @@ -76,7 +74,21 @@ if (isset($_POST['submit'])) $rs = $db->Execute($sql); if (!empty($rs)) - { + { + $sql = "UPDATE extension + SET current_operator_id = NULL + WHERE current_operator_id= $operator_id"; + $db->Execute($sql); + + if (!empty($_POST['extension_id'])) + { + $sql = "UPDATE extension + SET current_operator_id = $operator_id + WHERE extension_id = " . intval($_POST['extension_id']); + + $db->Execute($sql); + } + if (HTPASSWD_PATH !== false && !empty($_POST['password'])) { //update password in htaccess @@ -91,7 +103,7 @@ if (isset($_POST['submit'])) } else { - $msg = T_("Failed to update user. Please make sure the username and extension are unique"); + $msg = T_("Failed to update user. Please make sure the username is unique"); } } $_GET['edit'] = $operator_id; @@ -119,6 +131,14 @@ if (isset($_GET['edit'])) echo "

" . T_("Go back") . "

"; if (!empty($msg)) print "

$msg

"; + $sql = "SELECT extension_id as value, extension as description, + CASE WHEN current_operator_id = $operator_id THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM extension + WHERE current_operator_id IS NULL + OR current_operator_id = $operator_id"; + + $ers = $db->GetAll($sql); + ?>
@@ -130,9 +150,8 @@ if (isset($_GET['edit'])) ?>
-
-
-
+
+
value="1" />
@@ -200,9 +219,9 @@ if (isset($_GET['operator_id'])) { $operator_id = intval($_GET['operator_id']); - $sql = "SELECT *,SUBSTRING_INDEX(extension, '/', -1) as ext - FROM operator - WHERE operator_id = $operator_id"; + $sql = "SELECT *,SUBSTRING_INDEX(e.extension, '/', -1) as ext + FROM extension as e + WHERE e.current_operator_id = $operator_id"; $rs = $db->GetRow($sql); diff --git a/admin/operators.php b/admin/operators.php index 40c35d0f..e14bfd96 100644 --- a/admin/operators.php +++ b/admin/operators.php @@ -57,19 +57,12 @@ if (isset($_POST['operator'])) $chat_user = $db->qstr($_POST['chat_user'],get_magic_quotes_gpc()); $chat_password = $db->qstr($_POST['chat_password'],get_magic_quotes_gpc()); $time_zone_name = $db->qstr($_POST['Time_zone_name'],get_magic_quotes_gpc()); - $extension = 1000; - $extensionp = ""; - if (FREEPBX_PATH == false) - { - //Manually add extension information - $extension = $db->qstr($_POST['extension'],get_magic_quotes_gpc()); - $extensionp = $db->qstr($_POST['extensionp'],get_magic_quotes_gpc()); - } - else + $extension = ""; + if (FREEPBX_PATH != false) { //Generate new extension from last one in database and random password $sql = "SELECT SUBSTRING_INDEX(extension, '/', -1) as ext - FROM operator + FROM extension ORDER BY ext DESC LIMIT 1"; @@ -110,17 +103,32 @@ if (isset($_POST['operator'])) if (!empty($_POST['operator'])) { $sql = "INSERT INTO operator - (`operator_id` ,`username` ,`firstName` ,`lastName`, `extension`,`extension_password`, `Time_zone_name`,`voip`,`chat_enable`,`chat_user`,`chat_password`) - VALUES (NULL , $operator, $firstname , $lastname, $extension, $extensionp, $time_zone_name, $voip, $chat, $chat_user, $chat_password);"; + (`operator_id` ,`username` ,`firstName` ,`lastName`, `Time_zone_name`,`voip`,`chat_enable`,`chat_user`,`chat_password`) + VALUES (NULL , $operator, $firstname , $lastname, $time_zone_name, $voip, $chat, $chat_user, $chat_password);"; if ($db->Execute($sql)) - { + { + $oid = $db->Insert_ID(); + if (FREEPBX_PATH !== false) - { + { + //add extension + $sql = "INSERT INTO extension (`extension`,`extension_password`,`current_operator_id`) + VALUES ($extension, $extensionp, $oid)"; + + $db->Execute($sql); + //Generate new extension in freepbx include_once("../functions/functions.freepbx.php"); freepbx_add_extension($extensionn, $_POST["firstname"] . " " . $_POST["lastname"], $extensionnp); - } + } + else if (!empty($_POST['extension_id'])) + { + $sql = "UPDATE extension + SET current_operator_id = $oid + WHERE extension_id = " . intval($_POST['extension_id']); + $db->Execute($sql); + } if (HTPASSWD_PATH !== false && HTGROUP_PATH !== false) { @@ -141,7 +149,6 @@ if (isset($_POST['operator'])) if (FREEPBX_PATH !== false) $a .= "
" . T_("FreePBX has been reloaded for the new VoIP extension to take effect"); - $oid = $db->Insert_ID(); if ($temporary) { @@ -164,7 +171,7 @@ if (isset($_POST['operator'])) } else { - $a = T_("Could not add operator. There may already be an operator of this name:") . " $operator " . T_("Or there may already be an telephone extension number") . ":$extension" ; + $a = T_("Could not add operator. There may already be an operator of this name:") . " $operator "; } @@ -186,6 +193,12 @@ $sql = "SELECT Time_zone_name as value, Time_zone_name as description $rs = $db->GetAll($sql); +$sql = "SELECT extension_id as value, extension as description + FROM extension + WHERE current_operator_id IS NULL"; + +$ers = $db->GetAll($sql); + ?>

.

@@ -200,8 +213,7 @@ $rs = $db->GetAll($sql);

"; display_chooser($rs,"Time_zone_name","Time_zone_name",false,false,false,false,array("value",DEFAULT_TIME_ZONE)); ?>

-

-

+

"; display_chooser($ers,"extension_id","extension_id",true,false,false,false); ?>

:

diff --git a/functions/functions.operator.php b/functions/functions.operator.php index b3448b8d..88b5cdb9 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -130,13 +130,14 @@ function is_voip_enabled($operator_id) global $db; - $sql = "SELECT voip - FROM operator - WHERE operator_id = '$operator_id'"; + $sql = "SELECT o.voip + FROM `operator` as o, `extension` as e + WHERE o.operator_id = '$operator_id' + AND e.current_operator_id = o.operator_id"; $rs = $db->GetRow($sql); - if ($rs['voip'] == '1') + if (isset($rs['voip']) && $rs['voip'] == '1') return true; return false; @@ -617,9 +618,10 @@ function get_case_id($operator_id, $create = false) $db->Execute("SET @row := 0"); $sql = "INSERT INTO contact_phone (case_id,priority,phone,description) - SELECT $case_id as case_id,@row := @row + 1 AS priority,SUBSTRING_INDEX(extension,'/',-1) as phone, CONCAT(firstName, ' ', lastName) - FROM operator - WHERE enabled = 1"; + SELECT $case_id as case_id,@row := @row + 1 AS priority,SUBSTRING_INDEX(o.extension,'/',-1) as phone, CONCAT(o.firstName, ' ', o.lastName) + FROM operator as o, `extension` as e + WHERE o.enabled = 1 + AND e.current_operator_id = o.operator_id"; $db->Execute($sql); } @@ -796,9 +798,9 @@ function set_extension_status($operator_id,$online = true) if ($online) $s = 1; - $sql = "UPDATE `operator` - SET voip_status = '$s' - WHERE operator_id = '$operator_id'"; + $sql = "UPDATE `extension` + SET status = '$s' + WHERE current_operator_id = '$operator_id'"; $db->Execute($sql); } @@ -814,12 +816,12 @@ function get_extension_status($operator_id) { global $db; - $sql = "SELECT o.voip_status - FROM `operator` as o - WHERE o.operator_id = '$operator_id'"; + $sql = "SELECT e.status + FROM `extension` as e + WHERE e.current_operator_id = '$operator_id'"; $rs = $db->GetRow($sql); - if (!empty($rs) && $rs['voip_status'] == 1 ) return true; + if (!empty($rs) && $rs['status'] == 1 ) return true; return false; } @@ -834,9 +836,9 @@ function get_extension_password($operator_id) { global $db; - $sql = "SELECT o.extension_password - FROM `operator` as o - WHERE o.operator_id = '$operator_id'"; + $sql = "SELECT e.extension_password + FROM `extension` as e + WHERE e.current_operator_id = '$operator_id'"; $rs = $db->GetRow($sql); if (!empty($rs) && isset($rs['extension_password'])) return $rs['extension_password']; @@ -854,9 +856,9 @@ function get_extension($operator_id) { global $db; - $sql = "SELECT o.extension - FROM `operator` as o - WHERE o.operator_id = '$operator_id'"; + $sql = "SELECT e.extension + FROM `extension` as e + WHERE e.current_operator_id = '$operator_id'"; $rs = $db->GetRow($sql); if (!empty($rs) && isset($rs['extension'])) return $rs['extension']; diff --git a/functions/functions.voip.php b/functions/functions.voip.php index 029574c6..511cd0a5 100644 --- a/functions/functions.voip.php +++ b/functions/functions.voip.php @@ -417,15 +417,15 @@ class voipWatch extends voip { global $db; $sql = "SELECT l.call_id, c.case_id - FROM operator AS o + FROM `extension` AS e JOIN (`case` AS c, `call_attempt` AS ca, `call` AS l) ON - ( c.current_operator_id = o.operator_id + ( c.current_operator_id = e.current_operator_id AND c.case_id = ca.case_id - AND ca.operator_id = o.operator_id + AND ca.operator_id = e.current_operator_id AND ca.end IS NULL AND l.call_attempt_id = ca.call_attempt_id AND l.outcome_id =0 ) - WHERE o.extension = '$ext'"; + WHERE e.extension = '$ext'"; $rs = $db->GetRow($sql); $call_id =0; @@ -448,8 +448,8 @@ class voipWatch extends voip { if ($msg) print(T_("Extension") . " $ext " . ($online ? T_("online") : T_("offline")) . "\n"); - $sql = "UPDATE operator - SET voip_status = '$s' + $sql = "UPDATE `extension` + SET status = '$s' WHERE extension = '$ext'"; $db->Execute($sql); @@ -470,21 +470,19 @@ class voipWatch extends voip { } /** - * Update the extension status for all extensions + * Update the extension status for all extensions */ function updateAllExtensionStatus() { global $db; - $sql = "SELECT extension,operator_id - FROM operator - WHERE voip = 1 AND enabled = 1"; + $sql = "SELECT e.extension + FROM `extension` as e"; $rs = $db->GetAll($sql); foreach($rs as $r) { - $o = $r['operator_id']; $e = $r['extension']; $s = $this->getExtensionStatus($e);