1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_CONTEXT_ADMIN')) { return; } |
---|
13 | |
---|
14 | // Sidebar menu |
---|
15 | $_menu['Plugins']->addItem( |
---|
16 | __('Maintenance'), |
---|
17 | $core->adminurl->get('admin.plugin.maintenance'), |
---|
18 | dcPage::getPF('maintenance/icon.png'), |
---|
19 | preg_match('/'.preg_quote($core->adminurl->get('admin.plugin.maintenance')).'(&.*)?$/', $_SERVER['REQUEST_URI']), |
---|
20 | $core->auth->check('admin', $core->blog->id) |
---|
21 | ); |
---|
22 | |
---|
23 | // Admin behaviors |
---|
24 | $core->addBehavior('dcMaintenanceInit', array('dcMaintenanceAdmin', 'dcMaintenanceInit')); |
---|
25 | $core->addBehavior('adminDashboardFavorites', array('dcMaintenanceAdmin', 'adminDashboardFavorites')); |
---|
26 | $core->addBehavior('adminDashboardContents', array('dcMaintenanceAdmin', 'adminDashboardItems')); |
---|
27 | $core->addBehavior('adminDashboardOptionsForm', array('dcMaintenanceAdmin', 'adminDashboardOptionsForm')); |
---|
28 | $core->addBehavior('adminAfterDashboardOptionsUpdate', array('dcMaintenanceAdmin', 'adminAfterDashboardOptionsUpdate')); |
---|
29 | $core->addBehavior('adminPageHelpBlock', array('dcMaintenanceAdmin', 'adminPageHelpBlock')); |
---|
30 | $core->addBehavior('pluginsToolsHeaders', array('dcMaintenanceAdmin', 'pluginsToolsHeaders')); |
---|
31 | |
---|
32 | /** |
---|
33 | @ingroup PLUGIN_MAINTENANCE |
---|
34 | @nosubgrouping |
---|
35 | @brief Maintenance plugin admin class. |
---|
36 | |
---|
37 | Group of methods used on behaviors. |
---|
38 | */ |
---|
39 | class dcMaintenanceAdmin |
---|
40 | { |
---|
41 | /** |
---|
42 | * Register default tasks. |
---|
43 | * |
---|
44 | * @param $maintenance <b>dcMaintenance</b> dcMaintenance instance |
---|
45 | */ |
---|
46 | public static function dcMaintenanceInit($maintenance) |
---|
47 | { |
---|
48 | $maintenance |
---|
49 | ->addTab('maintenance', __('Servicing'), array('summary' => __('Tools to maintain the performance of your blogs.'))) |
---|
50 | ->addTab('backup', __('Backup'), array('summary' => __('Tools to back up your content.'))) |
---|
51 | ->addTab('dev', __('Development'), array('summary' => __('Tools to assist in development of plugins, themes and core.'))) |
---|
52 | |
---|
53 | ->addGroup('optimize', __('Optimize')) |
---|
54 | ->addGroup('index', __('Count and index')) |
---|
55 | ->addGroup('purge', __('Purge')) |
---|
56 | ->addGroup('other', __('Other')) |
---|
57 | ->addGroup('zipblog', __('Current blog')) |
---|
58 | ->addGroup('zipfull', __('All blogs')) |
---|
59 | |
---|
60 | ->addGroup('l10n', __('Translations'), array('summary' => __('Maintain translations'))) |
---|
61 | |
---|
62 | ->addTask('dcMaintenanceCache') |
---|
63 | ->addTask('dcMaintenanceIndexposts') |
---|
64 | ->addTask('dcMaintenanceIndexcomments') |
---|
65 | ->addTask('dcMaintenanceCountcomments') |
---|
66 | ->addTask('dcMaintenanceSynchpostsmeta') |
---|
67 | ->addTask('dcMaintenanceLogs') |
---|
68 | ->addTask('dcMaintenanceVacuum') |
---|
69 | ->addTask('dcMaintenanceZipmedia') |
---|
70 | ->addTask('dcMaintenanceZiptheme') |
---|
71 | ; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Favorites. |
---|
76 | * |
---|
77 | * @param $core <b>dcCore</b> dcCore instance |
---|
78 | * @param $favs <b>arrayObject</b> Array of favs |
---|
79 | */ |
---|
80 | public static function adminDashboardFavorites($core, $favs) |
---|
81 | { |
---|
82 | $favs->register('maintenance', array( |
---|
83 | 'title' => __('Maintenance'), |
---|
84 | 'url' => $core->adminurl->get('admin.plugin.maintenance'), |
---|
85 | 'small-icon' => dcPage::getPF('maintenance/icon.png'), |
---|
86 | 'large-icon' => dcPage::getPF('maintenance/icon-big.png'), |
---|
87 | 'permissions' => 'admin', |
---|
88 | 'active_cb' => array('dcMaintenanceAdmin', 'adminDashboardFavoritesActive'), |
---|
89 | 'dashboard_cb' => array('dcMaintenanceAdmin', 'adminDashboardFavoritesCallback') |
---|
90 | )); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Favorites selection. |
---|
95 | * |
---|
96 | * @param $request <b>string</b> Requested page |
---|
97 | * @param $params <b>array</b> Requested parameters |
---|
98 | */ |
---|
99 | public static function adminDashboardFavoritesActive($request, $params) |
---|
100 | { |
---|
101 | return $request == 'plugin.php' && isset($params['p']) && $params['p'] == 'maintenance'; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Favorites hack. |
---|
106 | * |
---|
107 | * This updates maintenance fav icon text |
---|
108 | * if there are tasks required maintenance. |
---|
109 | * |
---|
110 | * @param $core <b>dcCore</b> dcCore instance |
---|
111 | * @param $fav <b>arrayObject</b> fav attributes |
---|
112 | */ |
---|
113 | public static function adminDashboardFavoritesCallback($core, $fav) |
---|
114 | { |
---|
115 | // Check user option |
---|
116 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
117 | if (!$core->auth->user_prefs->maintenance->dashboard_icon) { |
---|
118 | return null; |
---|
119 | } |
---|
120 | |
---|
121 | // Check expired tasks |
---|
122 | $maintenance = new dcMaintenance($core); |
---|
123 | $count = 0; |
---|
124 | foreach($maintenance->getTasks() as $t) |
---|
125 | { |
---|
126 | if ($t->expired() !== false){ |
---|
127 | $count++; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | if (!$count) { |
---|
132 | return null; |
---|
133 | } |
---|
134 | |
---|
135 | $fav['title'] .= '<br />'.sprintf(__('One task to execute', '%s tasks to execute', $count), $count); |
---|
136 | $fav['large-icon'] = dcPage::getPF('maintenance/icon-big-update.png'); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Dashboard items stack. |
---|
141 | * |
---|
142 | * @param $core <b>dcCore</b> dcCore instance |
---|
143 | * @param $items <b>arrayObject</b> Dashboard items |
---|
144 | */ |
---|
145 | public static function adminDashboardItems($core, $items) |
---|
146 | { |
---|
147 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
148 | if (!$core->auth->user_prefs->maintenance->dashboard_item) { |
---|
149 | return null; |
---|
150 | } |
---|
151 | |
---|
152 | $maintenance = new dcMaintenance($core); |
---|
153 | |
---|
154 | $lines = array(); |
---|
155 | foreach($maintenance->getTasks() as $t) |
---|
156 | { |
---|
157 | $ts = $t->expired(); |
---|
158 | if ($ts === false){ |
---|
159 | continue; |
---|
160 | } |
---|
161 | |
---|
162 | $lines[] = |
---|
163 | '<li title="'.($ts === null ? |
---|
164 | __('This task has never been executed.') |
---|
165 | : |
---|
166 | sprintf(__('Last execution of this task was on %s.'), |
---|
167 | dt::dt2str($core->blog->settings->system->date_format, $ts).' '. |
---|
168 | dt::dt2str($core->blog->settings->system->time_format, $ts) |
---|
169 | ) |
---|
170 | ).'">'.$t->task().'</li>'; |
---|
171 | } |
---|
172 | |
---|
173 | if (empty($lines)) { |
---|
174 | return null; |
---|
175 | } |
---|
176 | |
---|
177 | $items[] = new ArrayObject(array( |
---|
178 | '<div id="maintenance-expired" class="box small">'. |
---|
179 | '<h3><img src="'.dcPage::getPF('maintenance/icon-small.png').'" alt="" /> '.__('Maintenance').'</h3>'. |
---|
180 | '<p class="warning no-margin">'.sprintf(__('There is a task to execute.', 'There are %s tasks to execute.', count($lines)), count($lines)).'</p>'. |
---|
181 | '<ul>'.implode('',$lines).'</ul>'. |
---|
182 | '<p><a href="'.$core->adminurl->get('admin.plugin.maintenance').'">'.__('Manage tasks').'</a></p>'. |
---|
183 | '</div>' |
---|
184 | )); |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * User preferences form. |
---|
189 | * |
---|
190 | * This add options for superadmin user |
---|
191 | * to show or not expired taks. |
---|
192 | * |
---|
193 | * @param $args <b>object</b> dcCore instance or record |
---|
194 | */ |
---|
195 | public static function adminDashboardOptionsForm($core) |
---|
196 | { |
---|
197 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
198 | |
---|
199 | echo |
---|
200 | '<div class="fieldset">'. |
---|
201 | '<h4>'.__('Maintenance').'</h4>'. |
---|
202 | |
---|
203 | '<p><label for="maintenance_dashboard_icon" class="classic">'. |
---|
204 | form::checkbox('maintenance_dashboard_icon', 1, $core->auth->user_prefs->maintenance->dashboard_icon). |
---|
205 | __('Display overdue tasks counter on maintenance dashboard icon').'</label></p>'. |
---|
206 | |
---|
207 | '<p><label for="maintenance_dashboard_item" class="classic">'. |
---|
208 | form::checkbox('maintenance_dashboard_item', 1, $core->auth->user_prefs->maintenance->dashboard_item). |
---|
209 | __('Display overdue tasks list on dashboard items').'</label></p>'. |
---|
210 | |
---|
211 | '</div>'; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * User preferences update. |
---|
216 | * |
---|
217 | * @param $user_id <b>string</b> User ID |
---|
218 | */ |
---|
219 | public static function adminAfterDashboardOptionsUpdate($user_id=null) |
---|
220 | { |
---|
221 | global $core; |
---|
222 | |
---|
223 | if (is_null($user_id)) { |
---|
224 | return null; |
---|
225 | } |
---|
226 | |
---|
227 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
228 | $core->auth->user_prefs->maintenance->put('dashboard_icon', !empty($_POST['maintenance_dashboard_icon']), 'boolean'); |
---|
229 | $core->auth->user_prefs->maintenance->put('dashboard_item', !empty($_POST['maintenance_dashboard_item']), 'boolean'); |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * Build a well sorted help for tasks. |
---|
234 | * |
---|
235 | * This method is not so good if used with lot of tranlsations |
---|
236 | * as it grows memory usage and translations files size, |
---|
237 | * it is better to use help ressource files |
---|
238 | * but keep it for exemple of how to use behavior adminPageHelpBlock. |
---|
239 | * Cheers, JC |
---|
240 | * |
---|
241 | * @param $block <b>arrayObject</b> Called helpblocks |
---|
242 | */ |
---|
243 | public static function adminPageHelpBlock($blocks) |
---|
244 | { |
---|
245 | $found = false; |
---|
246 | foreach($blocks as $block) { |
---|
247 | if ($block == 'maintenancetasks') { |
---|
248 | $found = true; |
---|
249 | break; |
---|
250 | } |
---|
251 | } |
---|
252 | if (!$found) { |
---|
253 | return null; |
---|
254 | } |
---|
255 | |
---|
256 | $maintenance = new dcMaintenance($GLOBALS['core']); |
---|
257 | |
---|
258 | $res_tab = ''; |
---|
259 | foreach($maintenance->getTabs() as $tab_obj) |
---|
260 | { |
---|
261 | $res_group = ''; |
---|
262 | foreach($maintenance->getGroups() as $group_obj) |
---|
263 | { |
---|
264 | $res_task = ''; |
---|
265 | foreach($maintenance->getTasks() as $t) |
---|
266 | { |
---|
267 | if ($t->group() != $group_obj->id() |
---|
268 | || $t->tab() != $tab_obj->id()) { |
---|
269 | continue; |
---|
270 | } |
---|
271 | if (($desc = $t->description()) != '') { |
---|
272 | $res_task .= |
---|
273 | '<dt>'.$t->task().'</dt>'. |
---|
274 | '<dd>'.$desc.'</dd>'; |
---|
275 | } |
---|
276 | } |
---|
277 | if (!empty($res_task)) { |
---|
278 | $desc = $group_obj->description ? $group_obj->description : $group_obj->summary; |
---|
279 | |
---|
280 | $res_group .= |
---|
281 | '<h5>'.$group_obj->name().'</h5>'. |
---|
282 | ($desc ? '<p>'.$desc.'</p>' : ''). |
---|
283 | '<dl>'.$res_task.'</dl>'; |
---|
284 | } |
---|
285 | } |
---|
286 | if (!empty($res_group)) { |
---|
287 | $desc = $tab_obj->description ? $tab_obj->description : $tab_obj->summary; |
---|
288 | |
---|
289 | $res_tab .= |
---|
290 | '<h4>'.$tab_obj->name().'</h4>'. |
---|
291 | ($desc ? '<p>'.$desc.'</p>' : ''). |
---|
292 | $res_group; |
---|
293 | } |
---|
294 | } |
---|
295 | if (!empty($res_tab)) { |
---|
296 | $res = new ArrayObject(); |
---|
297 | $res->content = $res_tab; |
---|
298 | $blocks[] = $res; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * Add javascript for plugin configuration. |
---|
304 | * |
---|
305 | * @param $core <b>dcCore</b> dcCore instance |
---|
306 | * @param $module <b>mixed</b> Module ID or false if none |
---|
307 | * @return <b>string</b> Header code for js inclusion |
---|
308 | */ |
---|
309 | public static function pluginsToolsHeaders($core, $module) |
---|
310 | { |
---|
311 | if ($module == 'maintenance') { |
---|
312 | return dcPage::jsLoad(dcPage::getPF('maintenance/js/settings.js')); |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|