diff --git a/app/destinations/destinations.php b/app/destinations/destinations.php index 104a9606c2..90d4ac9545 100644 --- a/app/destinations/destinations.php +++ b/app/destinations/destinations.php @@ -28,6 +28,7 @@ require_once "root.php"; require_once "resources/require.php"; require_once "resources/check_auth.php"; + require_once "resources/paging.php"; //check permissions if (permission_exists('destination_view')) { @@ -42,100 +43,37 @@ $language = new text; $text = $language->get(); -//get the action - if (is_array($_POST["destinations"])) { - $destinations = $_POST["destinations"]; - foreach($destinations as $row) { - if ($row['action'] == 'delete') { - $action = 'delete'; - break; - } +//get posted data + if (is_array($_POST['destinations'])) { + $action = $_POST['action']; + $search = $_POST['search']; + $destinations = $_POST['destinations']; + } + +//toggle the destinations + if (permission_exists('destination_edit')) { + if ($action == 'toggle' && is_array($destinations) && @sizeof($destinations) != 0) { + //toggle + $obj = new destinations; + $obj->toggle($destinations); + //redirect + header('Location: destinations.php'.($search != '' ? '?search='.urlencode($search) : null)); + exit; } } //delete the destinations if (permission_exists('destination_delete')) { - if ($action == "delete") { - //download + if ($action == 'delete' && is_array($destinations) && @sizeof($destinations) != 0) { + //delete $obj = new destinations; $obj->delete($destinations); - //delete message - message::add($text['message-delete']); + //redirect + header('Location: destinations.php'.($search != '' ? '?search='.urlencode($search) : null)); + exit; } } -//get variables used to control the order - $order_by = $_GET["order_by"]; - $order = $_GET["order"]; - -//set the type - switch ($_GET['type']) { - case 'inbound': $destination_type = 'inbound'; break; - case 'outbound': $destination_type = 'outbound'; break; - case 'local': $destination_type = 'local'; break; - default: $destination_type = 'inbound'; - } - -//add the search term - $search = strtolower($_GET["search"]); - if (strlen($search) > 0) { - $sql_search = " ("; - $sql_search .= "lower(destination_type) like :search "; - $sql_search .= "or lower(destination_number) like :search "; - $sql_search .= "or lower(destination_context) like :search "; - $sql_search .= "or lower(destination_accountcode) like :search "; - if (permission_exists('outbound_caller_id_select')) { - $sql_search .= "or lower(destination_caller_id_name) like :search "; - $sql_search .= "or destination_caller_id_number like :search "; - } - $sql_search .= "or lower(destination_enabled) like :search "; - $sql_search .= "or lower(destination_description) like :search "; - $sql_search .= "or lower(destination_data) like :search "; - $sql_search .= ") "; - } - -//common sql where - $sql_where = "where destination_type = :destination_type "; - if ($_GET['show'] == "all" && permission_exists('destination_all')) { - //show all - } - else { - $sql_where .= "and (domain_uuid = :domain_uuid or domain_uuid is null) "; - $parameters['domain_uuid'] = $domain_uuid; - } - if (isset($sql_search)) { - $sql_where .= "and ".$sql_search; - $parameters['search'] = '%'.$search.'%'; - } - $parameters['destination_type'] = $destination_type; - -//prepare to page the results - $sql = "select count(destination_uuid) from v_destinations "; - $sql .= $sql_where; - $database = new database; - $num_rows = $database->select($sql, $parameters, 'column'); - -//prepare to page the results - require_once "resources/paging.php"; - $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50; - $param = "&search=".escape($search); - if ($_GET['show'] == "all" && permission_exists('destination_all')) { - $param .= "&show=all"; - } - $page = $_GET['page']; - if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; } - list($paging_controls_mini, $rows_per_page, $var_3) = paging($num_rows, $param, $rows_per_page, true); - list($paging_controls, $rows_per_page, $var3) = paging($num_rows, $param, $rows_per_page); - $offset = $rows_per_page * $page; - -//get the list - $sql = str_replace('count(destination_uuid)', '*', $sql); - $sql .= order_by($order_by, $order); - $sql .= limit_offset($rows_per_page, $offset); - $database = new database; - $destinations = $database->select($sql, $parameters, 'all'); - unset($parameters); - //get the destination select list $destination = new destinations; $destination_array = $destination->all('dialplan'); @@ -161,81 +99,131 @@ } } -//alternate the row style - $c = 0; - $row_style["0"] = "row_style0"; - $row_style["1"] = "row_style1"; +//set the type + switch ($_GET['type']) { + case 'inbound': $destination_type = 'inbound'; break; + case 'outbound': $destination_type = 'outbound'; break; + case 'local': $destination_type = 'local'; break; + default: $destination_type = 'inbound'; + } + +//get variables used to control the order + $order_by = $_GET["order_by"]; + $order = $_GET["order"]; + +//add the search term + $search = strtolower($_GET["search"]); + if (strlen($search) > 0) { + $sql_search = "and ("; + $sql_search .= "lower(destination_type) like :search "; + $sql_search .= "or lower(destination_number) like :search "; + $sql_search .= "or lower(destination_context) like :search "; + $sql_search .= "or lower(destination_accountcode) like :search "; + if (permission_exists('outbound_caller_id_select')) { + $sql_search .= "or lower(destination_caller_id_name) like :search "; + $sql_search .= "or destination_caller_id_number like :search "; + } + $sql_search .= "or lower(destination_enabled) like :search "; + $sql_search .= "or lower(destination_description) like :search "; + $sql_search .= "or lower(destination_data) like :search "; + $sql_search .= ") "; + $parameters['search'] = '%'.$search.'%'; + } + +//prepare to page the results + $sql = "select count(*) from v_destinations "; + $sql .= "where destination_type = :destination_type "; + if ($_GET['show'] != "all" || !permission_exists('destination_all')) { + $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) "; + $parameters['domain_uuid'] = $domain_uuid; + } + $sql .= $sql_search; + $parameters['destination_type'] = $destination_type; + $database = new database; + $num_rows = $database->select($sql, $parameters, 'column'); + +//prepare to page the results + $rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50; + $param = "&search=".$search; + if ($_GET['show'] == "all" && permission_exists('destination_all')) { + $param .= "&show=all"; + } + $page = $_GET['page']; + if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; } + 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; + +//get the list + $sql = str_replace('count(*)', '*', $sql); + $sql .= order_by($order_by, $order, 'destination_number', 'asc'); + $sql .= limit_offset($rows_per_page, $offset); + $database = new database; + $destinations = $database->select($sql, $parameters, 'all'); + unset($sql, $parameters); + +//create token + $object = new token; + $token = $object->create($_SERVER['PHP_SELF']); //include the header require_once "resources/header.php"; -//define the checkbox_toggle function - echo "\n"; - //show the content - echo "\n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo "
".$text['title-destinations']." (".$num_rows.")
\n"; - - echo " \n"; - echo " \n"; - echo " \n"; - echo "  \n"; - if (permission_exists('destination_import')) { - echo " \n"; + echo "
\n"; + echo "
".$text['title-destinations']." (".$num_rows.")
\n"; + echo "
\n"; + if (permission_exists('destination_add')) { + echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'link'=>'destination_edit.php']); } - + if (permission_exists('destination_delete') && $destinations) { + echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'onclick'=>"if (confirm('".$text['confirm-delete']."')) { list_action_set('delete'); list_form_submit('form_list'); } else { this.blur(); return false; }"]); + } + echo "\n"; if (permission_exists('destination_all')) { if ($_GET['show'] == 'all') { echo " "; } else { - echo " \n"; + echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?type='.urlencode($destination_type).'&show=all'.($search != '' ? "&search=".urlencode($search) : null)]); } } - - echo " \n"; - echo " \n"; + echo ""; + echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search','style'=>($search != '' ? 'display: none;' : null)]); + echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','link'=>'destinations.php','style'=>($search == '' ? 'display: none;' : null)]); if ($paging_controls_mini != '') { - echo " ".$paging_controls_mini."\n"; + echo "".$paging_controls_mini.""; + } + echo button::create(['type'=>'button','label'=>$text['button-inbound'],'icon'=>'location-arrow fa-rotate-90', 'style'=>'margin-left: 15px;','link'=>'?type=inbound'.($_GET['show'] == 'all' ? '&show=all' : null).($search != '' ? "&search=".urlencode($search) : null)]); + echo button::create(['type'=>'button','label'=>$text['button-outbound'],'icon'=>'location-arrow','link'=>'?type=outbound'.($_GET['show'] == 'all' ? '&show=all' : null).($search != '' ? "&search=".urlencode($search) : null)]); + echo button::create(['type'=>'button','label'=>$text['button-local'],'icon'=>'vector-square','link'=>'?type=local'.($_GET['show'] == 'all' ? '&show=all' : null).($search != '' ? "&search=".urlencode($search) : null)]); + if (permission_exists('destination_import')) { + echo button::create(['type'=>'button','label'=>$text['button-import'],'icon'=>'file-import','link'=>'destination_imports.php']); } - echo "
\n"; - echo " ".$text['description-destinations']."

