From 2fa078eaea0c0ca39fd4547cd34c9560cd56c540 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 12 Feb 2022 23:04:31 +0100 Subject: Refactor rendering of the concerts table. The main render function was getting too large and difficult to work with. Splitting it up a little to make it more manageable. Also fix styling to make the pagination links fall on one line a bit nicer. --- includes/admin/views/_concerts_table.php | 137 ++++++++++++++++++------------- 1 file changed, 79 insertions(+), 58 deletions(-) (limited to 'includes') diff --git a/includes/admin/views/_concerts_table.php b/includes/admin/views/_concerts_table.php index b34fef1..7edb1df 100644 --- a/includes/admin/views/_concerts_table.php +++ b/includes/admin/views/_concerts_table.php @@ -29,29 +29,31 @@ if (!class_exists("GiglogAdmin_ConcertsTable")) . $this->render_concerts_table(); } - private function render_concerts_table() : string + private function render_concert_table_header() : string { - $content = '
'; - // $content .= ''; - - $content .= ''; + $content = + '
CITYIDBANDVENUEDATE
CITYDATENAMEVENUE
' + . '' + . ' '; if (!is_admin()) { $content .= ''; } else { - $content .= ''; + $content .= ''; if (current_user_can('administrator')) { - $content .= ''; + $content .= ''; } } - $content .= ''; - - //pagination. Change value as needed - $total_records_per_page = 15; + $content .= ''; + return $content; + } + private function get_concerts() : ?array + { + $total_records_per_page = 15; $filter = []; // Use the submitted "city" if any. Otherwise, use the default/static value. @@ -74,34 +76,82 @@ if (!class_exists("GiglogAdmin_ConcertsTable")) $filter['currentuser'] = wp_get_current_user()->user_login; } - $concerts = GiglogAdmin_Concert::find_concerts($filter); - - - //get number of pages for pagination - $total_records = count($concerts); - $total_no_of_pages = ceil($total_records / $total_records_per_page); - $second_last = $total_no_of_pages - 1; // total pages minus 1 - - if (isset($_GET['page_no']) && $_GET['page_no']!="" && $_GET['page_no']<=$total_no_of_pages) { - $page_no = $_GET['page_no']; + if (isset($_GET['page_no']) && $_GET['page_no'] != "" && is_numeric($_GET['page_no'])) { + $this->page_no = intval($_GET['page_no']); } else { - $page_no = 1; + $this->page_no = 1; } + + $total_concerts = GiglogAdmin_Concert::count( $filter ); + $this->total_no_of_pages = ceil( $total_concerts / $total_records_per_page ); + //calculate OFFSET Value and SET other Variables - $offset = ($page_no-1) * $total_records_per_page; - $previous_page = $page_no - 1; - $next_page = $page_no + 1; - $adjacents = "2"; + $offset = ($this->page_no - 1) * $total_records_per_page; + $this->previous_page = $this->page_no - 1; + $this->next_page = $this->page_no + 1; $filter['offset'] = $offset; $filter['recperpage'] = $total_records_per_page; - $concertsp = GiglogAdmin_Concert::find_concerts($filter); + return GiglogAdmin_Concert::find_concerts($filter); + } + + private function render_pagination() : string + { + $content = + '
' + . ''; + + if($this->page_no > 1) { + $content .= + '' + . '' + . 'First Page -' + . '' + . '' + . '' + . ' Previous'; + } + + $content .= '' + . '' + // . '
' + . 'Page ' . $this->page_no . ' of ' . $this->total_no_of_pages . '' + //. '
' + . '
'; + + $content .= ''; + + if ($this->page_no < $this->total_no_of_pages) { + $content .= + '' + . '' + . 'Next - ' + . '' + . '' + . '' + . 'Last Page' + . ''; + } + $content .= + '' + . '
'; + + return $content; + } + + private function render_concerts_table() : string + { + //pagination. Change value as needed + + $concerts = $this->get_concerts(); $lastType = ''; - foreach ( $concertsp AS $concert ) { + $content = $this->render_concert_table_header(); + + foreach ( $concerts AS $concert ) { $content .= ''; if ($lastType != '' && $lastType != $concert->venue()->city()) { @@ -149,36 +199,7 @@ if (!class_exists("GiglogAdmin_ConcertsTable")) $content .= '
CITYDATENAMEVENUEEVENTTICKETS PHOTO1PHOTO2TEXT1TEXT2STATUSPHOTO1PHOTO2TEXT1TEXT2STATUSAdminOptionsAdminOptions
'; - $content.='
'; - $content.=''; - - if($page_no > 1) { - $content.= 'First Page - '; - } - - if($page_no <= 1) { - $content .=" "; - } - else { - $content.= ' Previous'; - } - - $content.=''; - $content.='
Page '.$page_no.' of '.$total_no_of_pages.'
'; - $content.=''; - - if ($page_no >= $total_no_of_pages) { - $content .= ""; - } - - if ($page_no < $total_no_of_pages) { - global $wp; - $content .= 'Next - '; - $content .= 'Last Page'; - } - - $content.=''; - $content.='
'; + $content .= $this->render_pagination(); //from main form that includes filters $content .= '

'; -- cgit v1.2.3