summaryrefslogtreecommitdiff
path: root/admin.php
blob: 8e98db74d731e1785efa3cbc3f0b928a02b55b39 (plain)
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
<?php

function communievents_admin_page_content() {
    ?>
    <div class="wrap">
        <h1>CommuniEvents</h1>
        <form method="post" action="options.php">
            <?php
                settings_fields('communievents-admin-page');
                do_settings_sections('communievents-admin-page');

                submit_button();
            ?>
        </form>
        <p>&nbsp;<p>&nbsp;
        <h2>App-Informationen</h2>
        <?php $api = CommuniApi::defaultInstance(); ?>
        <table>
            <tr>
                <td>Benutzer:
                <td><?php
                    $user = $api->user();
                    if (!empty($user)) {
                        $url = $user->webappUrl();

                        if (!empty($url))
                            echo '<a href="' . esc_url($url) . '" target="_blank">';

                        echo esc_html("{$user->name()} ({$user->id()})");

                        if (!empty($url))
                            echo '</a>';
                    } else {
                        echo 'N/A (Zugangstoken falsch?)';
                    }
                ?>
            <tr>
                <td>Hauptgruppe:
                <td><?php
                    $group = $api->group();
                    if (!empty($group)) {
                        $url = $group->webappUrl();

                        if (!empty($url))
                            echo '<a href="' . esc_url($url) . '" target="_blank">';

                        echo esc_html("{$group->title()} ({$group->id()})");

                        if (!empty($url))
                            echo '</a>';
                    } else {
                        echo 'N/A (Zugangstoken falsch?)';
                    }
                ?>
            <tr>
                <td>Weitere Gruppen:
                <td><?php
                    if (!empty($user)) {
                        $groups = $user->groups();
                        $groups_html = array();
                        foreach ($groups as $g) {
                            if ($g === $group)
                                continue;

                            $html = '';
                            $url = $g->webappUrl();

                            if (!empty($url))
                                $html .= '<a href="' . esc_url($url) . '" target="_blank">';

                            $html .= esc_html("{$g->title()} ({$g->id()})");

                            if (!empty($url))
                                $html .= '</a>';

                            $groups_html[] = $html;
                        }

                        echo implode(', ', $groups_html);
                    }
                ?>
            <tr>
                <td>CommuniApp:
                <td><?php
                    $url = $api->webappUrl();
                    if (!empty($url)) {
                        echo '<a href="' . esc_url($url) . '" target="_blank">' . esc_url($url) . '</a>';
                    } else {
                        echo 'N/A (Zugangstoken falsch?)';
                    }
                ?>
        </table>
        <p>&nbsp;<p>&nbsp;
        <h2>Synchronisation</h2>
        <?php
            global $wpdb;

            $tablename = $wpdb->prefix . 'communievents_events';

            $evcount = $wpdb->get_var("SELECT COUNT(id) FROM $tablename", 0, 0);
            $lastsync = $wpdb->get_var("SELECT MAX(lastsync) FROM $tablename", 0, 0);
            $nextsync = wp_next_scheduled('communievents_cron_hook');

            if ($lastsync) {
                $lastsync_dt = new DateTime("@$lastsync");
                $lastsync_dt->setTimezone(wp_timezone());
                $lastsync_str = $lastsync_dt->format('Y-m-d H:i:s');
            } else {
                $lastsync_str = "nie";
            }

            if ($nextsync) {
                $nextsync_dt = new DateTime("@$nextsync");
                $nextsync_dt->setTimezone(wp_timezone());
                $nextsync_str = $nextsync_dt->format('Y-m-d H:i:s');
            } else {
                $nextsync_str = "nie";
            }
        ?>
        <table>
            <tr>
                <td>Anzahl Ereignisse:
                <td><?php echo esc_html($evcount); ?>
            <tr>
                <td>Letzte Synchronisation:
                <td><?php echo esc_html($lastsync_str); ?>
            <tr>
                <td>Nächste Synchronisation:
                <td><?php echo esc_html($nextsync_str); ?>
        </table>
        <form method="POST" action="<?php echo admin_url('admin-post.php'); ?>">
            <?php wp_nonce_field('communievents-admin-sync'); ?>
            <input type="hidden" name="action" value="communievents_sync" />
            <input type="hidden" name="backurl" value="<?php menu_page_url('communievents-admin-page'); ?>" />
            <input type="submit" value="Jetzt Synchronisieren" class="button button-primary" />
        </form>
    </div>
    <?php
}

function communievents_render_settings_section() {
    ?><p class="description">Das Token findest Du in deiner CommuniApp unter <em>Admin Bereich</em> ➔ <em>Rest-Api</em><?php
}

function communievents_render_auth_token_input() {
    ?><p><textarea class="large-text code" rows="5" name="communievents-auth-token"><?php echo esc_textarea(get_option('communievents-auth-token')); ?></textarea><?php
}

function communievents_setup_admin_menu() {
    add_options_page('CommuniEvents', 'CommuniEvents', 'manage_options', 'communievents-admin-page', 'communievents_admin_page_content');
}

function communievents_setup_settings() {
    add_settings_section(
        'communievents-settings-section',         // ID used to identify this section and with which to register options
        'CommuniApp REST Zugangstoken',                  // Title to be displayed on the administration page
        'communievents_render_settings_section', // Callback used to render the description of the section
        'communievents-admin-page'                           // Page on which to add this section of options
    );

    add_settings_field(
        'communievents-auth-token',                      // ID used to identify the field throughout the theme
        'Zugangstoken',                           // The label to the left of the option interface element
        'communievents_render_auth_token_input',   // The name of the function responsible for rendering the option interface
        'communievents-admin-page',                          // The page on which this option will be displayed
        'communievents-settings-section',         // The name of the section to which this field belongs
        array(                              // The array of arguments to pass to the callback. In this case, just a description.

        )
    );

    register_setting('communievents-admin-page', 'communievents-auth-token');
}

function communievents_admin_handle_sync_click() {
    if (!current_user_can('manage_options')) {
        wp_die('Insufficient permissions', 'CommuniEvents Error', array('response' => 403));
    }

    check_admin_referer('communievents-admin-sync');

    // disable output buffering so our headers_sent() trick works
    while (ob_get_level() > 0) {
        ob_end_flush();
    }

    communievents_scrape_events();

    // no news is good news! unless the scraper did output something,
    // immediately redirect to the admin page
    if (!headers_sent()) {
        header("Location: $_POST[backurl]");
        die();
    }

    ?>
    <p>
        Bei der Synchronisierung ist ein Fehler aufgetreten!
    </p>
    <form method="POST" action="<?php echo admin_url('admin-post.php'); ?>">
        <?php wp_nonce_field('communievents-admin-sync'); ?>
        <input type="hidden" name="action" value="<?php echo esc_attr($_POST['action']); ?>" />
        <input type="hidden" name="backurl" value="<?php echo esc_attr($_POST['backurl']); ?>" />
        <input type="submit" value="Nochmal" class="button button-primary" />
    </form>
    <p>
        <a href="<?php echo esc_url($_POST['backurl']); ?>">Zurück</a>
    </p>
    <?php
}

function communievents_setup_admin_page() {
    add_action('admin_menu', 'communievents_setup_admin_menu');
    add_action('admin_init', 'communievents_setup_settings');
    add_action('admin_post_communievents_sync', 'communievents_admin_handle_sync_click');
}