\n"; - echo "
\n"; + echo " \n"; + echo "
\n"; + echo "\n"; - echo "
\n"; - echo "\n"; - echo "\n"; - echo " \n"; - if ($_GET['show'] == "all" && permission_exists('destination_all')) { - echo th_order_by('domain_name', $text['label-domain'], $order_by, $order, $param); + echo $text['description-destinations']."\n"; + echo "

\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + + echo "
\n"; - echo " \n"; - echo "
\n"; + echo "\n"; + if (permission_exists('destination_delete')) { + echo " \n"; } - echo th_order_by('destination_type', $text['label-destination_type'], $order_by, $order, $param); - echo th_order_by('destination_number', $text['label-destination_number'], $order_by, $order, $param); + if ($_GET['show'] == "all" && permission_exists('destination_all')) { + echo th_order_by('domain_name', $text['label-domain'], $order_by, $order, $param, "class='shrink'"); + } + echo th_order_by('destination_type', $text['label-destination_type'], $order_by, $order, $param, "class='shrink'"); + echo th_order_by('destination_number', $text['label-destination_number'], $order_by, $order, $param, "class='shrink'"); if (!$_GET['show'] == "all") { echo ""; } @@ -247,30 +235,25 @@ echo th_order_by('destination_caller_id_number', $text['label-destination_caller_id_number'], $order_by, $order, $param); } echo th_order_by('destination_enabled', $text['label-destination_enabled'], $order_by, $order, $param); - echo th_order_by('destination_description', $text['label-destination_description'], $order_by, $order, $param); - echo " \n"; } - else { - echo " \n"; - } - echo " \n"; - echo "\n"; + echo "\n"; + if (is_array($destinations) && @sizeof($destinations) != 0) { $x = 0; foreach($destinations as $row) { - $action_name = action_name($destination_array, $row['destination_app'].':'.$row['destination_data']); if (permission_exists('destination_edit')) { - $tr_link = "href='destination_edit.php?id=".$row['destination_uuid']."'"; + $list_row_url = "destination_edit.php?id=".urlencode($row['destination_uuid']); + } + echo "\n"; + if (permission_exists('destination_delete')) { + echo " \n"; } - echo "\n"; - //echo " \n"; - //echo " \n"; - echo " \n"; if ($_GET['show'] == "all" && permission_exists('destination_all')) { if (strlen($_SESSION['domains'][$row['domain_uuid']]['domain_name']) > 0) { $domain = $_SESSION['domains'][$row['domain_uuid']]['domain_name']; @@ -278,67 +261,49 @@ else { $domain = $text['label-global']; } - echo " \n"; + echo " \n"; } - echo " \n"; - echo " \n"; - if (!$_GET['show'] == "all") { - echo " \n"; - } - //echo " \n"; - if (permission_exists("destination_context")) { - echo " \n"; - } - //echo " \n"; - if (permission_exists('outbound_caller_id_select')) { - echo " \n"; - echo " \n"; - } - //echo " \n"; - //echo " \n"; - //echo " \n"; - //echo " \n"; - //echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; + echo " \n"; + if (!$_GET['show'] == "all") { + echo " \n"; + } + if (permission_exists("destination_context")) { + echo " \n"; + } + if (permission_exists('outbound_caller_id_select')) { + echo " \n"; + echo " \n"; + } + echo " \n"; + echo " \n"; + if (permission_exists('destination_edit') && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') { + echo " \n"; + } echo "\n"; $x++; - if ($c==0) { $c=1; } else { $c=0; } - } //end foreach - unset($sql, $destinations, $row_count); - } //end if results + } + unset($destinations); + } + + echo "
\n"; + echo " \n"; + echo " ". $text['label-detail_action'].""; - if (permission_exists('destination_add')) { - echo " $v_link_label_add"; + echo th_order_by('destination_description', $text['label-destination_description'], $order_by, $order, $param, "class='hide-sm-dn'"); + if (permission_exists('destination_edit') && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') { + echo "  
\n"; + echo " \n"; + echo " \n"; + echo "
".$row['destination_uuid']." ".$row['dialplan_uuid']." ".escape($domain)."".escape($domain)."".escape($row['destination_type'])." ".escape(format_phone($row['destination_number']))." ".$action_name." ".$row['destination_number_regex']." ".escape($row['destination_context'])." ".escape($row['fax_uuid'])." ".escape($row['destination_caller_id_name'])." ".escape($row['destination_caller_id_number'])." ".escape($row['destination_cid_name_prefix'])." ".escape($row['destination_app'])." ".escape($row['destination_data'])." ".escape($row['destination_record'])." ".escape($row['destination_accountcode'])." ".escape($row['destination_enabled'])." ".escape($row['destination_description'])." "; + echo " ".escape($row['destination_type'])." "; if (permission_exists('destination_edit')) { - echo "$v_link_label_edit"; + echo "".escape(format_phone($row['destination_number'])).""; } - if (permission_exists('destination_delete')) { - echo ""; + else { + echo escape(format_phone($row['destination_number'])); } echo " ".action_name($destination_array, $row['destination_app'].':'.$row['destination_data'])." ".escape($row['destination_context'])." ".escape($row['destination_caller_id_name'])." ".escape($row['destination_caller_id_number'])." ".escape($text['label-'.$row['destination_enabled']])." ".escape($row['destination_description'])." "; + echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]); + echo "
\n"; + echo "
\n"; + echo "
".$paging_controls."
\n"; + + echo "\n"; - echo "\n"; - echo "\n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo "
 $paging_controls"; - if (permission_exists('destination_add')) { - echo "$v_link_label_add"; - } - else { - echo " "; - } - echo "
