From 5c69d7345e0739c1cb9f2e4fd42ba5d2d63ef658 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 29 Oct 2019 20:18:51 -0600 Subject: [PATCH] Follow Me, Call Forward, DND: List view updates. --- app/bridges/resources/classes/bridges.php | 2 +- app/calls/calls.php | 375 ++++++++++++------ app/calls/resources/classes/call_forward.php | 131 +++++- .../resources/classes/do_not_disturb.php | 122 +++++- app/calls/resources/classes/follow_me.php | 140 ++++++- 5 files changed, 635 insertions(+), 135 deletions(-) diff --git a/app/bridges/resources/classes/bridges.php b/app/bridges/resources/classes/bridges.php index 85a2d23392..8fd620b4c1 100644 --- a/app/bridges/resources/classes/bridges.php +++ b/app/bridges/resources/classes/bridges.php @@ -69,7 +69,7 @@ if (!class_exists('bridges')) { if (is_array($records) && @sizeof($records) != 0) { //build the delete array - foreach($records as $x => $record) { + foreach ($records as $x => $record) { if ($record['checked'] == 'true' && is_uuid($record['uuid'])) { $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $record['uuid']; $array[$this->table][$x]['domain_uuid'] = $_SESSION['domain_uuid']; diff --git a/app/calls/calls.php b/app/calls/calls.php index e2256f9195..340ab3a51e 100644 --- a/app/calls/calls.php +++ b/app/calls/calls.php @@ -28,6 +28,7 @@ include "root.php"; require_once "resources/require.php"; require_once "resources/check_auth.php"; + require_once "resources/paging.php"; //check permissions if (permission_exists('follow_me') || permission_exists('call_forward') || permission_exists('do_not_disturb')) { @@ -38,28 +39,69 @@ exit; } -//get the domain_uuid from the session - $domain_uuid = $_SESSION['domain_uuid']; - -//handle search term - $search = $_GET["search"]; - if (strlen($search) > 0) { - $sql_mod = "and ( "; - $sql_mod .= "extension like :search "; - $sql_mod .= "or description like :search "; - $sql_mod .= ") "; - } - //add multi-lingual support $language = new text; $text = $language->get($_SESSION['domain']['language']['code'], 'app/calls'); -//begin the content - require_once "resources/header.php"; - require_once "resources/paging.php"; +//get posted data + if (is_array($_POST['extensions'])) { + $action = $_POST['action']; + $search = $_POST['search']; + $extensions = $_POST['extensions']; + } + +//toggle the call forward feature + if (permission_exists('call_forward')) { + if ($action == 'toggle_call_forward' && is_array($extensions) && @sizeof($extensions) != 0) { + //toggle + $obj = new call_forward; + $obj->toggle($extensions); + //redirect + header('Location: calls.php'.($search != '' ? '?search='.urlencode($search) : null)); + exit; + } + } + +//toggle the follow me feature + if (permission_exists('follow_me')) { + if ($action == 'toggle_follow_me' && is_array($extensions) && @sizeof($extensions) != 0) { + //toggle + $obj = new follow_me; + $obj->toggle($extensions); + //redirect + header('Location: calls.php'.($search != '' ? '?search='.urlencode($search) : null)); + exit; + } + } + +//toggle the do not disturb feature + if (permission_exists('do_not_disturb')) { + if ($action == 'toggle_do_not_disturb' && is_array($extensions) && @sizeof($extensions) != 0) { + //toggle + $obj = new do_not_disturb; + $obj->toggle($extensions); + //redirect + header('Location: calls.php'.($search != '' ? '?search='.urlencode($search) : null)); + exit; + } + } + +//get order and order by + $order_by = $_GET["order_by"]; + $order = $_GET["order"]; + +//handle search term + $search = strtolower($_GET["search"]); + if (strlen($search) > 0) { + $sql_search = "and ( "; + $sql_search .= "extension like :search "; + $sql_search .= "or lower(description) like :search "; + $sql_search .= ") "; + $parameters['search'] = '%'.$search.'%'; + } //define select count query - $sql = "select count(extension_uuid) as count from v_extensions "; + $sql = "select count(*) from v_extensions "; $sql .= "where domain_uuid = :domain_uuid "; $sql .= "and enabled = 'true' "; if (!(if_group("admin") || if_group("superadmin"))) { @@ -78,13 +120,12 @@ $sql .= "and extension = 'disabled' "; } } - $sql .= $sql_mod; //add search mod from above + $sql .= $sql_search; $parameters['domain_uuid'] = $_SESSION['domain_uuid']; - if (strlen($search) > 0) { - $parameters['search'] = '%'.$search.'%'; - } $database = new database; - $result_count = $database->select($sql, $parameters, 'column'); + $num_rows = $database->select($sql, $parameters, 'column'); + +//prepare to page the results if ($is_included) { $rows_per_page = 10; } @@ -94,138 +135,218 @@ $param = "&search=".$search; $page = $_GET['page']; if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; } - list($paging_controls_mini, $rows_per_page, $var_3) = paging($result_count, $param, $rows_per_page, true); - list($paging_controls, $rows_per_page, $var_3) = paging($result_count, $param, $rows_per_page); + list($paging_controls, $rows_per_page) = paging($num_rows, $param, $rows_per_page); + list($paging_controls_mini, $rows_per_page) = paging($num_rows, $param, $rows_per_page, true); $offset = $rows_per_page * $page; -//select the extensions - $sql = "select * from v_extensions "; - $sql .= "where domain_uuid = :domain_uuid "; - $sql .= "and enabled = 'true' "; - if (!(if_group("admin") || if_group("superadmin"))) { - if (count($_SESSION['user']['extension']) > 0) { - $sql .= "and ("; - $x = 0; - foreach($_SESSION['user']['extension'] as $row) { - if ($x > 0) { $sql .= "or "; } - $sql .= "extension = '".$row['user']."' "; - $x++; - } - $sql .= ") "; - } - else { - //used to hide any results when a user has not been assigned an extension - $sql .= "and extension = 'disabled' "; - } - } - $sql .= $sql_mod; //add search mod from above - $sql .= "order by extension asc "; +//get the list + $sql = str_replace('count(*)', '*', $sql); + $sql .= order_by($order_by, $order, 'extension', 'asc'); $sql .= limit_offset($rows_per_page, $offset); $database = new database; $extensions = $database->select($sql, $parameters, 'all'); unset($parameters); -//set the row style - $c = 0; - $row_style["0"] = "row_style0"; - $row_style["1"] = "row_style1"; +//create token + $object = new token; + $token = $object->create($_SERVER['PHP_SELF']); -//start the content - echo "\n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo "
"; - echo " ".$text['header-call_routing']."
"; - echo "
\n"; - if ($result_count > 10 && $is_included) { - echo " "; - } - if (!$is_included) { - echo "
\n"; - echo " "; - echo " "; - echo "
\n"; - if ($paging_controls_mini != '') { - echo "".$paging_controls_mini."\n"; +//include header + $document['title'] = $text['title']; + require_once "resources/header.php"; + +//javascript for toggle select box + echo "\n"; + +//show the content + if ($is_included) { + echo "".$text['header-call_routing']."\n"; + if ($num_rows > 10) { + echo "
\n"; + echo button::create(['type'=>'button','label'=>$text['button-view_all'],'link'=>PROJECT_PATH.'/app/calls/calls.php']); + echo "
\n"; } - } echo "
"; - if (!$is_included) { - echo $text['description-call_routing']."
"; } - echo "
\n"; - echo "
"; + else { + echo "
\n"; + echo "
".$text['header-call_routing']." (".$num_rows.")
\n"; + echo "
\n"; + if ($extensions) { + echo button::create(['type'=>'button','label'=>$text['button-toggle'],'icon'=>$_SESSION['theme']['button_icon_toggle'],'onclick'=>"toggle_select(); this.blur();"]); + } + echo ""; + echo "\n"; + echo "
\n"; + echo "
\n"; + echo "
\n"; - echo "\n"; - echo "\n"; - echo "\n"; - if (permission_exists('call_forward')) { echo "\n"; } - if (permission_exists('follow_me')) { echo "\n"; } - if (permission_exists('do_not_disturb')) { echo "\n"; } - echo "\n"; - echo " \n"; + echo $text['description-call_routing']."\n"; + echo "

\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + } + + echo "
".$text['table-extension']."".$text['label-call-forward']."".$text['label-follow-me']."".$text['label-dnd']."".$text['label-description']." 
\n"; + echo "\n"; + if (!$is_included) { + echo " \n"; + } + echo " \n"; + if (permission_exists('call_forward')) { + echo " \n"; + } + if (permission_exists('follow_me')) { + echo " \n"; + } + if (permission_exists('do_not_disturb')) { + echo " \n"; + } + echo " \n"; + if ($_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') { + echo " \n"; + } echo "\n"; if (is_array($extensions)) { + $x = 0; foreach($extensions as $row) { - $tr_url = PROJECT_PATH."/app/calls/call_edit.php?id=".$row['extension_uuid']."&return_url=".urlencode($_SERVER['REQUEST_URI']); - $tr_link = (permission_exists('call_forward') || permission_exists('follow_me') || permission_exists('do_not_disturb')) ? "href='".$tr_url."'" : null; - echo "\n"; - echo " \n"; + $list_row_url = PROJECT_PATH."/app/calls/call_edit.php?id=".$row['extension_uuid']."&return_url=".urlencode($_SERVER['REQUEST_URI']); + echo "\n"; + if (!$is_included && $extensions) { + echo " \n"; + } + echo " \n"; if (permission_exists('call_forward')) { - echo " "; + //-- inline toggle ----------------- + //$button_label = $row['forward_all_enabled'] == 'true' ? ($row['forward_all_destination'] != '' ? escape(format_phone($row['forward_all_destination'])) : '('.$text['label-invalid'].')') : null; + //if (!$is_included) { + // echo " \n"; + //} + //else { + // echo " "; + //} + //unset($button_label); + //---------------------------------- + + echo " \n"; } if (permission_exists('follow_me')) { - if (is_uuid($row['follow_me_uuid'])) { - //get destination count if enabled - $follow_me_destination_count = 0; - if ($row['follow_me_enabled'] == 'true') { - $sql = "select count(follow_me_destination_uuid) as destination_count "; - $sql .= "from v_follow_me_destinations "; - $sql .= "where follow_me_uuid = :follow_me_uuid "; - $sql .= "and domain_uuid = :domain_uuid "; - $parameters['follow_me_uuid'] = $row['follow_me_uuid']; - $parameters['domain_uuid'] = $_SESSION['domain_uuid']; - $database = new database; - $follow_me_destination_count = $database->select($sql, $parameters, 'column'); - } + //-- inline toggle ----------------- + //get destination count + //if ($row['follow_me_enabled'] == 'true' && is_uuid($row['follow_me_uuid'])) { + // $sql = "select count(*) from v_follow_me_destinations "; + // $sql .= "where follow_me_uuid = :follow_me_uuid "; + // $sql .= "and domain_uuid = :domain_uuid "; + // $parameters['follow_me_uuid'] = $row['follow_me_uuid']; + // $parameters['domain_uuid'] = $_SESSION['domain_uuid']; + // $database = new database; + // $follow_me_destination_count = $database->select($sql, $parameters, 'column'); + // $button_label = $follow_me_destination_count ? $text['label-enabled'].' ('.$follow_me_destination_count.')' : $text['label-invalid']; + // unset($sql, $parameters); + //} + //if (!$is_included) { + // echo " \n"; + //} + //else { + // echo " "; + //} + //unset($button_label); + //---------------------------------- + + //get destination count + $follow_me_destination_count = 0; + if ($row['follow_me_enabled'] == 'true' && is_uuid($row['follow_me_uuid'])) { + $sql = "select count(*) from v_follow_me_destinations "; + $sql .= "where follow_me_uuid = :follow_me_uuid "; + $sql .= "and domain_uuid = :domain_uuid "; + $parameters['follow_me_uuid'] = $row['follow_me_uuid']; + $parameters['domain_uuid'] = $_SESSION['domain_uuid']; + $database = new database; + $follow_me_destination_count = $database->select($sql, $parameters, 'column'); + unset($sql, $parameters); } - echo " \n"; + echo " \n"; } if (permission_exists('do_not_disturb')) { - echo " "; + //-- inline toggle ----------------- + //$button_label = $row['do_not_disturb'] == 'true' ? $text['label-enabled'] : null; + //if (!$is_included) { + // echo " \n"; + //} + //else { + // echo " "; + //} + //---------------------------------- + + echo " \n"; + } + echo " \n"; + if ($_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') { + echo " \n"; } - echo " \n"; - - echo " \n"; echo "\n"; - $c = ($c) ? 0 : 1; - } //end foreach - unset($sql, $extensions); - } //end if results - - echo "
\n"; + echo " \n"; + echo " ".$text['label-extension']."".$text['label-call-forward']."".$text['label-follow-me']."".$text['label-dnd']."".$text['label-description']." 
".escape($row['extension'])."
\n"; + echo " \n"; + echo " \n"; + echo " ".escape($row['extension'])."".(($row['forward_all_enabled'] == 'true') ? escape(format_phone($row['forward_all_destination'])) : ' ')."".$button_label."\n"; + echo $row['forward_all_enabled'] == 'true' ? escape(format_phone($row['forward_all_destination'])) : ' '; + echo " ".$button_label."\n"; - if ($row['follow_me_enabled'] == 'true' && $follow_me_destination_count > 0) { - echo ' '.$text['label-enabled']." (".$follow_me_destination_count.")\n"; - } - else { - echo "  \n"; - } - echo "\n"; + echo $follow_me_destination_count ? $text['label-enabled'].' ('.$follow_me_destination_count.')' : ' '; + echo " ".(($row['do_not_disturb'] == 'true') ? $text['label-enabled'] : ' ')."".$button_label."\n"; + echo $row['do_not_disturb'] == 'true' ? $text['label-enabled'] : ' '; + echo " ".escape($row['description'])." "; + echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]); + echo " ".escape($row['description'])." ".$v_link_label_edit."
"; - echo "
"; - - if (strlen($paging_controls) > 0 && (!$is_included)) { - echo "
".$paging_controls."
\n"; - echo "

\n"; + $x++; + } + unset($extensions); } + echo "\n"; + if (!$is_included) { - echo "
"; + echo "
\n"; + echo "
".$paging_controls."
\n"; + + echo "\n"; + + echo "\n"; + require_once "resources/footer.php"; } -?> +?> \ No newline at end of file diff --git a/app/calls/resources/classes/call_forward.php b/app/calls/resources/classes/call_forward.php index 099eaaa8de..ba6402d56a 100644 --- a/app/calls/resources/classes/call_forward.php +++ b/app/calls/resources/classes/call_forward.php @@ -30,6 +30,7 @@ include "root.php"; //define the call_forward class class call_forward { + public $debug; public $domain_uuid; public $domain_name; @@ -87,8 +88,7 @@ include "root.php"; $database->save($array); unset($array); - //grant temporary permissions - $p = new permissions; + //revoke temporary permissions $p->delete('extension_add', 'temp'); //delete extension from the cache @@ -99,6 +99,131 @@ include "root.php"; } } - } + + /** + * declare private variables + */ + private $app_name; + private $app_uuid; + private $permission; + private $list_page; + private $table; + private $uuid_prefix; + private $toggle_field; + private $toggle_values; + + /** + * toggle records + */ + public function toggle($records) { + + //assign private variables + $this->app_name = 'calls'; + $this->app_uuid = '19806921-e8ed-dcff-b325-dd3e5da4959d'; + $this->permission = 'call_forward'; + $this->list_page = 'calls.php'; + $this->table = 'extensions'; + $this->uuid_prefix = 'extension_'; + $this->toggle_field = 'forward_all_enabled'; + $this->toggle_values = ['true','false']; + + if (permission_exists($this->permission)) { + + //add multi-lingual support + $language = new text; + $text = $language->get(); + + //validate the token + $token = new token; + if (!$token->validate($_SERVER['PHP_SELF'])) { + message::add($text['message-invalid_token'],'negative'); + header('Location: '.$this->list_page); + exit; + } + + //toggle the checked records + if (is_array($records) && @sizeof($records) != 0) { + + //get current toggle state + foreach($records as $x => $record) { + if ($record['checked'] == 'true' && is_uuid($record['uuid'])) { + $record_uuids[] = $this->uuid_prefix."uuid = '".$record['uuid']."'"; + } + } + if (is_array($record_uuids) && @sizeof($record_uuids) != 0) { + $sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle, forward_all_destination, follow_me_uuid from v_".$this->table." "; + $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) "; + $sql .= "and ( ".implode(' or ', $record_uuids)." ) "; + $parameters['domain_uuid'] = $_SESSION['domain_uuid']; + $database = new database; + $rows = $database->select($sql, $parameters, 'all'); + if (is_array($rows) && @sizeof($rows) != 0) { + foreach ($rows as $row) { + $extensions[$row['uuid']]['state'] = $row['toggle']; + $extensions[$row['uuid']]['follow_me_uuid'] = $row['follow_me_uuid']; + $extensions[$row['uuid']]['forward_all_destination'] = $row['forward_all_destination']; + } + } + unset($sql, $parameters, $rows, $row); + } + + //build update array + $x = 0; + foreach ($extensions as $uuid => $extension) { + + //check destination + $destination_exists = $extension['forward_all_destination'] != '' ? true : false; + + //determine new state + $new_state = $extension['state'] == $this->toggle_values[1] && $destination_exists ? $this->toggle_values[0] : $this->toggle_values[1]; + + //toggle feature + if ($new_state != $extension['state']) { + $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $uuid; + $array[$this->table][$x][$this->toggle_field] = $new_state; + } + + //disable other features + if ($new_state == $this->toggle_values[0]) { //true + $array[$this->table][$x]['do_not_disturb'] = $this->toggle_values[1]; //false + $array[$this->table][$x]['follow_me_enabled'] = $this->toggle_values[1]; //false + $array['follow_me'][$x]['follow_me_uuid'] = $extension['follow_me_uuid']; + $array['follow_me'][$x]['follow_me_enabled'] = $this->toggle_values[1]; //false + } + + //increment counter + $x++; + + } + + //save the changes + if (is_array($array) && @sizeof($array) != 0) { + + //grant temporary permissions + $p = new permissions; + $p->add('extension_edit', 'temp'); + + //save the array + $database = new database; + $database->app_name = $this->app_name; + $database->app_uuid = $this->app_uuid; + $database->save($array); + unset($array); + + //revoke temporary permissions + $p->delete('extension_edit', 'temp'); + + //set message + message::add($text['message-toggle']); + + } + unset($records, $states); + } + + } + + } //function + + }// class ?> \ No newline at end of file diff --git a/app/calls/resources/classes/do_not_disturb.php b/app/calls/resources/classes/do_not_disturb.php index 82f1507564..2233955372 100644 --- a/app/calls/resources/classes/do_not_disturb.php +++ b/app/calls/resources/classes/do_not_disturb.php @@ -121,7 +121,123 @@ include "root.php"; $cache->delete("directory:".$this->number_alias."@".$this->domain_name); } - } - } + } //function -?> + /** + * declare private variables + */ + private $app_name; + private $app_uuid; + private $permission; + private $list_page; + private $table; + private $uuid_prefix; + private $toggle_field; + private $toggle_values; + + /** + * toggle records + */ + public function toggle($records) { + + //assign private variables + $this->app_name = 'calls'; + $this->app_uuid = '19806921-e8ed-dcff-b325-dd3e5da4959d'; + $this->permission = 'do_not_disturb'; + $this->list_page = 'calls.php'; + $this->table = 'extensions'; + $this->uuid_prefix = 'extension_'; + $this->toggle_field = 'do_not_disturb'; + $this->toggle_values = ['true','false']; + + if (permission_exists($this->permission)) { + + //add multi-lingual support + $language = new text; + $text = $language->get(); + + //validate the token + $token = new token; + if (!$token->validate($_SERVER['PHP_SELF'])) { + message::add($text['message-invalid_token'],'negative'); + header('Location: '.$this->list_page); + exit; + } + + //toggle the checked records + if (is_array($records) && @sizeof($records) != 0) { + + //get current toggle state + foreach($records as $x => $record) { + if ($record['checked'] == 'true' && is_uuid($record['uuid'])) { + $record_uuids[] = $this->uuid_prefix."uuid = '".$record['uuid']."'"; + } + } + if (is_array($record_uuids) && @sizeof($record_uuids) != 0) { + $sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle, follow_me_uuid from v_".$this->table." "; + $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) "; + $sql .= "and ( ".implode(' or ', $record_uuids)." ) "; + $parameters['domain_uuid'] = $_SESSION['domain_uuid']; + $database = new database; + $rows = $database->select($sql, $parameters, 'all'); + if (is_array($rows) && @sizeof($rows) != 0) { + foreach ($rows as $row) { + $extensions[$row['uuid']]['state'] = $row['toggle']; + $extensions[$row['uuid']]['follow_me_uuid'] = $row['follow_me_uuid']; + } + } + unset($sql, $parameters, $rows, $row); + } + + //build update array + $x = 0; + foreach ($extensions as $uuid => $extension) { + + //toggle feature + $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $uuid; + $array[$this->table][$x][$this->toggle_field] = $extension['state'] == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0]; + + //disable other features + if ($array[$this->table][$x][$this->toggle_field] == $this->toggle_values[0]) { //true + $array[$this->table][$x]['forward_all_enabled'] = $this->toggle_values[1]; //false + $array[$this->table][$x]['follow_me_enabled'] = $this->toggle_values[1]; //false + $array['follow_me'][$x]['follow_me_uuid'] = $extension['follow_me_uuid']; + $array['follow_me'][$x]['follow_me_enabled'] = $this->toggle_values[1]; //false + } + + //increment counter + $x++; + + } + + //save the changes + if (is_array($array) && @sizeof($array) != 0) { + + //grant temporary permissions + $p = new permissions; + $p->add('extension_edit', 'temp'); + + //save the array + $database = new database; + $database->app_name = $this->app_name; + $database->app_uuid = $this->app_uuid; + $database->save($array); + unset($array); + + //revoke temporary permissions + $p->delete('extension_edit', 'temp'); + + //set message + message::add($text['message-toggle']); + + } + unset($records, $states); + } + + } + + } //function + + } //class + +?> \ No newline at end of file diff --git a/app/calls/resources/classes/follow_me.php b/app/calls/resources/classes/follow_me.php index 08e2b8d737..0f7a0b890d 100644 --- a/app/calls/resources/classes/follow_me.php +++ b/app/calls/resources/classes/follow_me.php @@ -264,6 +264,144 @@ include "root.php"; $p->delete("extension_edit", 'temp'); } //function + + + /** + * declare private variables + */ + private $app_name; + private $app_uuid; + private $permission; + private $list_page; + private $table; + private $uuid_prefix; + private $toggle_field; + private $toggle_values; + + /** + * toggle records + */ + public function toggle($records) { + + //assign private variables + $this->app_name = 'calls'; + $this->app_uuid = '19806921-e8ed-dcff-b325-dd3e5da4959d'; + $this->permission = 'follow_me'; + $this->list_page = 'calls.php'; + $this->table = 'extensions'; + $this->uuid_prefix = 'extension_'; + $this->toggle_field = 'follow_me_enabled'; + $this->toggle_values = ['true','false']; + + if (permission_exists($this->permission)) { + + //add multi-lingual support + $language = new text; + $text = $language->get(); + + //validate the token + $token = new token; + if (!$token->validate($_SERVER['PHP_SELF'])) { + message::add($text['message-invalid_token'],'negative'); + header('Location: '.$this->list_page); + exit; + } + + //toggle the checked records + if (is_array($records) && @sizeof($records) != 0) { + + //get current toggle state + foreach($records as $x => $record) { + if ($record['checked'] == 'true' && is_uuid($record['uuid'])) { + $record_uuids[] = $this->uuid_prefix."uuid = '".$record['uuid']."'"; + } + } + if (is_array($record_uuids) && @sizeof($record_uuids) != 0) { + $sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle, follow_me_uuid from v_".$this->table." "; + $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) "; + $sql .= "and ( ".implode(' or ', $record_uuids)." ) "; + $parameters['domain_uuid'] = $_SESSION['domain_uuid']; + $database = new database; + $rows = $database->select($sql, $parameters, 'all'); + if (is_array($rows) && @sizeof($rows) != 0) { + foreach ($rows as $row) { + $extensions[$row['uuid']]['state'] = $row['toggle']; + $extensions[$row['uuid']]['follow_me_uuid'] = $row['follow_me_uuid']; + } + } + unset($sql, $parameters, $rows, $row); + } + + //build update array + $x = 0; + foreach ($extensions as $uuid => $extension) { + + //count destinations + $destinations_exist = false; + if ( + $extension['state'] == $this->toggle_values[1] //false becoming true + && is_uuid($extension['follow_me_uuid']) + ) { + $sql .= "select count(*) from v_follow_me_destinations where follow_me_uuid = :follow_me_uuid"; + $parameters['follow_me_uuid'] = $extension['follow_me_uuid']; + $database = new database; + $num_rows = $database->select($sql, $parameters, 'column'); + $destinations_exist = $num_rows ? true : false; + unset($sql, $parameters, $num_rows); + } + + //determine new state + $new_state = $extension['state'] == $this->toggle_values[1] && $destinations_exist ? $this->toggle_values[0] : $this->toggle_values[1]; + + //toggle feature + if ($new_state != $extension['state']) { + $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $uuid; + $array[$this->table][$x][$this->toggle_field] = $new_state; + $array['follow_me'][$x]['follow_me_uuid'] = $extension['follow_me_uuid']; + $array['follow_me'][$x]['follow_me_enabled'] = $new_state; + } + + //disable other features + if ($new_state == $this->toggle_values[0]) { //true + $array[$this->table][$x]['forward_all_enabled'] = $this->toggle_values[1]; //false + $array[$this->table][$x]['do_not_disturb'] = $this->toggle_values[1]; //false + } + + //increment counter + $x++; + + } + + //save the changes + if (is_array($array) && @sizeof($array) != 0) { + + //grant temporary permissions + $p = new permissions; + $p->add('extension_edit', 'temp'); + $p->add('follow_me_edit', 'temp'); + + //save the array + $database = new database; + $database->app_name = $this->app_name; + $database->app_uuid = $this->app_uuid; + $database->save($array); + unset($array); + + //revoke temporary permissions + $p->delete('extension_edit', 'temp'); + $p->delete('follow_me_edit', 'temp'); + + //set message + message::add($text['message-toggle']); + + } + unset($records, $states); + } + + } + + } //function + } //class -?> +?> \ No newline at end of file