// SPDX-FileCopyrightText: 2021 Harald Eilertsen // // SPDX-License-Identifier: AGPL-3.0-or-later if (!class_exists("GiglogAdmin_ConcertsTable")) { class GiglogAdmin_ConcertsTable { const STATUS_LABELS = [ '', 'Accred Requested', 'Photo Approved', 'Text Approved', 'Photo and Text Approved', 'Rejected' ]; private string $username; public function __construct() { $this->username = wp_get_current_user()->user_login; } public function render(): string { return $this->render_filters() . $this->render_concerts_table(); } private function render_concert_table_header() : string { $content = '
' . '' . ' '; if (!is_admin()) { $content .= ''; } else { $content .= ''; if (current_user_can('administrator')) { $content .= ''; } } $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. $cty = filter_input( INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS ); if ($cty) { $filter['city'] = $cty; } $venue = filter_input( INPUT_POST, 'selectvenue', FILTER_SANITIZE_SPECIAL_CHARS ); if ($venue) { $filter['venue_id'] = $venue; } $smonth = filter_input( INPUT_POST, 'selectmonth', FILTER_SANITIZE_SPECIAL_CHARS ); if ($smonth) { $filter['month'] = $smonth; } if(isset($_POST['ownconcerts']) && $_POST['ownconcerts'] == '1') { $filter['currentuser'] = wp_get_current_user()->user_login; } if (isset($_GET['page_no']) && $_GET['page_no'] != "" && is_numeric($_GET['page_no'])) { $this->page_no = intval($_GET['page_no']); } else { $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 = ($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; 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 = ''; $content = $this->render_concert_table_header(); foreach ( $concerts AS $concert ) { $content .= ''; if ($lastType != '' && $lastType != $concert->venue()->city()) { $content .= ''; } if ($lastType == '' ) { $content .= ''; } if ($lastType != '' && $lastType == $concert->venue()->city()) { $content .= ''; } $fdate = strtotime($concert->cdate()); $newformat = date('d.M.Y',$fdate); //$content .= DATE_FORMAT($fdate,'%d.%b.%Y'); $content .= ''; $content .= ''; $content .= ''; if(!is_admin()){ $content .= ''; $content .= ''; } else { $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; if (current_user_can('administrator')) { $content .= ''; } } $content .= ''; $lastType = $concert->venue()->city(); } $content .= '
CITYDATENAMEVENUEEVENTTICKETSPHOTO1PHOTO2TEXT1TEXT2STATUSAdminOptions
' . $concert->venue()->city() . '' . $concert->venue()->city() . '' . $newformat . ''. $concert->cname() . '' . $concert->venue()->name() . 'LinkTickets' . $this->mark_new_concert($concert) . '' . $this->assign_role_for_user_form('photo1', $concert) . '' . $this->assign_role_for_user_form('photo2', $concert) . '' . $this->assign_role_for_user_form('rev1', $concert) . '' . $this->assign_role_for_user_form('rev2', $concert) . '' . self::STATUS_LABELS[$concert->status()] . '' . $this->adminactions($concert) . '
'; $content .= $this->render_pagination(); //from main form that includes filters $content .= '

'; // return the table return $content; } private function render_filters() : string { $cty = filter_input(INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS); $select = '

FILTER DATA: ' . \EternalTerror\ViewHelpers\select_field( "selectcity", array_map(fn($city) => [$city, $city], GiglogAdmin_Venue::all_cities()), $cty, "Select city..."); if ( !empty($cty) ) { //second drop down for venue $select .= \EternalTerror\ViewHelpers\select_field( "selectvenue", array_map( fn($venue) => [$venue->id(), $venue->name()], GiglogAdmin_Venue::venues_in_city($cty) ), filter_input(INPUT_POST, 'selectvenue', FILTER_SANITIZE_SPECIAL_CHARS), "Select venue..."); } $select.=' Filter by month: '; $select.= ''; if(is_admin()) { //option to select own concerts only $select .= ''; } //NOTE that I remvoed

and mvoed them up to render_concerts_table function $select .= ''; return $select; } private function adminactions( GiglogAdmin_Concert $concert ) : string { return '
' . '' . \EternalTerror\ViewHelpers\select_field( 'selectstatus', array_map(fn($i) => [ $i, self::STATUS_LABELS[$i] ], range(1, count(self::STATUS_LABELS) - 1)), $concert->status()) . '' . '' . '
'; } /** * Display a mark on the concert if it is new. * I.e. imported/created within the last ten days. * * @return null|string */ private function mark_new_concert(GiglogAdmin_Concert $concert) : string { $now = new DateTime(); $new_entry = $now->diff($concert->created())->days <= 10; if ($new_entry) { return 'NEW'; } else { return ''; } } private function assign_role_for_user_form(string $role, GiglogAdmin_Concert $concert) : ?string { $roles = $concert->roles(); $assigned_user = array_key_exists($role, $roles) ? $roles[$role] : NULL; //first check if current slot is taken by current user if ( $assigned_user == $this->username ) { $f = '
' . ' ' . ' ' . ' ' . '
'; } elseif ( $assigned_user ) { //check if slot is taken by another user $f = 'Taken' . '
Taken by ' . $assigned_user . '
'; } elseif ( array_search($this->username, $roles) ) { // other slots for this concert are taken by user $f = '-'; } else { //not taken by anyone $f = '
' . ' ' . ' ' . ' ' . '
'; } return $f; } } }