\n"; - echo "\n"; - echo "\n"; - echo ""; echo "
\n"; - echo "

"; //include the footer require_once "resources/footer.php"; -?> +?> \ No newline at end of file diff --git a/resources/classes/destinations.php b/resources/classes/destinations.php index 4c1500bcd5..c46e7e0e71 100644 --- a/resources/classes/destinations.php +++ b/resources/classes/destinations.php @@ -6,542 +6,584 @@ * @method get_array get the destinations * @method select build the html select */ -class destinations { +if (!class_exists('destinations')) { + class destinations { - /** - * destinations array - */ - public $destinations; - public $db; + /** + * destinations array + */ + public $destinations; + public $db; - /** - * Called when the object is created - */ - public function __construct() { + /** + * declare private variables + */ + private $app_name; + private $app_uuid; + private $permission_prefix; + private $list_page; + private $table; + private $uuid_prefix; + + /** + * Called when the object is created + */ + public function __construct() { //connect to the database if not connected - if (!$this->db) { - require_once "resources/classes/database.php"; - $database = new database; - $database->connect(); - $this->db = $database->db; - } - } - - /** - * Called when there are no references to a particular object - * unset the variables used in the class - */ - public function __destruct() { - foreach ($this as $key => $value) { - unset($this->$key); - } - } - - /** - * Build the destination select list - * @var string $destination_type can be ivr, dialplan, call_center_contact or bridge - * @var string $destination_name - current name - * @var string $destination_value - current value - */ - public function select($destination_type, $destination_name, $destination_value) { - - //set the global variables - global $db_type; - - //get the destinations - if (!is_array($this->destinations)) { - - //get the array from the app_config.php files - $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php"); - $x = 0; - foreach ($config_list as &$config_path) { - include($config_path); - $x++; - } - $i = 0; - foreach ($apps as $x => &$app) { - if (isset($app['destinations'])) foreach ($app['destinations'] as &$row) { - $this->destinations[] = $row; - } - } - - //put the array in order - foreach ($this->destinations as $row) { - $option_groups[] = $row['label']; - } - array_multisort($option_groups, SORT_ASC, $this->destinations); - - //add the sql and data to the array - $x = 0; - foreach ($this->destinations as $row) { - if ($row['type'] = 'sql') { - $table_name = preg_replace('#[^a-zA-Z0-9_]#', '', $row['name']); - if (isset($row['sql'])) { - if (is_array($row['sql'])) { - $sql = trim($row['sql'][$db_type])." "; - } - else { - $sql = trim($row['sql'])." "; - } - } - else { - $field_count = count($row['field']); - $fields = ''; - $c = 1; - foreach ($row['field'] as $key => $value) { - $key = preg_replace('#[^a-zA-Z0-9_]#', '', $key); - $value = preg_replace('#[^a-zA-Z0-9_]#', '', $value); - if ($field_count != $c) { $delimiter = ','; } else { $delimiter = ''; } - $fields .= $value." as ".$key.$delimiter." "; - $c++; - } - $sql = "select ".$fields; - $sql .= " from v_".$table_name." "; - } - if (isset($row['where'])) { - $sql .= trim($row['where'])." "; - } - $sql .= "order by ".trim($row['order_by']); - $sql = str_replace("\${domain_uuid}", $_SESSION['domain_uuid'], $sql); + if (!$this->db) { + require_once "resources/classes/database.php"; $database = new database; - $result = $database->select($sql, null, 'all'); - - $this->destinations[$x]['result']['sql'] = $sql; - $this->destinations[$x]['result']['data'] = $result; + $database->connect(); + $this->db = $database->db; } - $x++; - } - $this->destinations[$x]['type'] = 'array'; - $this->destinations[$x]['label'] = 'other'; - $this->destinations[$x]['name'] = 'dialplans'; - $this->destinations[$x]['field']['name'] = "name"; - $this->destinations[$x]['field']['destination'] = "destination"; - $this->destinations[$x]['select_value']['dialplan'] = "transfer:\${destination}"; - $this->destinations[$x]['select_value']['ivr'] = "menu-exec-app:transfer \${destination}"; - $this->destinations[$x]['select_label'] = "\${name}"; - $y = 0; - $this->destinations[$x]['result']['data'][$y]['label'] = 'check_voicemail'; - $this->destinations[$x]['result']['data'][$y]['name'] = '*98'; - $this->destinations[$x]['result']['data'][$y]['destination'] = '*98 XML ${context}'; - $y++; - $this->destinations[$x]['result']['data'][$y]['label'] = 'company_directory'; - $this->destinations[$x]['result']['data'][$y]['name'] = '*411'; - $this->destinations[$x]['result']['data'][$y]['destination'] = '*411 XML ${context}'; - $y++; - $this->destinations[$x]['result']['data'][$y]['label'] = 'hangup'; - $this->destinations[$x]['result']['data'][$y]['name'] = 'hangup'; - $this->destinations[$x]['result']['data'][$y]['application'] = 'hangup'; - $this->destinations[$x]['result']['data'][$y]['destination'] = ''; - $y++; - $this->destinations[$x]['result']['data'][$y]['label'] = 'record'; - $this->destinations[$x]['result']['data'][$y]['name'] = '*732'; - $this->destinations[$x]['result']['data'][$y]['destination'] = '*732 XML ${context}'; - $y++; + //assign private variables + $this->app_name = 'destinations'; + $this->app_uuid = '5ec89622-b19c-3559-64f0-afde802ab139'; + $this->permission_prefix = 'destination_'; + $this->list_page = 'destinations.php'; + $this->table = 'destinations'; + $this->uuid_prefix = 'destination_'; } - //remove special characters from the name - $destination_id = str_replace("]", "", $destination_name); - $destination_id = str_replace("[", "_", $destination_id); - - //set the css style - $select_style = 'width: 200px;'; - - //add additional - if (if_group("superadmin")) { - $response = "\n"; - $response .= "\n"; } - //set default to false - $select_found = false; + /** + * Build the destination select list + * @var string $destination_type can be ivr, dialplan, call_center_contact or bridge + * @var string $destination_name - current name + * @var string $destination_value - current value + */ + public function select($destination_type, $destination_name, $destination_value) { - $response .= " \n"; - if (if_group("superadmin")) { - $response .= ""; - } - - //return the formatted destinations - return $response; - } - - /** - * Get all the destinations - * @var string $destination_type can be ivr, dialplan, call_center_contact or bridge - */ - public function all($destination_type) { - - //set the global variables - global $db_type; - - //get the destinations - if (!is_array($this->destinations)) { - - //get the array from the app_config.php files - $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php"); - $x = 0; - foreach ($config_list as &$config_path) { - include($config_path); - $x++; - } - $i = 0; - foreach ($apps as $x => &$app) { - if (isset($app['destinations'])) { - foreach ($app['destinations'] as &$row) { + $i = 0; + foreach ($apps as $x => &$app) { + if (isset($app['destinations'])) foreach ($app['destinations'] as &$row) { $this->destinations[] = $row; } } - } - //put the array in order - foreach ($this->destinations as $row) { - $option_groups[] = $row['label']; - } - array_multisort($option_groups, SORT_ASC, $this->destinations); - - //add the sql and data to the array - $x = 0; - foreach ($this->destinations as $row) { - if ($row['type'] = 'sql') { - $table_name = preg_replace('#[^a-zA-Z0-9_]#', '', $row['name']); - if (isset($row['sql'])) { - if (is_array($row['sql'])) { - $sql = trim($row['sql'][$db_type])." "; - } - else { - $sql = trim($row['sql'])." "; - } - } - else { - $field_count = count($row['field']); - $fields = ''; - $c = 1; - foreach ($row['field'] as $key => $value) { - $key = preg_replace('#[^a-zA-Z0-9_]#', '', $key); - $value = preg_replace('#[^a-zA-Z0-9_]#', '', $value); - if ($field_count != $c) { $delimiter = ','; } else { $delimiter = ''; } - $fields .= $value." as ".$key.$delimiter." "; - $c++; - } - $sql = "select ".$fields; - $sql .= " from v_".$table_name." "; - } - if (isset($row['where'])) { - $sql .= trim($row['where'])." "; - } - $sql .= "order by ".trim($row['order_by']); - $sql = str_replace("\${domain_uuid}", $_SESSION['domain_uuid'], $sql); - $database = new database; - $result = $database->select($sql, null, 'all'); - - $this->destinations[$x]['result']['sql'] = $sql; - $this->destinations[$x]['result']['data'] = $result; + //put the array in order + foreach ($this->destinations as $row) { + $option_groups[] = $row['label']; } - $x++; - } - $this->destinations[$x]['type'] = 'array'; - $this->destinations[$x]['label'] = 'other'; - $this->destinations[$x]['name'] = 'dialplans'; - $this->destinations[$x]['field']['name'] = "name"; - $this->destinations[$x]['field']['destination'] = "destination"; - $this->destinations[$x]['select_value']['dialplan'] = "transfer:\${destination}"; - $this->destinations[$x]['select_value']['ivr'] = "menu-exec-app:transfer \${destination}"; - $this->destinations[$x]['select_label'] = "\${name}"; - $y = 0; - $this->destinations[$x]['result']['data'][$y]['label'] = 'check_voicemail'; - $this->destinations[$x]['result']['data'][$y]['name'] = '*98'; - $this->destinations[$x]['result']['data'][$y]['destination'] = '*98 XML ${context}'; - $y++; - $this->destinations[$x]['result']['data'][$y]['label'] = 'company_directory'; - $this->destinations[$x]['result']['data'][$y]['name'] = '*411'; - $this->destinations[$x]['result']['data'][$y]['destination'] = '*411 XML ${context}'; - $y++; - $this->destinations[$x]['result']['data'][$y]['label'] = 'hangup'; - $this->destinations[$x]['result']['data'][$y]['name'] = 'hangup'; - $this->destinations[$x]['result']['data'][$y]['application'] = 'hangup'; - $this->destinations[$x]['result']['data'][$y]['destination'] = ''; - $y++; - $this->destinations[$x]['result']['data'][$y]['label'] = 'record'; - $this->destinations[$x]['result']['data'][$y]['name'] = '*732'; - $this->destinations[$x]['result']['data'][$y]['destination'] = '*732 XML ${context}'; - $y++; - } + array_multisort($option_groups, SORT_ASC, $this->destinations); - //remove special characters from the name - $destination_id = str_replace("]", "", $destination_name); - $destination_id = str_replace("[", "_", $destination_id); - - //set default to false - $select_found = false; - - foreach ($this->destinations as $row) { - - $name = $row['name']; - $label = $row['label']; - $destination = $row['field']['destination']; - - //add multi-lingual support - if (file_exists($_SERVER["PROJECT_ROOT"]."/app/".$name."/app_languages.php")) { - $language2 = new text; - $text2 = $language2->get($_SESSION['domain']['language']['code'], 'app/'.$name); - } - - if (count($row['result']['data']) > 0 and strlen($row['select_value'][$destination_type]) > 0) { - $label2 = $label; - foreach ($row['result']['data'] as $data) { - $select_value = $row['select_value'][$destination_type]; - $select_label = $row['select_label']; - foreach ($row['field'] as $key => $value) { - if ($key == 'destination' and is_array($value)){ - if ($value['type'] == 'csv') { - $array = explode($value['delimiter'], $data[$key]); - $select_value = str_replace("\${destination}", $array[0], $select_value); - $select_label = str_replace("\${destination}", $array[0], $select_label); + //add the sql and data to the array + $x = 0; + foreach ($this->destinations as $row) { + if ($row['type'] = 'sql') { + $table_name = preg_replace('#[^a-zA-Z0-9_]#', '', $row['name']); + if (isset($row['sql'])) { + if (is_array($row['sql'])) { + $sql = trim($row['sql'][$db_type])." "; + } + else { + $sql = trim($row['sql'])." "; } } else { - if (strpos($value,',') !== false) { - $keys = explode(",", $value); - foreach ($keys as $k) { - if (strlen($data[$k]) > 0) { - $select_value = str_replace("\${".$key."}", $data[$k], $select_value); - if (strlen($data['label']) == 0) { - $select_label = str_replace("\${".$key."}", $data[$k], $select_label); - } - else { - $label = $data['label']; - $select_label = str_replace("\${".$key."}", $text2['option-'.$label], $select_label); - } - } + $field_count = count($row['field']); + $fields = ''; + $c = 1; + foreach ($row['field'] as $key => $value) { + $key = preg_replace('#[^a-zA-Z0-9_]#', '', $key); + $value = preg_replace('#[^a-zA-Z0-9_]#', '', $value); + if ($field_count != $c) { $delimiter = ','; } else { $delimiter = ''; } + $fields .= $value." as ".$key.$delimiter." "; + $c++; + } + $sql = "select ".$fields; + $sql .= " from v_".$table_name." "; + } + if (isset($row['where'])) { + $sql .= trim($row['where'])." "; + } + $sql .= "order by ".trim($row['order_by']); + $sql = str_replace("\${domain_uuid}", $_SESSION['domain_uuid'], $sql); + $database = new database; + $result = $database->select($sql, null, 'all'); + + $this->destinations[$x]['result']['sql'] = $sql; + $this->destinations[$x]['result']['data'] = $result; + } + $x++; + } + + $this->destinations[$x]['type'] = 'array'; + $this->destinations[$x]['label'] = 'other'; + $this->destinations[$x]['name'] = 'dialplans'; + $this->destinations[$x]['field']['name'] = "name"; + $this->destinations[$x]['field']['destination'] = "destination"; + $this->destinations[$x]['select_value']['dialplan'] = "transfer:\${destination}"; + $this->destinations[$x]['select_value']['ivr'] = "menu-exec-app:transfer \${destination}"; + $this->destinations[$x]['select_label'] = "\${name}"; + $y = 0; + $this->destinations[$x]['result']['data'][$y]['label'] = 'check_voicemail'; + $this->destinations[$x]['result']['data'][$y]['name'] = '*98'; + $this->destinations[$x]['result']['data'][$y]['destination'] = '*98 XML ${context}'; + $y++; + $this->destinations[$x]['result']['data'][$y]['label'] = 'company_directory'; + $this->destinations[$x]['result']['data'][$y]['name'] = '*411'; + $this->destinations[$x]['result']['data'][$y]['destination'] = '*411 XML ${context}'; + $y++; + $this->destinations[$x]['result']['data'][$y]['label'] = 'hangup'; + $this->destinations[$x]['result']['data'][$y]['name'] = 'hangup'; + $this->destinations[$x]['result']['data'][$y]['application'] = 'hangup'; + $this->destinations[$x]['result']['data'][$y]['destination'] = ''; + $y++; + $this->destinations[$x]['result']['data'][$y]['label'] = 'record'; + $this->destinations[$x]['result']['data'][$y]['name'] = '*732'; + $this->destinations[$x]['result']['data'][$y]['destination'] = '*732 XML ${context}'; + $y++; + } + + //remove special characters from the name + $destination_id = str_replace("]", "", $destination_name); + $destination_id = str_replace("[", "_", $destination_id); + + //set the css style + $select_style = 'width: 200px;'; + + //add additional + if (if_group("superadmin")) { + $response = "\n"; + $response .= "\n"; + } + + //set default to false + $select_found = false; + + $response .= " \n"; + if (if_group("superadmin")) { + $response .= ""; + } + + //return the formatted destinations + return $response; + } + + /** + * Get all the destinations + * @var string $destination_type can be ivr, dialplan, call_center_contact or bridge + */ + public function all($destination_type) { + + //set the global variables + global $db_type; + + //get the destinations + if (!is_array($this->destinations)) { + + //get the array from the app_config.php files + $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php"); + $x = 0; + foreach ($config_list as &$config_path) { + include($config_path); + $x++; + } + $i = 0; + foreach ($apps as $x => &$app) { + if (isset($app['destinations'])) { + foreach ($app['destinations'] as &$row) { + $this->destinations[] = $row; + } + } + } + + //put the array in order + foreach ($this->destinations as $row) { + $option_groups[] = $row['label']; + } + array_multisort($option_groups, SORT_ASC, $this->destinations); + + //add the sql and data to the array + $x = 0; + foreach ($this->destinations as $row) { + if ($row['type'] = 'sql') { + $table_name = preg_replace('#[^a-zA-Z0-9_]#', '', $row['name']); + if (isset($row['sql'])) { + if (is_array($row['sql'])) { + $sql = trim($row['sql'][$db_type])." "; + } + else { + $sql = trim($row['sql'])." "; } } - } - - $select_value = str_replace("\${domain_name}", $_SESSION['domain_name'], $select_value); - $select_value = str_replace("\${context}", $_SESSION['domain_name'], $select_value); - $select_label = str_replace("\${domain_name}", $_SESSION['domain_name'], $select_label); - $select_label = str_replace("\${context}", $_SESSION['domain_name'], $select_label); - $select_label = str_replace("✉", 'email-icon', $select_label); - $select_label = escape(trim($select_label)); - $select_label = str_replace('email-icon', '✉', $select_label); - if ($select_value == $destination_value) { $selected = "selected='selected' "; $select_found = true; } else { $selected = ''; } - if ($label2 == 'destinations') { $select_label = format_phone($select_label); } - $array[$label][$select_label] = $select_value; - } - unset($text); - } - } - if (!$select_found) { - $destination_label = str_replace(":", " ", $destination_value); - $destination_label = str_replace("menu-exec-app", "", $destination_label); - $destination_label = str_replace("transfer", "", $destination_label); - $destination_label = str_replace("XML ".$_SESSION['domain_name'], "", $destination_label); - $array[$label][$destination_label] = $destination_value; - } - - //return the formatted destinations - return $array; - } - - /** - * delete destinations and related dialplan - */ - public function delete($destinations) { - if (permission_exists('destination_delete')) { - - //delete multiple destinations - if (is_array($destinations)) { - //get the action - foreach($destinations as $row) { - if ($row['action'] == 'delete') { - $action = 'delete'; - break; - } - } - - //delete the checked rows - if ($action == 'delete') { - - //add the dialplan permission - $p = new permissions; - $p->add('dialplan_delete', 'temp'); - $p->add('dialplan_detail_delete', 'temp'); - - //loop through selected destinations - foreach($destinations as $row) { - if ($row['action'] == 'delete' or $row['checked'] == 'true') { - - //get the dialplan uuid and context - $sql = "select * from v_destinations "; - $sql .= "where destination_uuid = :destination_uuid "; - $database = new database; - $parameters['destination_uuid'] = $row['destination_uuid']; - $destinations = $database->select($sql,$parameters); - $row = $destinations[0]; - - //prepare and then delete the selected data - if (isset($row["dialplan_uuid"]) && is_uuid($row["dialplan_uuid"])) { - $array['dialplan_details'][]['dialplan_uuid'] = $row["dialplan_uuid"]; - $array['dialplans'][]['dialplan_uuid'] = $row["dialplan_uuid"]; - } - $array['destinations'][]['destination_uuid'] = $row['destination_uuid']; - $database->app_name = 'destinations'; - $database->app_uuid = '5ec89622-b19c-3559-64f0-afde802ab139'; - $database->delete($array); - //$message = $database->message; - - //clear the cache - $cache = new cache; - $cache->delete("dialplan:".$row['destination_context']); - + else { + $field_count = count($row['field']); + $fields = ''; + $c = 1; + foreach ($row['field'] as $key => $value) { + $key = preg_replace('#[^a-zA-Z0-9_]#', '', $key); + $value = preg_replace('#[^a-zA-Z0-9_]#', '', $value); + if ($field_count != $c) { $delimiter = ','; } else { $delimiter = ''; } + $fields .= $value." as ".$key.$delimiter." "; + $c++; + } + $sql = "select ".$fields; + $sql .= " from v_".$table_name." "; } - } - unset($destinations, $row); + if (isset($row['where'])) { + $sql .= trim($row['where'])." "; + } + $sql .= "order by ".trim($row['order_by']); + $sql = str_replace("\${domain_uuid}", $_SESSION['domain_uuid'], $sql); + $database = new database; + $result = $database->select($sql, null, 'all'); - //remove the temporary permission - $p->delete('dialplan_delete', 'temp'); - $p->delete('dialplan_detail_delete', 'temp'); + $this->destinations[$x]['result']['sql'] = $sql; + $this->destinations[$x]['result']['data'] = $result; + } + $x++; + } + $this->destinations[$x]['type'] = 'array'; + $this->destinations[$x]['label'] = 'other'; + $this->destinations[$x]['name'] = 'dialplans'; + $this->destinations[$x]['field']['name'] = "name"; + $this->destinations[$x]['field']['destination'] = "destination"; + $this->destinations[$x]['select_value']['dialplan'] = "transfer:\${destination}"; + $this->destinations[$x]['select_value']['ivr'] = "menu-exec-app:transfer \${destination}"; + $this->destinations[$x]['select_label'] = "\${name}"; + $y = 0; + $this->destinations[$x]['result']['data'][$y]['label'] = 'check_voicemail'; + $this->destinations[$x]['result']['data'][$y]['name'] = '*98'; + $this->destinations[$x]['result']['data'][$y]['destination'] = '*98 XML ${context}'; + $y++; + $this->destinations[$x]['result']['data'][$y]['label'] = 'company_directory'; + $this->destinations[$x]['result']['data'][$y]['name'] = '*411'; + $this->destinations[$x]['result']['data'][$y]['destination'] = '*411 XML ${context}'; + $y++; + $this->destinations[$x]['result']['data'][$y]['label'] = 'hangup'; + $this->destinations[$x]['result']['data'][$y]['name'] = 'hangup'; + $this->destinations[$x]['result']['data'][$y]['application'] = 'hangup'; + $this->destinations[$x]['result']['data'][$y]['destination'] = ''; + $y++; + $this->destinations[$x]['result']['data'][$y]['label'] = 'record'; + $this->destinations[$x]['result']['data'][$y]['name'] = '*732'; + $this->destinations[$x]['result']['data'][$y]['destination'] = '*732 XML ${context}'; + $y++; + } + + //remove special characters from the name + $destination_id = str_replace("]", "", $destination_name); + $destination_id = str_replace("[", "_", $destination_id); + + //set default to false + $select_found = false; + + foreach ($this->destinations as $row) { + + $name = $row['name']; + $label = $row['label']; + $destination = $row['field']['destination']; + + //add multi-lingual support + if (file_exists($_SERVER["PROJECT_ROOT"]."/app/".$name."/app_languages.php")) { + $language2 = new text; + $text2 = $language2->get($_SESSION['domain']['language']['code'], 'app/'.$name); + } + + if (count($row['result']['data']) > 0 and strlen($row['select_value'][$destination_type]) > 0) { + $label2 = $label; + foreach ($row['result']['data'] as $data) { + $select_value = $row['select_value'][$destination_type]; + $select_label = $row['select_label']; + foreach ($row['field'] as $key => $value) { + if ($key == 'destination' and is_array($value)){ + if ($value['type'] == 'csv') { + $array = explode($value['delimiter'], $data[$key]); + $select_value = str_replace("\${destination}", $array[0], $select_value); + $select_label = str_replace("\${destination}", $array[0], $select_label); + } + } + else { + if (strpos($value,',') !== false) { + $keys = explode(",", $value); + foreach ($keys as $k) { + if (strlen($data[$k]) > 0) { + $select_value = str_replace("\${".$key."}", $data[$k], $select_value); + if (strlen($data['label']) == 0) { + $select_label = str_replace("\${".$key."}", $data[$k], $select_label); + } + else { + $label = $data['label']; + $select_label = str_replace("\${".$key."}", $text2['option-'.$label], $select_label); + } + } + } + } + else { + $select_value = str_replace("\${".$key."}", $data[$key], $select_value); + if (strlen($data['label']) == 0) { + $select_label = str_replace("\${".$key."}", $data[$key], $select_label); + } + else { + $label = $data['label']; + $select_label = str_replace("\${".$key."}", $text2['option-'.$label], $select_label); + } + } + //application: hangup + if (strlen($data['application']) > 0) { + $select_value = str_replace("transfer", $data['application'], $select_value); + } + } + } + + $select_value = str_replace("\${domain_name}", $_SESSION['domain_name'], $select_value); + $select_value = str_replace("\${context}", $_SESSION['domain_name'], $select_value); + $select_label = str_replace("\${domain_name}", $_SESSION['domain_name'], $select_label); + $select_label = str_replace("\${context}", $_SESSION['domain_name'], $select_label); + $select_label = str_replace("✉", 'email-icon', $select_label); + $select_label = escape(trim($select_label)); + $select_label = str_replace('email-icon', '✉', $select_label); + if ($select_value == $destination_value) { $selected = "selected='selected' "; $select_found = true; } else { $selected = ''; } + if ($label2 == 'destinations') { $select_label = format_phone($select_label); } + $array[$label][$select_label] = $select_value; + } + unset($text); } } + if (!$select_found) { + $destination_label = str_replace(":", " ", $destination_value); + $destination_label = str_replace("menu-exec-app", "", $destination_label); + $destination_label = str_replace("transfer", "", $destination_label); + $destination_label = str_replace("XML ".$_SESSION['domain_name'], "", $destination_label); + $array[$label][$destination_label] = $destination_value; + } + + //return the formatted destinations + return $array; } - } //end the delete function + + /** + * delete records + */ + public function delete($records) { + if (permission_exists($this->permission_prefix.'delete')) { + + //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; + } + + //delete multiple records + if (is_array($records) && @sizeof($records) != 0) { + + //build the delete array + foreach ($records as $x => $record) { + if ($record['checked'] == 'true' && is_uuid($record['uuid'])) { + + //build delete array + $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $record['uuid']; + + //get the dialplan uuid and context + $sql = "select dialplan_uuid, destination_context from v_destinations "; + $sql .= "where destination_uuid = :destination_uuid "; + $parameters['destination_uuid'] = $record['uuid']; + $database = new database; + $row = $database->select($sql, $parameters, 'row'); + unset($sql, $parameters); + + //include dialplan in array + if (is_uuid($row['dialplan_uuid'])) { + $array['dialplan_details'][$x]['dialplan_uuid'] = $row["dialplan_uuid"]; + $array['dialplans'][$x]['dialplan_uuid'] = $row["dialplan_uuid"]; + $destination_contexts[] = $row['destination_context']; + } + + } + } + + //delete the checked rows + if (is_array($array) && @sizeof($array) != 0) { + + //grant temporary permissions + $p = new permissions; + $p->add('dialplan_delete', 'temp'); + $p->add('dialplan_detail_delete', 'temp'); + + //execute delete + $database = new database; + $database->app_name = $this->app_name; + $database->app_uuid = $this->app_uuid; + $database->delete($array); + unset($array); + + //revoke temporary permissions + $p->delete('dialplan_delete', 'temp'); + $p->delete('dialplan_detail_delete', 'temp'); + + //clear the cache + if (is_array($destination_contexts) && @sizeof($destination_contexts) != 0) { + $destination_contexts = array_unique($destination_contexts); + $cache = new cache; + foreach ($destination_contexts as $destination_context) { + $cache->delete("dialplan:".$destination_context); + } + } + + //set message + message::add($text['message-delete']); + + } + unset($records); + + } + } + } //method + + } //class } /* $obj = new destinations; @@ -554,4 +596,4 @@ echo $obj->select('ivr', 'example5', ''); echo $obj->select('ivr', 'example6', ''); */ -?> +?> \ No newline at end of file