diff options
author | AndreaChirulescu <andrea.chirulescu@gmail.com> | 2021-06-15 18:34:29 +0200 |
---|---|---|
committer | AndreaChirulescu <andrea.chirulescu@gmail.com> | 2021-06-15 18:34:29 +0200 |
commit | 1a8155ea64352b0eaf088675d51547361f6f17a7 (patch) | |
tree | 7da669474e0896cefbec89a16b65413a7ed25cb1 | |
parent | bfe4eb5efd705b64943a0e1c0b7e18bfe0eee4d8 (diff) | |
parent | 82c4a8f2c4e5acd80b813829cecc40f621da3b21 (diff) | |
download | gigologadmin-1a8155ea64352b0eaf088675d51547361f6f17a7.tar.gz gigologadmin-1a8155ea64352b0eaf088675d51547361f6f17a7.tar.bz2 gigologadmin-1a8155ea64352b0eaf088675d51547361f6f17a7.zip |
Merge branch 'dev' of https://code.volse.net/wordpress/plugins/gigologadmin.git into andreaschanges
-rw-r--r-- | giglogadmin.php | 1 | ||||
-rw-r--r-- | includes/admin/register_db_tables.php | 18 | ||||
-rw-r--r-- | includes/admin/views/giglog_admin_page.php | 243 | ||||
-rw-r--r-- | includes/admin/views/giglog_import_gigs.php | 2 | ||||
-rw-r--r-- | includes/concert.php | 131 | ||||
-rw-r--r-- | includes/concertlogs.php | 86 | ||||
-rw-r--r-- | includes/venue.php | 4 | ||||
-rw-r--r-- | includes/view-helpers/select_field.php | 32 | ||||
-rw-r--r-- | tests/ConcertTest.php | 59 | ||||
-rw-r--r-- | tests/SelectFieldTest.php | 70 |
10 files changed, 453 insertions, 193 deletions
diff --git a/giglogadmin.php b/giglogadmin.php index c8ae9b0..c35c470 100644 --- a/giglogadmin.php +++ b/giglogadmin.php @@ -33,6 +33,7 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) { require_once __DIR__ . '/includes/admin/helpfiles/instrunctions.php'; require_once __DIR__ . '/includes/admin/helpfiles/instr_reviewers.php'; require_once __DIR__ . '/includes/admin/helpfiles/instr_photog.php'; + require_once __DIR__ . '/includes/view-helpers/select_field.php'; class GiglogAdmin_Plugin { diff --git a/includes/admin/register_db_tables.php b/includes/admin/register_db_tables.php index 11223b3..64123d9 100644 --- a/includes/admin/register_db_tables.php +++ b/includes/admin/register_db_tables.php @@ -260,7 +260,7 @@ if ( !function_exists( "giglog_register_db_tables") ) function giglog_register_db_tables() { $db_version = get_option('giglogadmin_db_version'); - if ($db_version == 5) { + if ($db_version == 6) { return; } @@ -445,7 +445,21 @@ if ( !function_exists( "giglog_register_db_tables") ) "ALTER TABLE `wpg_concerts` DROP FOREIGN KEY `wpgconcert_band`;"); } - update_option("giglogadmin_db_version", 5); + if ($db_version == NULL || $db_version < 6) + { + /* + * Move status and roles from concertlogs to main concerts table + * Don't really see the value in a separate table for these items + * Also make the roles fiels a JSON field instead of separate + * fields for each role. + */ + $wpdb->query( + "ALTER TABLE `wpg_concerts` ADD COLUMN IF NOT EXISTS ( + wpgconcert_status INT DEFAULT 1, + wpgconcert_roles JSON CHECK (JSON_VALID(wpgconcert_roles)))"); + } + + update_option("giglogadmin_db_version", 6); } giglog_register_db_tables(); diff --git a/includes/admin/views/giglog_admin_page.php b/includes/admin/views/giglog_admin_page.php index 9d9d759..6c1f059 100644 --- a/includes/admin/views/giglog_admin_page.php +++ b/includes/admin/views/giglog_admin_page.php @@ -9,7 +9,21 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { require_once __DIR__ . '/../../venue.php'; class GiglogAdmin_AdminPage { - static function render_html(): void { + private string $username; + + public function __construct() + { + $this->username = wp_get_current_user()->user_login; + } + + public static function render_html() : void + { + $page = new self(); + $page->render_page(); + } + + public function render_page() : void + { ?> <div class="wrap"> <h1>Giglog Admin</h1> @@ -28,96 +42,60 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { what the accreditation status is. You will get personal message if this is really close to the concert date.</p> - <p><?php echo GiglogAdmin_AdminPage::get_filters() ?></p> - <p><?php echo GiglogAdmin_AdminPage::get_concerts() ?></p> + <p><?php echo $this->get_filters() ?></p> + <p><?php echo $this->get_concerts() ?></p> </div> <?php if (current_user_can('administrator')) echo(GiglogAdmin_AdminPage::editforms()); //not sure why it doesn't show without the echo? } - static function get_venue_selector( ?GiglogAdmin_Venue $invenue ): string + private function get_venue_selector( ?GiglogAdmin_Venue $invenue ): string { - $select = '<select name="selectvenueadmin">'; - $select .= '<option value="">Please Select..</option>'; - foreach ( GiglogAdmin_Venue::all_venues() AS $venue ) { - if($invenue && $invenue->id() == $venue->id() ) { - $select .= '<option value="' . $venue->id(). '" selected="selected">'.$venue->name(); - } - else { - $select .= '<option value="' . $venue->id() . '">'. $venue->name(); - } - $select .='</option>'; - } - $select .= '</select>'; - return($select); + return \EternalTerror\ViewHelpers\select_field( + "selectvenueadmin", + array_map(fn($venue) => [$venue->id(), $venue->name()], GiglogAdmin_Venue::all_venues()), + $invenue ? $invenue->id() : null); } - static function get_user( ?int $cid, string $ctype): string + private function get_user( ?int $cid, string $ctype): string { - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; - $select = '<select name="'.$ctype.'">'; - $select .= '<option value="">Please Select..</option>'; - $users = get_users( array( 'fields' => array( 'user_login' ) ) ); - foreach ( $users as $user ) { - $usr = $user->user_login; - $taken = strpos(GiglogAdmin_AdminPage::returnuser($ctype, $cid),$usr); - if($taken) $select .= '<option value="' .$usr. '" selected="selected">'.$usr; - else - { - $takenbyself = strpos(GiglogAdmin_AdminPage::returnuser($ctype, $cid),'name="unassignitem"'); - if($takenbyself && $usr==$hf_username) $select .= '<option value="' .$usr. '" selected="selected">'.$usr; - else - $select .= '<option value="'.$usr. '">'. $usr; - $select .='</option>'; - } - } - $select .= '</select>'; - return($select); - } + $users = array_map( + fn($usr) => $usr->user_login, + get_users( array( 'fields' => array( 'user_login' ) ) ) ); + $current_user = $cid ? GiglogAdmin_Concertlogs::get_assigned_user( $cid, $ctype ) : null; - static function get_filters(): string - { - $cities = array_merge(["ALL"], GiglogAdmin_Venue::all_cities()); - $cty = filter_input( INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS ); - $selected_city = - filter_input(INPUT_POST, "selectcity", FILTER_SANITIZE_SPECIAL_CHARS) - || $cities[0]; + return \EternalTerror\ViewHelpers\select_field( + $ctype, + array_map( fn($user) => [ $user, $user ], $users ), + $current_user); + } - $select = '<form method="POST" action="">FILTER DATA: <select name="selectcity">'; - foreach ( $cities AS $city ) { - $select .= '<option value="' . $city . '"' . selected($city,$cty ) . '>'; - $select .= $city . '</option>'; - } + private function get_filters() : string + { + $cty = filter_input(INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS); - $select .= '</select>'; + $select = '<form method="POST" action="">FILTER DATA:'; + $select .= \EternalTerror\ViewHelpers\select_field( + "selectcity", + array_map(fn($city) => [$city, $city], GiglogAdmin_Venue::all_cities()), + $cty, + "Select city..."); - if ( $cty && $cty!= "ALL" ) { + if ( !empty($cty) ) { //second drop down for venue - $venues = GiglogAdmin_Venue::venues_in_city($cty); - - $venue_list = array_merge( - [0, "ALL"], + $select .= \EternalTerror\ViewHelpers\select_field( + "selectvenue", array_map( - function($v) { return [$v->id(), $v->name()]; }, - $venues)); - - $selected_venue = filter_input(INPUT_POST, "selectvenue", FILTER_SANITIZE_SPECIAL_CHARS) - || $venue_list[0]; - - $select .= '<select name="selectvenue">'; - - foreach ( $venue_list as $venue ) { - $select .= '<option value="' . $venue[0] . '"' . selected($venue, $selected_venue) . '>'; - $select .= $venue[1] . '</option>'; - } - $select .= '</select>'; - + fn($venue) => [$venue->id(), $venue->name()], + GiglogAdmin_Venue::venues_in_city($cty) + ), + filter_input(INPUT_POST, 'selectvenue', FILTER_SANITIZE_SPECIAL_CHARS), + "Select venue..."); } //option to select own concerts only $select .= '<input class="ownconc" type="checkbox" value="1"'; @@ -129,7 +107,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return $select; } - static function editforms(): string + private function editforms(): string { $cid = filter_input(INPUT_POST, "cid"); $editing = filter_input(INPUT_POST, "edit") == "EDIT"; @@ -144,7 +122,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { .'<div class="concertitems"><strong>CONCERT DETAILS</strong><br><br><fieldset>' .'<input type="hidden" name="pid" value="' .$c->id(). '" />' .'<label for="cname">Concert Name:</label><textarea id="cname" name="cname" value="'.$c->cname().'">'.$c->cname().'</textarea><br>' - .'<label for="venue">Venue:</label>' . GiglogAdmin_AdminPage::get_venue_selector($c->venue()) . '<br>' + .'<label for="venue">Venue:</label>' . $this->get_venue_selector($c->venue()) . '<br>' .'<label for="cdate">Date:</label><input type="date" id="cdate" name="cdate" value="'.$c->cdate().'"><br>' .'<label for="ticket">Tickets:</label><input type="text" id="ticket" name="ticket" value="'.$c->tickets().'"><br>' .'<label for="eventurl">Event link:</label><input type="text" id="eventurl" name="eventurl" value="'.$c->eventlink().'"><br>' @@ -157,10 +135,10 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $content.='</div>'; $content.='<div class="useritems"><strong>ASSIGNMENT DETAILS</strong><br><br><fieldset>' - .'<label for="photo1">Photo1:</label>'.GiglogAdmin_AdminPage::get_user($c->id(),'photo1').'<br>' - .'<label for="photo2">Photo2:</label>'.GiglogAdmin_AdminPage::get_user($c->id(),'photo2').'<br>' - .'<label for="rev1">Text1:</label>'.GiglogAdmin_AdminPage::get_user($c->id(),'rev1').'<br>' - .'<label for="rev2">Text2:</label>'.GiglogAdmin_AdminPage::get_user($c->id(),'rev2').'<br>'; + .'<label for="photo1">Photo1:</label>'.$this->get_user($c->id(),'photo1').'<br>' + .'<label for="photo2">Photo2:</label>'.$this->get_user($c->id(),'photo2').'<br>' + .'<label for="rev1">Text1:</label>'.$this->get_user($c->id(),'rev1').'<br>' + .'<label for="rev2">Text2:</label>'.$this->get_user($c->id(),'rev2').'<br>'; $content.='<fieldset></div></form></div>'; $content.='<div class="venueform"><form method="POST" action="" class="venue" ><strong>VENUE DETAILS</strong><br><br>' @@ -172,35 +150,29 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { return $content; } - static function adminactions( int $concert_id ) : string + private function adminactions( int $concert_id ) : string { global $wpdb; $query = "SELECT id,wpgs_name from wpg_pressstatus" ; $statuses = $wpdb->get_results($query); - $select = + return '<form method="POST" action="">' . '<input type="hidden" name="cid" value="' . $concert_id . '" />' - . '<select name="selectstatus">'; - - foreach ( $statuses AS $sts ) { - $select .= '<option value="' . $sts->id . '">' . $sts->wpgs_name . '</option>'; - } - - $select .= - '</select>' + . \EternalTerror\ViewHelpers\select_field( + 'selectstatus', + array_map(fn($status) => [ $status->id, $status->wpgs_name ], $statuses), + GiglogAdmin_Concertlogs::get_status($concert_id)) . '<input type="submit" value="SetStatus">' . '<input type="submit" name ="edit" value="EDIT">' . '</form>'; - - return $select; } //function to calculate if the concert has been added in the past 10 days or before that and show a green NEW for the newest rows /** * @return null|string */ - static function getpublishstatus(int $concert_id) + private function getpublishstatus(int $concert_id) { global $wpdb; $date1 = new DateTime("now"); @@ -217,11 +189,8 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } - static function get_concerts(): string + private function get_concerts(): string { - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; - $roles = $hf_user->roles; global $wpdb; $content = '<table class="assignit">'; @@ -231,7 +200,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { <th>CITY</th><th>NAME</th><th>VENUE</th><th>DATE</th><th> </th> <th>PHOTO1</th><th>PHOTO2</th><th>TEXT1</th><th>TEXT2</th> <th>STATUS</th>'; - if (current_user_can('administrator')) //($hf_username == 'etadmin') + if (current_user_can('administrator')) $content .= '<th>AdminOptions</th>'; $content .= '</tr>'; @@ -252,7 +221,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { $query .= ($cty == "ALL") ? "" : " and wpgv.wpgvenue_city='" .$cty ."'"; $query .= ($venue == "0") ? "" : " and wpgv.id='" .$venue ."'"; - $query.= (empty($_POST['my_checkbox'])) ? "": " and (wpgcl_photo1 ='".$hf_username."' or wpgcl_photo2 ='".$hf_username."' or wpgcl_rev1 ='".$hf_username."' or wpgcl_rev2 ='".$hf_username."')"; + $query.= (empty($_POST['my_checkbox'])) ? "": " and (wpgcl_photo1 ='".$this->username."' or wpgcl_photo2 ='".$this->username."' or wpgcl_rev1 ='".$this->username."' or wpgcl_rev2 ='".$this->username."')"; $query .=" order by wpgv.wpgvenue_city, wpgconcert_date, wpgc.id" ; $results = $wpdb->get_results($query); $lastType = ''; @@ -277,17 +246,17 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { //$content .= DATE_FORMAT($fdate,'%d.%b.%Y'); $content .= '<td>' .$newformat. '</td>'; - $content .= '<td>'.GiglogAdmin_AdminPage::getpublishstatus($row->id ).'</td>'; - $content .= '<td>'.GiglogAdmin_AdminPage::returnuser('photo1', $row->id ).'</td>'; - $content .= '<td>'.GiglogAdmin_AdminPage::returnuser('photo2', $row->id ).'</td>'; - $content .= '<td>'.GiglogAdmin_AdminPage::returnuser('rev1', $row->id ).'</td>'; - $content .= '<td>'.GiglogAdmin_AdminPage::returnuser('rev2', $row->id ).'</td>'; + $content .= '<td>'.$this->getpublishstatus($row->id ).'</td>'; + $content .= '<td>'.$this->returnuser('photo1', $row->id ).'</td>'; + $content .= '<td>'.$this->returnuser('photo2', $row->id ).'</td>'; + $content .= '<td>'.$this->returnuser('rev1', $row->id ).'</td>'; + $content .= '<td>'.$this->returnuser('rev2', $row->id ).'</td>'; $content .= '<td>'.$row -> wpgs_name.'</td>'; if (current_user_can('administrator')) { $content .= '<td class="adminbuttons">' - . GiglogAdmin_AdminPage::adminactions($row->id) + . $this->adminactions($row->id) . '</td>'; } $content .= '</tr>'; @@ -334,7 +303,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { } //handling the admin drop down menu - if(isset($_POST['selectstatus']) && $_POST['edit']!="EDIT" && !empty($_POST['cid'])) + if(isset($_POST['selectstatus']) && (isset($_POST['edit']) && $_POST['edit']!="EDIT") && !empty($_POST['cid'])) { $usql = "UPDATE wpg_concertlogs SET wpgcl_status=".$_POST['selectstatus']." WHERE wpgcl_concertid=".$_POST['cid']; $uresults = $wpdb->get_results($usql); @@ -363,7 +332,7 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { else { GiglogAdmin_Concert::update_concert($_POST['pid'],$_POST['cname'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl']); - GiglogAdmin_Concert::update_concertlog($_POST['pid'],$_POST['photo1'], $_POST['photo2'], $_POST['rev1'], $_POST['rev2']); + GiglogAdmin_Concertlogs::update($_POST['pid'],$_POST['photo1'], $_POST['photo2'], $_POST['rev1'], $_POST['rev2']); echo '<script language="javascript">alert("Yay, concert updated"); </script>'; } @@ -386,17 +355,15 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { { global $wpdb; - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; $to = 'live@eternal-terror.com'; - $subject = $hf_username.' has taken '.$p1. 'for a concert with id '.$c; + $subject = $this->username.' has taken '.$p1. 'for a concert with id '.$c; $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); - $usql = "UPDATE wpg_concertlogs SET wpgcl_".$p1."='".$hf_username."' WHERE wpgcl_concertid=".$c; + $usql = "UPDATE wpg_concertlogs SET wpgcl_".$p1."='".$this->username."' WHERE wpgcl_concertid=".$c; $uresults = $wpdb->get_results($usql); $wpdb->insert( 'wpg_logchanges', array ( 'id' => '', - 'userid' => $hf_username, + 'userid' => $this->username, 'action' => 'assigned '.$p1, 'concertid' => $c)); echo ($wpdb->last_error ); @@ -407,48 +374,56 @@ if ( !class_exists( 'GiglogAdmin_AdminPage' ) ) { { global $wpdb; - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; $to = 'live@eternal-terror.com'; - $subject = $hf_username.' has UNASSINED '.$p1. 'for a concert with id '.$c; + $subject = $this->username.' has UNASSINED '.$p1. 'for a concert with id '.$c; $body = 'The email body content'; $headers = array('Content-Type: text/html; charset=UTF-8'); $usql = "UPDATE wpg_concertlogs SET wpgcl_".$p1."='' WHERE wpgcl_concertid=".$c; $uresults = $wpdb->get_results($usql); $wpdb->insert( 'wpg_logchanges', array ( 'id' => '', - 'userid' => $hf_username, + 'userid' => $this->username, 'action' => 'unassigned '.$p1, 'concertid' => $c)); echo ($wpdb->last_error ); wp_mail( $to, $subject, $body, $headers ); } - static function returnuser(string $p1, ?int $c): string + private function returnuser(string $p1, ?int $c) : ?string { - global $wpdb; - $hf_user = wp_get_current_user(); - $hf_username = $hf_user->user_login; - + if (!$c) { + return null; + } - if (!empty($c)) - { - $sql = "select * from wpg_concertlogs where wpgcl_concertid=".$c; - $crow = $wpdb->get_results($sql); - $array = array('photo1' => $crow[0]->wpgcl_photo1, 'photo2'=> $crow[0]->wpgcl_photo2, 'rev1' => $crow[0]->wpgcl_rev1, 'rev2'=> $crow[0]->wpgcl_rev2); - - //first check if current slot is taken by current user - if ($array[$p1] == $hf_username) return ('<form class="unassignit" method="POST" action=""> <input type="hidden" name="cid" value="' . $c. '" /><input type="hidden" name="pid" value="' . $p1. '" /><input type="submit" name="unassignitem" value="Your"/></form>'); - else //check if slot is taken by another user - if (!empty($array[$p1])) return ('<span class="takenby">Taken</span><div class="takenby">Taken by '.$array[$p1].'</div>'); - else //check if other slots for this concert are taken by user - if (in_array($hf_username,$array)) return ('<span class="taken_by_self">-</span>'); - else //not taken by anyone - return ('<form method="POST" action=""> <input type="hidden" name="cid" value="' . $c. '" /><input type="hidden" name="pid" value="' . $p1. '" /><input type="submit" name="assignitem" value=""/> - </form>'); + $cl = GiglogAdmin_Concertlogs::get($c); + $role = $cl->get_assigned_role( $this->username ); + $assigned_user = $cl->assigned_user( $p1 ); + + //first check if current slot is taken by current user + if ( $role == $p1 ) { + $f = '<form class="unassignit" method="POST" action="">' + . ' <input type="hidden" name="cid" value="' . $c. '" />' + . ' <input type="hidden" name="pid" value="' . $p1. '" />' + . ' <input type="submit" name="unassignitem" value="Your"/>' + . '</form>'; + } + elseif ( $assigned_user ) { //check if slot is taken by another user + $f = '<span class="takenby">Taken</span>' + . '<div class="takenby">Taken by ' . $assigned_user . '</div>'; + } + elseif ( $role ) { + // other slots for this concert are taken by user + $f = '<span class="taken_by_self">-</span>'; + } + else { //not taken by anyone + $f = '<form method="POST" action="">' + . ' <input type="hidden" name="cid" value="' . $c. '" />' + . ' <input type="hidden" name="pid" value="' . $p1. '" />' + . ' <input type="submit" name="assignitem" value=""/>' + . '</form>'; } - else return ('no concert selected'); + return $f; } } } diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index a8daa3f..aeaf974 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -51,7 +51,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { * * @return void * - * @param ArrayAccess|array $file + * @param array<int, mixed> */ static function process_upload(array $file): void { $newconcert= []; diff --git a/includes/concert.php b/includes/concert.php index 050c924..88a20da 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -6,6 +6,8 @@ // SPDX-License-Identifier: AGPL-3.0-or-later if ( !class_exists('GiglogAdmin_Concert') ) { + require_once __DIR__ . '/venue.php'; + class GiglogAdmin_Concert { private $id; @@ -14,6 +16,15 @@ if ( !class_exists('GiglogAdmin_Concert') ) { private $cdate; private $tickets; private $eventlink; + private int $status; + private array $roles; + + public const STATUS_NONE = 1; + public const STATUS_ACCRED_REQ = 2; + public const STATUS_PHOTO_APPROVED = 3; + public const STATUS_TEXT_APPROVED = 4; + public const STATUS_ALL_APPROVED = 5; + public const STATUS_REJECTED = 6; /* * Constructs a new concert object from an array of attributes. @@ -28,7 +39,8 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $this->cdate = isset($attrs->wpgconcert_date) ? $attrs->wpgconcert_date : NULL; $this->tickets = isset($attrs->wpgconcert_tickets) ? $attrs->wpgconcert_tickets : NULL; $this->eventlink = isset($attrs->wpgconcert_event) ? $attrs->wpgconcert_event : NULL; - + $this->status = isset($attrs->wpgconcert_status) ? $attrs->wpgconcert_status : 1; + $this->roles = isset($attrs->wpgconcert_roles) ? json_decode($attrs->wpgconcert_roles, true) : []; if ( isset( $attrs->venue ) ) { if (isset($attrs->wpgvenue_name) && isset($attrs->wpgvenue_city)) { @@ -63,6 +75,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { . 'WHERE ' . $wpdb->prepare('wpg_concerts.id = %d', $id); $results = $wpdb->get_results($query); + var_dump($results); return $results ? new GiglogAdmin_Concert($results[0]) : NULL; } @@ -126,36 +139,13 @@ if ( !class_exists('GiglogAdmin_Concert') ) { array('id' => $id) ); - if ( !$res ) { - // exit( var_dump( $wpdb->last_query ) ); //for onscreen debugging when needed - error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); - die; - } - - return ($wpdb->last_error); - } - - static function update_concertlog($cid, $ph1, $ph2, $rev1, $rev2) - { - global $wpdb; - - $res = $wpdb->update('wpg_concertlogs', array( - 'wpgcl_photo1' => $ph1, - 'wpgcl_photo2' => $ph2, - 'wpgcl_rev1' => $rev1, - 'wpgcl_rev2' => $rev2 - ), - array('wpgcl_concertid' => $cid) - ); - - if ( !$res ) { + if ( $res === false ) { // exit( var_dump( $wpdb->last_query ) ); //for onscreen debugging when needed error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); die; } return ($wpdb->last_error); - } public static function find($cname, $venue, $date) @@ -171,48 +161,78 @@ if ( !class_exists('GiglogAdmin_Concert') ) { } - public static function find_concerts_in(string $city) : array + /** + * Return an array of concert objects optionally limited by a specified + * filter. + * + * Valid filters are: + * - 'venue_id' => int : only include concerts at the given venue + * - 'city' => string : only include concerts in the given city + * + * @param array<string, mixed> $filter + * @return array<GiglogAdmin_Concert> + */ + public static function find_concerts(array $filter = []) : array { global $wpdb; $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' . 'FROM wpg_concerts ' - . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' - . 'WHERE wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $city); + . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id '; - $results = $wpdb->get_results($query); + $where = []; - return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); - } + if ( isset( $filter["city"] ) ) { + array_push($where, 'wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $filter["city"])); + } - public static function find_concerts_at(GiglogAdmin_Venue $venue) : array - { - global $wpdb; + if ( isset( $filter["venue_id"] ) ) { + array_push($where, 'wpg_venues.id = ' . $wpdb->prepare('%s', $filter["venue_id"])); + } - $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city ' - . 'FROM wpg_concerts ' - . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ' - . 'WHERE wpg_concerts.venue = ' . $wpdb->prepare('%d', $venue->id()); + if ( ! empty( $where ) ) { + $query .= 'WHERE ' . implode(' and ', $where); + } $results = $wpdb->get_results($query); return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results); } - public function save(): void + public function save() : void { global $wpdb; - $wpdb->insert('wpg_concerts', array( - 'id' => '', - 'wpgconcert_name' => $this->cname, - 'venue' => $this->venue->id(), - 'wpgconcert_date' => $this->cdate, - 'wpgconcert_tickets' => $this->tickets, - 'wpgconcert_event' => $this->eventlink - )); + if ( $this->id !== NULL ) { + $res = $wpdb->update('wpg_concerts', array( + 'id' => $this->id, + 'wpgconcert_name' => $this->cname, + 'venue' => $this->venue->id(), + 'wpgconcert_date' => $this->cdate, + 'wpgconcert_tickets' => $this->tickets, + 'wpgconcert_event' => $this->eventlink, + 'wpgconcert_status' => $this->status, + ), + array( 'id' => $this->id ) ); + + } + else { + $res = $wpdb->insert('wpg_concerts', array( + 'wpgconcert_name' => $this->cname, + 'venue' => $this->venue->id(), + 'wpgconcert_date' => $this->cdate, + 'wpgconcert_tickets' => $this->tickets, + 'wpgconcert_event' => $this->eventlink, + 'wpgconcert_status' => $this->status, + )); + } - $this->id = $wpdb->insert_id; + if ( $res === false ) { + $wpdb->print_error( __METHOD__ ); + } + else { + $this->id = $wpdb->insert_id; + } } public function id() @@ -240,6 +260,21 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { return $this->eventlink; } + + public function status() + { + return $this->status; + } + + public function set_status( int $new_status ) + { + $this->status = $new_status; + } + + public function roles() + { + return $this->roles; + } } } ?> diff --git a/includes/concertlogs.php b/includes/concertlogs.php index 3b8083a..8b1ca63 100644 --- a/includes/concertlogs.php +++ b/includes/concertlogs.php @@ -9,6 +9,16 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) { class GiglogAdmin_Concertlogs { + private array $roles; + + private function __construct( $attr = [] ) + { + $this->roles['photo1'] = $attr->{"wpgcl_photo1"}; + $this->roles['photo2'] = $attr->{"wpgcl_photo2"}; + $this->roles['rev1'] = $attr->{"wpgcl_rev1"}; + $this->roles['rev2'] = $attr->{"wpgcl_rev2"}; + } + /** * Adds a default entry for the given concert id in the * concert logs table. @@ -25,5 +35,81 @@ if ( !class_exists( 'GiglogAdmin_Concertlogs' ) ) $wpdb->query($q); } + + static function update($cid, $ph1, $ph2, $rev1, $rev2) + { + global $wpdb; + + $res = $wpdb->update('wpg_concertlogs', array( + 'wpgcl_photo1' => $ph1, + 'wpgcl_photo2' => $ph2, + 'wpgcl_rev1' => $rev1, + 'wpgcl_rev2' => $rev2 + ), + array('wpgcl_concertid' => $cid) + ); + + if ( !$res ) { + // exit( var_dump( $wpdb->last_query ) ); //for onscreen debugging when needed + error_log( __CLASS__ . '::' . __FUNCTION__ . ": {$wpdb->last_error}"); + die; + } + + return ($wpdb->last_error); + } + + public static function get_status(int $concert_id) : ?int + { + global $wpdb; + + $q = $wpdb->prepare( + 'select wpgcl_status from wpg_concertlogs where id = %d', + $concert_id); + $res = $wpdb->get_results($q); + + return $res ? $res[0]->wpgcl_status : null; + } + + public static function get_assigned_user( int $concert_id, string $role ) : ?string + { + global $wpdb; + + if ( ! in_array( $role, [ 'photo1', 'photo2', 'rev1', 'rev2' ] ) ) { + error_log(__METHOD__ . ": invalid \$role ({$role}) given."); + return null; + } + + $column = "wpgcl_{$role}"; + $q = $wpdb->prepare( + "select {$column} from wpg_concertlogs where id = %d", + $concert_id); + + $res = $wpdb->get_row($q, ARRAY_A); + + return array_shift( $res ); + } + + public static function get(int $concert_id) : ?self + { + global $wpdb; + + $q = $wpdb->prepare( + "select * from wpg_concertlogs where id = %d", + $concert_id); + + $res = $wpdb->get_row($q); + + return $res ? new self($res) : null; + } + + public function get_assigned_role(string $username) : ?string + { + return array_search( $username, $this->roles ) || NULL; + } + + public function assigned_user(string $role) : ?string + { + return $this->roles[$role]; + } } } diff --git a/includes/venue.php b/includes/venue.php index f9f7e4f..aedb6a7 100644 --- a/includes/venue.php +++ b/includes/venue.php @@ -31,8 +31,8 @@ if ( !class_exists('GiglogAdmin_Venue') ) { /** * Get venue by given id. * - * @param int $id. - * @return null|self. + * @param int $id + * @return null|self */ static function get(int $id) : ?self { diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php new file mode 100644 index 0000000..816f8ef --- /dev/null +++ b/includes/view-helpers/select_field.php @@ -0,0 +1,32 @@ +<?php +// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com> +// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +namespace EternalTerror\ViewHelpers; + +/** + * Return HTML code for a selction box with the given options and preselected value. + * + * @param string $name The name attribute for the selection box + * @param array $opts The options as arrays of [value, label] pairs + * @param mixed|int $selected The value of the preselected option, or null if no + * option is preselected. + * @param string $blank Text to use for "no selection", defaults to "Please + * select..." + * @return string + */ +function select_field( + string $name, + ?array $opts = [], + $selected = null, + string $blank = "Please select...") : string +{ + $body = "<option value=\"\">{$blank}</option>"; + foreach ($opts as $opt) { + $sel = selected($selected, $opt[0], false); + $body .= "<option value=\"{$opt[0]}\"{$sel}>{$opt[1]}</option>"; + } + return "<select name=\"{$name}\">{$body}</select>"; +} diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 96fc72b..7edda46 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -86,6 +86,30 @@ final class ConcertTest extends WP_UnitTestCase $this->assertEquals($gig->id(), $fetched_gig->id()); $this->assertEquals($gig->cname(), $fetched_gig->cname()); $this->assertEquals($venue->id(), $fetched_gig->venue()->id()); + $this->assertEquals(GiglogAdmin_Concert::STATUS_NONE, $fetched_gig->status()); + $this->assertEquals([], $fetched_gig->roles()); + } + + public function testSetConcertStatus() : void + { + $venue = GiglogAdmin_Venue::create("a venue"); + $today = date("Y-m-d"); + + $gig = GiglogAdmin_Concert::create( + "a concert123", + $venue->id(), + $today, + "https://example.com/tickets/42", + "https://example.com/events/93"); + + $fetched_gig = GiglogAdmin_Concert::get($gig->id()); + $fetched_gig->set_status( GiglogAdmin_Concert::STATUS_ACCRED_REQ ); + $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig->status() ); + + $fetched_gig->save(); + + $fetched_gig_2 = GiglogAdmin_Concert::get($gig->id()); + $this->assertEquals( GiglogAdmin_Concert::STATUS_ACCRED_REQ, $fetched_gig_2->status() ); } public function testOnlyFetchConcertsFromGivenCity() : void @@ -106,7 +130,7 @@ final class ConcertTest extends WP_UnitTestCase GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); } - $gigs_in_svene = GiglogAdmin_Concert::find_concerts_in("Svene"); + $gigs_in_svene = GiglogAdmin_Concert::find_concerts([ "city" => "Svene"]); $this->assertEquals(4, count($gigs_in_svene)); while ($gig = array_pop($gigs_in_svene)) { @@ -114,14 +138,14 @@ final class ConcertTest extends WP_UnitTestCase } - $gigs_in_oslo = GiglogAdmin_Concert::find_concerts_in("Oslo"); + $gigs_in_oslo = GiglogAdmin_Concert::find_concerts(["city" => "Oslo"]); $this->assertEquals(2, count($gigs_in_oslo)); while ($gig = array_pop($gigs_in_oslo)) { $this->assertEquals("Oslo", $gig->venue()->city()); } - $gigs_in_sogndal = GiglogAdmin_Concert::find_concerts_in("Sogndal"); + $gigs_in_sogndal = GiglogAdmin_Concert::find_concerts(["city" => "Sogndal"]); $this->assertEquals(5, count($gigs_in_sogndal)); while ($gig = array_pop($gigs_in_sogndal)) { @@ -147,25 +171,48 @@ final class ConcertTest extends WP_UnitTestCase GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); } - $gigs_at_ss = GiglogAdmin_Concert::find_concerts_at($venue1); + $gigs_at_ss = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue1->id()]); $this->assertEquals(4, count($gigs_at_ss)); while ($gig = array_pop($gigs_at_ss)) { $this->assertEquals("Sentrum Scene", $gig->venue()->name()); } - $gigs_at_rmh = GiglogAdmin_Concert::find_concerts_at($venue2); + $gigs_at_rmh = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue2->id()]); $this->assertEquals(2, count($gigs_at_rmh)); while ($gig = array_pop($gigs_at_rmh)) { $this->assertEquals("Rockefeller Music Hall", $gig->venue()->name()); } - $gigs_at_r = GiglogAdmin_Concert::find_concerts_at($venue3); + $gigs_at_r = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue3->id()]); $this->assertEquals(5, count($gigs_at_r)); while ($gig = array_pop($gigs_at_r)) { $this->assertEquals("Revolver", $gig->venue()->name()); } } + + public function testFetchAllConcerts() : void + { + $venue1 = GiglogAdmin_Venue::create("Svene Bedehus", "Svene"); + $venue2 = GiglogAdmin_Venue::create("Rockefeller Music Hall", "Oslo"); + $venue3 = GiglogAdmin_Venue::create("Meieriet", "Sogndal"); + + for ($i = 0; $i < 4; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue1->id(), '', '', ''); + } + + for ($i = 4; $i < 6; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue2->id(), '', '', ''); + } + + for ($i = 6; $i < 11; $i++) { + GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', ''); + } + + $gigs = GiglogAdmin_Concert::find_concerts(); + + $this->assertEquals(11, count($gigs)); + } } diff --git a/tests/SelectFieldTest.php b/tests/SelectFieldTest.php new file mode 100644 index 0000000..fb6c298 --- /dev/null +++ b/tests/SelectFieldTest.php @@ -0,0 +1,70 @@ +<?php +// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com> +// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +declare(strict_types=1); + +use \EternalTerror\ViewHelpers; + +final class SelectFieldTest extends WP_UnitTestCase +{ + public function testEmptySelectField() + { + $res = ViewHelpers\select_field("fooselect"); + $this->assertStringStartsWith('<select name="fooselect">', $res); + $this->assertStringEndsWith('</select>', $res); + } + + public function testSelectFieldWithOneOption() + { + $res = ViewHelpers\select_field("fooselect", [[42, 'An option']]); + $this->assertStringStartsWith('<select name="fooselect">', $res); + $this->assertStringEndsWith('</select>', $res); + $this->assertStringContainsString('<option value="">Please select...</option>', $res); + $this->assertStringContainsString('<option value="42">An option</option>', $res); + } + + public function testSelectFieldWithMultipleOption() + { + $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']]; + + $res = ViewHelpers\select_field("fooselect", $opts); + + $this->assertStringStartsWith('<select name="fooselect">', $res); + $this->assertStringEndsWith('</select>', $res); + $this->assertStringContainsString('<option value="">Please select...</option>', $res); + $this->assertStringContainsString('<option value="42">An option</option>', $res); + $this->assertStringContainsString('<option value="666">Another option</option>', $res); + $this->assertStringContainsString('<option value="foo">A foo option</option>', $res); + } + + public function testSelectFieldWithSelectedOption() + { + $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']]; + + $res = ViewHelpers\select_field("fooselect", $opts, 666); + + $this->assertStringStartsWith('<select name="fooselect">', $res); + $this->assertStringEndsWith('</select>', $res); + $this->assertStringContainsString('<option value="">Please select...</option>', $res); + $this->assertStringContainsString('<option value="42">An option</option>', $res); + $this->assertStringContainsString('<option value="666" selected=\'selected\'>Another option</option>', $res); + $this->assertStringContainsString('<option value="foo">A foo option</option>', $res); + } + + public function testSelectFieldWithCustomBlankSelectionText() + { + $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']]; + + $res = ViewHelpers\select_field("fooselect", $opts, 666, "None"); + + $this->assertStringStartsWith('<select name="fooselect">', $res); + $this->assertStringEndsWith('</select>', $res); + $this->assertStringContainsString('<option value="">None</option>', $res); + $this->assertStringContainsString('<option value="42">An option</option>', $res); + $this->assertStringContainsString('<option value="666" selected=\'selected\'>Another option</option>', $res); + $this->assertStringContainsString('<option value="foo">A foo option</option>', $res); + } +} |