From b9462f18171d4938ec82dc07841b2adbc8a938c5 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 9 Dec 2019 10:31:49 -0700 Subject: [PATCH] Streams: List view updates. --- app/streams/resources/classes/streams.php | 277 ++++++++++++++++----- app/streams/streams.php | 285 +++++++++++----------- 2 files changed, 356 insertions(+), 206 deletions(-) diff --git a/app/streams/resources/classes/streams.php b/app/streams/resources/classes/streams.php index e4248bda10..df78c3c7b7 100644 --- a/app/streams/resources/classes/streams.php +++ b/app/streams/resources/classes/streams.php @@ -1,43 +1,64 @@ - Portions created by the Initial Developer are Copyright (C) 2018 - the Initial Developer. All Rights Reserved. + The Initial Developer of the Original Code is + Mark J Crane + Portions created by the Initial Developer are Copyright (C) 2018-2019 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane */ -/** - * call_recordings class - * - * @method null download - */ +//define the streams class if (!class_exists('streams')) { class streams { /** - * Called when the object is created + * declare private variables + */ + private $app_name; + private $app_uuid; + private $permission_prefix; + private $list_page; + private $table; + private $uuid_prefix; + private $toggle_field; + private $toggle_values; + + /** + * called when the object is created */ public function __construct() { + //assign private variables + $this->app_name = 'streams'; + $this->app_uuid = 'ffde6287-aa18-41fc-9a38-076d292e0a38'; + $this->permission_prefix = 'stream_'; + $this->list_page = 'streams.php'; + $this->table = 'streams'; + $this->uuid_prefix = 'stream_'; + $this->toggle_field = 'stream_enabled'; + $this->toggle_values = ['true','false']; + } /** - * Called when there are no references to a particular object + * called when there are no references to a particular object * unset the variables used in the class */ public function __destruct() { @@ -47,57 +68,193 @@ if (!class_exists('streams')) { } /** - * delete streams + * delete records */ - public function delete($streams) { - if (permission_exists('stream_delete')) { + public function delete($records) { + if (permission_exists($this->permission_prefix.'delete')) { - //delete multiple streams - if (is_array($streams)) { - //get the action - foreach($streams as $row) { - if ($row['action'] == 'delete') { - $action = 'delete'; - break; + //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'])) { + $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $record['uuid']; + $array[$this->table][$x]['domain_uuid'] = $_SESSION['domain_uuid']; } } + //delete the checked rows - if ($action == 'delete') { - $x = 0; - foreach($streams as $row) { - if ($row['action'] == 'delete' or $row['checked'] == 'true') { - //build delete array - $array['streams'][$x]['stream_uuid'] = $row['stream_uuid']; - $x++; + if (is_array($array) && @sizeof($array) != 0) { + + //execute delete + $database = new database; + $database->app_name = $this->app_name; + $database->app_uuid = $this->app_uuid; + $database->delete($array); + unset($array); + + //set message + message::add($text['message-delete']); + } + unset($records); + } + } + } + + /** + * toggle records + */ + public function toggle($records) { + if (permission_exists($this->permission_prefix.'edit')) { + + //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'])) { + $uuids[] = "'".$record['uuid']."'"; + } + } + if (is_array($uuids) && @sizeof($uuids) != 0) { + $sql = "select ".$this->uuid_prefix."uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." "; + $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) "; + $sql .= "and ".$this->uuid_prefix."uuid in (".implode(', ', $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) { + $states[$row['uuid']] = $row['toggle']; } } - if (is_array($array) && @sizeof($array) != 0) { - //grant temporary permissions - $p = new permissions; - $p->add('stream_delete', 'temp'); - - //execute delete - $database = new database; - $database->app_name = 'streams'; - $database->app_uuid = 'ffde6287-aa18-41fc-9a38-076d292e0a38'; - $database->delete($array); - unset($array); - - //revoke temporary permissions - $p->delete('stream_delete', 'temp'); - } - unset($streams); + unset($sql, $parameters, $rows, $row); } + + //build update array + $x = 0; + foreach ($states as $uuid => $state) { + $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $uuid; + $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0]; + $x++; + } + + //save the changes + if (is_array($array) && @sizeof($array) != 0) { + + //save the array + $database = new database; + $database->app_name = $this->app_name; + $database->app_uuid = $this->app_uuid; + $database->save($array); + unset($array); + + //set message + message::add($text['message-toggle']); + } + unset($records, $states); } + + } + } + + /** + * copy records + */ + public function copy($records) { + if (permission_exists($this->permission_prefix.'add')) { + + //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; + } + + //copy the checked records + if (is_array($records) && @sizeof($records) != 0) { + + //get checked records + foreach ($records as $x => $record) { + if ($record['checked'] == 'true' && is_uuid($record['uuid'])) { + $uuids[] = "'".$record['uuid']."'"; + } + } + + //create insert array from existing data + if (is_array($uuids) && @sizeof($uuids) != 0) { + $sql = "select * from v_".$this->table." "; + $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) "; + $sql .= "and ".$this->uuid_prefix."uuid in (".implode(', ', $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 $x => $row) { + + //copy data + $array[$this->table][$x] = $row; + + //overwrite + $array[$this->table][$x][$this->uuid_prefix.'uuid'] = uuid(); + $array[$this->table][$x]['stream_description'] = trim($row['stream_description'].' ('.$text['label-copy'].')'); + + } + } + unset($sql, $parameters, $rows, $row); + } + + //save the changes and set the message + if (is_array($array) && @sizeof($array) != 0) { + + //save the array + $database = new database; + $database->app_name = $this->app_name; + $database->app_uuid = $this->app_uuid; + $database->save($array); + unset($array); + + //set message + message::add($text['message-copy']); + + } + unset($records); + } + } } } } -/* -$obj = new streams; -$obj->delete(); -*/ - ?> \ No newline at end of file diff --git a/app/streams/streams.php b/app/streams/streams.php index 58337c9faf..349b3f5857 100644 --- a/app/streams/streams.php +++ b/app/streams/streams.php @@ -25,6 +25,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('stream_view')) { @@ -39,32 +40,41 @@ $language = new text; $text = $language->get(); -//get the action - if (is_array($_POST["streams"])) { - $streams = $_POST["streams"]; - foreach($streams as $row) { - if ($row['action'] == 'delete') { - $action = 'delete'; +//get the http post data + if (is_array($_POST['streams'])) { + $action = $_POST['action']; + $search = $_POST['search']; + $streams = $_POST['streams']; + } + +//process the http post data by action + if ($action != '' && is_array($streams) && @sizeof($streams) != 0) { + switch ($action) { + case 'copy': + if (permission_exists('stream_add')) { + $obj = new streams; + $obj->copy($streams); + } + break; + case 'toggle': + if (permission_exists('stream_edit')) { + $obj = new streams; + $obj->toggle($streams); + } + break; + case 'delete': + if (permission_exists('stream_delete')) { + $obj = new streams; + $obj->delete($streams); + } break; - } } + + header('Location: streams.php'.($search != '' ? '?search='.urlencode($search) : null)); + exit; } -//delete the streams - if (permission_exists('stream_delete')) { - if ($action == "delete") { - //download - $obj = new streams; - $obj->delete($streams); - //delete message - message::add($text['message-delete']); - //redirect - header('Location: streams.php'); - exit; - } - } - -//get variables used to control the order +//get order and order by $order_by = $_GET["order_by"]; $order = $_GET["order"]; @@ -75,200 +85,183 @@ $sql_search .= "lower(stream_name) like :search "; $sql_search .= "or lower(stream_location) like :search "; $sql_search .= "or lower(stream_enabled) like :search "; - $sql_search .= "or lower(domain_uuid) like :search "; $sql_search .= "or lower(stream_description) like :search "; $sql_search .= ") "; $parameters['search'] = '%'.$search.'%'; } -//additional includes - require_once "resources/header.php"; - require_once "resources/paging.php"; - //prepare to page the results - $sql = "select count(*) from v_streams "; - $sql .= "where true "; + $sql = "select count(stream_uuid) from v_streams where true "; $sql .= $sql_search; if (!($_GET['show'] == "all" && permission_exists('stream_all'))) { $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) "; $parameters['domain_uuid'] = $domain_uuid; } $database = new database; - $num_rows = $database->select($sql, (is_array($parameters) && @sizeof($parameters) != 0 ? $parameters : null), 'column'); + $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('stream_all')) { - $param .= "&show=all"; - } - $page = $_GET['page']; + $param = ($_GET['show'] == 'all' && permission_exists('stream_all')) ? "&show=all" : null; + $page = is_numeric($_GET['page']) ? $_GET['page'] : 0; if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; } - list($paging_controls, $rows_per_page, $var3) = paging($num_rows, $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; //get the list - $sql = str_replace('count(*)', '*', $sql); - $sql .= order_by($order_by, $order); + $sql = str_replace('count(stream_uuid)', '*', $sql); + $sql .= order_by($order_by, $order, 'stream_name', 'asc'); $sql .= limit_offset($rows_per_page, $offset); $database = new database; $streams = $database->select($sql, (is_array($parameters) && @sizeof($parameters) != 0 ? $parameters : null), 'all'); unset($sql, $parameters); -//alternate 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']); + +//include header + require_once "resources/header.php"; //audio control styles echo "\n"; -//define the checkbox_toggle function - echo "\n"; - //show the content - echo "\n"; - echo " \n"; - echo " \n"; - 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'=>'streams.php','style'=>($search == '' ? 'display: none;' : null)]); + if ($paging_controls_mini != '') { + echo "".$paging_controls_mini."\n"; + } echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo "
".$text['title-streams']."
\n"; - + echo "
\n"; + echo "
".$text['title-streams']." (".$num_rows.")
\n"; + echo "
\n"; + if (permission_exists('stream_add')) { + echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'link'=>'stream_edit.php']); + } + if (permission_exists('stream_add') && $streams) { + echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'onclick'=>"if (confirm('".$text['confirm-copy']."')) { list_action_set('copy'); list_form_submit('form_list'); } else { this.blur(); return false; }"]); + } + if (permission_exists('stream_edit') && $streams) { + echo button::create(['type'=>'button','label'=>$text['button-toggle'],'icon'=>$_SESSION['theme']['button_icon_toggle'],'onclick'=>"if (confirm('".$text['confirm-toggle']."')) { list_action_set('toggle'); list_form_submit('form_list'); } else { this.blur(); return false; }"]); + } + if (permission_exists('stream_delete') && $streams) { + 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('stream_all')) { if ($_GET['show'] == 'all') { - echo " "; + echo " \n"; } else { - echo " \n"; + echo button::create(['type'=>'button','label'=>$text['button-show_all'],'icon'=>$_SESSION['theme']['button_icon_all'],'link'=>'?show=all']); } } - - echo " \n"; - echo " \n"; - echo "
\n"; - echo " ".$text['title_description-stream']."

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

