From 7f08b92631835864ce5a5c49a4fa34c51212fa37 Mon Sep 17 00:00:00 2001 From: fusionate Date: Mon, 4 Jan 2021 17:41:33 -0700 Subject: [PATCH] Time Conditions: Bug fixes. --- app/time_conditions/time_condition_edit.php | 108 ++++++++++++-------- 1 file changed, 66 insertions(+), 42 deletions(-) diff --git a/app/time_conditions/time_condition_edit.php b/app/time_conditions/time_condition_edit.php index 40ecd31b09..5f8cfa2a21 100644 --- a/app/time_conditions/time_condition_edit.php +++ b/app/time_conditions/time_condition_edit.php @@ -50,6 +50,7 @@ if (is_array($_SESSION['time_conditions'][$preset_region])) { foreach ($_SESSION['time_conditions'][$preset_region] as $json) { $available_presets[] = json_decode($json, true); + $valid_presets[] = array_key_first(end($available_presets)); } } unset($preset_region); @@ -562,11 +563,6 @@ $sql .= " ) "; $sql .= " or ( "; $sql .= " dialplan_detail_tag = 'action' "; - $sql .= " and "; - $sql .= " ( "; - $sql .= " dialplan_detail_data not like 'preset=%' "; - $sql .= " or dialplan_detail_data is null "; - $sql .= " ) "; $sql .= " ) "; $sql .= ") "; $sql .= "order by dialplan_detail_group asc, dialplan_detail_order asc"; @@ -578,46 +574,52 @@ //load current conditions into array (combined by group), and retrieve action and anti-action $c = 0; - if (is_array($dialplan_details)) { - foreach ($dialplan_details as $row) { + if (is_array($dialplan_details) && @sizeof($dialplan_details) != 0) { + //detect dialplan detail group has valid preset + $dialplan_detail_group_max = 0; + foreach ($dialplan_details as $i => $row) { + if ($row['dialplan_detail_tag'] == 'action' && $row['dialplan_detail_type'] == 'set' && strpos($row['dialplan_detail_data'], 'preset=') === 0) { + $preset_name = explode('=',$row['dialplan_detail_data'])[1]; + if (in_array($preset_name, $valid_presets)) { + $dialplan_detail_group_preset[$row['dialplan_detail_group']] = $preset_name; + } + else { + $invalid_presets_dialplan_detail_groups[] = $row['dialplan_detail_group']; + unset($dialplan_details[$i]); + } + } + if ($row['dialplan_detail_group'] > $dialplan_detail_group_max) { $dialplan_detail_group_max = $row['dialplan_detail_group']; } + } + //reorder any invalid preset dialplan detail groups + if (is_array($invalid_presets_dialplan_detail_groups) && @sizeof($invalid_presets_dialplan_detail_groups) != 0) { + foreach ($dialplan_details as $i => $row) { + if (in_array($row['dialplan_detail_group'], $invalid_presets_dialplan_detail_groups)) { + $dialplan_details[$i]['dialplan_detail_group'] = $dialplan_detail_group_max + 5; + } + } + } + //parse out dialplan actions, anti-actions and conditions + foreach ($dialplan_details as $i => $row) { if ($row['dialplan_detail_tag'] == 'action') { if ($row['dialplan_detail_group'] == '999') { $dialplan_anti_action = $row['dialplan_detail_type'].($row['dialplan_detail_data'] != '' || $row['dialplan_detail_type'] == 'hangup' ? ':'.$row['dialplan_detail_data'] : null); } else { - $dialplan_actions[$row['dialplan_detail_group']] = $row['dialplan_detail_type'].($row['dialplan_detail_data'] != '' || $row['dialplan_detail_type'] == 'hangup' ? ':'.$row['dialplan_detail_data'] : null); + $dialplan_detail_group = $dialplan_detail_group_preset[$row['dialplan_detail_group']] ?: $row['dialplan_detail_group']; + $dialplan_actions[$dialplan_detail_group] = $row['dialplan_detail_type'].($row['dialplan_detail_data'] != '' || $row['dialplan_detail_type'] == 'hangup' ? ':'.$row['dialplan_detail_data'] : null); } } else if ($row['dialplan_detail_tag'] == 'condition') { - $current_conditions[$row['dialplan_detail_group']][$row['dialplan_detail_type']] = $row['dialplan_detail_data']; + $dialplan_detail_group = $dialplan_detail_group_preset[$row['dialplan_detail_group']] ?: $row['dialplan_detail_group']; + $current_conditions[$dialplan_detail_group][$row['dialplan_detail_type']] = $row['dialplan_detail_data']; } } } -// //find the selected presets -// if (is_array($available_presets)) { -// foreach ($available_presets as $preset_number => &$preset) { -// if (is_array($preset)) { -// foreach ($preset as $preset_name => $preset_variables) { -// $preset_checked[$preset_name] = 'false'; -// if (is_array($dialplan_details)) { -// foreach ($dialplan_details as $row) { -// if ($row['dialplan_detail_tag'] == 'action') { -// if ($row['dialplan_detail_data'] == 'preset='.$preset_name) { -// $preset_checked[$preset_name] = 'true'; -// } -// } -// } -// } -// } -// } -// } -// } - //loop through available presets (if any) - if (is_array($available_presets)) { + if (is_array($available_presets) && @sizeof($available_presets) != 0) { foreach ($available_presets as $preset_number => $preset) { - if (is_array($preset)) { + if (is_array($preset) && @sizeof($preset) != 0) { foreach ($preset as $preset_name => $preset_variables) { //loop through each condition group if (is_array($current_conditions)) { @@ -631,8 +633,17 @@ } //if all preset variables found, then condition is a preset if ($matches == sizeof($preset_variables)) { - - $current_presets[$preset_number] = $group_id; + //preset found + if (!is_numeric($group_id)) { + $current_presets[] = $group_id; + } + //preset *conditions* found, but wasn't marked as a preset in the dialplan, so promote and update current conditions and dialplan actions + else { + $current_presets[] = $preset_name; + $current_conditions[$preset_name] = $current_conditions[$group_id]; + $dialplan_actions[$preset_name] = $dialplan_actions[$group_id]; + unset($current_conditions[$group_id], $dialplan_actions[$group_id]); + } } } } @@ -640,6 +651,11 @@ } } } + + //sort arrays by keys + if (is_array($dialplan_actions)) { ksort($dialplan_actions); } + if (is_array($current_conditions)) { ksort($current_conditions); } + } //set the defaults @@ -653,6 +669,16 @@ $document['title'] = $text['title-time_condition']; require_once "resources/header.php"; +//debug +// echo "
\n"; +// echo "".'$dialplan_details'."\n"; view_array($dialplan_details, false); +// echo "".'$dialplan_anti_action'."\n"; view_array($dialplan_anti_action, false); +// echo "".'$dialplan_actions'."\n"; view_array($dialplan_actions, false); // +// echo "".'$current_conditions'."\n"; view_array($current_conditions, false); // +// echo "".'$available_presets'."\n"; view_array($available_presets, false); +// echo "".'$current_presets'."\n"; view_array($current_presets, false); // +// echo "


\n"; + ?>