Dotclear

source: plugins/maintenance/index.php @ 3994:c335bb3ff76a

Revision 3994:c335bb3ff76a, 10.3 KB checked in by franck <carnet.franck.paul@…>, 6 years ago (diff)

Switching from inline JS variables to JSON script, maintenance plugin

Line 
1<?php
2/**
3 * @brief maintenance, a plugin for Dotclear 2
4 *
5 * @package Dotclear
6 * @subpackage Plugins
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
12if (!defined('DC_CONTEXT_ADMIN')) {return;}
13
14// Set env
15
16$core->blog->settings->addNamespace('maintenance');
17
18$maintenance = new dcMaintenance($core);
19$tasks       = $maintenance->getTasks();
20
21$headers = '';
22$p_url   = $core->adminurl->get('admin.plugin.maintenance');
23$task    = null;
24$expired = [];
25
26$code = empty($_POST['code']) ? null : (integer) $_POST['code'];
27$tab  = empty($_REQUEST['tab']) ? '' : $_REQUEST['tab'];
28
29// Get task object
30
31if (!empty($_REQUEST['task'])) {
32    $task = $maintenance->getTask($_REQUEST['task']);
33
34    if ($task === null) {
35        $core->error->add('Unknow task ID');
36    }
37
38    $task->code($code);
39}
40
41// Execute task
42
43if ($task && !empty($_POST['task']) && $task->id() == $_POST['task']) {
44    try {
45        $code = $task->execute();
46        if (false === $code) {
47            throw new Exception($task->error());
48        }
49        if (true === $code) {
50            $maintenance->setLog($task->id());
51
52            dcPage::addSuccessNotice($task->success());
53            http::redirect($p_url . '&task=' . $task->id() . '&tab=' . $tab . '#' . $tab);
54        }
55    } catch (Exception $e) {
56        $core->error->add($e->getMessage());
57    }
58}
59
60// Save settings
61
62if (!empty($_POST['save_settings'])) {
63
64    try {
65        $core->blog->settings->maintenance->put(
66            'plugin_message',
67            !empty($_POST['settings_plugin_message']),
68            'boolean',
69            'Display alert message of late tasks on plugin page',
70            true,
71            true
72        );
73
74        foreach ($tasks as $t) {
75            if (!$t->id()) {
76                continue;
77            }
78
79            if (!empty($_POST['settings_recall_type']) && $_POST['settings_recall_type'] == 'all') {
80                $ts = $_POST['settings_recall_time'];
81            } else {
82                $ts = empty($_POST['settings_ts_' . $t->id()]) ? 0 : $_POST['settings_ts_' . $t->id()];
83            }
84            $core->blog->settings->maintenance->put(
85                'ts_' . $t->id(),
86                abs((integer) $ts),
87                'integer',
88                sprintf('Recall time for task %s', $t->id()),
89                true,
90                $t->blog()
91            );
92        }
93
94        dcPage::addSuccessNotice(__('Maintenance plugin has been successfully configured.'));
95        http::redirect($p_url . '&tab=' . $tab . '#' . $tab);
96    } catch (Exception $e) {
97        $core->error->add($e->getMessage());
98    }
99}
100
101// Combos
102
103$combo_ts = [
104    __('Never')            => 0,
105    __('Every week')       => 604800,
106    __('Every two weeks')  => 1209600,
107    __('Every month')      => 2592000,
108    __('Every two months') => 5184000
109];
110
111// Display page
112
113echo '<html><head>
114<title>' . __('Maintenance') . '</title>' .
115dcPage::jsPageTabs($tab) .
116dcPage::jsLoad(dcPage::getPF('maintenance/js/settings.js'));
117
118if ($task && $task->ajax()) {
119    echo
120    dcPage::jsJson('maintenance', ['wait' => __('Please wait...')]) .
121    dcPage::jsLoad(dcPage::getPF('maintenance/js/dc.maintenance.js'));
122}
123
124echo
125$maintenance->getHeaders() . '
126</head>
127<body>';
128
129// Check if there is somthing to display according to user permissions
130if (empty($tasks)) {
131    echo dcPage::breadcrumb(
132        [
133            __('Plugins')     => '',
134            __('Maintenance') => ''
135        ]
136    ) .
137    '<p class="warn">' . __('You have not sufficient permissions to view this page.') . '</p>' .
138        '</body></html>';
139
140    return;
141}
142
143if ($task && ($res = $task->step()) !== null) {
144
145    // Page title
146
147    echo dcPage::breadcrumb(
148        [
149            __('Plugins')                                            => '',
150            '<a href="' . $p_url . '">' . __('Maintenance') . '</a>' => '',
151            html::escapeHTML($task->name())                          => ''
152        ]
153    ) . dcPage::notices();
154
155    // content
156    if (substr($res, 0, 1) != '<') {
157        $res = sprintf('<p class="step-msg">%s</p>', $res);
158    }
159
160    // Intermediate task (task required several steps)
161
162    echo
163    '<div class="step-box" id="' . $task->id() . '">' .
164    '<p class="step-back">' .
165    '<a class="back" href="' . $p_url . '&amp;tab=' . $task->tab() . '#' . $task->tab() . '">' . __('Back') . '</a>' .
166    '</p>' .
167    '<h3>' . html::escapeHTML($task->name()) . '</h3>' .
168    '<form action="' . $p_url . '" method="post">' .
169    $res .
170    '<p class="step-submit">' .
171    '<input type="submit" value="' . $task->task() . '" /> ' .
172    form::hidden(['task'], $task->id()) .
173    form::hidden(['code'], (integer) $code) .
174    $core->formNonce() .
175        '</p>' .
176        '</form>' .
177        '</div>';
178} else {
179
180    // Page title
181
182    echo dcPage::breadcrumb(
183        [
184            __('Plugins')     => '',
185            __('Maintenance') => ''
186        ]
187    ) . dcPage::notices();
188
189    // Simple task (with only a button to start it)
190
191    foreach ($maintenance->getTabs() as $tab_obj) {
192        $res_group = '';
193        foreach ($maintenance->getGroups() as $group_obj) {
194            $res_task = '';
195            foreach ($tasks as $t) {
196                if (!$t->id()
197                    || $t->group() != $group_obj->id()
198                    || $t->tab() != $tab_obj->id()) {
199                    continue;
200                }
201
202                $res_task .=
203                '<p>' . form::radio(['task', $t->id()], $t->id()) . ' ' .
204                '<label class="classic" for="' . $t->id() . '">' .
205                html::escapeHTML($t->task()) . '</label>';
206
207                // Expired task alert message
208                $ts = $t->expired();
209                if ($core->blog->settings->maintenance->plugin_message && $ts !== false) {
210                    if ($ts === null) {
211                        $res_task .=
212                        '<br /> <span class="warn">' .
213                        __('This task has never been executed.') . ' ' .
214                        __('You should execute it now.') . '</span>';
215                    } else {
216                        $res_task .=
217                        '<br /> <span class="warn">' . sprintf(
218                            __('Last execution of this task was on %s.'),
219                            dt::str($core->blog->settings->system->date_format, $ts) . ' ' .
220                            dt::str($core->blog->settings->system->time_format, $ts)
221                        ) . ' ' .
222                        __('You should execute it now.') . '</span>';
223                    }
224                }
225
226                $res_task .= '</p>';
227            }
228
229            if (!empty($res_task)) {
230                $res_group .=
231                '<div class="fieldset">' .
232                '<h4 id="' . $group_obj->id() . '">' . $group_obj->name() . '</h4>' .
233                    $res_task .
234                    '</div>';
235            }
236        }
237
238        if (!empty($res_group)) {
239            echo
240            '<div id="' . $tab_obj->id() . '" class="multi-part" title="' . $tab_obj->name() . '">' .
241            '<h3>' . $tab_obj->name() . '</h3>' .
242            // ($tab_obj->option('summary') ? '<p>'.$tab_obj->option('summary').'</p>' : '').
243            '<form action="' . $p_url . '" method="post">' .
244            $res_group .
245            '<p><input type="submit" value="' . __('Execute task') . '" /> ' .
246            form::hidden(['tab'], $tab_obj->id()) .
247            $core->formNonce() . '</p>' .
248            '<p class="form-note info">' . __('This may take a very long time.') . '</p>' .
249                '</form>' .
250                '</div>';
251        }
252    }
253
254    // Advanced tasks (that required a tab)
255
256    foreach ($tasks as $t) {
257        if (!$t->id() || $t->group() !== null) {
258            continue;
259        }
260
261        echo
262        '<div id="' . $t->id() . '" class="multi-part" title="' . $t->name() . '">' .
263        '<h3>' . $t->name() . '</h3>' .
264        '<form action="' . $p_url . '" method="post">' .
265        $t->content() .
266        '<p><input type="submit" value="' . __('Execute task') . '" /> ' .
267        form::hidden(['task'], $t->id()) .
268        form::hidden(['tab'], $t->id()) .
269        $core->formNonce() . '</p>' .
270            '</form>' .
271            '</div>';
272    }
273
274    // Settings
275
276    echo
277    '<div id="settings" class="multi-part" title="' . __('Alert settings') . '">' .
278    '<h3>' . __('Alert settings') . '</h3>' .
279    '<form action="' . $p_url . '" method="post">' .
280
281    '<h4 class="pretty-title">' . __('Activation') . '</h4>' .
282    '<p><label for="settings_plugin_message" class="classic">' .
283    form::checkbox('settings_plugin_message', 1, $core->blog->settings->maintenance->plugin_message) .
284    __('Display alert messages on late tasks') . '</label></p>' .
285
286    '<p class="info">' . sprintf(
287        __('You can place list of late tasks on your %s.'),
288        '<a href="' . $core->adminurl->get('admin.user.preferences') . '#user-favorites">' . __('Dashboard') . '</a>'
289    ) . '</p>' .
290
291    '<h4 class="pretty-title vertical-separator">' . __('Frequency') . '</h4>' .
292
293    '<p class="vertical-separator">' . form::radio(['settings_recall_type', 'settings_recall_all'], 'all') . ' ' .
294    '<label class="classic" for="settings_recall_all">' .
295    '<strong>' . __('Use one recall time for all tasks') . '</strong></label></p>' .
296
297    '<p class="field wide vertical-separator"><label for="settings_recall_time">' . __('Recall time for all tasks:') . '</label>' .
298    form::combo('settings_recall_time', $combo_ts, 'seperate', 'recall-for-all') .
299    '</p>' .
300
301    '<p class="vertical-separator">' . form::radio(['settings_recall_type', 'settings_recall_separate'], 'separate', 1) . ' ' .
302    '<label class="classic" for="settings_recall_separate">' .
303    '<strong>' . __('Use one recall time per task') . '</strong></label></p>';
304
305    foreach ($tasks as $t) {
306        if (!$t->id()) {
307            continue;
308        }
309        echo
310        '<div class="two-boxes">' .
311
312        '<p class="field wide"><label for="settings_ts_' . $t->id() . '">' . $t->task() . '</label>' .
313        form::combo('settings_ts_' . $t->id(), $combo_ts, $t->ts(), 'recall-per-task') .
314            '</p>' .
315
316            '</div>';
317    }
318
319    echo
320    '<p class="field wide"><input type="submit" value="' . __('Save') . '" /> ' .
321    form::hidden(['tab'], 'settings') .
322    form::hidden(['save_settings'], 1) .
323    $core->formNonce() . '</p>' .
324        '</form>' .
325        '</div>';
326}
327
328dcPage::helpBlock('maintenance', 'maintenancetasks');
329
330echo
331    '</body></html>';
Note: See TracBrowser for help on using the repository browser.

Sites map