summaryrefslogtreecommitdiffstats
path: root/includes/concertlogs.php
blob: 8b485cae3b44f6bb763778a84d3621634f8b5a31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?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

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.
         *
         * @return void
         */
        public static function add($concert_id): void
        {
            global $wpdb;

            $q = $wpdb->prepare(
                "INSERT INTO wpg_concertlogs SET wpgcl_concertid = %d",
                intval($concert_id));

            $wpdb->query($q);
        }

        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 );
        }

        public function assigned_user(string $role) : ?string
        {
            return $this->roles[$role];
        }
    }
}