\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + + echo "
\n"; - echo " \n"; - echo "
\n"; + echo "\n"; + if (permission_exists('stream_add') || permission_exists('stream_edit') || permission_exists('stream_delete')) { + echo " \n"; + } + if ($_GET['show'] == 'all' && permission_exists('stream_all')) { + echo th_order_by('domain_name', $text['label-domain'], $order_by, $order); } echo th_order_by('stream_name', $text['label-stream_name'], $order_by, $order); - echo " \n"; - //echo th_order_by('stream_location', $text['label-stream_location'], $order_by, $order); - echo th_order_by('stream_enabled', $text['label-stream_enabled'], $order_by, $order); - echo th_order_by('stream_description', $text['label-stream_description'], $order_by, $order); - echo " \n"; + echo th_order_by('stream_enabled', $text['label-stream_enabled'], $order_by, $order, null, "class='center'"); + echo th_order_by('stream_description', $text['label-stream_description'], $order_by, $order, null, "class='hide-sm-dn'"); + if (permission_exists('stream_edit') && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') { + echo " \n"; } - else { - echo " \n"; - } - echo " \n"; - echo "\n"; + echo "\n"; - if (is_array($streams)) { + if (is_array($streams) && @sizeof($streams) != 0) { $x = 0; - foreach($streams as $row) { + foreach ($streams as $row) { if (permission_exists('stream_edit')) { - $tr_link = "href='stream_edit.php?id=".escape($row['stream_uuid'])."'"; + $list_row_url = "stream_edit.php?id=".urlencode($row['stream_uuid']); } - echo "\n"; - echo " \n"; - if ($_GET['show'] == "all" && permission_exists('stream_all')) { - if (strlen($_SESSION['domains'][$row['domain_uuid']]['domain_name']) > 0) { - $domain = $_SESSION['domains'][$row['domain_uuid']]['domain_name']; + echo "\n"; + if (permission_exists('stream_add') || permission_exists('stream_edit') || permission_exists('stream_delete')) { + echo " \n"; + } + if ($_GET['show'] == 'all' && permission_exists('stream_all')) { + echo " \n"; + echo " \n"; } - echo " \n"; - - echo " \n"; + echo " \n"; - - //echo " \n"; - echo " \n"; - //echo " \n"; - echo " \n"; - echo " \n"; + echo " \n"; + if (permission_exists('stream_edit') && $_SESSION['theme']['list_row_edit_button']['boolean'] == 'true') { + echo " \n"; + } echo "\n"; $x++; - $c = $c ? 0 : 1; } } - unset($streams, $row); + unset($streams); - echo "\n"; - echo "\n"; - echo "\n"; - echo "
\n"; + echo " \n"; + echo " ".$text['label-play'].""; - if (permission_exists('stream_add')) { - echo " $v_link_label_add"; + echo " ".$text['label-play']." 
\n"; + echo " \n"; + echo " \n"; + echo " "; + if ($_SESSION['domains'][$row['domain_uuid']]['domain_name'] != '') { + echo escape($_SESSION['domains'][$row['domain_uuid']]['domain_name']); } else { - $domain = $text['label-global']; + echo $text['label-global']; } - echo " ".$domain."".escape($row['stream_name'])." \n"; + echo " \n"; + if (permission_exists('stream_edit')) { + echo " ".escape($row['stream_name'])."\n"; + } + else { + echo " ".escape($row['stream_name']); + } + echo " \n"; if (strlen($row['stream_location']) > 0) { $location_parts = explode('://',$row['stream_location']); if ($location_parts[0] == "shout") { - echo "".escape($row['stream_location'])." ".($row['stream_enabled'] == 'true' ? $text['label-true'] : $text['label-false'])."".escape($row['domain_uuid'])." ".escape($row['stream_description'])." "; if (permission_exists('stream_edit')) { - echo "$v_link_label_edit"; + echo " \n"; + echo $text['label-'.$row['stream_enabled']]; } echo " ".escape($row['stream_description'])." \n"; + echo button::create(['type'=>'button','title'=>$text['button-edit'],'icon'=>$_SESSION['theme']['button_icon_edit'],'link'=>$list_row_url]); + echo "
\n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo "
 $paging_controls"; - if (permission_exists('stream_add')) { - echo "$v_link_label_add"; - } - else { - echo " "; - } - echo "
\n"; - echo "
"; + echo "\n"; + echo "
\n"; + echo "
".$paging_controls."
\n"; + echo "\n"; echo "
\n"; - echo "

"; //include the footer require_once "resources/footer.php";