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