summaryrefslogtreecommitdiffstats
path: root/includes/admin/views/_concerts_table.php
blob: 13d723a8d543190ed5857a90dba33eb0f7171b31 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?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

require_once __DIR__ . '/../../view-helpers/select_field.php';

if (!class_exists("GiglogAdmin_ConcertsTable"))
{
    class GiglogAdmin_ConcertsTable
    {
        const STATUS_LABELS = [
            '',
            'Accred Requested',
            'Photo Approved',
            'Text Approved',
            'Photo and Text Approved',
            'Rejected'
        ];

        const FILTER_KEYS = [
            'city',
            'venue',
            'month',
            'only_mine'
        ];

        private string $username;
        private array $filter;
        private int $page_no;
        private string $nonce;

        public static function update() : void
        {
            //
            // Check that we get a nonce, and that it is valid to prevent CSRF attacks.
            //
            if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'concerts-table')) {
                wp_die('You are not allowed to do that.', 403);
                exit();
            }

            if (isset($_POST['assignitem'])) {
                $concert = GiglogAdmin_Concert::get(intval($_POST['cid']));

                if ($concert) {
                    $role = sanitize_text_field($_POST['pid']);
                    self::assignconcert($role, $concert);
                }

                return;
            }

            if (isset($_POST['unassignitem'])) {
                $concert = GiglogAdmin_Concert::get(intval($_POST['cid']));

                if ($concert) {
                    $role = sanitize_text_field($_POST['pid']);
                    self::unassignconcert($role, $concert);
                }

                return;
            }

            // handle the status drop down
            if (isset($_POST['selectstatus']) && !empty($_POST['selectstatus']) && !empty($_POST['cid'])) {
                if ($_POST['selectstatus'] > 0 && $_POST['selectstatus'] < count(self::STATUS_LABELS)) {
                    $concert = GiglogAdmin_Concert::get(intval($_POST['cid']));
                    if ( $concert ) {
                        $concert->set_status(intval($_POST['selectstatus']));
                        $concert->save();
                        self::emailuser($concert,intval($_POST['selectstatus']));
                    }
                }
            }
        }

        static function assignconcert(string $p1, GiglogAdmin_Concert $concert): void
        {
            $username = wp_get_current_user()->user_login;
            $concert->assign_role($p1, $username);
            $concert->save();

            $cuser = get_user_by( 'login', 'etadmin');

            if ( $cuser ) {
                $dest = $cuser->user_email;
                $subject = 'WP-GIGLOG '.$username.' has taken '.$p1. 'for concert '.$concert->cname();
                $body = 'WP-GIGLOG '.$username.' has taken '.$p1. 'for concert '.$concert->cname().', concert with ID ' .$concert->id();
                $headers = array('Content-Type: text/html; charset=UTF-8');

                wp_mail( $dest, $subject, $body );
            }
        }

        static function unassignconcert(string $p1, GiglogAdmin_Concert $concert): void
        {
            $username = wp_get_current_user()->user_login;
            $concert->remove_user_from_roles($username);
            $concert->save();

            $cuser = get_user_by( 'login', 'etadmin');

            if ( $cuser ) {
                $dest = $cuser->user_email;
                $subject = 'WP-GIGLOG '.$username.' has UNASSIGNED  '.$p1. 'for concert '.$concert->cname();
                $body = 'WP-GIGLOG '.$username.' has UNASSIGNED  '.$p1. 'for concert '.$concert->cname().', concert with ID ' .$concert->id();
                $headers = array('Content-Type: text/html; charset=UTF-8');

                wp_mail( $dest, $subject, $body );
            }
        }

        static function emailuser(GiglogAdmin_Concert $concert, string $cstatus): void
        {
            $username = wp_get_current_user()->user_login;
            $useremail = 'live@eternal-terror.com';
            $dest = '';
            $roles = $concert -> roles();
            $x = '';

            foreach ($roles as $role) {
                if($role){
                    $cuser = get_user_by( 'login', $role);

                    if ( $cuser ) {
                        $dest .= $cuser->user_email . ',';
                    }
                }
            }

            $subject = 'Message from GIGLOG: Concert '.$concert->cname().' has a new status  '.$cstatus. '.';
            $body = 'You receive this message because you have assigned one of the roles for Concert '.$concert->cname().'.';
            $body .= '\r\n This is to inform you that there is a new status for the acreditation  '.$cstatus. '.';
            $body .= '\r\n Should you no longer want to receive updates about this concert, please log in to Giglog and remove yourself from the concert. Thanks!';
            $headers = array('Content-Type: text/plain; charset=UTF-8'); //it is text by default so no need for headers actually

            wp_mail( $dest, $subject, $body );
        }

        public function __construct()
        {
            $this->username = wp_get_current_user()->user_login;

            // Set the nonce we use to check for CSRF attacks.
            $this->nonce = wp_create_nonce('concerts-table');
            $this->get_args();
        }

        public function render(): string
        {
            return $this->render_filters()
                . $this->render_concerts_table();
        }

        private function render_concert_table_header() : string
        {
            $content = '<div style="overflow-x:auto;"><table class="assignit">';
            $content.= '<span style="font-size:0.8em;font-style: italic;">Note: the iCal link will download a file with extension .ical which can be used to add the event to your calendar. For convenience, we set all events with start time at 19:00 but please check the actual event for the correct time.</span>';

            $content.= '<tr class="assignithrow">';
            $content.= '<th>CITY</th><th>DATE</th><th>NAME</th><th>VENUE</th>';

            if (!is_admin()) {
                $content .= '<th>EVENT</th><th>TICKETS</th><th>Calendar</th>';
            }
            else {
                $content .= '<th></th><th>PHOTO1</th><th>PHOTO2</th><th>TEXT1</th><th>TEXT2</th><th>STATUS</th>';
                if (current_user_can('administrator')) {
                    $content .= '<th>AdminOptions</th>';
                }
            }

            $content .= '</tr>';

            return $content;
        }

        private function get_args() : void
        {
            $this->filter = [];

            // Use the submitted "city" if any. Otherwise, use the default/static value.
            $cty = filter_input( INPUT_GET, 'city', FILTER_SANITIZE_SPECIAL_CHARS );
            if ($cty) {
                $this->filter['city'] = $cty;
            }

            $venue = filter_input( INPUT_GET, 'venue', FILTER_SANITIZE_SPECIAL_CHARS );
            if ($venue) {
                $this->filter['venue_id'] = $venue;
            }

            $smonth = filter_input( INPUT_GET, 'month', FILTER_SANITIZE_SPECIAL_CHARS );
            if ($smonth) {
                $this->filter['month'] = $smonth;
            }

            if(isset($_GET['only_mine']) && $_GET['only_mone'] == '1') {
                $this->filter['currentuser'] = $this->username;
            }

            if (isset($_GET['page_no']) && $_GET['page_no'] != "" && is_numeric($_GET['page_no']) && isset($_GET['page_no']) == $this->page_no  ) {
                $this->page_no = intval($_GET['page_no']);
            } else {
                $this->page_no = 1;
            }
        }

        private function get_concerts() : ?array
        {
            $total_records_per_page = 15;

            $total_concerts = GiglogAdmin_Concert::count( $this->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;


            if ($this->page_no > $this->total_no_of_pages ) {
                $this->page_no = 1;
            }

            $this->filter['offset'] =  $offset;
            $this->filter['recperpage'] =  $total_records_per_page;

            return GiglogAdmin_Concert::find_concerts($this->filter);
        }

        private function get_filter(string $f) : ?string
        {
            return isset( $this->filter[$f] ) ? $this->filter[$f] : null;
        }

        private function render_pagination() : string
        {
            $content =
                '<div id="pagtextbox" style="display:flex">'
                . '<span class="alignleft" style="text-align:left;flex:auto;">';

            if($this->page_no > 1) {
                $content .=
                    '<span>'
                    . '<a href="'. add_query_arg( 'page_no', 1 ) . '">'
                    . 'First Page</a> -'
                    . '</span>'
                    . '<span>'
                    . '<a href="' . add_query_arg( 'page_no', $this->previous_page ) . '">'
                    . ' Previous</a></span>';
            }

            $content .= '</span>'
                . '<span class="aligncenter" style="text-align:center;flex:auto">'
                . '<strong>Page ' . $this->page_no . ' of ' . $this->total_no_of_pages . '</strong>'
                . '</span>';

            $content .= '<span class="alignright" style="text-align:right;flex:auto;float:none">';

            if ($this->page_no < $this->total_no_of_pages) {
                $content .=
                    '<span>'
                    . '<a href="' . add_query_arg( 'page_no', $this->next_page ) . '">'
                    . 'Next</a> - '
                    . '</span>'
                    . '<span>'
                    . '<a href="' . add_query_arg( 'page_no', $this->total_no_of_pages ) .'">'
                    . 'Last Page</a>'
                    . '</span>';
            }

            $content .=
                '</span>'
                . '</div>';

            return $content;
        }

        private function render_concerts_table() : string
        {
            $concerts = $this->get_concerts();

            $last_city = '';

            $content = $this->render_concert_table_header();

            foreach ( $concerts as $concert ) {
                $content .= '<tr class="assignitr"><td>';

                if ($last_city != $concert->venue()->city()) {
                    $content .= $concert->venue()->city();
                }

                $content .= '</td>';

                $content .=
                    "<td>" . date( 'd.M.Y', strtotime( $concert->cdate() ) ) . "</td>"
                    . "<td>{$concert->cname()}</td>"
                    . "<td>{$concert->venue()->name()}</td>";

                if( is_admin() ) {
                    $content .= '<td class="publishstatus">' . $this->mark_new_concert($concert) . '</td>';

                    foreach ( [ 'photo1', 'photo2', 'rev1', 'rev2' ] as $role ) {
                        $content .= '<td class="assigneduser">'
                            . $this->assign_role_for_user_form( $role, $concert )
                            . '</td>';
                    }

                    $content .= '<td>' . self::STATUS_LABELS[$concert->status()] . '</td>';

                    if (current_user_can('administrator')) {
                        $content .= "<td class=\"adminbuttons\">{$this->adminactions( $concert )}</td>";
                    }
                }
                else {
                    $content .= "<td><a target=\"_blank\" href=\"{$concert->eventlink()}\">Link</a></td>";
                    $content .= "<td><a target=\"_blank\" href=\"{$concert->tickets()}\">Tickets</a></td>";
                }

                $content .= '<td> <a href="'.get_admin_url().'admin-ajax.php?action=giglog_export_ical&evid='.$concert->id().'">iCal</td>';
                $content .= '</tr>';
                $last_city = $concert->venue()->city();
            }

            $content .= '</table>';

            $content .= $this->render_pagination();

            //from main form that includes filters
            $content .= '</div></form></p>';

            // return the table
            return $content;
        }

        private function render_filters() : string
        {
            global $wp_locale;

            $select = '<p><form method="GET" action="" class="filterclass">FILTER DATA:  ';

            foreach( $_GET as $name => $val ) {
                if ( in_array( $name, self::FILTER_KEYS ) ) {
                    continue;
                }

                $select .= '<input type="hidden" name="' . esc_attr( $name )
                    . '" value="' . esc_attr( $val ) . '">';
            }

            $cty = $this->get_filter('city');

            $select .= \EternalTerror\ViewHelpers\select_field(
                "city",
                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(
                    "venue",
                    array_map(
                        fn($venue) => [$venue->id(), $venue->name()],
                        GiglogAdmin_Venue::venues_in_city($cty)
                    ),
                    $this->get_filter('venue_id'),
                    "Select venue...");
            }

            $select .= \EternalTerror\ViewHelpers\select_field(
                "month",
                array_map(
                    fn($m) => [ $m, $wp_locale->get_month( $m ) ],
                    range( 1, 12 )
                ),
                $this->get_filter('month'),
                "Select month...");

            $select.='</select>';

            if(is_admin()) {
                //option to select own concerts only
                $select .= '<input name="only_mine" class="ownconc" type="checkbox" value="1"'
                    . checked( $this->get_filter( 'current_user' ) )
                    . '><label for="ownconcerts">Show own concerts only</label>';

            }
            //NOTE that I remvoed </form></p> and mvoed them up to render_concerts_table function
            $select .= '<input class="applybutton" type="submit" value="Apply Filters">';

            return $select;
        }

        private function adminactions( GiglogAdmin_Concert $concert ) : string
        {
            return
                '<form class="adminactions" method="POST" action="">'
                . '<input type="hidden" name="nonce" value="' . $this->nonce . '">'
                . '<input type="hidden" name="cid" value="' . $concert->id() .  '" />'
                . \EternalTerror\ViewHelpers\select_field(
                    'selectstatus',
                    array_map(fn($i) => [ $i, self::STATUS_LABELS[$i] ], range(1, count(self::STATUS_LABELS) - 1)),
                    $concert->status())
                    . '<input type="submit" value="SetStatus">'
                    . '<input type="submit" name ="edit" value="EDIT">'
                    . '</form>';
        }

        /**
         * 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 '<span style="color:green">NEW</span>';
            }
            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 = '<form class="unassign_concert" method="POST" action="">'
                    . '  <input type="hidden" name="nonce" value="' . $this->nonce . '">'
                    . '  <input type="hidden" name="cid" value="' . $concert->id() . '" />'
                    . '  <input type="hidden" name="pid" value="' . $role . '" />'
                    . '  <input type="submit" name="unassignitem" value=""/>'
                    . '</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 ( array_search($this->username, $roles) ) {
                // other slots for this concert are taken by user
                $f = '<span class="taken_by_self">-</span>';
            }
            else { //not taken by anyone
                $f = '<form class="assign_concert" method="POST" action="">'
                    . '  <input type="hidden" name="nonce" value="' . $this->nonce . '">'
                    . '  <input type="hidden" name="cid" value="' . $concert->id() . '" />'
                    . '  <input type="hidden" name="pid" value="' . $role. '" />'
                    . '  <input  type="submit" name="assignitem" value=""/>'
                    . '</form>';
            }

            return $f;
        }
    }
}