summaryrefslogtreecommitdiffstats
path: root/includes/class-giglogadmin-venue.php
blob: 049f1174a4c262f34104835b8e7d91b9a310e26b (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
 * Class representing venues.
 *
 * @package giglogadmin
 *
 * 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_Venue' ) ) {
    /**
     * Class representing venues.
     */
    class GiglogAdmin_Venue {

        /**
         * The internal database ID of the entry.
         *
         * @var int|null
         */
        private ?int $id;

        /**
         * Venue name.
         *
         * @var string
         * @psalm-suppress PropertyNotSetInConstructor
         */
        private string $name;

        /**
         * Venue city.
         *
         * @var string|null
         * @psalm-suppress PropertyNotSetInConstructor
         */
        private string $city;

        /**
         * Venue address, optional.
         *
         * @var string|null
         */
        private ?string $address;

        /**
         * Venue website, optional.
         *
         * @var string|null
         */
        private ?string $webpage;

        /**
         * Constructs a new venue object from an array of attributes.
         * The attributes are expected to be named as in the database,
         * so this constructor can be used to construct the object
         * directly from the database row.
         *
         * @param object $attrs The attributes describing this venue.
         */
        public function __construct( object $attrs ) {
            $this->id = isset( $attrs->id ) ? $attrs->id : null;

            if ( isset( $attrs->wpgvenue_name, $attrs->wpgvenue_city ) ) {
                $this->name = $attrs->wpgvenue_name;
                $this->city = $attrs->wpgvenue_city;
            } else {
                error_log( 'Trying to construct a Venue without a name or a city' );
                wp_die();
            }

            $this->address = isset( $attrs->wpgvenue_address ) ? $attrs->wpgvenue_address : null;
            $this->webpage = isset( $attrs->wpgvenue_webpage ) ? $attrs->wpgvenue_webpage : null;
        }

        /**
         * Get venue by given id.
         *
         * @param int $id       The DB id of the venue to get.
         * @return null|self
         */
        public static function get( int $id ) : ?self {
            global $wpdb;

            $res = $wpdb->get_results(
                $wpdb->prepare( "SELECT * from {$wpdb->prefix}giglogadmin_venues WHERE id = %d", $id )
            );

            if ( ! $res ) {
                error_log( "GiglogAdmin: Query for Venue with id={$id} returned null." );
                return null;
            } elseif ( count( $res ) > 1 ) {
                error_log(
                    "GiglogAdmin: Query for venye with id={$id} returned "
                    . count( $result )
                    . 'entries.'
                );
                return null;
            }

            return new GiglogAdmin_Venue( $res[0] );
        }

        /**
         * Create a new venue and store it in the database.
         *
         * @param string $name      The name of the venue.
         * @param string $city      The city where the venue is located (defaults to Oslo).
         */
        public static function create( string $name, string $city = 'Oslo' ): self {
            $venue = new GiglogAdmin_Venue(
                (object) array(
					'wpgvenue_name' => $name,
					'wpgvenue_city' => $city,
                )
            );
            $venue->save();

            return $venue;
        }

        /**
         * Find a venue by name and city, or create it if it does not exist.
         *
         * @param string $name      The name of the venue.
         * @param string $city      The city of the venue (defaults to Oslo).
         */
        public static function find_or_create( string $name, string $city = 'Oslo' ): self {
            global $wpdb;

            $results  = $wpdb->get_results(
                $wpdb->prepare(
                    "SELECT * FROM {$wpdb->prefix}giglogadmin_venues "
                    . 'WHERE upper(wpgvenue_name)=upper(%s) and upper(wpgvenue_city)=upper(%s)',
                    $name,
                    $city
                )
            );

            if ( ! $results ) {
                return self::create( $name, $city );
            } elseif ( count( $results ) > 1 ) {
                error_log(
                    "GiglogAdmin: Query for venye with id={$id} returned "
                    . count( $result )
                    . 'entries.'
                );
            } else {
                return new GiglogAdmin_Venue( $results[0] );
            }
        }

        /**
         * Return a list of all cities with venues.
         */
        public static function all_cities(): array {
            global $wpdb;

            $results = $wpdb->get_results(
                "select distinct wpgvenue_city from {$wpdb->prefix}giglogadmin_venues"
            );

            return array_map( fn( $r ) => $r->wpgvenue_city, $results );
        }

        /**
         * Return a list of all venues.
         */
        public static function all_venues(): array {
            global $wpdb;

            $results = $wpdb->get_results(
                "select * from {$wpdb->prefix}giglogadmin_venues ORDER BY wpgvenue_name"
            );

            return array_map( fn( $r ) => new GiglogAdmin_Venue( $r ), $results );
        }


        /**
         * Return all venues within the given city.
         *
         * @param string $city      The city whose venues to return.
         */
        public static function venues_in_city( string $city ): array {
            global $wpdb;
            $results = $wpdb->get_results(
                $wpdb->prepare(
                    "SELECT * FROM {$wpdb->prefix}giglogadmin_venues WHERE wpgvenue_city=%s ORDER BY wpgvenue_name",
                    $city
                )
            );

            return array_map( fn( $r ) => new GiglogAdmin_Venue( $r ), $results );
        }

        /**
         * Save the venue to the database.
         */
        public function save(): void {
            global $wpdb;

            $wpdb->insert(
                $wpdb->prefix . 'giglogadmin_venues',
                array(
					'id' => '',
					'wpgvenue_name' => $this->name,
					'wpgvenue_city' => $this->city,
                )
            );

            $this->id = $wpdb->insert_id;
        }

        /**
         * Return database ID of venue.
         */
        public function id() : int {
             return $this->id;
        }

        /**
         * Return name of venue.
         */
        public function name() : string {
             return $this->name;
        }

        /**
         * Return city of venue
         */
        public function city() : string {
             return $this->city;
        }
    }
}