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 | 'plugin.php?p=maintenance', |
---|
18 | 'index.php?pf=maintenance/icon.png', |
---|
19 | preg_match('/plugin.php\?p=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('adminDashboardFavs', array('dcMaintenanceAdmin', 'adminDashboardFavs')); |
---|
26 | $core->addBehavior('adminDashboardFavsIcon', array('dcMaintenanceAdmin', 'adminDashboardFavsIcon')); |
---|
27 | $core->addBehavior('adminDashboardContents', array('dcMaintenanceAdmin', 'adminDashboardItems')); |
---|
28 | $core->addBehavior('adminDashboardOptionsForm', array('dcMaintenanceAdmin', 'adminDashboardOptionsForm')); |
---|
29 | $core->addBehavior('adminAfterDashboardOptionsUpdate', array('dcMaintenanceAdmin', 'adminAfterDashboardOptionsUpdate')); |
---|
30 | $core->addBehavior('adminPageHelpBlock', array('dcMaintenanceAdmin', 'adminPageHelpBlock')); |
---|
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 | * Dashboard favs. |
---|
76 | * |
---|
77 | * @param $core <b>dcCore</b> dcCore instance |
---|
78 | * @param $favs <b>arrayObject</b> Array of favs |
---|
79 | */ |
---|
80 | public static function adminDashboardFavs($core, $favs) |
---|
81 | { |
---|
82 | $favs['maintenance'] = new ArrayObject(array( |
---|
83 | 'maintenance', |
---|
84 | 'Maintenance', |
---|
85 | 'plugin.php?p=maintenance', |
---|
86 | 'index.php?pf=maintenance/icon.png', |
---|
87 | 'index.php?pf=maintenance/icon-big.png', |
---|
88 | null,null,null |
---|
89 | )); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Dashboard favs icon. |
---|
94 | * |
---|
95 | * This updates maintenance fav icon text |
---|
96 | * if there are tasks required maintenance. |
---|
97 | * |
---|
98 | * @param $core <b>dcCore</b> dcCore instance |
---|
99 | * @param $name <b>string</b> Current fav name |
---|
100 | * @param $icon <b>arrayObject</b> Current fav attributes |
---|
101 | */ |
---|
102 | public static function adminDashboardFavsIcon($core, $name, $icon) |
---|
103 | { |
---|
104 | // Check icon |
---|
105 | if ($name !== 'maintenance') { |
---|
106 | return null; |
---|
107 | } |
---|
108 | |
---|
109 | // Check user option |
---|
110 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
111 | if (!$core->auth->user_prefs->maintenance->dashboard_icon) { |
---|
112 | return null; |
---|
113 | } |
---|
114 | |
---|
115 | // Check expired tasks |
---|
116 | $maintenance = new dcMaintenance($core); |
---|
117 | $count = 0; |
---|
118 | foreach($maintenance->getTasks() as $t) |
---|
119 | { |
---|
120 | if ($t->expired() !== false){ |
---|
121 | $count++; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | if (!$count) { |
---|
126 | return null; |
---|
127 | } |
---|
128 | |
---|
129 | $icon[0] .= '<br />'.sprintf(__('One task to execute', '%s tasks to execute', $count), $count); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Dashboard items stack. |
---|
134 | * |
---|
135 | * @param $core <b>dcCore</b> dcCore instance |
---|
136 | * @param $items <b>arrayObject</b> Dashboard items |
---|
137 | */ |
---|
138 | public static function adminDashboardItems($core, $items) |
---|
139 | { |
---|
140 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
141 | if (!$core->auth->user_prefs->maintenance->dashboard_item) { |
---|
142 | return null; |
---|
143 | } |
---|
144 | |
---|
145 | $maintenance = new dcMaintenance($core); |
---|
146 | |
---|
147 | $lines = array(); |
---|
148 | foreach($maintenance->getTasks() as $t) |
---|
149 | { |
---|
150 | $ts = $t->expired(); |
---|
151 | if ($ts === false){ |
---|
152 | continue; |
---|
153 | } |
---|
154 | |
---|
155 | $lines[] = |
---|
156 | '<li title="'.($ts === null ? |
---|
157 | __('This task has never been executed.') |
---|
158 | : |
---|
159 | sprintf(__('Last execution of this task was on %s.'), |
---|
160 | dt::dt2str($core->blog->settings->system->date_format, $ts).' '. |
---|
161 | dt::dt2str($core->blog->settings->system->time_format, $ts) |
---|
162 | ) |
---|
163 | ).'">'.$t->task().'</li>'; |
---|
164 | } |
---|
165 | |
---|
166 | if (empty($lines)) { |
---|
167 | return null; |
---|
168 | } |
---|
169 | |
---|
170 | $items[] = new ArrayObject(array( |
---|
171 | '<div id="maintenance-expired" class="box small">'. |
---|
172 | '<h3><img src="index.php?pf=maintenance/icon.png" alt="" /> '.__('Maintenance').'</h3>'. |
---|
173 | '<p class="warn">'.sprintf(__('There is a task to execute.', 'There are %s tasks to execute.', count($lines)), count($lines)).'</p>'. |
---|
174 | '<ul>'.implode('',$lines).'</ul>'. |
---|
175 | '<p><a href="plugin.php?p=maintenance">'.__('Manage tasks').'</a></p>'. |
---|
176 | '</div>' |
---|
177 | )); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * User preferences form. |
---|
182 | * |
---|
183 | * This add options for superadmin user |
---|
184 | * to show or not expired taks. |
---|
185 | * |
---|
186 | * @param $args <b>object</b> dcCore instance or record |
---|
187 | */ |
---|
188 | public static function adminDashboardOptionsForm($core) |
---|
189 | { |
---|
190 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
191 | |
---|
192 | echo |
---|
193 | '<div class="fieldset">'. |
---|
194 | '<h4>'.__('Maintenance').'</h4>'. |
---|
195 | |
---|
196 | '<p><label for="maintenance_dashboard_icon" class="classic">'. |
---|
197 | form::checkbox('maintenance_dashboard_icon', 1, $core->auth->user_prefs->maintenance->dashboard_icon). |
---|
198 | __('Display count of late tasks on maintenance dashboard icon').'</label></p>'. |
---|
199 | |
---|
200 | '<p><label for="maintenance_dashboard_item" class="classic">'. |
---|
201 | form::checkbox('maintenance_dashboard_item', 1, $core->auth->user_prefs->maintenance->dashboard_item). |
---|
202 | __('Display list of late tasks on dashboard items').'</label></p>'. |
---|
203 | |
---|
204 | '</div>'; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * User preferences update. |
---|
209 | * |
---|
210 | * @param $user_id <b>string</b> User ID |
---|
211 | */ |
---|
212 | public static function adminAfterDashboardOptionsUpdate($user_id=null) |
---|
213 | { |
---|
214 | global $core; |
---|
215 | |
---|
216 | if (is_null($user_id)) { |
---|
217 | return null; |
---|
218 | } |
---|
219 | |
---|
220 | $core->auth->user_prefs->addWorkspace('maintenance'); |
---|
221 | $core->auth->user_prefs->maintenance->put('dashboard_icon', !empty($_POST['maintenance_dashboard_icon']), 'boolean'); |
---|
222 | $core->auth->user_prefs->maintenance->put('dashboard_item', !empty($_POST['maintenance_dashboard_item']), 'boolean'); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | /** |
---|
227 | * Build a well sorted help for tasks. |
---|
228 | * |
---|
229 | * This method is not so good if used with lot of tranlsations |
---|
230 | * as it grows memory usage and translations files size, |
---|
231 | * it is better to use help ressource files |
---|
232 | * but keep it for exemple of how to use behavior adminPageHelpBlock. |
---|
233 | * Cheers, JC |
---|
234 | * |
---|
235 | * @param $block <b>arrayObject</b> Called helpblocks |
---|
236 | */ |
---|
237 | public static function adminPageHelpBlock($blocks) |
---|
238 | { |
---|
239 | $found = false; |
---|
240 | foreach($blocks as $block) { |
---|
241 | if ($block == 'maintenancetasks') { |
---|
242 | $found = true; |
---|
243 | break; |
---|
244 | } |
---|
245 | } |
---|
246 | if (!$found) { |
---|
247 | return null; |
---|
248 | } |
---|
249 | |
---|
250 | $maintenance = new dcMaintenance($GLOBALS['core']); |
---|
251 | |
---|
252 | $res_tab = ''; |
---|
253 | foreach($maintenance->getTabs() as $tab_obj) |
---|
254 | { |
---|
255 | $res_group = ''; |
---|
256 | foreach($maintenance->getGroups() as $group_obj) |
---|
257 | { |
---|
258 | $res_task = ''; |
---|
259 | foreach($maintenance->getTasks() as $t) |
---|
260 | { |
---|
261 | if ($t->group() != $group_obj->id() |
---|
262 | || $t->tab() != $tab_obj->id()) { |
---|
263 | continue; |
---|
264 | } |
---|
265 | if (($desc = $t->description()) != '') { |
---|
266 | $res_task .= |
---|
267 | '<dt>'.$t->task().'</dt>'. |
---|
268 | '<dd>'.$desc.'</dd>'; |
---|
269 | } |
---|
270 | } |
---|
271 | if (!empty($res_task)) { |
---|
272 | $desc = $group_obj->description ? $group_obj->description : $group_obj->summary; |
---|
273 | |
---|
274 | $res_group .= |
---|
275 | '<h5>'.$group_obj->name().'</h5>'. |
---|
276 | ($desc ? '<p>'.$desc.'</p>' : ''). |
---|
277 | '<dl>'.$res_task.'</dl>'; |
---|
278 | } |
---|
279 | } |
---|
280 | if (!empty($res_group)) { |
---|
281 | $desc = $tab_obj->description ? $tab_obj->description : $tab_obj->summary; |
---|
282 | |
---|
283 | $res_tab .= |
---|
284 | '<h4>'.$tab_obj->name().'</h4>'. |
---|
285 | ($desc ? '<p>'.$desc.'</p>' : ''). |
---|
286 | $res_group; |
---|
287 | } |
---|
288 | } |
---|
289 | if (!empty($res_tab)) { |
---|
290 | $res = new ArrayObject(); |
---|
291 | $res->content = $res_tab; |
---|
292 | $blocks[] = $res; |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|