summaryrefslogtreecommitdiffstats
path: root/includes/admin/views/giglog_admin_page.php
blob: 8d210bf99cfa9485d3135f12e2562e1816bf59bf (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
<?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_AdminPage' ) ) {
    require_once __DIR__ . '/../../venue.php';
    require_once __DIR__ . '/_concerts_table.php';
    require_once __DIR__ . '/_edit_concert_form.php';
    require_once __DIR__ . '/_new_venue_form.php';

    class GiglogAdmin_AdminPage
    {
        const STATUS_LABELS = [
            '',
            'Accred Requested',
            'Photo Approved',
            'Text Approved',
            'Photo and Text Approved',
            'Rejected'
        ];

        public static function render_html() : void
        {
            $page = new self();
            $page->render_page();
        }

        private function render_page() : void
        {
            $concerts = new GiglogAdmin_ConcertsTable();
            ?>
            <div class="wrap">
                <h1>Giglog Admin</h1>

                <p>The available slots are marked with the green checkbox.
                If you click on it, it will be assigned to you and if you no longer
                wish to cover that concert, click on the red icon and you will be
                unassigned. A mail should be sent to the admin when this happens,
                but in order for the accreditation request to be sent, you have to
                mail live@eternal-terror.com with the template containing concert
                information. There might be some exceptions, but those are discussed
                case by case. So whenever you want a concert, assign yourself and send
                the template no later than 3 weeks before the concert.</p>

                <p>Admin will try to keep the concert status updated so that you know
                what the accreditation status is. You will get personal message if this
                is really close to the concert date.</p>

                <p><?php echo $concerts->render() ?></p>
            </div>
            <?php
            if (current_user_can('administrator')) {
                $edit_form = new GiglogAdmin_EditConcertForm();
                $venue_form = new GiglogAdmin_NewVenueForm(); ?>
                <div>
                    <h3>Form to create/edit concerts and venues</h3>
                </div>
                <div class="editform">
                    <?php echo $edit_form->render() . $venue_form->render(); ?>
                </div><?php
            }
        }

        /**
         * @return void
         */
        static function update()
        {
            if ('POST' !== $_SERVER['REQUEST_METHOD'])
                return;

            // Use the submitted "city" if any. Otherwise, use the default/static value.
            $cty = filter_input( INPUT_POST, 'selectcity', FILTER_SANITIZE_SPECIAL_CHARS );
            $cty = $cty ? $cty: 'ALL';

            $venue = filter_input( INPUT_POST, 'selectvenue', FILTER_SANITIZE_SPECIAL_CHARS );
            $venue = $venue ? $venue : '0';

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

                if ($concert) {
                    GiglogAdmin_AdminPage::assignconcert($role, $concert);
                }

                $url2=$_SERVER['REQUEST_URI'];
                header("Refresh: 1; URL=$url2");  //reload page
            }

            if(isset($_POST['unassignitem']))
            {
                $concert = GiglogAdmin_Concert::get(intval($_POST['cid']));
                if ( ! $concert ) {
                    wp_die( "Invalid concert specified." );
                }

                $role = sanitize_text_field($_POST['pid']);

                GiglogAdmin_AdminPage::unassignconcert($role, $concert);

                $url3=$_SERVER['REQUEST_URI'];
                header("Refresh: 1; URL=$url3");  //reload page
            }

            // 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();
                        GiglogAdmin_AdminPage::emailuser($concert,intval($_POST['selectstatus']));
                    }
                }
            }

            if(isset($_POST['newconcert'])) {
                if (empty($_POST['cname'])  || empty($_POST['selectvenueadmin']) || empty($_POST['cdate']) || empty($_POST['ticket']) || empty($_POST['eventurl'])) {
                    echo '<script language="javascript">alert("You are missing a value, concert was not created"); </script>';
                }
                else {
                    if (GiglogAdmin_Concert::create($_POST['cname'], $_POST['selectvenueadmin'], $_POST['cdate'], $_POST['ticket'], $_POST['eventurl'])) {
                        echo '<script language="javascript">alert("Yey, concert created"); </script>';
                    }
                    else {
                        echo '<script language="javascript">alert("Nay, concert was duplicated"); </script>';
                    }
                }
            }

            if (isset($_POST['editconcert']))
            {
                if (!isset($_POST['giglog_edit_concert_nonce'])
                    || wp_verify_nonce($_POST['giglog_edit_concert_nonce'], plugin_basename( __FILE__ )))
                {
                    header("{$_SERVER['SERVER_PROTOCOL']} 403 Forbidden");
                    wp_die('CSRF validation failed.', 403);
                }

                $roles = array_reduce(
                    ['photo1', 'photo1', 'rev1', 'rev2'],
                    function($roles, $r) {
                        if (isset($_POST[$r])) {
                            $roles[$r] = sanitize_user($_POST[$r]);
                        }
                        return $roles;
                    },
                    []
                );

                $attributes = [
                    'wpgconcert_name' => sanitize_text_field($_POST['cname']),
                    'venue' => intval($_POST['selectvenueadmin']),
                    'wpgconcert_date' => sanitize_text_field($_POST['cdate']),
                    'wpgconcert_ticket' => esc_url_raw($_POST['ticket']),
                    'wpgconcert_event' => esc_url_raw($_POST['eventurl']),
                    'wpgconcert_roles' => $roles,
                ];

                $concert = GiglogAdmin_Concert::get(intval($_POST['pid']));
                if ($concert && $concert->update((object) $attributes)) {
                    // let user know the concert was updated.
                    // Look into admin_notices
                }
            }


            if(isset($_POST['newvenue']))
            {
                if (!isset($_POST['giglog_new_venue_nonce'])
                    || wp_verify_nonce($_POST['giglog_new_venue_nonce'], plugin_basename( __FILE__ )))
                {
                    header("{$_SERVER['SERVER_PROTOCOL']} 403 Forbidden");
                    wp_die('CSRF validation failed.', 403);
                }

                if (empty($_POST['venuename']) || empty($_POST['venuecity'])) {
                    echo '<script language="javascript">alert("You are missing a value, venue was not created"); </script>';
                }
                else
                {
                    GiglogAdmin_Venue::create($_POST['venuename'],$_POST['venuecity']);
                    echo '<script language="javascript">alert("Yey, venue created"); </script>';
                }
            }
        }

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

    }
}
?>